Skip to content

Commit

Permalink
v1/handlers: Return 404 correctly when Blueprint not found
Browse files Browse the repository at this point in the history
  • Loading branch information
ezr-ondrej committed Sep 26, 2024
1 parent c9753e5 commit 9b739f8
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
6 changes: 6 additions & 0 deletions internal/v1/handler_blueprints.go
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,9 @@ func (h *Handlers) UpdateBlueprint(ctx echo.Context, blueprintId uuid.UUID) erro
if blueprintRequest.Customizations.Users != nil {
be, err := h.server.db.GetBlueprint(ctx.Request().Context(), blueprintId, userID.OrgID(), nil)
if err != nil {
if errors.Is(err, db.BlueprintNotFoundError) {
return echo.NewHTTPError(http.StatusNotFound, err)
}
return err
}
eb, err := BlueprintFromEntry(be)
Expand Down Expand Up @@ -406,6 +409,9 @@ func (h *Handlers) ComposeBlueprint(ctx echo.Context, id openapi_types.UUID) err

blueprintEntry, err := h.server.db.GetBlueprint(ctx.Request().Context(), id, userID.OrgID(), nil)
if err != nil {
if errors.Is(err, db.BlueprintNotFoundError) {
return echo.NewHTTPError(http.StatusNotFound, err)
}
return err
}
blueprint, err := BlueprintFromEntryWithRedactedPasswords(blueprintEntry)
Expand Down
9 changes: 9 additions & 0 deletions internal/v1/handler_blueprints_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -499,9 +499,14 @@ func TestHandlers_UpdateBlueprint(t *testing.T) {
require.NoError(t, err)
require.Equal(t, "Invalid blueprint name", jsonResp.Errors[0].Title)

// Test non-existing blueprint
body["name"] = "Changing to correct body"
respStatusCodeNotFound, _ := tutils.PutResponseBody(t, db_srv.URL+fmt.Sprintf("/api/image-builder/v1/blueprints/%s", uuid.New()), body)
require.Equal(t, http.StatusNotFound, respStatusCodeNotFound)

body["customizations"] = map[string]interface{}{"users": []map[string]interface{}{{"name": "test", "password": "test"}}}
statusCode, _ = tutils.PutResponseBody(t, db_srv.URL+fmt.Sprintf("/api/image-builder/v1/blueprints/%s", uuid.New()), body)
require.Equal(t, http.StatusNotFound, statusCode)
}

func TestHandlers_ComposeBlueprint(t *testing.T) {
Expand Down Expand Up @@ -618,6 +623,10 @@ func TestHandlers_ComposeBlueprint(t *testing.T) {
}
})
}
t.Run("non-existing blueprint", func(t *testing.T) {
respStatusCode, _ := tutils.PostResponseBody(t, srv.URL+fmt.Sprintf("/api/image-builder/v1/blueprints/%s/compose", uuid.New()), ComposeBlueprintJSONBody{})
require.Equal(t, http.StatusNotFound, respStatusCode)
})
}

func TestHandlers_GetBlueprintComposes(t *testing.T) {
Expand Down

0 comments on commit 9b739f8

Please sign in to comment.