Skip to content

Commit

Permalink
Merge pull request #301 from canonical/fix/role-creation-use-name
Browse files Browse the repository at this point in the history
fix(role): use name for creation
  • Loading branch information
BarcoMasile authored May 6, 2024
2 parents cc928d0 + 5260248 commit 5a841a0
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
17 changes: 15 additions & 2 deletions pkg/roles/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,8 +199,21 @@ func (a *API) handleCreate(w http.ResponseWriter, r *http.Request) {
return

}

if role.ID != "" {
w.WriteHeader(http.StatusBadRequest)
json.NewEncoder(w).Encode(
types.Response{
Message: "Role ID field is not allowed to be passed in",
Status: http.StatusBadRequest,
},
)

return
}

user := a.userFromContext(r.Context())
err = a.service.CreateRole(r.Context(), user.ID, role.ID)
err = a.service.CreateRole(r.Context(), user.ID, role.Name)

if err != nil {

Expand All @@ -218,7 +231,7 @@ func (a *API) handleCreate(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusCreated)
json.NewEncoder(w).Encode(
types.Response{
Message: fmt.Sprintf("Created role %s", role.ID),
Message: fmt.Sprintf("Created role %s", role.Name),
Status: http.StatusCreated,
},
)
Expand Down
2 changes: 1 addition & 1 deletion pkg/roles/handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1205,7 +1205,7 @@ func TestHandleCreate(t *testing.T) {
mockService := NewMockServiceInterface(ctrl)

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

req := httptest.NewRequest(http.MethodPost, "/api/v0/roles", bytes.NewReader(payload))
Expand Down

0 comments on commit 5a841a0

Please sign in to comment.