Skip to content

Commit

Permalink
refactor: fix tests and clean up
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 19, 2024
1 parent 706a498 commit e676ad5
Show file tree
Hide file tree
Showing 32 changed files with 1,482 additions and 1,561 deletions.
1 change: 1 addition & 0 deletions api/openapi/auth.yml
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,7 @@ paths:
summary: Retrieves service health check info.
tags:
- health
security: []
responses:
"200":
$ref: "#/components/responses/HealthRes"
Expand Down
9 changes: 4 additions & 5 deletions api/openapi/things.yml
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ paths:
description: Database can't process request.
"500":
$ref: "#/components/responses/ServiceError"

/things/members:
get:
operationId: listThingMembers
Expand Down Expand Up @@ -439,7 +439,6 @@ paths:
"500":
$ref: "#/components/responses/ServiceError"


/channels:
post:
operationId: createChannel
Expand All @@ -462,7 +461,7 @@ paths:
"403":
description: Failed to perform authorization over the entity.
"404":
descripttion: A non-existent entity request.
description: A non-existent entity request.
"409":
description: Failed due to using an existing identity.
"415":
Expand Down Expand Up @@ -589,7 +588,7 @@ paths:
"403":
description: Unauthorized access to thing id.
"404":
descripttion: A non-existent entity request.
description: A non-existent entity request.
"500":
$ref: "#/components/responses/ServiceError"

Expand Down Expand Up @@ -773,7 +772,6 @@ paths:
"500":
$ref: "#/components/responses/ServiceError"


/connect:
post:
operationId: connectThingsAndChannels
Expand Down Expand Up @@ -893,6 +891,7 @@ paths:
summary: Retrieves service health check info.
tags:
- health
security: []
responses:
"200":
$ref: "#/components/responses/HealthRes"
Expand Down
23 changes: 19 additions & 4 deletions api/openapi/users.yml
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,7 @@ paths:
"500":
$ref: "#/components/responses/ServiceError"

/users/search:
/users/search:
get:
operationId: searchUsers
summary: Search users
Expand Down Expand Up @@ -483,7 +483,6 @@ paths:
"500":
$ref: "#/components/responses/ServiceError"


/users/tokens/issue:
post:
operationId: issueToken
Expand Down Expand Up @@ -896,6 +895,7 @@ paths:
summary: Retrieves service health check info.
tags:
- health
security: []
responses:
"200":
$ref: "#/components/responses/HealthRes"
Expand Down Expand Up @@ -1560,11 +1560,26 @@ components:
in: query
schema:
type: string
enum: ["admin", "delete", "edit", "view", "share", "publish", "subscribe", "membership", "create", "administrator", "editor", "contributor", "member", "guest"]
enum:
[
"admin",
"delete",
"edit",
"view",
"share",
"publish",
"subscribe",
"membership",
"create",
"administrator",
"editor",
"contributor",
"member",
"guest",
]
required: false
example: edit


ParentID:
name: parentID
description: Unique parent identifier for a group.
Expand Down
10 changes: 1 addition & 9 deletions auth/api/http/domains/endpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1222,12 +1222,6 @@ func TestListDomainsByUserID(t *testing.T) {
userID: validID,
err: nil,
},
{
desc: "list domains by user id with empty user id",
token: validToken,
status: http.StatusOK,
err: nil,
},
{
desc: "list domains by user id with empty token",
token: "",
Expand Down Expand Up @@ -1288,16 +1282,14 @@ func TestListDomainsByUserID(t *testing.T) {
req := testRequest{
client: ds.Client(),
method: http.MethodGet,
url: fmt.Sprintf("%s/domains?users=%s", ds.URL, tc.userID) + tc.query,
url: fmt.Sprintf("%s/domains?user=%s", ds.URL, tc.userID) + tc.query,
token: tc.token,
}
svcCall := svc.On("ListUserDomains", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(tc.listDomainsRequest, tc.svcErr)
svcCall1 := svc.On("ListDomains", mock.Anything, mock.Anything, mock.Anything).Return(tc.listDomainsRequest, tc.svcErr)
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))
svcCall.Unset()
svcCall1.Unset()
}
}

