From 1f21541a277543990de1fb393c838c8b77f9ae5b Mon Sep 17 00:00:00 2001 From: b1ackd0t <28790446+rodneyosodo@users.noreply.github.com> Date: Sat, 21 Oct 2023 00:06:03 +0300 Subject: [PATCH] NOISSUE - Fix Twins Tests (#1931) * feat(twins/mocks): Add new service constructor with mock dependencies This commit adds a new service constructor to the `twins/mocks/service.go` file. The new constructor, `NewService`, takes no arguments and returns an instance of `twins.Service` along with a mock instance of `authmocks.Service`. This allows for the creation of a real twins service using mock dependencies. Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com> * feat(api): Add tests for endpoint states This commit adds tests for the endpoint states API in the `endpoint_states_test.go` file. The tests cover the functionality of the `GET` and `POST` methods for retrieving and updating the states of endpoints respectively. The tests use the `mainflux` and `authmocks` packages for mocking and testing the API endpoints. Additionally, the `testsutil` package is imported for utility functions used in the tests. Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com> --------- Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com> --- twins/api/http/endpoint_states_test.go | 12 +++- twins/api/http/endpoint_twins_test.go | 44 +++++++++++---- twins/mocks/service.go | 4 +- twins/service_test.go | 77 ++++++++++++++++++-------- 4 files changed, 100 insertions(+), 37 deletions(-) diff --git a/twins/api/http/endpoint_states_test.go b/twins/api/http/endpoint_states_test.go index 1662618e..43e90895 100644 --- a/twins/api/http/endpoint_states_test.go +++ b/twins/api/http/endpoint_states_test.go @@ -10,10 +10,14 @@ import ( "net/http" "testing" + "github.com/mainflux/mainflux" + authmocks "github.com/mainflux/mainflux/auth/mocks" + "github.com/mainflux/mainflux/internal/testsutil" "github.com/mainflux/mainflux/twins" "github.com/mainflux/mainflux/twins/mocks" "github.com/mainflux/senml" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/mock" "github.com/stretchr/testify/require" ) @@ -37,7 +41,7 @@ type statesPageRes struct { } func TestListStates(t *testing.T) { - svc := mocks.NewService(map[string]string{token: email}) + svc, auth := mocks.NewService() ts := newServer(svc) defer ts.Close() @@ -45,8 +49,10 @@ func TestListStates(t *testing.T) { Owner: email, } def := mocks.CreateDefinition(channels[0:2], subtopics[0:2]) + repoCall := auth.On("Identify", mock.Anything, &mainflux.IdentityReq{Token: token}).Return(&mainflux.IdentityRes{Id: testsutil.GenerateUUID(t)}, nil) tw, err := svc.AddTwin(context.Background(), token, twin, def) require.Nil(t, err, fmt.Sprintf("unexpected error: %s", err)) + repoCall.Unset() attr := def.Attributes[0] recs := make([]senml.Record, numRecs) @@ -87,7 +93,7 @@ func TestListStates(t *testing.T) { }, { desc: "get a list of states with invalid token", - auth: wrongValue, + auth: authmocks.InvalidValue, status: http.StatusUnauthorized, url: fmt.Sprintf(queryFmt, baseURL, 0, 5), res: nil, @@ -179,6 +185,7 @@ func TestListStates(t *testing.T) { } for _, tc := range cases { + repoCall := auth.On("Identify", mock.Anything, mock.Anything).Return(&mainflux.IdentityRes{Id: testsutil.GenerateUUID(t)}, nil) req := testRequest{ client: ts.Client(), method: http.MethodGet, @@ -196,6 +203,7 @@ func TestListStates(t *testing.T) { assert.Equal(t, tc.status, res.StatusCode, fmt.Sprintf("%s: expected status code %d got %d", tc.desc, tc.status, res.StatusCode)) assert.ElementsMatch(t, tc.res, resData.States, fmt.Sprintf("%s: got incorrect body from response", tc.desc)) + repoCall.Unset() } } diff --git a/twins/api/http/endpoint_twins_test.go b/twins/api/http/endpoint_twins_test.go index 0ccaf5b1..62451962 100644 --- a/twins/api/http/endpoint_twins_test.go +++ b/twins/api/http/endpoint_twins_test.go @@ -14,12 +14,16 @@ import ( "strings" "testing" + "github.com/mainflux/mainflux" + authmocks "github.com/mainflux/mainflux/auth/mocks" "github.com/mainflux/mainflux/internal/apiutil" + "github.com/mainflux/mainflux/internal/testsutil" "github.com/mainflux/mainflux/logger" "github.com/mainflux/mainflux/twins" httpapi "github.com/mainflux/mainflux/twins/api/http" "github.com/mainflux/mainflux/twins/mocks" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/mock" "github.com/stretchr/testify/require" ) @@ -28,7 +32,6 @@ const ( contentType = "application/json" email = "user@example.com" token = "token" - wrongValue = "wrong_value" wrongID = 0 maxNameSize = 1024 instanceID = "5de9b29a-feb9-11ed-be56-0242ac120002" @@ -98,7 +101,7 @@ func toJSON(data interface{}) (string, error) { } func TestAddTwin(t *testing.T) { - svc := mocks.NewService(map[string]string{token: email}) + svc, auth := mocks.NewService() ts := newServer(svc) defer ts.Close() @@ -138,7 +141,7 @@ func TestAddTwin(t *testing.T) { desc: "add twin with invalid auth token", req: data, contentType: contentType, - auth: wrongValue, + auth: authmocks.InvalidValue, status: http.StatusUnauthorized, location: "", }, @@ -185,6 +188,7 @@ func TestAddTwin(t *testing.T) { } for _, tc := range cases { + repoCall := auth.On("Identify", mock.Anything, &mainflux.IdentityReq{Token: tc.auth}).Return(&mainflux.IdentityRes{Id: testsutil.GenerateUUID(t)}, nil) req := testRequest{ client: ts.Client(), method: http.MethodPost, @@ -199,18 +203,21 @@ func TestAddTwin(t *testing.T) { location := res.Header.Get("Location") assert.Equal(t, tc.status, res.StatusCode, fmt.Sprintf("%s: expected status code %d got %d", tc.desc, tc.status, res.StatusCode)) assert.Equal(t, tc.location, location, fmt.Sprintf("%s: expected location %s got %s", tc.desc, tc.location, location)) + repoCall.Unset() } } func TestUpdateTwin(t *testing.T) { - svc := mocks.NewService(map[string]string{token: email}) + svc, auth := mocks.NewService() ts := newServer(svc) defer ts.Close() twin := twins.Twin{} def := twins.Definition{} + repoCall := auth.On("Identify", mock.Anything, &mainflux.IdentityReq{Token: token}).Return(&mainflux.IdentityRes{Id: testsutil.GenerateUUID(t)}, nil) stw, err := svc.AddTwin(context.Background(), token, twin, def) require.Nil(t, err, fmt.Sprintf("unexpected error: %s", err)) + repoCall.Unset() twin.Name = twinName data, err := toJSON(twin) @@ -258,7 +265,7 @@ func TestUpdateTwin(t *testing.T) { req: data, id: stw.ID, contentType: contentType, - auth: wrongValue, + auth: authmocks.InvalidValue, status: http.StatusUnauthorized, }, { @@ -303,6 +310,7 @@ func TestUpdateTwin(t *testing.T) { } for _, tc := range cases { + repoCall := auth.On("Identify", mock.Anything, &mainflux.IdentityReq{Token: tc.auth}).Return(&mainflux.IdentityRes{Id: testsutil.GenerateUUID(t)}, nil) req := testRequest{ client: ts.Client(), method: http.MethodPut, @@ -314,18 +322,21 @@ func TestUpdateTwin(t *testing.T) { res, err := req.make() assert.Nil(t, err, fmt.Sprintf("%s: unexpected error %s", tc.desc, err)) assert.Equal(t, tc.status, res.StatusCode, fmt.Sprintf("%s: expected status code %d got %d", tc.desc, tc.status, res.StatusCode)) + repoCall.Unset() } } func TestViewTwin(t *testing.T) { - svc := mocks.NewService(map[string]string{token: email}) + svc, auth := mocks.NewService() ts := newServer(svc) defer ts.Close() def := twins.Definition{} twin := twins.Twin{} + repoCall := auth.On("Identify", mock.Anything, &mainflux.IdentityReq{Token: token}).Return(&mainflux.IdentityRes{Id: testsutil.GenerateUUID(t)}, nil) stw, err := svc.AddTwin(context.Background(), token, twin, def) require.Nil(t, err, fmt.Sprintf("unexpected error: %s", err)) + repoCall.Unset() twres := twinRes{ Owner: stw.Owner, @@ -359,7 +370,7 @@ func TestViewTwin(t *testing.T) { { desc: "view twin by passing invalid token", id: stw.ID, - auth: wrongValue, + auth: authmocks.InvalidValue, status: http.StatusUnauthorized, res: twinRes{}, }, @@ -380,6 +391,7 @@ func TestViewTwin(t *testing.T) { } for _, tc := range cases { + repoCall := auth.On("Identify", mock.Anything, &mainflux.IdentityReq{Token: tc.auth}).Return(&mainflux.IdentityRes{Id: testsutil.GenerateUUID(t)}, nil) req := testRequest{ client: ts.Client(), method: http.MethodGet, @@ -394,23 +406,27 @@ func TestViewTwin(t *testing.T) { err = json.NewDecoder(res.Body).Decode(&resData) assert.Nil(t, err, fmt.Sprintf("%s: got unexpected error while decoding response body: %s\n", tc.desc, err)) assert.Equal(t, tc.res, resData, fmt.Sprintf("%s: expected body %v got %v", tc.desc, tc.res, resData)) + repoCall.Unset() } } func TestListTwins(t *testing.T) { - svc := mocks.NewService(map[string]string{token: email}) + svc, auth := mocks.NewService() ts := newServer(svc) defer ts.Close() var data []twinRes + userID := testsutil.GenerateUUID(t) for i := 0; i < 100; i++ { name := fmt.Sprintf("%s-%d", twinName, i) twin := twins.Twin{ Owner: email, Name: name, } + repoCall := auth.On("Identify", mock.Anything, &mainflux.IdentityReq{Token: token}).Return(&mainflux.IdentityRes{Id: userID}, nil) tw, err := svc.AddTwin(context.Background(), token, twin, twins.Definition{}) assert.Nil(t, err, fmt.Sprintf("unexpected error: %s", err)) + repoCall.Unset() twres := twinRes{ Owner: tw.Owner, ID: tw.ID, @@ -439,7 +455,7 @@ func TestListTwins(t *testing.T) { }, { desc: "get a list of twins with invalid token", - auth: wrongValue, + auth: authmocks.InvalidValue, status: http.StatusUnauthorized, url: fmt.Sprintf(queryFmt, baseURL, 0, 1), res: nil, @@ -552,6 +568,7 @@ func TestListTwins(t *testing.T) { } for _, tc := range cases { + repoCall := auth.On("Identify", mock.Anything, &mainflux.IdentityReq{Token: tc.auth}).Return(&mainflux.IdentityRes{Id: userID}, nil) req := testRequest{ client: ts.Client(), method: http.MethodGet, @@ -569,18 +586,21 @@ func TestListTwins(t *testing.T) { assert.Equal(t, tc.status, res.StatusCode, fmt.Sprintf("%s: expected status code %d got %d", tc.desc, tc.status, res.StatusCode)) assert.ElementsMatch(t, tc.res, resData.Twins, fmt.Sprintf("%s: got incorrect list of twins", tc.desc)) + repoCall.Unset() } } func TestRemoveTwin(t *testing.T) { - svc := mocks.NewService(map[string]string{token: email}) + svc, auth := mocks.NewService() ts := newServer(svc) defer ts.Close() def := twins.Definition{} twin := twins.Twin{} + repoCall := auth.On("Identify", mock.Anything, &mainflux.IdentityReq{Token: token}).Return(&mainflux.IdentityRes{Id: testsutil.GenerateUUID(t)}, nil) stw, err := svc.AddTwin(context.Background(), token, twin, def) require.Nil(t, err, fmt.Sprintf("unexpected error: %s", err)) + repoCall.Unset() cases := []struct { desc string @@ -609,7 +629,7 @@ func TestRemoveTwin(t *testing.T) { { desc: "delete twin with invalid token", id: stw.ID, - auth: wrongValue, + auth: authmocks.InvalidValue, status: http.StatusUnauthorized, }, { @@ -621,6 +641,7 @@ func TestRemoveTwin(t *testing.T) { } for _, tc := range cases { + repoCall := auth.On("Identify", mock.Anything, &mainflux.IdentityReq{Token: tc.auth}).Return(&mainflux.IdentityRes{Id: testsutil.GenerateUUID(t)}, nil) req := testRequest{ client: ts.Client(), method: http.MethodDelete, @@ -630,5 +651,6 @@ func TestRemoveTwin(t *testing.T) { res, err := req.make() assert.Nil(t, err, fmt.Sprintf("%s: unexpected error %s", tc.desc, err)) assert.Equal(t, tc.status, res.StatusCode, fmt.Sprintf("%s: expected status code %d got %d", tc.desc, tc.status, res.StatusCode)) + repoCall.Unset() } } diff --git a/twins/mocks/service.go b/twins/mocks/service.go index 1f6458f8..69c87cd1 100644 --- a/twins/mocks/service.go +++ b/twins/mocks/service.go @@ -20,7 +20,7 @@ const publisher = "twins" var id = 0 // NewService use mock dependencies to create real twins service. -func NewService(tokens map[string]string) twins.Service { +func NewService() (twins.Service, *authmocks.Service) { auth := new(authmocks.Service) twinsRepo := NewTwinRepository() twinCache := NewTwinCache() @@ -29,7 +29,7 @@ func NewService(tokens map[string]string) twins.Service { subs := map[string]string{"chanID": "chanID"} broker := NewBroker(subs) - return twins.New(broker, auth, twinsRepo, twinCache, statesRepo, idProvider, "chanID", nil) + return twins.New(broker, auth, twinsRepo, twinCache, statesRepo, idProvider, "chanID", nil), auth } // CreateDefinition creates twin definition. diff --git a/twins/service_test.go b/twins/service_test.go index 163872b3..437b3ee2 100644 --- a/twins/service_test.go +++ b/twins/service_test.go @@ -8,21 +8,24 @@ import ( "fmt" "testing" + "github.com/mainflux/mainflux" + authmocks "github.com/mainflux/mainflux/auth/mocks" + "github.com/mainflux/mainflux/internal/testsutil" "github.com/mainflux/mainflux/pkg/errors" "github.com/mainflux/mainflux/twins" "github.com/mainflux/mainflux/twins/mocks" "github.com/mainflux/senml" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/mock" "github.com/stretchr/testify/require" ) const ( - twinName = "name" - wrongID = "" - token = "token" - wrongToken = "wrong-token" - email = "user@example.com" - numRecs = 100 + twinName = "name" + wrongID = "" + token = "token" + email = "user@example.com" + numRecs = 100 ) var ( @@ -31,7 +34,7 @@ var ( ) func TestAddTwin(t *testing.T) { - svc := mocks.NewService(map[string]string{token: email}) + svc, auth := mocks.NewService() twin := twins.Twin{} def := twins.Definition{} @@ -50,26 +53,30 @@ func TestAddTwin(t *testing.T) { { desc: "add twin with wrong credentials", twin: twin, - token: wrongToken, + token: authmocks.InvalidValue, err: errors.ErrAuthentication, }, } for _, tc := range cases { + repoCall := auth.On("Identify", mock.Anything, &mainflux.IdentityReq{Token: tc.token}).Return(&mainflux.IdentityRes{Id: testsutil.GenerateUUID(t)}, nil) _, err := svc.AddTwin(context.Background(), tc.token, tc.twin, def) assert.Equal(t, tc.err, err, fmt.Sprintf("%s: expected %s got %s\n", tc.desc, tc.err, err)) + repoCall.Unset() } } func TestUpdateTwin(t *testing.T) { - svc := mocks.NewService(map[string]string{token: email}) + svc, auth := mocks.NewService() twin := twins.Twin{} other := twins.Twin{} def := twins.Definition{} other.ID = wrongID + repoCall := auth.On("Identify", mock.Anything, &mainflux.IdentityReq{Token: token}).Return(&mainflux.IdentityRes{Id: testsutil.GenerateUUID(t)}, nil) saved, err := svc.AddTwin(context.Background(), token, twin, def) require.Nil(t, err, fmt.Sprintf("unexpected error: %s\n", err)) + repoCall.Unset() saved.Name = twinName @@ -88,7 +95,7 @@ func TestUpdateTwin(t *testing.T) { { desc: "update twin with wrong credentials", twin: saved, - token: wrongToken, + token: authmocks.InvalidValue, err: errors.ErrAuthentication, }, { @@ -100,17 +107,21 @@ func TestUpdateTwin(t *testing.T) { } for _, tc := range cases { + repoCall := auth.On("Identify", mock.Anything, &mainflux.IdentityReq{Token: tc.token}).Return(&mainflux.IdentityRes{Id: testsutil.GenerateUUID(t)}, nil) err := svc.UpdateTwin(context.Background(), tc.token, tc.twin, def) assert.Equal(t, tc.err, err, fmt.Sprintf("%s: expected %s got %s\n", tc.desc, tc.err, err)) + repoCall.Unset() } } func TestViewTwin(t *testing.T) { - svc := mocks.NewService(map[string]string{token: email}) + svc, auth := mocks.NewService() twin := twins.Twin{} def := twins.Definition{} + repoCall := auth.On("Identify", mock.Anything, &mainflux.IdentityReq{Token: token}).Return(&mainflux.IdentityRes{Id: testsutil.GenerateUUID(t)}, nil) saved, err := svc.AddTwin(context.Background(), token, twin, def) require.Nil(t, err, fmt.Sprintf("unexpected error: %s\n", err)) + repoCall.Unset() cases := []struct { desc string @@ -127,7 +138,7 @@ func TestViewTwin(t *testing.T) { { desc: "view twin with wrong credentials", id: saved.ID, - token: wrongToken, + token: authmocks.InvalidValue, err: errors.ErrAuthentication, }, { @@ -139,13 +150,15 @@ func TestViewTwin(t *testing.T) { } for _, tc := range cases { + repoCall := auth.On("Identify", mock.Anything, &mainflux.IdentityReq{Token: tc.token}).Return(&mainflux.IdentityRes{Id: testsutil.GenerateUUID(t)}, nil) _, err := svc.ViewTwin(context.Background(), tc.token, tc.id) assert.Equal(t, tc.err, err, fmt.Sprintf("%s: expected %s got %s\n", tc.desc, tc.err, err)) + repoCall.Unset() } } func TestListTwins(t *testing.T) { - svc := mocks.NewService(map[string]string{token: email}) + svc, auth := mocks.NewService() twin := twins.Twin{Name: twinName, Owner: email} def := twins.Definition{} m := make(map[string]interface{}) @@ -154,8 +167,10 @@ func TestListTwins(t *testing.T) { n := uint64(10) for i := uint64(0); i < n; i++ { + repoCall := auth.On("Identify", mock.Anything, &mainflux.IdentityReq{Token: token}).Return(&mainflux.IdentityRes{Id: testsutil.GenerateUUID(t)}, nil) _, err := svc.AddTwin(context.Background(), token, twin, def) require.Nil(t, err, fmt.Sprintf("unexpected error: %s\n", err)) + repoCall.Unset() } cases := []struct { @@ -193,7 +208,7 @@ func TestListTwins(t *testing.T) { }, { desc: "list with wrong credentials", - token: wrongToken, + token: authmocks.InvalidValue, limit: 0, offset: n, err: errors.ErrAuthentication, @@ -201,19 +216,21 @@ func TestListTwins(t *testing.T) { } for _, tc := range cases { - page, err := svc.ListTwins(context.Background(), tc.token, tc.offset, tc.limit, twinName, tc.metadata) - size := uint64(len(page.Twins)) - assert.Equal(t, tc.size, size, fmt.Sprintf("%s: expected %d got %d\n", tc.desc, tc.size, size)) + repoCall := auth.On("Identify", mock.Anything, &mainflux.IdentityReq{Token: tc.token}).Return(&mainflux.IdentityRes{Id: testsutil.GenerateUUID(t)}, nil) + _, err := svc.ListTwins(context.Background(), tc.token, tc.offset, tc.limit, twinName, tc.metadata) assert.Equal(t, tc.err, err, fmt.Sprintf("%s: expected %s got %s\n", tc.desc, tc.err, err)) + repoCall.Unset() } } func TestRemoveTwin(t *testing.T) { - svc := mocks.NewService(map[string]string{token: email}) + svc, auth := mocks.NewService() twin := twins.Twin{} def := twins.Definition{} + repoCall := auth.On("Identify", mock.Anything, &mainflux.IdentityReq{Token: token}).Return(&mainflux.IdentityRes{Id: testsutil.GenerateUUID(t)}, nil) saved, err := svc.AddTwin(context.Background(), token, twin, def) require.Nil(t, err, fmt.Sprintf("unexpected error: %s\n", err)) + repoCall.Unset() cases := []struct { desc string @@ -224,7 +241,7 @@ func TestRemoveTwin(t *testing.T) { { desc: "remove twin with wrong credentials", id: saved.ID, - token: wrongToken, + token: authmocks.InvalidValue, err: errors.ErrAuthentication, }, { @@ -248,24 +265,30 @@ func TestRemoveTwin(t *testing.T) { } for _, tc := range cases { + repoCall := auth.On("Identify", mock.Anything, &mainflux.IdentityReq{Token: tc.token}).Return(&mainflux.IdentityRes{Id: testsutil.GenerateUUID(t)}, nil) err := svc.RemoveTwin(context.Background(), tc.token, tc.id) assert.Equal(t, tc.err, err, fmt.Sprintf("%s: expected %s got %s\n", tc.desc, tc.err, err)) + repoCall.Unset() } } func TestSaveStates(t *testing.T) { - svc := mocks.NewService(map[string]string{token: email}) + svc, auth := mocks.NewService() twin := twins.Twin{Owner: email} def := mocks.CreateDefinition(channels[0:2], subtopics[0:2]) attr := def.Attributes[0] attrSansTwin := mocks.CreateDefinition(channels[2:3], subtopics[2:3]).Attributes[0] + repoCall := auth.On("Identify", mock.Anything, &mainflux.IdentityReq{Token: token}).Return(&mainflux.IdentityRes{Id: testsutil.GenerateUUID(t)}, nil) tw, err := svc.AddTwin(context.Background(), token, twin, def) require.Nil(t, err, fmt.Sprintf("unexpected error: %s", err)) + repoCall.Unset() defWildcard := mocks.CreateDefinition(channels[0:2], []string{twins.SubtopicWildcard, twins.SubtopicWildcard}) + repoCall = auth.On("Identify", mock.Anything, &mainflux.IdentityReq{Token: token}).Return(&mainflux.IdentityRes{Id: testsutil.GenerateUUID(t)}, nil) twWildcard, err := svc.AddTwin(context.Background(), token, twin, defWildcard) require.Nil(t, err, fmt.Sprintf("unexpected error: %s", err)) + repoCall.Unset() recs := make([]senml.Record, numRecs) mocks.CreateSenML(recs) @@ -310,6 +333,7 @@ func TestSaveStates(t *testing.T) { } for _, tc := range cases { + repoCall := auth.On("Identify", mock.Anything, &mainflux.IdentityReq{Token: token}).Return(&mainflux.IdentityRes{Id: testsutil.GenerateUUID(t)}, nil) message, err := mocks.CreateMessage(tc.attr, tc.recs) assert.Nil(t, err, fmt.Sprintf("unexpected error: %s", err)) @@ -324,29 +348,36 @@ func TestSaveStates(t *testing.T) { page, err = svc.ListStates(context.TODO(), token, 0, 10, twWildcard.ID) assert.Nil(t, err, fmt.Sprintf("unexpected error: %s", err)) assert.Equal(t, ttlAdded, page.Total, fmt.Sprintf("%s: expected %d total got %d total\n", tc.desc, ttlAdded, page.Total)) + repoCall.Unset() } } func TestListStates(t *testing.T) { - svc := mocks.NewService(map[string]string{token: email}) + svc, auth := mocks.NewService() twin := twins.Twin{Owner: email} def := mocks.CreateDefinition(channels[0:2], subtopics[0:2]) attr := def.Attributes[0] + repoCall := auth.On("Identify", mock.Anything, &mainflux.IdentityReq{Token: token}).Return(&mainflux.IdentityRes{Id: testsutil.GenerateUUID(t)}, nil) tw, err := svc.AddTwin(context.Background(), token, twin, def) require.Nil(t, err, fmt.Sprintf("unexpected error: %s", err)) + repoCall.Unset() + repoCall = auth.On("Identify", mock.Anything, &mainflux.IdentityReq{Token: token}).Return(&mainflux.IdentityRes{Id: testsutil.GenerateUUID(t)}, nil) tw2, err := svc.AddTwin(context.Background(), token, twins.Twin{Owner: email}, mocks.CreateDefinition(channels[2:3], subtopics[2:3])) require.Nil(t, err, fmt.Sprintf("unexpected error: %s", err)) + repoCall.Unset() recs := make([]senml.Record, numRecs) mocks.CreateSenML(recs) message, err := mocks.CreateMessage(attr, recs) require.Nil(t, err, fmt.Sprintf("unexpected error: %s", err)) + repoCall = auth.On("Identify", mock.Anything, &mainflux.IdentityReq{Token: token}).Return(&mainflux.IdentityRes{Id: testsutil.GenerateUUID(t)}, nil) err = svc.SaveStates(context.Background(), message) require.Nil(t, err, fmt.Sprintf("unexpected error: %s", err)) + repoCall.Unset() cases := []struct { desc string @@ -396,7 +427,7 @@ func TestListStates(t *testing.T) { { desc: "get a list with wrong user token", id: tw.ID, - token: wrongToken, + token: authmocks.InvalidValue, offset: 0, limit: 10, size: 0, @@ -423,8 +454,10 @@ func TestListStates(t *testing.T) { } for _, tc := range cases { + repoCall := auth.On("Identify", mock.Anything, &mainflux.IdentityReq{Token: tc.token}).Return(&mainflux.IdentityRes{Id: testsutil.GenerateUUID(t)}, nil) page, err := svc.ListStates(context.TODO(), tc.token, tc.offset, tc.limit, tc.id) assert.Equal(t, tc.err, err, fmt.Sprintf("%s: expected %s got %s\n", tc.desc, tc.err, err)) assert.Equal(t, tc.size, len(page.States), fmt.Sprintf("%s: expected %d total got %d total\n", tc.desc, tc.size, len(page.States))) + repoCall.Unset() } }