Skip to content

Commit

Permalink
Refactor mqtt handler tests to include eventStore in newHandler function
Browse files Browse the repository at this point in the history
Signed-off-by: JeffMboya <[email protected]>
  • Loading branch information
JeffMboya committed Apr 11, 2024
1 parent 09dbde5 commit 26c915f
Showing 1 changed file with 14 additions and 10 deletions.
24 changes: 14 additions & 10 deletions mqtt/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ var (
)

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

cases := []struct {
desc string
Expand Down Expand Up @@ -107,17 +107,19 @@ func TestAuthConnect(t *testing.T) {
}

for _, tc := range cases {
repoCall := eventStore.On("Connect", mock.Anything, mock.Anything).Return(nil)
ctx := context.TODO()
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))
repoCall.Unset()
}
}

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

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

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

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

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

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

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

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

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

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

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

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

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

cases := []struct {
Expand All @@ -432,22 +434,24 @@ func TestDisconnect(t *testing.T) {
}

for _, tc := range cases {
repoCall := eventStore.On("Disconnect", mock.Anything, mock.Anything).Return(nil)
ctx := context.TODO()
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)
repoCall.Unset()
}
}

func newHandler(t *testing.T) (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(t)
return mqtt.NewHandler(mocks.NewPublisher(), eventStore, logger, auth), auth
return mqtt.NewHandler(mocks.NewPublisher(), eventStore, logger, auth), auth, eventStore
}

0 comments on commit 26c915f

Please sign in to comment.