Skip to content

Commit

Permalink
test: ajust service and handlers tests
Browse files Browse the repository at this point in the history
  • Loading branch information
BarcoMasile committed May 2, 2024
1 parent c10f131 commit a99e9eb
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
18 changes: 12 additions & 6 deletions pkg/roles/handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,10 @@ func TestHandleDetail(t *testing.T) {
input: "administrator",
expected: nil,
output: &types.Response{
Data: []string{"administrator"},
Data: []Role{{
ID: "administrator",
Name: "administrator",
}},
Message: "Rule detail",
Status: http.StatusOK,
},
Expand All @@ -216,12 +219,15 @@ func TestHandleDetail(t *testing.T) {
req := httptest.NewRequest(http.MethodGet, fmt.Sprintf("/api/v0/roles/%s", test.input), nil)

mockService.EXPECT().GetRole(gomock.Any(), "anonymous", test.input).DoAndReturn(
func(context.Context, string, string) (string, error) {
func(context.Context, string, string) (*Role, error) {
if test.expected != nil {
return "", test.expected
return nil, test.expected
}

return test.input, nil
return &Role{
ID: test.input,
Name: test.input,
}, nil

},
)
Expand All @@ -246,7 +252,7 @@ func TestHandleDetail(t *testing.T) {

// duplicate types.Response attribute we care and assign the proper type instead of interface{}
type Response struct {
Data []string `json:"data"`
Data []Role `json:"data"`
Message string `json:"message"`
Status int `json:"status"`
Meta *types.Pagination `json:"_meta"`
Expand Down Expand Up @@ -1198,7 +1204,7 @@ func TestHandleCreate(t *testing.T) {
mockMonitor := monitoring.NewMockMonitorInterface(ctrl)
mockService := NewMockServiceInterface(ctrl)

upr := new(RoleRequest)
upr := new(Role)
upr.ID = test.input
payload, _ := json.Marshal(upr)

Expand Down
2 changes: 1 addition & 1 deletion pkg/roles/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ func TestServiceGetRole(t *testing.T) {
t.Errorf("expected error to be %v got %v", test.expected.err, err)
}

if test.expected.err == nil && test.expected.check && role != test.input.role {
if test.expected.err == nil && test.expected.check && role.ID != test.input.role {
t.Errorf("invalid result, expected: %v, got: %v", test.input.role, role)
}
})
Expand Down

0 comments on commit a99e9eb

Please sign in to comment.