Skip to content

Commit

Permalink
fix and add tests for role assignment endpoints
Browse files Browse the repository at this point in the history
This adds http tests and fixes the http status codes for the role
assignment and unassignment endpoints to resolve 500 errors occurring
when provided role ids were didn't exist.

Signed-off-by: Mike Mason <[email protected]>
  • Loading branch information
mikemrm committed Mar 11, 2024
1 parent 5d0f308 commit a61e1f6
Show file tree
Hide file tree
Showing 3 changed files with 523 additions and 10 deletions.
31 changes: 24 additions & 7 deletions internal/api/assignments.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package api

import (
"errors"
"net/http"

"go.infratographer.com/x/gidx"

"go.infratographer.com/permissions-api/internal/query"
"go.infratographer.com/permissions-api/internal/types"

"github.com/labstack/echo/v4"
Expand Down Expand Up @@ -37,7 +39,7 @@ func (r *Router) assignmentCreate(c echo.Context) error {

assigneeResource, err := r.engine.NewResourceFromID(assigneeID)
if err != nil {
return echo.NewHTTPError(http.StatusBadRequest, "error creating resource").SetInternal(err)
return echo.NewHTTPError(http.StatusBadRequest, "error assigning subject").SetInternal(err)
}

subjectResource, err := r.currentSubject(c)
Expand All @@ -51,8 +53,13 @@ func (r *Router) assignmentCreate(c echo.Context) error {
}

resource, err := r.engine.GetRoleResource(ctx, roleResource)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "error getting resource").SetInternal(err)

switch {
case err == nil:
case errors.Is(err, query.ErrRoleNotFound):
return echo.NewHTTPError(http.StatusNotFound, "role not found").SetInternal(err)
default:
return echo.NewHTTPError(http.StatusInternalServerError, "error getting role").SetInternal(err)
}

if err := r.checkActionWithResponse(ctx, subjectResource, actionRoleUpdate, resource); err != nil {
Expand Down Expand Up @@ -96,8 +103,13 @@ func (r *Router) assignmentsList(c echo.Context) error {
}

resource, err := r.engine.GetRoleResource(ctx, roleResource)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "error getting resource").SetInternal(err)

switch {
case err == nil:
case errors.Is(err, query.ErrRoleNotFound):
return echo.NewHTTPError(http.StatusNotFound, "role not found").SetInternal(err)
default:
return echo.NewHTTPError(http.StatusInternalServerError, "error getting role").SetInternal(err)
}

if err := r.checkActionWithResponse(ctx, subjectResource, actionRoleGet, resource); err != nil {
Expand Down Expand Up @@ -169,8 +181,13 @@ func (r *Router) assignmentDelete(c echo.Context) error {
}

resource, err := r.engine.GetRoleResource(ctx, roleResource)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "error getting resource").SetInternal(err)

switch {
case err == nil:
case errors.Is(err, query.ErrRoleNotFound):
return echo.NewHTTPError(http.StatusNotFound, "role not found").SetInternal(err)
default:
return echo.NewHTTPError(http.StatusInternalServerError, "error getting role").SetInternal(err)
}

if err := r.checkActionWithResponse(ctx, subjectResource, actionRoleUpdate, resource); err != nil {
Expand Down
Loading

0 comments on commit a61e1f6

Please sign in to comment.