Expand Down
6 changes: 3 additions & 3 deletions cli/channels_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,7 @@ func TestListConnectionsCmd(t *testing.T) {

for _, tc := range cases {
t.Run(tc.desc, func(t *testing.T) {
sdkCall := sdkMock.On("ThingsByChannel", tc.args[0], mock.Anything, tc.args[1]).Return(tc.page, tc.sdkErr)
sdkCall := sdkMock.On("ThingsByChannel", mock.Anything, tc.args[1]).Return(tc.page, tc.sdkErr)
out := executeCommand(t, rootCmd, append([]string{connsCmd}, tc.args...)...)
switch tc.logType {
case entityLog:
Expand Down Expand Up @@ -664,7 +664,7 @@ func TestUsersChannelCmd(t *testing.T) {

for _, tc := range cases {
t.Run(tc.desc, func(t *testing.T) {
sdkCall := sdkMock.On("ListChannelUsers", tc.args[0], mock.Anything, tc.args[1]).Return(tc.page, tc.sdkErr)
sdkCall := sdkMock.On("ListChannelUsers", mock.Anything, tc.args[1]).Return(tc.page, tc.sdkErr)
out := executeCommand(t, rootCmd, append([]string{usrCmd}, tc.args...)...)

switch tc.logType {
Expand Down Expand Up @@ -737,7 +737,7 @@ func TestListGroupCmd(t *testing.T) {
}
for _, tc := range cases {
t.Run(tc.desc, func(t *testing.T) {
sdkCall := sdkMock.On("ListChannelUserGroups", tc.args[0], mock.Anything, tc.args[1]).Return(tc.page, tc.sdkErr)
sdkCall := sdkMock.On("ListChannelUserGroups", mock.Anything, tc.args[1]).Return(tc.page, tc.sdkErr)
out := executeCommand(t, rootCmd, append([]string{grpCmd}, tc.args...)...)
switch tc.logType {
case entityLog:
Expand Down
2 changes: 1 addition & 1 deletion cli/domains_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ func TestListDomainUsers(t *testing.T) {

for _, tc := range cases {
t.Run(tc.desc, func(t *testing.T) {
sdkCall := sdkMock.On("ListDomainUsers", tc.args[0], mock.Anything, tc.args[1]).Return(tc.page, tc.sdkErr)
sdkCall := sdkMock.On("ListDomainUsers", mock.Anything, tc.args[1]).Return(tc.page, tc.sdkErr)
out := executeCommand(t, rootCmd, append([]string{usrCmd}, tc.args...)...)

switch tc.logType {
Expand Down
4 changes: 2 additions & 2 deletions cli/groups_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -506,7 +506,7 @@ func TestListUsersCmd(t *testing.T) {
}
for _, tc := range cases {
t.Run(tc.desc, func(t *testing.T) {
sdkCall := sdkMock.On("ListGroupUsers", tc.args[0], mock.Anything, tc.args[1]).Return(tc.page, tc.sdkErr)
sdkCall := sdkMock.On("ListGroupUsers", mock.Anything, tc.args[1]).Return(tc.page, tc.sdkErr)
out := executeCommand(t, rootCmd, append([]string{usrCmd}, tc.args...)...)
switch tc.logType {
case entityLog:
Expand Down Expand Up @@ -578,7 +578,7 @@ func TestListChannelsCmd(t *testing.T) {
}
for _, tc := range cases {
t.Run(tc.desc, func(t *testing.T) {
sdkCall := sdkMock.On("ListGroupChannels", tc.args[0], mock.Anything, tc.args[1]).Return(tc.page, tc.sdkErr)
sdkCall := sdkMock.On("ListGroupChannels", mock.Anything, tc.args[1]).Return(tc.page, tc.sdkErr)
out := executeCommand(t, rootCmd, append([]string{chansCmd}, tc.args...)...)
switch tc.logType {
case entityLog:
Expand Down
2 changes: 1 addition & 1 deletion cli/things_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1026,7 +1026,7 @@ func TestListConnectionCmd(t *testing.T) {
}
for _, tc := range cases {
t.Run(tc.desc, func(t *testing.T) {
sdkCall := sdkMock.On("ChannelsByThing", tc.args[0], mock.Anything, tc.args[1]).Return(tc.page, tc.sdkErr)
sdkCall := sdkMock.On("ChannelsByThing", mock.Anything, tc.args[1]).Return(tc.page, tc.sdkErr)
out := executeCommand(t, rootCmd, append([]string{connsCmd}, tc.args...)...)

switch tc.logType {
Expand Down
8 changes: 4 additions & 4 deletions cli/users_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1233,7 +1233,7 @@ func TestListUserChannelsCmd(t *testing.T) {

for _, tc := range cases {
t.Run(tc.desc, func(t *testing.T) {
sdkCall := sdkMock.On("ListUserChannels", tc.args[0], mock.Anything, tc.args[1]).Return(tc.page, tc.sdkerr)
sdkCall := sdkMock.On("ListUserChannels", mock.Anything, tc.args[1]).Return(tc.page, tc.sdkerr)
out := executeCommand(t, rootCmd, append([]string{chansCmd}, tc.args...)...)

switch tc.logType {
Expand Down Expand Up @@ -1310,7 +1310,7 @@ func TestListUserThingsCmd(t *testing.T) {

for _, tc := range cases {
t.Run(tc.desc, func(t *testing.T) {
sdkCall := sdkMock.On("ListUserThings", tc.args[0], mock.Anything, tc.args[1]).Return(tc.page, tc.sdkerr)
sdkCall := sdkMock.On("ListUserThings", mock.Anything, tc.args[1]).Return(tc.page, tc.sdkerr)
out := executeCommand(t, rootCmd, append([]string{thsCmd}, tc.args...)...)

switch tc.logType {
Expand Down Expand Up @@ -1385,7 +1385,7 @@ func TestListUserDomainsCmd(t *testing.T) {
for _, tc := range cases {
t.Run(tc.desc, func(t *testing.T) {
var pg mgsdk.DomainsPage
sdkCall := sdkMock.On("ListUserDomains", tc.args[0], mock.Anything, tc.args[1]).Return(tc.page, tc.sdkerr)
sdkCall := sdkMock.On("ListUserDomains", mock.Anything, tc.args[1]).Return(tc.page, tc.sdkerr)
out := executeCommand(t, rootCmd, append([]string{domsCmd}, tc.args...)...)

switch tc.logType {
Expand Down Expand Up @@ -1460,7 +1460,7 @@ func TestListUserGroupsCmd(t *testing.T) {
for _, tc := range cases {
t.Run(tc.desc, func(t *testing.T) {
var pg mgsdk.GroupsPage
sdkCall := sdkMock.On("ListUserGroups", tc.args[0], mock.Anything, tc.args[1]).Return(tc.page, tc.sdkerr)
sdkCall := sdkMock.On("ListUserGroups", mock.Anything, tc.args[1]).Return(tc.page, tc.sdkerr)
out := executeCommand(t, rootCmd, append([]string{grpCmd}, tc.args...)...)

switch tc.logType {
Expand Down
2 changes: 1 addition & 1 deletion internal/groups/api/decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ var (
queryKeys = []string{api.ThingKey, api.ChannelKey, api.DomainKey, api.GroupKey, api.UserKey}
entityTypes = map[string]string{
api.ThingKey: auth.ThingsKind,
api.ChannelKey: auth.GroupsKind,
api.ChannelKey: auth.ChannelsKind,
api.DomainKey: auth.DomainsKind,
api.GroupKey: auth.GroupsKind,
api.UserKey: auth.UsersKind,
Expand Down
92 changes: 90 additions & 2 deletions internal/groups/api/decode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func TestDecodeListGroupsRequest(t *testing.T) {
desc: "valid request with all parameters",
url: "http://localhost:8080?status=enabled&offset=10&limit=10&name=random&metadata={\"test\":\"test\"}&level=2&parent_id=random&tree=true&dir=-1&member_kind=random&permission=random&list_perms=true",
header: map[string][]string{
"Authorization": {"Bearer 123"},
"Authorization": {"Bearer validToken"},
},
resp: listGroupsReq{
Page: groups.Page{
Expand All @@ -65,11 +65,99 @@ func TestDecodeListGroupsRequest(t *testing.T) {
Direction: -1,
ListPerms: true,
},
token: "123",
token: "validToken",
tree: true,
},
err: nil,
},
{
desc: "valid request with user",
url: "http://localhost:8080?user=valid",
header: map[string][]string{
"Authorization": {"Bearer validToken"},
},
resp: listGroupsReq{
Page: groups.Page{
PageMeta: groups.PageMeta{
Status: clients.EnabledStatus,
Offset: 0,
Limit: 10,
},
Permission: api.DefPermission,
Direction: -1,
},
token: "validToken",
memberKind: "users",
memberID: "valid",
},
err: nil,
},
{
desc: "valid request with group",
url: "http://localhost:8080?group=valid",
header: map[string][]string{
"Authorization": {"Bearer validToken"},
},
resp: listGroupsReq{
Page: groups.Page{
PageMeta: groups.PageMeta{
Status: clients.EnabledStatus,
Offset: 0,
Limit: 10,
},
Permission: api.DefPermission,
Direction: -1,
},
token: "validToken",
memberKind: "groups",
memberID: "valid",
},
err: nil,
},
{
desc: "valid request with domain",
url: "http://localhost:8080?domain=valid",
header: map[string][]string{
"Authorization": {"Bearer validToken"},
},
resp: listGroupsReq{
Page: groups.Page{
PageMeta: groups.PageMeta{
Status: clients.EnabledStatus,
Offset: 0,
Limit: 10,
},
Permission: api.DefPermission,
Direction: -1,
},
token: "validToken",
memberKind: "domains",
memberID: "valid",
},
err: nil,
},
{
desc: "valid request with channel",
url: "http://localhost:8080?channel=valid",
header: map[string][]string{
"Authorization": {"Bearer validToken"},
},
resp: listGroupsReq{
Page: groups.Page{
PageMeta: groups.PageMeta{
Status: clients.EnabledStatus,
Offset: 0,
Limit: 10,
},
Permission: api.DefPermission,
Direction: -1,
},
token: "validToken",
memberKind: "channels",
memberID: "valid",
},
err: nil,
},
{
desc: "valid request with invalid page metadata",
url: "http://localhost:8080?metadata=random",
Expand Down
2 changes: 1 addition & 1 deletion internal/groups/api/endpoints.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ func DisableGroupEndpoint(svc groups.Service) endpoint.Endpoint {
func ListGroupsEndpoint(svc groups.Service, groupType, memberKind string) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (interface{}, error) {
req := request.(listGroupsReq)
if memberKind != "" {
if memberKind != "" && req.memberKind == "" {
req.memberKind = memberKind
}
if err := req.validate(); err != nil {
Expand Down
Loading

0 comments on commit e676ad5

Please sign in to comment.