Skip to content

Commit

Permalink
Test route handler using fake request validator
Browse files Browse the repository at this point in the history
This includes:
- switching list routes to use the requestValidator to parse URL params
- changing from go-playground to jellydator validation

Issue: #1996
  • Loading branch information
Kieron Browne committed Jun 26, 2023
1 parent 6f569e4 commit 965ac11
Show file tree
Hide file tree
Showing 5 changed files with 479 additions and 401 deletions.
55 changes: 25 additions & 30 deletions api/handlers/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ type Route struct {
domainRepo CFDomainRepository
appRepo CFAppRepository
spaceRepo SpaceRepository
decoderValidator *DecoderValidator
requestValidator RequestValidator
}

func NewRoute(
Expand All @@ -50,15 +50,15 @@ func NewRoute(
domainRepo CFDomainRepository,
appRepo CFAppRepository,
spaceRepo SpaceRepository,
decoderValidator *DecoderValidator,
requestValidator RequestValidator,
) *Route {
return &Route{
serverURL: serverURL,
routeRepo: routeRepo,
domainRepo: domainRepo,
appRepo: appRepo,
spaceRepo: spaceRepo,
decoderValidator: decoderValidator,
requestValidator: requestValidator,
}
}

Expand All @@ -80,13 +80,8 @@ func (h *Route) list(r *http.Request) (*routing.Response, error) {
authInfo, _ := authorization.InfoFromContext(r.Context())
logger := logr.FromContextOrDiscard(r.Context()).WithName("handlers.route.list")

if err := r.ParseForm(); err != nil {
return nil, apierrors.LogAndReturn(logger, err, "Unable to parse request query parameters")
}

routeListFilter := new(payloads.RouteList)
err := payloads.Decode(routeListFilter, r.Form)
if err != nil {
if err := h.requestValidator.DecodeAndValidateURLValues(r, routeListFilter); err != nil {
return nil, apierrors.LogAndReturn(logger, err, "Unable to decode request query parameters")
}

Expand Down Expand Up @@ -117,7 +112,7 @@ func (h *Route) create(r *http.Request) (*routing.Response, error) {
logger := logr.FromContextOrDiscard(r.Context()).WithName("handlers.route.create")

var payload payloads.RouteCreate
if err := h.decoderValidator.DecodeAndValidateJSONPayload(r, &payload); err != nil {
if err := h.requestValidator.DecodeAndValidateJSONPayload(r, &payload); err != nil {
return nil, apierrors.LogAndReturn(logger, err, "failed to decode payload")
}

Expand Down Expand Up @@ -166,8 +161,8 @@ func (h *Route) insertDestinations(r *http.Request) (*routing.Response, error) {
authInfo, _ := authorization.InfoFromContext(r.Context())
logger := logr.FromContextOrDiscard(r.Context()).WithName("handlers.route.insert-destinations")

var destinationCreatePayload payloads.DestinationListCreate
if err := h.decoderValidator.DecodeAndValidateJSONPayload(r, &destinationCreatePayload); err != nil {
var destinationCreatePayload payloads.RouteDestinationCreate
if err := h.requestValidator.DecodeAndValidateJSONPayload(r, &destinationCreatePayload); err != nil {
return nil, apierrors.LogAndReturn(logger, err, "failed to decode payload")
}

Expand Down Expand Up @@ -236,23 +231,6 @@ func (h *Route) delete(r *http.Request) (*routing.Response, error) {
return routing.NewResponse(http.StatusAccepted).WithHeader("Location", presenter.JobURLForRedirects(routeGUID, presenter.RouteDeleteOperation, h.serverURL)), nil
}

func (h *Route) UnauthenticatedRoutes() []routing.Route {
return nil
}

func (h *Route) AuthenticatedRoutes() []routing.Route {
return []routing.Route{
{Method: "GET", Pattern: RoutePath, Handler: h.get},
{Method: "GET", Pattern: RoutesPath, Handler: h.list},
{Method: "GET", Pattern: RouteDestinationsPath, Handler: h.listDestinations},
{Method: "POST", Pattern: RoutesPath, Handler: h.create},
{Method: "DELETE", Pattern: RoutePath, Handler: h.delete},
{Method: "POST", Pattern: RouteDestinationsPath, Handler: h.insertDestinations},
{Method: "DELETE", Pattern: RouteDestinationPath, Handler: h.deleteDestination},
{Method: "PATCH", Pattern: RoutePath, Handler: h.update},
}
}

// Fetch Route and compose related Domain information within
func (h *Route) lookupRouteAndDomain(ctx context.Context, logger logr.Logger, authInfo authorization.Info, routeGUID string) (repositories.RouteRecord, error) {
route, err := h.routeRepo.GetRoute(ctx, authInfo, routeGUID)
Expand Down Expand Up @@ -305,7 +283,7 @@ func (h *Route) update(r *http.Request) (*routing.Response, error) {
}

var payload payloads.RoutePatch
if err = h.decoderValidator.DecodeAndValidateJSONPayload(r, &payload); err != nil {
if err = h.requestValidator.DecodeAndValidateJSONPayload(r, &payload); err != nil {
return nil, apierrors.LogAndReturn(logger, err, "failed to decode payload")
}

Expand All @@ -315,3 +293,20 @@ func (h *Route) update(r *http.Request) (*routing.Response, error) {
}
return routing.NewResponse(http.StatusOK).WithBody(presenter.ForRoute(route, h.serverURL)), nil
}

func (h *Route) UnauthenticatedRoutes() []routing.Route {
return nil
}

func (h *Route) AuthenticatedRoutes() []routing.Route {
return []routing.Route{
{Method: "GET", Pattern: RoutePath, Handler: h.get},
{Method: "GET", Pattern: RoutesPath, Handler: h.list},
{Method: "GET", Pattern: RouteDestinationsPath, Handler: h.listDestinations},
{Method: "POST", Pattern: RoutesPath, Handler: h.create},
{Method: "DELETE", Pattern: RoutePath, Handler: h.delete},
{Method: "POST", Pattern: RouteDestinationsPath, Handler: h.insertDestinations},
{Method: "DELETE", Pattern: RouteDestinationPath, Handler: h.deleteDestination},
{Method: "PATCH", Pattern: RoutePath, Handler: h.update},
}
}
Loading

0 comments on commit 965ac11

Please sign in to comment.