3
0

Reverted and reimplemented route deleted flag handling

This commit is contained in:
Denis Arh 2022-11-23 07:30:25 +01:00
parent 7aded0c977
commit 4ab9ca16c8
2 changed files with 52 additions and 15 deletions

View File

@ -97,39 +97,48 @@ func (svc *apigwRoute) Create(ctx context.Context, new *types.ApigwRoute) (q *ty
return q, svc.recordAction(ctx, qProps, ApigwRouteActionCreate, err)
}
func (svc *apigwRoute) Update(ctx context.Context, upd *types.ApigwRoute) (q *types.ApigwRoute, err error) {
func (svc *apigwRoute) Update(ctx context.Context, upd *types.ApigwRoute) (res *types.ApigwRoute, err error) {
var (
qProps = &apigwRouteActionProps{update: upd}
qq *types.ApigwRoute
e error
)
err = func() (err error) {
if qq, e = loadApigwRoute(ctx, svc.store, upd.ID); e != nil {
if res, e = loadApigwRoute(ctx, svc.store, upd.ID); e != nil {
return ApigwRouteErrNotFound(qProps)
}
if !svc.ac.CanUpdateApigwRoute(ctx, qq) {
var (
// check if old endpoint moved and attach the 404 handler
endpointMoved = res.Enabled != upd.Enabled || res.Method != upd.Method || res.Endpoint != upd.Endpoint
)
if !svc.ac.CanUpdateApigwRoute(ctx, res) {
return ApigwRouteErrNotAllowedToUpdate(qProps)
}
// temp todo - update itself with the same endpoint
// if qq, e = store.LookupApigwRouteByEndpoint(ctx, svc.store, upd.Endpoint); e == nil && qq == nil {
// return ApigwRouteErrExistsEndpoint(qProps)
// }
// copy (potentially) updated files from the payload
res.Meta = upd.Meta
res.Method = upd.Method
res.Endpoint = upd.Endpoint
res.Enabled = upd.Enabled
res.Group = upd.Group
qq.UpdatedAt = now()
qq.UpdatedBy = a.GetIdentityFromContext(ctx).Identity()
// ensure we have a valid endpoint
res.UpdatedAt = now()
res.UpdatedBy = a.GetIdentityFromContext(ctx).Identity()
if err = store.UpdateApigwRoute(ctx, svc.store, qq); err != nil {
if err = store.UpdateApigwRoute(ctx, svc.store, res); err != nil {
return
}
// @todo move this into struct of the service (svc),
// same as we do for services for other structs
ags := apigw.Service()
// If method or endpoint doesn't match then attach 404 handler
if qq.Enabled != upd.Enabled || qq.Method != upd.Method || qq.Endpoint != upd.Endpoint {
ags.NotFound(ctx, qq.Method, qq.Endpoint)
// old endpoint moved: attach 404 handler
if endpointMoved {
ags.NotFound(ctx, res.Method, res.Endpoint)
}
// send the signal to reload updated route
@ -142,7 +151,7 @@ func (svc *apigwRoute) Update(ctx context.Context, upd *types.ApigwRoute) (q *ty
return nil
}()
return qq, svc.recordAction(ctx, qProps, ApigwRouteActionUpdate, err)
return res, svc.recordAction(ctx, qProps, ApigwRouteActionUpdate, err)
}
func (svc *apigwRoute) DeleteByID(ctx context.Context, ID uint64) (err error) {

View File

@ -241,6 +241,34 @@ func TestApigwRouteUpdate(t *testing.T) {
Assert(jsonpath.Present(`$.response.routeID`)).
Assert(jsonpath.Equal(`$.response.endpoint`, "/test-edited")).
Assert(jsonpath.Equal(`$.response.enabled`, false)).
Assert(jsonpath.NotPresent(`$.response.deletedBy`)).
Assert(jsonpath.NotPresent(`$.response.deletedAt`)).
End()
}
func TestApigwRouteUpdateDeleted(t *testing.T) {
h := newHelper(t)
h.clearRoutes()
now := time.Now()
r := &types.ApigwRoute{Endpoint: "/deleted", Enabled: true, DeletedAt: &now, DeletedBy: 1}
h.createRoute(r)
helpers.AllowMe(h, types.ApigwRouteRbacResource(r.ID), "update")
h.apiInit().
Put(fmt.Sprintf("/apigw/route/%d", r.ID)).
Header("Accept", "application/json").
FormData("endpoint", "/deleted-and-updated").
FormData("enabled", "false").
Expect(t).
Status(http.StatusOK).
Assert(helpers.AssertNoErrors).
Assert(jsonpath.Present(`$.response.routeID`)).
Assert(jsonpath.Equal(`$.response.endpoint`, "/deleted-and-updated")).
Assert(jsonpath.Present(`$.response.deletedBy`)).
Assert(jsonpath.Present(`$.response.deletedAt`)).
End()
}