Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implementing Galadriel Server Management Handlers API #150

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
bfdfcc4
feat: Implementing Admin Handlers
Victorblsilveira May 5, 2023
cbdaf26
feat: Adding unit testes helpers and implemenenting get relationships…
Victorblsilveira May 8, 2023
c55c71b
Merge branch 'main' into victor/admin-handlers
Victorblsilveira May 8, 2023
664cb58
feat: Creating unit tests for relationshio request creation and impro…
Victorblsilveira May 9, 2023
d02e836
Merge remote-tracking branch 'origin/main' into victor/admin-handlers
Victorblsilveira May 9, 2023
a55e8fa
fix: Unmashaling assertion
Victorblsilveira May 9, 2023
a7edd5d
fix: Removin code duplication
Victorblsilveira May 9, 2023
c621801
fix: Organization
Victorblsilveira May 9, 2023
4b49fcc
feat: Adding more unit tests
Victorblsilveira May 10, 2023
5f3dcbd
feat: Changin return code for relationship request creation
Victorblsilveira May 10, 2023
9bc7b3a
feat: Finishing unit tests
Victorblsilveira May 11, 2023
46ca412
Update pkg/server/endpoints/management.go
Victorblsilveira May 11, 2023
02de6e6
Update pkg/server/endpoints/management.go
Victorblsilveira May 11, 2023
990cd61
Merge remote-tracking branch 'origin/main' into victor/admin-handlers
Victorblsilveira May 15, 2023
85ce235
fix: Function names
Victorblsilveira May 15, 2023
63306ec
fix: Centralizing common stuff
Victorblsilveira May 15, 2023
2b5d958
fix: Removing const_test file and unit test changes
Victorblsilveira May 15, 2023
38449f0
fix: Misstypes and const places
Victorblsilveira May 15, 2023
21b679b
fix: Refactoring trust domain lookup functions
Victorblsilveira May 15, 2023
d9b7d39
Merge branch 'HewlettPackard:main' into victor/admin-handlers
Victorblsilveira May 15, 2023
7eafbe5
fix: Unit tests failing
Victorblsilveira May 15, 2023
f085b5d
Merge remote-tracking branch 'origin/main' into victor/admin-handlers
Victorblsilveira May 16, 2023
b60cec3
fix: Removing unused values
Victorblsilveira May 16, 2023
4da954f
fix: Unit test failing
Victorblsilveira May 16, 2023
b92ffcb
fix: Unit test checking
Victorblsilveira May 16, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions pkg/common/http/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,26 @@ package http
import (
"errors"
"fmt"
"net/http"

"github.com/labstack/echo/v4"
)

