From e63fdaa70fb58e8c3001a23cc2ecc9e140e9cb81 Mon Sep 17 00:00:00 2001 From: barco Date: Mon, 6 May 2024 09:55:51 +0200 Subject: [PATCH 1/3] fix(role): use `Name` field for creation --- pkg/roles/handlers.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pkg/roles/handlers.go b/pkg/roles/handlers.go index 5d4791df8..b63354241 100644 --- a/pkg/roles/handlers.go +++ b/pkg/roles/handlers.go @@ -200,7 +200,7 @@ func (a *API) handleCreate(w http.ResponseWriter, r *http.Request) { } 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 { @@ -218,7 +218,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, }, ) From 2a46a5ec7a04a3aafbe6a899167acfe8ea8ff00e Mon Sep 17 00:00:00 2001 From: barco Date: Mon, 6 May 2024 09:57:14 +0200 Subject: [PATCH 2/3] fix(role): error out when ID is passed for creation --- pkg/roles/handlers.go | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/pkg/roles/handlers.go b/pkg/roles/handlers.go index b63354241..4412290fa 100644 --- a/pkg/roles/handlers.go +++ b/pkg/roles/handlers.go @@ -199,6 +199,19 @@ 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.Name) From 5260248722828bea770cd94920ef7e816f1446e8 Mon Sep 17 00:00:00 2001 From: barco Date: Mon, 6 May 2024 10:05:00 +0200 Subject: [PATCH 3/3] test: adjust test --- pkg/roles/handlers_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/roles/handlers_test.go b/pkg/roles/handlers_test.go index cd1e12a83..8904b4997 100644 --- a/pkg/roles/handlers_test.go +++ b/pkg/roles/handlers_test.go @@ -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))