diff --git a/.github/workflows/check-generated-files.yml b/.github/workflows/check-generated-files.yml index d02ebd9148..0adfd9bb69 100644 --- a/.github/workflows/check-generated-files.yml +++ b/.github/workflows/check-generated-files.yml @@ -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' @@ -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/events.go ./mqtt/mocks/events.go.tmp make mocks @@ -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/events.go "MQTT Events Store ./mqtt/mocks/events.go" diff --git a/mqtt/events/streams.go b/mqtt/events/streams.go index ee03a243ab..8889accab2 100644 --- a/mqtt/events/streams.go +++ b/mqtt/events/streams.go @@ -12,6 +12,7 @@ import ( const streamID = "magistrala.mqtt" +//go:generate mockery --name EventStore --output=../mocks --filename events.go --quiet --note "Copyright (c) Abstract Machines" type EventStore interface { Connect(ctx context.Context, clientID string) error Disconnect(ctx context.Context, clientID string) error diff --git a/mqtt/handler_test.go b/mqtt/handler_test.go index 0dd52808e2..f5fb599f7c 100644 --- a/mqtt/handler_test.go +++ b/mqtt/handler_test.go @@ -64,7 +64,7 @@ var ( ) func TestAuthConnect(t *testing.T) { - handler, _ := newHandler() + handler, _, eventStore := newHandler() cases := []struct { desc string @@ -105,19 +105,22 @@ func TestAuthConnect(t *testing.T) { session: &sessionClient, }, } - for _, tc := range cases { ctx := context.TODO() + password := "" if tc.session != nil { ctx = session.NewContext(ctx, tc.session) + password = string(tc.session.Password) } + svcCall := eventStore.On("Connect", mock.Anything, password).Return(tc.err) 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() cases := []struct { desc string @@ -157,7 +160,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) @@ -169,7 +172,7 @@ func TestAuthPublish(t *testing.T) { } func TestAuthSubscribe(t *testing.T) { - handler, auth := newHandler() + handler, auth, _ := newHandler() cases := []struct { desc string @@ -210,7 +213,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) @@ -222,7 +225,7 @@ func TestAuthSubscribe(t *testing.T) { } func TestConnect(t *testing.T) { - handler, _ := newHandler() + handler, _, _ := newHandler() logBuffer.Reset() cases := []struct { @@ -256,7 +259,7 @@ func TestConnect(t *testing.T) { } func TestPublish(t *testing.T) { - handler, _ := newHandler() + handler, _, _ := newHandler() logBuffer.Reset() malformedSubtopics := topic + "/" + subtopic + "%" @@ -335,7 +338,7 @@ func TestPublish(t *testing.T) { } func TestSubscribe(t *testing.T) { - handler, _ := newHandler() + handler, _, _ := newHandler() logBuffer.Reset() cases := []struct { @@ -371,7 +374,7 @@ func TestSubscribe(t *testing.T) { } func TestUnsubscribe(t *testing.T) { - handler, _ := newHandler() + handler, _, _ := newHandler() logBuffer.Reset() cases := []struct { @@ -407,7 +410,7 @@ func TestUnsubscribe(t *testing.T) { } func TestDisconnect(t *testing.T) { - handler, _ := newHandler() + handler, _, eventStore := newHandler() logBuffer.Reset() cases := []struct { @@ -433,21 +436,25 @@ func TestDisconnect(t *testing.T) { for _, tc := range cases { ctx := context.TODO() + password := "" if tc.session != nil { ctx = session.NewContext(ctx, tc.session) + password = string(tc.session.Password) } + svcCall := eventStore.On("Disconnect", mock.Anything, password).Return(tc.err) 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() (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 := new(mocks.EventStore) + return mqtt.NewHandler(mocks.NewPublisher(), eventStore, logger, auth), auth, eventStore } diff --git a/mqtt/mocks/events.go b/mqtt/mocks/events.go new file mode 100644 index 0000000000..d7ba830bad --- /dev/null +++ b/mqtt/mocks/events.go @@ -0,0 +1,66 @@ +// Code generated by mockery v2.42.1. DO NOT EDIT. + +// Copyright (c) Abstract Machines + +package mocks + +import ( + context "context" + + mock "github.com/stretchr/testify/mock" +) + +// EventStore is an autogenerated mock type for the EventStore type +type EventStore struct { + mock.Mock +} + +// Connect provides a mock function with given fields: ctx, clientID +func (_m *EventStore) Connect(ctx context.Context, clientID string) error { + ret := _m.Called(ctx, clientID) + + if len(ret) == 0 { + panic("no return value specified for Connect") + } + + var r0 error + if rf, ok := ret.Get(0).(func(context.Context, string) error); ok { + r0 = rf(ctx, clientID) + } else { + r0 = ret.Error(0) + } + + return r0 +} + +// Disconnect provides a mock function with given fields: ctx, clientID +func (_m *EventStore) Disconnect(ctx context.Context, clientID string) error { + ret := _m.Called(ctx, clientID) + + if len(ret) == 0 { + panic("no return value specified for Disconnect") + } + + var r0 error + if rf, ok := ret.Get(0).(func(context.Context, string) error); ok { + r0 = rf(ctx, clientID) + } else { + r0 = ret.Error(0) + } + + return r0 +} + +// NewEventStore creates a new instance of EventStore. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations. +// The first argument is typically a *testing.T value. +func NewEventStore(t interface { + mock.TestingT + Cleanup(func()) +}) *EventStore { + mock := &EventStore{} + mock.Mock.Test(t) + + t.Cleanup(func() { mock.AssertExpectations(t) }) + + return mock +} diff --git a/mqtt/mocks/redis.go b/mqtt/mocks/redis.go deleted file mode 100644 index be63be8a44..0000000000 --- a/mqtt/mocks/redis.go +++ /dev/null @@ -1,24 +0,0 @@ -// Copyright (c) Abstract Machines -// SPDX-License-Identifier: Apache-2.0 - -package mocks - -import ( - "context" - - "github.com/absmach/magistrala/mqtt/events" -) - -type MockEventStore struct{} - -func NewEventStore() events.EventStore { - return MockEventStore{} -} - -func (es MockEventStore) Connect(ctx context.Context, clientID string) error { - return nil -} - -func (es MockEventStore) Disconnect(ctx context.Context, clientID string) error { - return nil -}