// WriteResponse parses a struct into a json and writes in the response
func WriteResponse(ctx echo.Context, body interface{}) error {
func WriteResponse(ctx echo.Context, code int, body interface{}) error {
if body == nil {
return errors.New("body is required")
}

if err := ctx.JSON(http.StatusOK, body); err != nil {
if err := ctx.JSON(code, body); err != nil {
return fmt.Errorf("failed to write response body: %v", err)
}

return nil
}

// BodylessResponse wraps error echo body-less responses.
func BodylessResponse(ctx echo.Context) error {
if err := ctx.NoContent(http.StatusOK); err != nil {
// BodilessResponse wraps error echo body-less responses.
func BodilessResponse(ctx echo.Context, code int) error {
Copy link
Contributor

@maxlambrecht maxlambrecht May 15, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why add this parameter that is not used?

Name suggestions for this function: NoContentResponse or EmptyResponse

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Misstype

if err := ctx.NoContent(code); err != nil {
return fmt.Errorf("failed to respond without body: %v", err)
}

Expand Down
13 changes: 7 additions & 6 deletions pkg/common/http/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,21 +34,22 @@ func Setup() *HTTPSetup {
func TestWriteResponse(t *testing.T) {
t.Run("Error when nil body is passed", func(t *testing.T) {
setup := Setup()
err := WriteResponse(setup.EchoContext, nil)
err := WriteResponse(setup.EchoContext, http.StatusOK, nil)
assert.EqualError(t, err, "body is required")
assert.Empty(t, setup.Recorder.Body)
})

t.Run("No error when an empty body is passed", func(t *testing.T) {
setup := Setup()
err := WriteResponse(setup.EchoContext, TestBody{})
err := WriteResponse(setup.EchoContext, http.StatusOK, TestBody{})
assert.NoError(t, err)
assert.Equal(t, http.StatusOK, setup.Recorder.Code)
})

t.Run("Ensuring that the body is being full filled with the entity", func(t *testing.T) {
expectedResponseBody := TestBody{Name: "teste"}
setup := Setup()
err := WriteResponse(setup.EchoContext, expectedResponseBody)
err := WriteResponse(setup.EchoContext, http.StatusOK, expectedResponseBody)
assert.NoError(t, err)

responseBody := TestBody{}
Expand All @@ -60,10 +61,10 @@ func TestWriteResponse(t *testing.T) {
})
}

func TestBodylessResponse(t *testing.T) {
func TestBodilessResponse(t *testing.T) {
t.Run("Ensuring that the body is empty", func(t *testing.T) {
setup := Setup()
err := BodylessResponse(setup.EchoContext)
err := BodilessResponse(setup.EchoContext, http.StatusOK)
assert.NoError(t, err)

assert.NoError(t, err)
Expand All @@ -75,7 +76,7 @@ func TestBodylessResponse(t *testing.T) {
func TestFromBody(t *testing.T) {
t.Run("Ensuring that the body is empty", func(t *testing.T) {
setup := Setup()
err := BodylessResponse(setup.EchoContext)
err := BodilessResponse(setup.EchoContext, http.StatusOK)
assert.NoError(t, err)

assert.NoError(t, err)
Expand Down
294 changes: 142 additions & 152 deletions pkg/server/api/admin/admin.gen.go

Large diffs are not rendered by default.

9 changes: 7 additions & 2 deletions pkg/server/api/admin/admin.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ tags:
paths:
/trust-domain/{trustDomainName}:
get:
operationId: GetTrustDomainByName
tags:
- Trust Domain
summary: Get a specific trust domain
Expand All @@ -43,6 +44,7 @@ paths:
default:
$ref: '#/components/responses/Default'
put:
operationId: PutTrustDomainByName
tags:
- Trust Domain
summary: Update a specific trust domain
Expand Down Expand Up @@ -70,6 +72,7 @@ paths:

/trust-domain:
put:
operationId: PutTrustDomain
tags:
- Trust Domain
summary: Add a specific trust domain
Expand All @@ -90,15 +93,15 @@ paths:

/relationships:
get:
operationId: GetRelationships
tags:
- Relationships
summary: Get relationships
parameters:
- name: status
in: query
schema:
type: string
enum: [approved, denied, pending]
$ref: '../../../common/api/schemas.yaml#/components/schemas/ConsentStatus'
description: relationship status from a Trust Domain perspective,
- name: trustDomainName
in: query
Expand All @@ -117,6 +120,7 @@ paths:
default:
$ref: '#/components/responses/Default'
put:
operationId: PutRelationship
tags:
- Relationships
summary: Create a relationship request between two Trust Domains
Expand All @@ -138,6 +142,7 @@ paths:

/relationships/{relationshipID}:
get:
operationId: GetRelationshipByID
tags:
- Relationships
summary: Get a specific relationship
Expand Down
26 changes: 18 additions & 8 deletions pkg/server/datastore/fakedatabase.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,15 @@ func (db *FakeDatabase) CreateOrUpdateTrustDomain(ctx context.Context, req *enti
return nil, err
}

req.ID = uuid.NullUUID{
UUID: uuid.New(),
Valid: true,
if !req.ID.Valid {
req.ID = uuid.NullUUID{
UUID: uuid.New(),
Valid: true,
}
req.CreatedAt = time.Now()
}

req.CreatedAt = time.Now()
req.UpdatedAt = time.Now()

db.trustDomains[req.ID.UUID] = req

return req, nil
Expand Down Expand Up @@ -174,9 +175,7 @@ func (db *FakeDatabase) CreateOrUpdateBundle(ctx context.Context, req *entity.Bu
}
}

req.CreatedAt = time.Now()
req.UpdatedAt = time.Now()

db.bundles[req.ID.UUID] = req

return req, nil
Expand Down Expand Up @@ -390,7 +389,18 @@ func (db *FakeDatabase) CreateOrUpdateRelationship(ctx context.Context, req *ent
return nil, err
}

return nil, nil
if !req.ID.Valid {
req.ID = uuid.NullUUID{
Valid: true,
UUID: uuid.New(),
}
req.CreatedAt = time.Now()
}

req.UpdatedAt = time.Now()
db.relationships[req.ID.UUID] = req

return req, nil
}

func (db *FakeDatabase) FindRelationshipByID(ctx context.Context, relationshipID uuid.UUID) (*entity.Relationship, error) {
Expand Down
2 changes: 0 additions & 2 deletions pkg/server/endpoints/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ import (
"github.com/stretchr/testify/require"
)

const testTrustDomain = "test.com"

type AuthNTestSetup struct {
EchoCtx echo.Context
Middleware *AuthenticationMiddleware
Expand Down
12 changes: 6 additions & 6 deletions pkg/server/endpoints/harvester.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func (h *HarvesterAPIHandlers) GetRelationships(echoCtx echo.Context, params har
apiRelationships = append(apiRelationships, api.RelationshipFromEntity(r))
}

return chttp.WriteResponse(echoCtx, apiRelationships)
return chttp.WriteResponse(echoCtx, http.StatusOK, apiRelationships)
}

// PatchRelationship accept/denies relationships requests - (PATCH /relationships/{relationshipID})
Expand Down Expand Up @@ -145,7 +145,7 @@ func (h *HarvesterAPIHandlers) PatchRelationship(echoCtx echo.Context, relations
return h.handleErrorAndLog(err, msg, http.StatusInternalServerError)
}

if err = chttp.BodylessResponse(echoCtx); err != nil {
if err = chttp.BodilessResponse(echoCtx, http.StatusOK); err != nil {
return h.handleErrorAndLog(err, err.Error(), http.StatusInternalServerError)
}

Expand Down Expand Up @@ -215,7 +215,7 @@ func (h *HarvesterAPIHandlers) Onboard(echoCtx echo.Context, params harvester.On
return h.handleErrorAndLog(err, msg, http.StatusInternalServerError)
}

return chttp.WriteResponse(echoCtx, jwtToken)
return chttp.WriteResponse(echoCtx, http.StatusOK, jwtToken)
}

// GetNewJWTToken renews a JWT access token - (GET /trust-domain/jwt)
Expand Down Expand Up @@ -255,7 +255,7 @@ func (h *HarvesterAPIHandlers) GetNewJWTToken(echoCtx echo.Context) error {
return h.handleErrorAndLog(err, msg, http.StatusInternalServerError)
}

return chttp.WriteResponse(echoCtx, newToken)
return chttp.WriteResponse(echoCtx, http.StatusOK, newToken)
}

// BundleSync synchronize the status of trust bundles between server and harvester - (POST /trust-domain/{trustDomainName}/bundles/sync)
Expand Down Expand Up @@ -294,7 +294,7 @@ func (h *HarvesterAPIHandlers) BundleSync(echoCtx echo.Context, trustDomainName
return h.handleErrorAndLog(err, msg, http.StatusInternalServerError)
}

return chttp.WriteResponse(echoCtx, resp)
return chttp.WriteResponse(echoCtx, http.StatusOK, resp)
}

// BundlePut uploads a new trust bundle to the server - (PUT /trust-domain/{trustDomainName}/bundles)
Expand Down Expand Up @@ -351,7 +351,7 @@ func (h *HarvesterAPIHandlers) BundlePut(echoCtx echo.Context, trustDomainName a
return h.handleErrorAndLog(err, msg, http.StatusInternalServerError)
}

if err = chttp.BodylessResponse(echoCtx); err != nil {
if err = chttp.BodilessResponse(echoCtx, http.StatusOK); err != nil {
return h.handleErrorAndLog(err, err.Error(), http.StatusInternalServerError)
}

Expand Down
Loading