Skip to content

Commit

Permalink
test: fix tests
Browse files Browse the repository at this point in the history
Signed-off-by: 1998-felix <felix.gateru@gmail.com>
  • Loading branch information
felixgateru committed Aug 29, 2024
1 parent 3b36b00 commit 8a7c8c3
Showing 1 changed file with 72 additions and 80 deletions.
152 changes: 72 additions & 80 deletions things/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -428,8 +428,6 @@ func TestListClients(t *testing.T) {
retrieveAllResponse mgclients.ClientsPage
listPermissionsResponse *magistrala.ListPermissionsRes
response mgclients.ClientsPage
id string
size uint64
identifyErr error
authorizeErr error
retrieveAllErr error
Expand Down Expand Up @@ -483,7 +481,6 @@ func TestListClients(t *testing.T) {
},
{
desc: "list all clients as non admin with empty domain id",
id: nonAdminID,
page: mgclients.Page{
Offset: 0,
Limit: 100,
Expand Down Expand Up @@ -545,14 +542,14 @@ func TestListClients(t *testing.T) {
ObjectType: authsvc.DomainType,
Object: tc.identifyResponse.DomainId,
}).Return(tc.authorizeResponse, tc.authorizeErr)
searchClientsCall := cRepo.On("RetrieveAll", mock.Anything, mock.Anything).Return(tc.retrieveAllResponse, tc.retrieveAllErr)
retrieveAllCall := cRepo.On("RetrieveAll", mock.Anything, mock.Anything).Return(tc.retrieveAllResponse, tc.retrieveAllErr)
listPermissionsCall := auth.On("ListPermissions", mock.Anything, mock.Anything).Return(tc.listPermissionsResponse, tc.listPermissionsErr)
page, err := svc.ListClients(context.Background(), tc.token, tc.page)
assert.True(t, errors.Contains(err, tc.err), fmt.Sprintf("%s: expected %s got %s\n", tc.desc, tc.err, err))
assert.Equal(t, tc.response, page, fmt.Sprintf("%s: expected %v got %v\n", tc.desc, tc.response, page))
repoCall.Unset()
authorizeCall.Unset()
searchClientsCall.Unset()
retrieveAllCall.Unset()
listPermissionsCall.Unset()
}

Expand All @@ -565,16 +562,13 @@ func TestListClients(t *testing.T) {
authorizeResponse *magistrala.AuthorizeRes
domainCallResponse *magistrala.AuthorizeRes
listObjectsResponse *magistrala.ListObjectsRes
listObjectsResponse1 *magistrala.ListObjectsRes
retrieveAllResponse mgclients.ClientsPage
listPermissionsResponse *magistrala.ListPermissionsRes
response mgclients.ClientsPage
size uint64
identifyErr error
authorizeErr error
domainCallErr error
listObjectsErr error
listObjectsErr1 error
retrieveAllErr error
listPermissionsErr error
err error
Expand All @@ -593,7 +587,6 @@ func TestListClients(t *testing.T) {
authorizeResponse: &magistrala.AuthorizeRes{Authorized: false},
domainCallResponse: &magistrala.AuthorizeRes{Authorized: true},
listObjectsResponse: &magistrala.ListObjectsRes{Policies: []string{"test", "test"}},
listObjectsResponse1: &magistrala.ListObjectsRes{Policies: []string{"test", "test"}},
retrieveAllResponse: mgclients.ClientsPage{
Page: mgclients.Page{
Total: 2,
Expand All @@ -615,81 +608,81 @@ func TestListClients(t *testing.T) {
},
err: nil,
},
{
desc: "list all clients as admin with unauthorized user",
userKind: "admin",
token: validToken,
page: mgclients.Page{
Offset: 0,
Limit: 100,
ListPerms: true,
Domain: domainID,
},
identifyResponse: &magistrala.IdentityRes{Id: nonAdminID, UserId: nonAdminID, DomainId: domainID},
authorizeResponse: &magistrala.AuthorizeRes{Authorized: false},
domainCallResponse: &magistrala.AuthorizeRes{Authorized: false},
domainCallErr: svcerr.ErrAuthorization,
err: svcerr.ErrAuthorization,
},
{
desc: "list all clients as admin with failed to retrieve all",
userKind: "admin",
token: validToken,
page: mgclients.Page{
Offset: 0,
Limit: 100,
ListPerms: true,
Domain: domainID,
{
desc: "list all clients as admin with unauthorized user",
userKind: "admin",
token: validToken,
page: mgclients.Page{
Offset: 0,
Limit: 100,
ListPerms: true,
Domain: domainID,
},
identifyResponse: &magistrala.IdentityRes{Id: nonAdminID, UserId: nonAdminID, DomainId: domainID},
authorizeResponse: &magistrala.AuthorizeRes{Authorized: false},
domainCallResponse: &magistrala.AuthorizeRes{Authorized: false},
domainCallErr: svcerr.ErrAuthorization,
err: svcerr.ErrAuthorization,
},
identifyResponse: &magistrala.IdentityRes{Id: nonAdminID, UserId: nonAdminID, DomainId: domainID},
authorizeResponse: &magistrala.AuthorizeRes{Authorized: false},
domainCallResponse: &magistrala.AuthorizeRes{Authorized: true},
listObjectsResponse: &magistrala.ListObjectsRes{Policies: []string{"test", "test"}},
retrieveAllResponse: mgclients.ClientsPage{},
retrieveAllErr: repoerr.ErrNotFound,
err: svcerr.ErrNotFound,
},
{
desc: "list all clients as admin with failed to list permissions",
token: validToken,
page: mgclients.Page{
Offset: 0,
Limit: 100,
ListPerms: true,
Domain: domainID,
{
desc: "list all clients as admin with failed to retrieve all",
userKind: "admin",
token: validToken,
page: mgclients.Page{
Offset: 0,
Limit: 100,
ListPerms: true,
Domain: domainID,
},
identifyResponse: &magistrala.IdentityRes{Id: nonAdminID, UserId: nonAdminID, DomainId: domainID},
authorizeResponse: &magistrala.AuthorizeRes{Authorized: false},
domainCallResponse: &magistrala.AuthorizeRes{Authorized: true},
listObjectsResponse: &magistrala.ListObjectsRes{Policies: []string{"test", "test"}},
retrieveAllResponse: mgclients.ClientsPage{},
retrieveAllErr: repoerr.ErrNotFound,
err: svcerr.ErrNotFound,
},
identifyResponse: &magistrala.IdentityRes{Id: nonAdminID, UserId: nonAdminID, DomainId: domainID},
authorizeResponse: &magistrala.AuthorizeRes{Authorized: false},
domainCallResponse: &magistrala.AuthorizeRes{Authorized: true},
listObjectsResponse: &magistrala.ListObjectsRes{Policies: []string{"test", "test"}},
retrieveAllResponse: mgclients.ClientsPage{
Page: mgclients.Page{
Total: 2,
Offset: 0,
Limit: 100,
{
desc: "list all clients as admin with failed to list permissions",
token: validToken,
page: mgclients.Page{
Offset: 0,
Limit: 100,
ListPerms: true,
Domain: domainID,
},
Clients: []mgclients.Client{client, client},
identifyResponse: &magistrala.IdentityRes{Id: nonAdminID, UserId: nonAdminID, DomainId: domainID},
authorizeResponse: &magistrala.AuthorizeRes{Authorized: false},
domainCallResponse: &magistrala.AuthorizeRes{Authorized: true},
listObjectsResponse: &magistrala.ListObjectsRes{Policies: []string{"test", "test"}},
retrieveAllResponse: mgclients.ClientsPage{
Page: mgclients.Page{
Total: 2,
Offset: 0,
Limit: 100,
},
Clients: []mgclients.Client{client, client},
},
listPermissionsResponse: &magistrala.ListPermissionsRes{},
listPermissionsErr: svcerr.ErrNotFound,
err: svcerr.ErrNotFound,
},
listPermissionsResponse: &magistrala.ListPermissionsRes{},
listPermissionsErr: svcerr.ErrNotFound,
err: svcerr.ErrNotFound,
},
{
desc: "list all clients as admin with failed to list objects",
token: validToken,
page: mgclients.Page{
Offset: 0,
Limit: 100,
ListPerms: true,
Domain: domainID,
{
desc: "list all clients as admin with failed to list objects",
token: validToken,
page: mgclients.Page{
Offset: 0,
Limit: 100,
ListPerms: true,
Domain: domainID,
},
identifyResponse: &magistrala.IdentityRes{Id: nonAdminID, UserId: nonAdminID, DomainId: domainID},
authorizeResponse: &magistrala.AuthorizeRes{Authorized: false},
domainCallResponse: &magistrala.AuthorizeRes{Authorized: true},
listObjectsResponse: &magistrala.ListObjectsRes{},
listObjectsErr: svcerr.ErrNotFound,
err: svcerr.ErrNotFound,
},
identifyResponse: &magistrala.IdentityRes{Id: nonAdminID, UserId: nonAdminID, DomainId: domainID},
authorizeResponse: &magistrala.AuthorizeRes{Authorized: false},
domainCallResponse: &magistrala.AuthorizeRes{Authorized: true},
listObjectsResponse: &magistrala.ListObjectsRes{},
listObjectsErr: svcerr.ErrNotFound,
err: svcerr.ErrNotFound,
},
}

for _, tc := range cases2 {
Expand All @@ -703,7 +696,6 @@ func TestListClients(t *testing.T) {
Object: tc.identifyResponse.GetDomainId(),
}).Return(tc.authorizeResponse, tc.authorizeErr)
domainCall := auth.On("Authorize", mock.Anything, &magistrala.AuthorizeReq{
Domain: tc.identifyResponse.GetDomainId(),
SubjectType: authsvc.UserType,
SubjectKind: authsvc.UsersKind,
Subject: tc.identifyResponse.GetId(),
Expand Down

0 comments on commit 8a7c8c3

Please sign in to comment.