Skip to content

Commit

Permalink
fix: sdk tests
Browse files Browse the repository at this point in the history
Signed-off-by: Rodney Osodo <[email protected]>
  • Loading branch information
rodneyosodo committed Apr 3, 2024
1 parent 726dff6 commit 6fcac71
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 35 deletions.
10 changes: 5 additions & 5 deletions pkg/sdk/go/channels_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ func TestCreateChannel(t *testing.T) {
Status: mgclients.EnabledStatus.String(),
},
token: token,
err: errors.NewSDKErrorWithStatus(repoerr.ErrCreateEntity, http.StatusUnprocessableEntity),
err: errors.NewSDKErrorWithStatus(errors.Wrap(svcerr.ErrCreateEntity, svcerr.ErrCreateEntity), http.StatusUnprocessableEntity),
},
{
desc: "create channel with missing name",
Expand Down Expand Up @@ -203,7 +203,7 @@ func TestListChannels(t *testing.T) {
token: invalidToken,
offset: offset,
limit: limit,
err: errors.NewSDKErrorWithStatus(svcerr.ErrAuthentication, http.StatusUnauthorized),
err: errors.NewSDKErrorWithStatus(errors.Wrap(svcerr.ErrAuthentication, svcerr.ErrAuthentication), http.StatusUnauthorized),
response: nil,
},
{
Expand Down Expand Up @@ -616,7 +616,7 @@ func TestListChannelsByThing(t *testing.T) {
clientID: testsutil.GenerateUUID(t),
page: sdk.PageMetadata{},
response: []sdk.Channel(nil),
err: errors.NewSDKErrorWithStatus(svcerr.ErrAuthentication, http.StatusUnauthorized),
err: errors.NewSDKErrorWithStatus(errors.Wrap(svcerr.ErrAuthentication, svcerr.ErrAuthentication), http.StatusUnauthorized),
},
}

Expand Down Expand Up @@ -659,7 +659,7 @@ func TestEnableChannel(t *testing.T) {
repoCall1 := grepo.On("RetrieveByID", mock.Anything, mock.Anything).Return(mggroups.Group{}, repoerr.ErrNotFound)
repoCall2 := grepo.On("ChangeStatus", mock.Anything, mock.Anything).Return(nil)
_, err := mgsdk.EnableChannel("wrongID", validToken)
assert.Equal(t, errors.NewSDKErrorWithStatus(svcerr.ErrNotFound, http.StatusNotFound), err, fmt.Sprintf("Enable channel with wrong id: expected %v got %v", svcerr.ErrNotFound, err))
assert.Equal(t, errors.NewSDKErrorWithStatus(errors.Wrap(svcerr.ErrViewEntity, svcerr.ErrNotFound), http.StatusNotFound), err, fmt.Sprintf("Enable channel with wrong id: expected %v got %v", svcerr.ErrNotFound, err))
ok := repoCall1.Parent.AssertCalled(t, "RetrieveByID", mock.Anything, "wrongID")
assert.True(t, ok, "RetrieveByID was not called on enabling channel")
repoCall.Unset()
Expand Down Expand Up @@ -711,7 +711,7 @@ func TestDisableChannel(t *testing.T) {
repoCall1 := grepo.On("ChangeStatus", mock.Anything, mock.Anything).Return(nil)
repoCall2 := grepo.On("RetrieveByID", mock.Anything, mock.Anything).Return(mggroups.Group{}, repoerr.ErrNotFound)
_, err := mgsdk.DisableChannel("wrongID", validToken)
assert.Equal(t, err, errors.NewSDKErrorWithStatus(svcerr.ErrNotFound, http.StatusNotFound), fmt.Sprintf("Disable channel with wrong id: expected %v got %v", svcerr.ErrNotFound, err))
assert.Equal(t, err, errors.NewSDKErrorWithStatus(errors.Wrap(svcerr.ErrViewEntity, svcerr.ErrNotFound), http.StatusNotFound), fmt.Sprintf("Disable channel with wrong id: expected %v got %v", svcerr.ErrNotFound, err))
ok := repoCall1.Parent.AssertCalled(t, "RetrieveByID", mock.Anything, "wrongID")
assert.True(t, ok, "Memberships was not called on disabling channel with wrong id")
repoCall.Unset()
Expand Down
12 changes: 6 additions & 6 deletions pkg/sdk/go/groups_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func TestCreateGroup(t *testing.T) {
ParentID: wrongID,
Status: clients.EnabledStatus.String(),
},
err: errors.NewSDKErrorWithStatus(svcerr.ErrCreateEntity, http.StatusUnprocessableEntity),
err: errors.NewSDKErrorWithStatus(errors.Wrap(svcerr.ErrCreateEntity, svcerr.ErrCreateEntity), http.StatusUnprocessableEntity),
},
{
desc: "create group with missing name",
Expand Down Expand Up @@ -203,7 +203,7 @@ func TestListGroups(t *testing.T) {
token: invalidToken,
offset: offset,
limit: limit,
err: errors.NewSDKErrorWithStatus(svcerr.ErrAuthentication, http.StatusUnauthorized),
err: errors.NewSDKErrorWithStatus(errors.Wrap(svcerr.ErrAuthentication, svcerr.ErrAuthentication), http.StatusUnauthorized),
response: nil,
},
{
Expand Down Expand Up @@ -333,7 +333,7 @@ func TestListParentGroups(t *testing.T) {
token: invalidToken,
offset: offset,
limit: limit,
err: errors.NewSDKErrorWithStatus(svcerr.ErrAuthentication, http.StatusUnauthorized),
err: errors.NewSDKErrorWithStatus(errors.Wrap(svcerr.ErrAuthentication, svcerr.ErrAuthentication), http.StatusUnauthorized),
response: nil,
},
{
Expand Down Expand Up @@ -464,7 +464,7 @@ func TestListChildrenGroups(t *testing.T) {
token: invalidToken,
offset: offset,
limit: limit,
err: errors.NewSDKErrorWithStatus(svcerr.ErrAuthentication, http.StatusUnauthorized),
err: errors.NewSDKErrorWithStatus(errors.Wrap(svcerr.ErrAuthentication, svcerr.ErrAuthentication), http.StatusUnauthorized),
response: nil,
},
{
Expand Down Expand Up @@ -796,7 +796,7 @@ func TestEnableGroup(t *testing.T) {
repoCall1 := grepo.On("RetrieveByID", mock.Anything, mock.Anything).Return(mggroups.Group{}, repoerr.ErrNotFound)
repoCall2 := grepo.On("ChangeStatus", mock.Anything, mock.Anything).Return(nil)
_, err := mgsdk.EnableGroup("wrongID", validToken)
assert.Equal(t, err, errors.NewSDKErrorWithStatus(svcerr.ErrNotFound, http.StatusNotFound), fmt.Sprintf("Enable group with wrong id: expected %v got %v", svcerr.ErrNotFound, err))
assert.Equal(t, err, errors.NewSDKErrorWithStatus(errors.Wrap(svcerr.ErrViewEntity, svcerr.ErrNotFound), http.StatusNotFound), fmt.Sprintf("Enable group with wrong id: expected %v got %v", svcerr.ErrNotFound, err))
ok := repoCall1.Parent.AssertCalled(t, "RetrieveByID", mock.Anything, "wrongID")
assert.True(t, ok, "RetrieveByID was not called on enabling group")
repoCall.Unset()
Expand Down Expand Up @@ -849,7 +849,7 @@ func TestDisableGroup(t *testing.T) {
repoCall1 := grepo.On("ChangeStatus", mock.Anything, mock.Anything).Return(nil)
repoCall2 := grepo.On("RetrieveByID", mock.Anything, mock.Anything).Return(mggroups.Group{}, repoerr.ErrNotFound)
_, err := mgsdk.DisableGroup("wrongID", validToken)
assert.Equal(t, err, errors.NewSDKErrorWithStatus(svcerr.ErrNotFound, http.StatusNotFound), fmt.Sprintf("Disable group with wrong id: expected %v got %v", svcerr.ErrNotFound, err))
assert.Equal(t, err, errors.NewSDKErrorWithStatus(errors.Wrap(svcerr.ErrViewEntity, svcerr.ErrNotFound), http.StatusNotFound), fmt.Sprintf("Disable group with wrong id: expected %v got %v", svcerr.ErrNotFound, err))
ok := repoCall1.Parent.AssertCalled(t, "RetrieveByID", mock.Anything, "wrongID")
assert.True(t, ok, "Memberships was not called on disabling group with wrong id")
repoCall.Unset()
Expand Down
2 changes: 1 addition & 1 deletion pkg/sdk/go/things_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -581,7 +581,7 @@ func TestListThingsByChannel(t *testing.T) {
channelID: wrongID,
page: sdk.PageMetadata{},
response: []sdk.Thing(nil),
err: errors.NewSDKErrorWithStatus(errors.Wrap(svcerr.ErrNotFound, svcerr.ErrNotFound), http.StatusNotFound),
err: errors.NewSDKErrorWithStatus(errors.Wrap(svcerr.ErrViewEntity, svcerr.ErrViewEntity), http.StatusUnprocessableEntity),
},
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/sdk/go/tokens_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"github.com/absmach/magistrala"
"github.com/absmach/magistrala/internal/apiutil"
"github.com/absmach/magistrala/pkg/errors"
repoerr "github.com/absmach/magistrala/pkg/errors/repository"
svcerr "github.com/absmach/magistrala/pkg/errors/service"
sdk "github.com/absmach/magistrala/pkg/sdk/go"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
Expand Down Expand Up @@ -69,7 +69,7 @@ func TestIssueToken(t *testing.T) {
login: sdk.Login{Identity: "invalid", Secret: "secret"},
token: &magistrala.Token{},
dbClient: wrongClient,
err: errors.NewSDKErrorWithStatus(errors.Wrap(repoerr.ErrNotFound, repoerr.ErrNotFound), http.StatusNotFound),
err: errors.NewSDKErrorWithStatus(errors.Wrap(svcerr.ErrViewEntity, svcerr.ErrViewEntity), http.StatusUnprocessableEntity),
},
}
for _, tc := range cases {
Expand Down
10 changes: 5 additions & 5 deletions pkg/sdk/go/users_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,23 +257,23 @@ func TestListClients(t *testing.T) {
token: invalidToken,
offset: offset,
limit: limit,
err: errors.NewSDKErrorWithStatus(errors.Wrap(repoerr.ErrNotFound, repoerr.ErrNotFound), http.StatusNotFound),
err: errors.NewSDKErrorWithStatus(errors.Wrap(svcerr.ErrViewEntity, svcerr.ErrViewEntity), http.StatusUnprocessableEntity),
response: nil,
},
{
desc: "get a list of users with empty token",
token: "",
offset: offset,
limit: limit,
err: errors.NewSDKErrorWithStatus(errors.Wrap(repoerr.ErrNotFound, repoerr.ErrNotFound), http.StatusNotFound),
err: errors.NewSDKErrorWithStatus(errors.Wrap(svcerr.ErrViewEntity, svcerr.ErrViewEntity), http.StatusUnprocessableEntity),
response: nil,
},
{
desc: "get a list of users with zero limit",
token: token,
offset: offset,
limit: 0,
err: errors.NewSDKErrorWithStatus(errors.Wrap(repoerr.ErrNotFound, repoerr.ErrNotFound), http.StatusNotFound),
err: errors.NewSDKErrorWithStatus(errors.Wrap(svcerr.ErrViewEntity, svcerr.ErrViewEntity), http.StatusUnprocessableEntity),
response: nil,
},
{
Expand Down Expand Up @@ -411,7 +411,7 @@ func TestClient(t *testing.T) {
response: sdk.User{},
token: validToken,
clientID: wrongID,
err: errors.NewSDKErrorWithStatus(errors.Wrap(svcerr.ErrNotFound, svcerr.ErrNotFound), http.StatusNotFound),
err: errors.NewSDKErrorWithStatus(errors.Wrap(svcerr.ErrViewEntity, svcerr.ErrViewEntity), http.StatusUnprocessableEntity),
},
{
desc: "view client with an invalid token and invalid client id",
Expand Down Expand Up @@ -821,7 +821,7 @@ func TestUpdateClientSecret(t *testing.T) {
token: validToken,
response: sdk.User{},
repoErr: apiutil.ErrMissingSecret,
err: errors.NewSDKErrorWithStatus(errors.Wrap(svcerr.ErrNotFound, repoerr.ErrMissingSecret), http.StatusBadRequest),
err: errors.NewSDKErrorWithStatus(errors.Wrap(svcerr.ErrViewEntity, repoerr.ErrMissingSecret), http.StatusBadRequest),
},
}

Expand Down
2 changes: 1 addition & 1 deletion users/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func (svc service) RegisterClient(ctx context.Context, token string, cli mgclien
defer func() {
if err != nil {
if errRollback := svc.addClientPolicyRollback(ctx, cli.ID, cli.Role); errRollback != nil {
err = errors.Wrap(err, errors.Wrap(repoerr.ErrRollbackTx, errRollback))
err = errors.Wrap(errors.Wrap(repoerr.ErrRollbackTx, errRollback), err)
}
}
}()
Expand Down
28 changes: 13 additions & 15 deletions users/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,11 @@ var (
Metadata: validCMetadata,
Status: mgclients.EnabledStatus,
}
validToken = "token"
inValidToken = "invalid"
validID = "d4ebb847-5d0e-4e46-bdd9-b6aceaaa3a22"
wrongID = testsutil.GenerateUUID(&testing.T{})
errHashPassword = errors.New("generate hash from password failed")
errAddPolicies = errors.New("failed to add policies")
errDeletePolicies = errors.New("failed to delete policies")
validToken = "token"
inValidToken = "invalid"
validID = "d4ebb847-5d0e-4e46-bdd9-b6aceaaa3a22"
wrongID = testsutil.GenerateUUID(&testing.T{})
errHashPassword = errors.New("generate hash from password failed")
)

func newService(selfRegister bool) (users.Service, *mocks.Repository, *authmocks.AuthClient, users.Emailer) {
Expand Down Expand Up @@ -225,8 +223,8 @@ func TestRegisterClient(t *testing.T) {
Role: mgclients.AdminRole,
},
addPoliciesResponse: &magistrala.AddPoliciesRes{Added: true},
addPoliciesResponseErr: errAddPolicies,
err: errAddPolicies,
addPoliciesResponseErr: svcerr.ErrAddPolicies,
err: svcerr.ErrAddPolicies,
},
{
desc: "register a new client with failed to delete policies with err",
Expand All @@ -240,9 +238,9 @@ func TestRegisterClient(t *testing.T) {
},
addPoliciesResponse: &magistrala.AddPoliciesRes{Added: true},
deletePoliciesResponse: &magistrala.DeletePoliciesRes{Deleted: false},
deletePoliciesResponseErr: errDeletePolicies,
deletePoliciesResponseErr: svcerr.ErrConflict,
saveErr: repoerr.ErrConflict,
err: errDeletePolicies,
err: svcerr.ErrConflict,
},
{
desc: "register a new client with failed to delete policies with failed to delete",
Expand All @@ -257,7 +255,7 @@ func TestRegisterClient(t *testing.T) {
addPoliciesResponse: &magistrala.AddPoliciesRes{Added: true},
deletePoliciesResponse: &magistrala.DeletePoliciesRes{Deleted: false},
saveErr: repoerr.ErrConflict,
err: svcerr.ErrAuthorization,
err: svcerr.ErrConflict,
},
}

Expand Down Expand Up @@ -1034,7 +1032,7 @@ func TestUpdateClientRole(t *testing.T) {
addPolicyResponse: &magistrala.AddPolicyRes{},
addPolicyErr: errors.ErrMalformedEntity,
token: validToken,
err: errAddPolicies,
err: svcerr.ErrAddPolicies,
},
{
desc: "update client role to user role successfully ",
Expand All @@ -1053,7 +1051,7 @@ func TestUpdateClientRole(t *testing.T) {
deletePolicyResponse: &magistrala.DeletePolicyRes{Deleted: false},
updateRoleResponse: mgclients.Client{},
token: validToken,
err: errDeletePolicies,
err: svcerr.ErrFailedPolicyUpdate,
},
{
desc: "update client role to user role with failed to delete policy with error",
Expand All @@ -1063,7 +1061,7 @@ func TestUpdateClientRole(t *testing.T) {
updateRoleResponse: mgclients.Client{},
token: validToken,
deletePolicyErr: svcerr.ErrMalformedEntity,
err: errDeletePolicies,
err: svcerr.ErrDeletePolicies,
},
{
desc: "Update client with failed repo update and roll back",
Expand Down

0 comments on commit 6fcac71

Please sign in to comment.