Skip to content

Commit

Permalink
Add mocks with mockery for mqtt
Browse files Browse the repository at this point in the history
Signed-off-by: JeffMboya <[email protected]>

Refactor mqtt handler tests to include eventStore in newHandler function

Signed-off-by: JeffMboya <[email protected]>

NOISSUE - Add property based testing to things service (absmach#2088)

Signed-off-by: Rodney Osodo <[email protected]>
Signed-off-by: rodneyosodo <[email protected]>
Signed-off-by: JeffMboya <[email protected]>

Fix URLs in api-tests.yml

Signed-off-by: JeffMboya <[email protected]>

Add MQTT EventStore mock directive to check-generated-files.yml

Signed-off-by: JeffMboya <[email protected]>

Refactor mqtt handler tests to use svcCall instead of repoCall in handler_test.go

Signed-off-by: JeffMboya <[email protected]>

Replace mock.Anything in TestAuthConnect

Signed-off-by: JeffMboya <[email protected]>

Replace mock.Anything in TestDisconnect

Signed-off-by: JeffMboya <[email protected]>

Replace nil with tc.err in return values

Signed-off-by: JeffMboya <[email protected]>

Replace nil with tc.err in return values

Signed-off-by: JeffMboya <[email protected]>

NOISSUE - Update the location of MQTT API docs (absmach#2172)

Signed-off-by: Emmanuel Ferdman <[email protected]>

Replace context.Anything with respective context

Signed-off-by: JeffMboya <[email protected]>
  • Loading branch information
JeffMboya committed Apr 15, 2024
1 parent 2246d79 commit aa5e5aa
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 25 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/api-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -183,4 +183,4 @@ jobs:

- name: Stop containers
if: always()
run: make run down args="-v"
run: make run down args="-v"
3 changes: 3 additions & 0 deletions .github/workflows/check-generated-files.yml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ jobs:
- "invitations/invitations.go"
- "users/emailer.go"
- "users/hasher.go"
- "mqtt/events/streams.go"
- name: Set up protoc
if: steps.changes.outputs.proto == 'true'
Expand Down Expand Up @@ -132,6 +133,7 @@ jobs:
mv ./invitations/mocks/repository.go ./invitations/mocks/repository.go.tmp
mv ./users/mocks/emailer.go ./users/mocks/emailer.go.tmp
mv ./users/mocks/hasher.go ./users/mocks/hasher.go.tmp
mv ./mqtt/mocks/redis.go ./mqtt/mocks/redis.go.tmp
make mocks
Expand Down Expand Up @@ -170,3 +172,4 @@ jobs:
check_mock_changes ./invitations/mocks/repository.go "Invitations Repository ./invitations/mocks/repository.go"
check_mock_changes ./users/mocks/emailer.go "Users Emailer ./users/mocks/emailer.go"
check_mock_changes ./users/mocks/hasher.go "Users Hasher ./users/mocks/hasher.go"
check_mock_changes ./mqtt/mocks/redis.go "MQTT Redis ./mqtt/mocks/redis.go"
1 change: 1 addition & 0 deletions mqtt/events/streams.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

const streamID = "magistrala.mqtt"

//go:generate mockery --name EventStore --output=../mocks --filename redis.go --quiet --note "Copyright (c) Abstract Machines"
type EventStore interface {
Connect(ctx context.Context, clientID string) error
Disconnect(ctx context.Context, clientID string) error
Expand Down
40 changes: 26 additions & 14 deletions mqtt/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,11 @@ var (
Username: invalidID,
Password: []byte(password),
}
svcCall *mock.Call
)

func TestAuthConnect(t *testing.T) {
handler, _ := newHandler()
handler, _, eventStore := newHandler(t)

cases := []struct {
desc string
Expand Down Expand Up @@ -105,19 +106,24 @@ func TestAuthConnect(t *testing.T) {
session: &sessionClient,
},
}

for _, tc := range cases {
ctx := context.TODO()
if tc.session != nil {
svcCall = eventStore.On("Connect", mock.Anything, string(tc.session.Password)).Return(tc.err)
} else {
svcCall = eventStore.On("Connect", ctx, "").Return(tc.err)
}
if tc.session != nil {
ctx = session.NewContext(ctx, tc.session)
}
err := handler.AuthConnect(ctx)
assert.True(t, errors.Contains(err, tc.err), fmt.Sprintf("%s: expected %s got %s\n", tc.desc, tc.err, err))
svcCall.Unset()
}
}

func TestAuthPublish(t *testing.T) {
handler, auth := newHandler()
handler, auth, _ := newHandler(t)

cases := []struct {
desc string
Expand Down Expand Up @@ -157,7 +163,7 @@ func TestAuthPublish(t *testing.T) {
}

for _, tc := range cases {
repocall := auth.On("Authorize", mock.Anything, mock.Anything).Return(&magistrala.AuthorizeRes{Authorized: true, Id: testsutil.GenerateUUID(t)}, nil)
repocall := auth.On("Authorize", mock.Anything, mock.Anything).Return(&magistrala.AuthorizeRes{Authorized: true, Id: testsutil.GenerateUUID(t)}, tc.err)
ctx := context.TODO()
if tc.session != nil {
ctx = session.NewContext(ctx, tc.session)
Expand All @@ -169,7 +175,7 @@ func TestAuthPublish(t *testing.T) {
}

func TestAuthSubscribe(t *testing.T) {
handler, auth := newHandler()
handler, auth, _ := newHandler(t)

cases := []struct {
desc string
Expand Down Expand Up @@ -210,7 +216,7 @@ func TestAuthSubscribe(t *testing.T) {
}

for _, tc := range cases {
repocall := auth.On("Authorize", mock.Anything, mock.Anything).Return(&magistrala.AuthorizeRes{Authorized: true, Id: testsutil.GenerateUUID(t)}, nil)
repocall := auth.On("Authorize", mock.Anything, mock.Anything).Return(&magistrala.AuthorizeRes{Authorized: true, Id: testsutil.GenerateUUID(t)}, tc.err)
ctx := context.TODO()
if tc.session != nil {
ctx = session.NewContext(ctx, tc.session)
Expand All @@ -222,7 +228,7 @@ func TestAuthSubscribe(t *testing.T) {
}

func TestConnect(t *testing.T) {
handler, _ := newHandler()
handler, _, _ := newHandler(t)
logBuffer.Reset()

cases := []struct {
Expand Down Expand Up @@ -256,7 +262,7 @@ func TestConnect(t *testing.T) {
}

func TestPublish(t *testing.T) {
handler, _ := newHandler()
handler, _, _ := newHandler(t)
logBuffer.Reset()

malformedSubtopics := topic + "/" + subtopic + "%"
Expand Down Expand Up @@ -335,7 +341,7 @@ func TestPublish(t *testing.T) {
}

func TestSubscribe(t *testing.T) {
handler, _ := newHandler()
handler, _, _ := newHandler(t)
logBuffer.Reset()

cases := []struct {
Expand Down Expand Up @@ -371,7 +377,7 @@ func TestSubscribe(t *testing.T) {
}

func TestUnsubscribe(t *testing.T) {
handler, _ := newHandler()
handler, _, _ := newHandler(t)
logBuffer.Reset()

cases := []struct {
Expand Down Expand Up @@ -407,7 +413,7 @@ func TestUnsubscribe(t *testing.T) {
}

func TestDisconnect(t *testing.T) {
handler, _ := newHandler()
handler, _, eventStore := newHandler(t)
logBuffer.Reset()

cases := []struct {
Expand All @@ -433,21 +439,27 @@ func TestDisconnect(t *testing.T) {

for _, tc := range cases {
ctx := context.TODO()
if tc.session != nil {
svcCall = eventStore.On("Disconnect", mock.Anything, string(tc.session.Password)).Return(tc.err)
} else {
svcCall = eventStore.On("Disconnect", ctx, "").Return(tc.err)
}
if tc.session != nil {
ctx = session.NewContext(ctx, tc.session)
}
err := handler.Disconnect(ctx)
assert.Contains(t, logBuffer.String(), tc.logMsg)
assert.Equal(t, tc.err, err)
svcCall.Unset()
}
}

func newHandler() (session.Handler, *authmocks.AuthClient) {
func newHandler(t *testing.T) (session.Handler, *authmocks.AuthClient, *mocks.EventStore) {
logger, err := mglog.New(&logBuffer, "debug")
if err != nil {
log.Fatalf("failed to create logger: %s", err)
}
auth := new(authmocks.AuthClient)
eventStore := mocks.NewEventStore()
return mqtt.NewHandler(mocks.NewPublisher(), eventStore, logger, auth), auth
eventStore := mocks.NewEventStore(t)
return mqtt.NewHandler(mocks.NewPublisher(), eventStore, logger, auth), auth, eventStore
}
62 changes: 52 additions & 10 deletions mqtt/mocks/redis.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit aa5e5aa

Please sign in to comment.