From a22083573c543da839d20ad8b646394b775b0e02 Mon Sep 17 00:00:00 2001 From: Vivek Patel Date: Fri, 1 Oct 2021 14:45:56 +0530 Subject: [PATCH] APIGw updates - Fixes filter status update - Updates search route API param for status - Updates envoy marshaling for filter - Fixes/Updated test for route and filter --- pkg/apigw/route_test.go | 2 + pkg/apigw/service.go | 10 +- pkg/envoy/yaml/api_gateway_marshal.go | 1 + pkg/envoy/yaml/api_gateway_unmarshal.go | 3 + store/apigw_filter.yaml | 1 + store/rdbms/apigw_filter.gen.go | 3 + store/rdbms/apigw_filter.go | 1 + store/rdbms/apigw_route.go | 5 +- store/rdbms/rdbms_schema.go | 1 + system/rest.yaml | 2 + system/rest/apigw_filter.go | 33 ++-- system/rest/apigw_route.go | 7 +- system/rest/request/apigwFilter.go | 36 +++++ system/service/apigw_filter.go | 5 +- system/types/apigw_filter.go | 17 +- system/types/apigw_route.go | 8 +- tests/apigw/main_test.go | 2 +- .../prefilter_header_failing/def.yaml | 2 + .../prefilter_header_n_routes/def.yaml | 2 + .../prefilter_header_passing/def.yaml | 2 + .../testdata/prefilter_query_failing/def.yaml | 2 + .../prefilter_query_n_routes/def.yaml | 2 + .../testdata/prefilter_query_passing/def.yaml | 2 + .../processor_payload_simple/def.yaml | 1 + tests/apigw/testdata/route_reload/def.yaml | 1 + tests/envoy/store_yaml_api_gateway_test.go | 2 +- tests/envoy/system.go | 11 +- tests/system/apigw_test.go | 151 +++++++++++++++++- 28 files changed, 265 insertions(+), 50 deletions(-) diff --git a/pkg/apigw/route_test.go b/pkg/apigw/route_test.go index 836381950..80f15794a 100644 --- a/pkg/apigw/route_test.go +++ b/pkg/apigw/route_test.go @@ -18,6 +18,7 @@ func Test_pl(t *testing.T) { tf struct { name string method string + endpoint string expError string expStatus int handler *types.MockHandler @@ -91,6 +92,7 @@ func Test_pl(t *testing.T) { route := &route{ method: tc.method, + endpoint: tc.endpoint, log: zap.NewNop(), opts: options.Apigw(), handler: pipe.Handler(), diff --git a/pkg/apigw/service.go b/pkg/apigw/service.go index 62ed8943c..0b6bed775 100644 --- a/pkg/apigw/service.go +++ b/pkg/apigw/service.go @@ -248,8 +248,8 @@ func (s *apigw) ProxyAuthDef() (list []*proxy.ProxyAuthDefinition) { func (s *apigw) loadRoutes(ctx context.Context) (rr []*route, err error) { routes, _, err := s.storer.SearchApigwRoutes(ctx, st.ApigwRouteFilter{ - Enabled: true, - Deleted: f.StateExcluded, + Deleted: f.StateExcluded, + Disabled: f.StateExcluded, }) if err != nil { @@ -275,9 +275,9 @@ func (s *apigw) loadRoutes(ctx context.Context) (rr []*route, err error) { func (s *apigw) loadFilters(ctx context.Context, route uint64) (ff []*st.ApigwFilter, err error) { ff, _, err = s.storer.SearchApigwFilters(ctx, st.ApigwFilterFilter{ - RouteID: route, - Deleted: f.StateExcluded, - Enabled: true, + RouteID: route, + Deleted: f.StateExcluded, + Disabled: f.StateExcluded, }) return diff --git a/pkg/envoy/yaml/api_gateway_marshal.go b/pkg/envoy/yaml/api_gateway_marshal.go index 741bee93a..b1ab54eb1 100644 --- a/pkg/envoy/yaml/api_gateway_marshal.go +++ b/pkg/envoy/yaml/api_gateway_marshal.go @@ -95,6 +95,7 @@ func (f *apiGwFilter) MarshalYAML() (interface{}, error) { "weight", f.res.Weight, "ref", f.res.Ref, "kind", f.res.Kind, + "enabled", f.res.Enabled, "params", f.res.Params, ) if err != nil { diff --git a/pkg/envoy/yaml/api_gateway_unmarshal.go b/pkg/envoy/yaml/api_gateway_unmarshal.go index f499058ed..362f80eea 100644 --- a/pkg/envoy/yaml/api_gateway_unmarshal.go +++ b/pkg/envoy/yaml/api_gateway_unmarshal.go @@ -112,6 +112,9 @@ func (wrap *apiGwFilter) UnmarshalYAML(n *yaml.Node) (err error) { case "kind": return y7s.DecodeScalar(v, "route filter kind", &wrap.res.Kind) + case "enabled": + return y7s.DecodeScalar(v, "route filter enabled", &wrap.res.Enabled) + case "params": return v.Decode(&wrap.res.Params) } diff --git a/store/apigw_filter.yaml b/store/apigw_filter.yaml index da4f88074..736af48ae 100644 --- a/store/apigw_filter.yaml +++ b/store/apigw_filter.yaml @@ -12,6 +12,7 @@ fields: - { field: Weight } - { field: Kind } - { field: Ref } + - { field: Enabled } - { field: Params, type: "map[string]interface{}" } - { field: CreatedBy } - { field: UpdatedBy } diff --git a/store/rdbms/apigw_filter.gen.go b/store/rdbms/apigw_filter.gen.go index 6e308d203..3f3217e4e 100644 --- a/store/rdbms/apigw_filter.gen.go +++ b/store/rdbms/apigw_filter.gen.go @@ -414,6 +414,7 @@ func (s Store) internalApigwFilterRowScanner(row rowScanner) (res *types.ApigwFi &res.Weight, &res.Kind, &res.Ref, + &res.Enabled, &res.Params, &res.CreatedBy, &res.UpdatedBy, @@ -465,6 +466,7 @@ func (Store) apigwFilterColumns(aa ...string) []string { alias + "weight", alias + "kind", alias + "ref", + alias + "enabled", alias + "params", alias + "created_by", alias + "updated_by", @@ -497,6 +499,7 @@ func (s Store) internalApigwFilterEncoder(res *types.ApigwFilter) store.Payload "weight": res.Weight, "kind": res.Kind, "ref": res.Ref, + "enabled": res.Enabled, "params": res.Params, "created_by": res.CreatedBy, "updated_by": res.UpdatedBy, diff --git a/store/rdbms/apigw_filter.go b/store/rdbms/apigw_filter.go index c41702bb9..ae6da5b6f 100644 --- a/store/rdbms/apigw_filter.go +++ b/store/rdbms/apigw_filter.go @@ -10,6 +10,7 @@ func (s Store) convertApigwFilterFilter(f types.ApigwFilterFilter) (query squirr query = s.apigwFiltersSelectBuilder() query = filter.StateCondition(query, "af.deleted_at", f.Deleted) + query = filter.StateConditionNegBool(query, "af.enabled", f.Disabled) if f.RouteID > 0 { query = query.Where(squirrel.Eq{"af.rel_route": f.RouteID}) diff --git a/store/rdbms/apigw_route.go b/store/rdbms/apigw_route.go index 51f41909d..3a860a4a4 100644 --- a/store/rdbms/apigw_route.go +++ b/store/rdbms/apigw_route.go @@ -9,10 +9,7 @@ import ( func (s Store) convertApigwRouteFilter(f types.ApigwRouteFilter) (query squirrel.SelectBuilder, err error) { query = s.apigwRoutesSelectBuilder() query = filter.StateCondition(query, "ar.deleted_at", f.Deleted) - - if f.Enabled { - query = query.Where(squirrel.Eq{"ar.enabled": f.Enabled}) - } + query = filter.StateConditionNegBool(query, "ar.enabled", f.Disabled) return } diff --git a/store/rdbms/rdbms_schema.go b/store/rdbms/rdbms_schema.go index 62dd4567c..1b9137559 100644 --- a/store/rdbms/rdbms_schema.go +++ b/store/rdbms/rdbms_schema.go @@ -733,6 +733,7 @@ func (Schema) ApigwFilter() *Table { ColumnDef("weight", ColumnTypeInteger), ColumnDef("kind", ColumnTypeVarchar, ColumnTypeLength(handleLength)), ColumnDef("ref", ColumnTypeVarchar, ColumnTypeLength(handleLength)), + ColumnDef("enabled", ColumnTypeBoolean), ColumnDef("params", ColumnTypeJson), CUDTimestamps, CUDUsers, diff --git a/system/rest.yaml b/system/rest.yaml index 8d3b5a283..ebfc7b6fa 100644 --- a/system/rest.yaml +++ b/system/rest.yaml @@ -1715,6 +1715,7 @@ endpoints: - { name: weight, type: uint64, title: "Filter priority" } - { name: kind, type: string, title: "Filter kind" } - { name: ref, type: string, title: "Filter ref" } + - { name: enabled, type: bool, title: "Is Filter enabled" } - { name: params, type: "types.ApigwFilterParams", title: "Filter parameters", parser: "types.ParseApigwfFilterParams" } - name: update method: POST @@ -1727,6 +1728,7 @@ endpoints: - { name: weight, type: uint64, title: "Filter priority" } - { name: kind, type: string, title: "Filter kind" } - { name: ref, type: string, title: "Filter ref" } + - { name: enabled, type: bool, title: "Is Filter enabled" } - { name: params, type: "types.ApigwFilterParams", title: "Filter parameters", parser: "types.ParseApigwfFilterParams" } - name: read method: GET diff --git a/system/rest/apigw_filter.go b/system/rest/apigw_filter.go index 489caeba7..ea76b6a15 100644 --- a/system/rest/apigw_filter.go +++ b/system/rest/apigw_filter.go @@ -49,8 +49,9 @@ func (ctrl *ApigwFilter) List(ctx context.Context, r *request.ApigwFilterList) ( var ( err error f = types.ApigwFilterFilter{ - RouteID: r.RouteID, - Deleted: filter.State(r.Deleted), + RouteID: r.RouteID, + Deleted: filter.State(r.Deleted), + Disabled: filter.State(r.Disabled), } ) @@ -62,20 +63,21 @@ func (ctrl *ApigwFilter) List(ctx context.Context, r *request.ApigwFilterList) ( return nil, err } - set, filter, err := ctrl.svc.Search(ctx, f) + set, f, err := ctrl.svc.Search(ctx, f) - return ctrl.makeFilterPayload(ctx, set, filter, err) + return ctrl.makeFilterPayload(ctx, set, f, err) } func (ctrl *ApigwFilter) Create(ctx context.Context, r *request.ApigwFilterCreate) (interface{}, error) { var ( err error q = &types.ApigwFilter{ - Route: r.RouteID, - Weight: r.Weight, - Kind: r.Kind, - Ref: r.Ref, - Params: r.Params, + Route: r.RouteID, + Weight: r.Weight, + Kind: r.Kind, + Ref: r.Ref, + Enabled: r.Enabled, + Params: r.Params, } ) @@ -92,12 +94,13 @@ func (ctrl *ApigwFilter) Update(ctx context.Context, r *request.ApigwFilterUpdat var ( err error q = &types.ApigwFilter{ - ID: r.FilterID, - Route: r.RouteID, - Weight: r.Weight, - Kind: r.Kind, - Ref: r.Ref, - Params: r.Params, + ID: r.FilterID, + Route: r.RouteID, + Weight: r.Weight, + Kind: r.Kind, + Ref: r.Ref, + Enabled: r.Enabled, + Params: r.Params, } ) diff --git a/system/rest/apigw_route.go b/system/rest/apigw_route.go index 232dc51ef..3d9ce5eee 100644 --- a/system/rest/apigw_route.go +++ b/system/rest/apigw_route.go @@ -46,7 +46,8 @@ func (ctrl *ApigwRoute) List(ctx context.Context, r *request.ApigwRouteList) (in var ( err error f = types.ApigwRouteFilter{ - Deleted: filter.State(r.Deleted), + Deleted: filter.State(r.Deleted), + Disabled: filter.State(r.Disabled), } ) @@ -58,9 +59,9 @@ func (ctrl *ApigwRoute) List(ctx context.Context, r *request.ApigwRouteList) (in return nil, err } - set, filter, err := ctrl.svc.Search(ctx, f) + set, f, err := ctrl.svc.Search(ctx, f) - return ctrl.makeFilterPayload(ctx, set, filter, err) + return ctrl.makeFilterPayload(ctx, set, f, err) } func (ctrl *ApigwRoute) Create(ctx context.Context, r *request.ApigwRouteCreate) (interface{}, error) { diff --git a/system/rest/request/apigwFilter.go b/system/rest/request/apigwFilter.go index b57513330..284763294 100644 --- a/system/rest/request/apigwFilter.go +++ b/system/rest/request/apigwFilter.go @@ -87,6 +87,11 @@ type ( // Filter ref Ref string + // Enabled POST parameter + // + // Is Filter enabled + Enabled bool + // Params POST parameter // // Filter parameters @@ -119,6 +124,11 @@ type ( // Filter ref Ref string + // Enabled POST parameter + // + // Is Filter enabled + Enabled bool + // Params POST parameter // // Filter parameters @@ -264,6 +274,7 @@ func (r ApigwFilterCreate) Auditable() map[string]interface{} { "weight": r.Weight, "kind": r.Kind, "ref": r.Ref, + "enabled": r.Enabled, "params": r.Params, } } @@ -288,6 +299,11 @@ func (r ApigwFilterCreate) GetRef() string { return r.Ref } +// Auditable returns all auditable/loggable parameters +func (r ApigwFilterCreate) GetEnabled() bool { + return r.Enabled +} + // Auditable returns all auditable/loggable parameters func (r ApigwFilterCreate) GetParams() types.ApigwFilterParams { return r.Params @@ -342,6 +358,13 @@ func (r *ApigwFilterCreate) Fill(req *http.Request) (err error) { } } + if val, ok := req.Form["enabled"]; ok && len(val) > 0 { + r.Enabled, err = payload.ParseBool(val[0]), nil + if err != nil { + return err + } + } + if val, ok := req.Form["params[]"]; ok { r.Params, err = types.ParseApigwfFilterParams(val) if err != nil { @@ -371,6 +394,7 @@ func (r ApigwFilterUpdate) Auditable() map[string]interface{} { "weight": r.Weight, "kind": r.Kind, "ref": r.Ref, + "enabled": r.Enabled, "params": r.Params, } } @@ -400,6 +424,11 @@ func (r ApigwFilterUpdate) GetRef() string { return r.Ref } +// Auditable returns all auditable/loggable parameters +func (r ApigwFilterUpdate) GetEnabled() bool { + return r.Enabled +} + // Auditable returns all auditable/loggable parameters func (r ApigwFilterUpdate) GetParams() types.ApigwFilterParams { return r.Params @@ -454,6 +483,13 @@ func (r *ApigwFilterUpdate) Fill(req *http.Request) (err error) { } } + if val, ok := req.Form["enabled"]; ok && len(val) > 0 { + r.Enabled, err = payload.ParseBool(val[0]), nil + if err != nil { + return err + } + } + if val, ok := req.Form["params[]"]; ok { r.Params, err = types.ParseApigwfFilterParams(val) if err != nil { diff --git a/system/service/apigw_filter.go b/system/service/apigw_filter.go index 5f117318b..f3e4308d8 100644 --- a/system/service/apigw_filter.go +++ b/system/service/apigw_filter.go @@ -7,6 +7,7 @@ import ( "github.com/cortezaproject/corteza-server/pkg/apigw" agtypes "github.com/cortezaproject/corteza-server/pkg/apigw/types" a "github.com/cortezaproject/corteza-server/pkg/auth" + "github.com/cortezaproject/corteza-server/pkg/filter" "github.com/cortezaproject/corteza-server/store" "github.com/cortezaproject/corteza-server/system/types" ) @@ -107,8 +108,8 @@ func (svc *apigwFilter) Create(ctx context.Context, new *types.ApigwFilter) (q * func (svc *apigwFilter) validateAsyncRoute(ctx context.Context, r *types.ApigwRoute, f *types.ApigwFilter, props *apigwFilterActionProps) (err error) { filters, _, err := svc.Search(ctx, types.ApigwFilterFilter{ - RouteID: r.ID, - Enabled: true, + RouteID: r.ID, + Disabled: filter.StateExcluded, }) if err != nil { diff --git a/system/types/apigw_filter.go b/system/types/apigw_filter.go index 5f36ffd89..2f4cc040c 100644 --- a/system/types/apigw_filter.go +++ b/system/types/apigw_filter.go @@ -13,12 +13,13 @@ type ( ApigwFilterParams map[string]interface{} ApigwFilter struct { - ID uint64 `json:"filterID,string"` - Route uint64 `json:"routeID,string"` - Weight uint64 `json:"weight,string"` - Ref string `json:"ref,omitempty"` - Kind string `json:"kind,omitempty"` - Params ApigwFilterParams `json:"params"` + ID uint64 `json:"filterID,string"` + Route uint64 `json:"routeID,string"` + Weight uint64 `json:"weight,string"` + Ref string `json:"ref,omitempty"` + Kind string `json:"kind,omitempty"` + Enabled bool `json:"enabled,omitempty"` + Params ApigwFilterParams `json:"params"` CreatedAt time.Time `json:"createdAt,omitempty"` CreatedBy uint64 `json:"createdBy,string" ` @@ -30,9 +31,9 @@ type ( ApigwFilterFilter struct { RouteID uint64 `json:"routeID,string"` - Enabled bool `json:"enabled"` - Deleted filter.State `json:"deleted"` + Deleted filter.State `json:"deleted"` + Disabled filter.State `json:"disabled"` // Check fn is called by store backend for each resource found function can // modify the resource and return false if store should not return it diff --git a/system/types/apigw_route.go b/system/types/apigw_route.go index 3911f012a..e1ab542ba 100644 --- a/system/types/apigw_route.go +++ b/system/types/apigw_route.go @@ -32,11 +32,11 @@ type ( } ApigwRouteFilter struct { - Route string `json:"route"` - Group string `json:"group"` - Enabled bool `json:"enabled"` + Route string `json:"route"` + Group string `json:"group"` - Deleted filter.State `json:"deleted"` + Deleted filter.State `json:"deleted"` + Disabled filter.State `json:"disabled"` // Check fn is called by store backend for each resource found function can // modify the resource and return false if store should not return it diff --git a/tests/apigw/main_test.go b/tests/apigw/main_test.go index d90b2500b..f67c9d1fe 100644 --- a/tests/apigw/main_test.go +++ b/tests/apigw/main_test.go @@ -142,7 +142,7 @@ func (h helper) apiInit() *apitest.APITest { func setupScenario(t *testing.T) (context.Context, helper, store.Storer) { ctx, h, s := setup(t) loadScenario(ctx, s, t, h) - apigw.Service().Reload(ctx) + _ = apigw.Service().Reload(ctx) return ctx, h, s } diff --git a/tests/apigw/testdata/prefilter_header_failing/def.yaml b/tests/apigw/testdata/prefilter_header_failing/def.yaml index d911ac534..ba3b73314 100644 --- a/tests/apigw/testdata/prefilter_header_failing/def.yaml +++ b/tests/apigw/testdata/prefilter_header_failing/def.yaml @@ -5,6 +5,7 @@ apigateway: filters: - ref: "header" kind: "prefilter" + enabled: true params: expr: "Token == \"super-secret-token\"" @@ -14,5 +15,6 @@ apigateway: filters: - ref: "header" kind: "prefilter" + enabled: true params: expr: "Accept-Language == \"fr-CH, fr;q=0.9\"" diff --git a/tests/apigw/testdata/prefilter_header_n_routes/def.yaml b/tests/apigw/testdata/prefilter_header_n_routes/def.yaml index 60d1c4856..057e542b6 100644 --- a/tests/apigw/testdata/prefilter_header_n_routes/def.yaml +++ b/tests/apigw/testdata/prefilter_header_n_routes/def.yaml @@ -5,6 +5,7 @@ apigateway: filters: - ref: "header" kind: "prefilter" + enabled: true params: expr: "P == \"a\"" @@ -14,5 +15,6 @@ apigateway: filters: - ref: "header" kind: "prefilter" + enabled: true params: expr: "P == \"b\"" diff --git a/tests/apigw/testdata/prefilter_header_passing/def.yaml b/tests/apigw/testdata/prefilter_header_passing/def.yaml index 13ff1506e..b9dd78885 100644 --- a/tests/apigw/testdata/prefilter_header_passing/def.yaml +++ b/tests/apigw/testdata/prefilter_header_passing/def.yaml @@ -5,6 +5,7 @@ apigateway: filters: - ref: "header" kind: "prefilter" + enabled: true params: expr: "Token == \"super-secret-token\"" @@ -14,5 +15,6 @@ apigateway: filters: - ref: "header" kind: "prefilter" + enabled: true params: expr: "headers[\"Accept-Language\"] == \"fr-CH, fr;q=0.9\"" diff --git a/tests/apigw/testdata/prefilter_query_failing/def.yaml b/tests/apigw/testdata/prefilter_query_failing/def.yaml index 709d8860a..07dc700b6 100644 --- a/tests/apigw/testdata/prefilter_query_failing/def.yaml +++ b/tests/apigw/testdata/prefilter_query_failing/def.yaml @@ -5,6 +5,7 @@ apigateway: filters: - ref: "queryParam" kind: "prefilter" + enabled: true params: expr: "token == \"super-secret-token\"" @@ -14,6 +15,7 @@ apigateway: filters: - ref: "queryParam" kind: "prefilter" + enabled: true params: expr: "foo-bar == \"encrypted-string\"" diff --git a/tests/apigw/testdata/prefilter_query_n_routes/def.yaml b/tests/apigw/testdata/prefilter_query_n_routes/def.yaml index 2aac8c921..b0aa3eee9 100644 --- a/tests/apigw/testdata/prefilter_query_n_routes/def.yaml +++ b/tests/apigw/testdata/prefilter_query_n_routes/def.yaml @@ -5,6 +5,7 @@ apigateway: filters: - ref: "queryParam" kind: "prefilter" + enabled: true params: expr: "p == \"a\"" @@ -14,5 +15,6 @@ apigateway: filters: - ref: "queryParam" kind: "prefilter" + enabled: true params: expr: "p == \"b\"" diff --git a/tests/apigw/testdata/prefilter_query_passing/def.yaml b/tests/apigw/testdata/prefilter_query_passing/def.yaml index 6c021e48c..9334af5db 100644 --- a/tests/apigw/testdata/prefilter_query_passing/def.yaml +++ b/tests/apigw/testdata/prefilter_query_passing/def.yaml @@ -5,6 +5,7 @@ apigateway: filters: - ref: "queryParam" kind: "prefilter" + enabled: true params: expr: "token == \"super-secret-token\"" @@ -14,5 +15,6 @@ apigateway: filters: - ref: "queryParam" kind: "prefilter" + enabled: true params: expr: "params[\"foo-bar\"] == \"encrypted-string\"" diff --git a/tests/apigw/testdata/processor_payload_simple/def.yaml b/tests/apigw/testdata/processor_payload_simple/def.yaml index a89e7c4e5..c56ed23df 100644 --- a/tests/apigw/testdata/processor_payload_simple/def.yaml +++ b/tests/apigw/testdata/processor_payload_simple/def.yaml @@ -5,6 +5,7 @@ apigateway: filters: - ref: "payload" kind: "processer" + enabled: true params: jsfunc: | const x = 10; diff --git a/tests/apigw/testdata/route_reload/def.yaml b/tests/apigw/testdata/route_reload/def.yaml index a89e7c4e5..c56ed23df 100644 --- a/tests/apigw/testdata/route_reload/def.yaml +++ b/tests/apigw/testdata/route_reload/def.yaml @@ -5,6 +5,7 @@ apigateway: filters: - ref: "payload" kind: "processer" + enabled: true params: jsfunc: | const x = 10; diff --git a/tests/envoy/store_yaml_api_gateway_test.go b/tests/envoy/store_yaml_api_gateway_test.go index 60b1c5ecb..f74ef57cf 100644 --- a/tests/envoy/store_yaml_api_gateway_test.go +++ b/tests/envoy/store_yaml_api_gateway_test.go @@ -47,7 +47,7 @@ func TestStoreYaml_APIGateway(t *testing.T) { _ = sTestAPIGatewayFilter(ctx, t, s, gwr.ID, "test") df := su.NewDecodeFilter(). - APIGWRoutes(&types.ApigwRouteFilter{Enabled: true}) + APIGWRoutes(&types.ApigwRouteFilter{}) return nil, df }, diff --git a/tests/envoy/system.go b/tests/envoy/system.go index ee06818d5..e1507744e 100644 --- a/tests/envoy/system.go +++ b/tests/envoy/system.go @@ -230,11 +230,12 @@ func sTestAPIGatewayRoute(ctx context.Context, t *testing.T, s store.Storer, r s func sTestAPIGatewayFilter(ctx context.Context, t *testing.T, s store.Storer, routeID uint64, pfx string) *types.ApigwFilter { gwf := &types.ApigwFilter{ - ID: su.NextID(), - Route: routeID, - Weight: 0, - Ref: pfx + "_ref", - Kind: pfx + "_kind", + ID: su.NextID(), + Route: routeID, + Weight: 0, + Ref: pfx + "_ref", + Kind: pfx + "_kind", + Enabled: true, Params: map[string]interface{}{ "param1": "value1", }, diff --git a/tests/system/apigw_test.go b/tests/system/apigw_test.go index e2e04f1e6..97b4a5f12 100644 --- a/tests/system/apigw_test.go +++ b/tests/system/apigw_test.go @@ -17,8 +17,16 @@ import ( ) func (h helper) createRouteWithFilter(s string, fkind string) (*types.ApigwRoute, *types.ApigwFilter) { - r := h.createRoute(&types.ApigwRoute{Endpoint: "/" + s, Method: "GET"}) - f := h.createFilters(&types.ApigwFilter{Kind: fkind}, r.ID) + return h.createRouteAndFilterWithEnabled(s, fkind, true, true) +} + +func (h helper) createRouteWithFilterEnabled(s string, fkind string, enable bool) (*types.ApigwRoute, *types.ApigwFilter) { + return h.createRouteAndFilterWithEnabled(s, fkind, true, enable) +} + +func (h helper) createRouteAndFilterWithEnabled(s string, fkind string, rEnable, fEnable bool) (*types.ApigwRoute, *types.ApigwFilter) { + r := h.createRoute(&types.ApigwRoute{Endpoint: "/" + s, Method: "GET", Enabled: rEnable}) + f := h.createFilters(&types.ApigwFilter{Kind: fkind, Enabled: fEnable}, r.ID) return r, f } @@ -96,6 +104,7 @@ func TestApigwRouteSearch(t *testing.T) { h.createRouteWithFilter("test1", "") h.createRouteWithFilter("test2", "") + h.createRouteAndFilterWithEnabled("test3", "", false, true) helpers.AllowMe(h, types.ComponentRbacResource(), "apigw-routes.search") helpers.AllowMe(h, types.ApigwRouteRbacResource(0), "read") @@ -112,6 +121,31 @@ func TestApigwRouteSearch(t *testing.T) { End() } +func TestApigwRouteSearch_includeDisabled(t *testing.T) { + h := newHelper(t) + h.clearRoutes() + + h.createRouteWithFilter("test1", "") + h.createRouteWithFilter("test2", "") + h.createRouteAndFilterWithEnabled("test3", "", false, true) + h.createRouteAndFilterWithEnabled("test4", "", false, false) + + helpers.AllowMe(h, types.ComponentRbacResource(), "apigw-routes.search") + helpers.AllowMe(h, types.ApigwRouteRbacResource(0), "read") + + h.apiInit(). + Get(fmt.Sprintf("/apigw/route/")). + Query("disabled", "1"). + Header("Accept", "application/json"). + Expect(t). + Status(http.StatusOK). + Assert(helpers.AssertNoErrors). + Assert(jsonpath.Len(`$.response.set`, 4)). + Assert(jsonpath.Equal(`$.response.set[0].endpoint`, "/test1")). + Assert(jsonpath.Equal(`$.response.set[1].endpoint`, "/test2")). + End() +} + func TestApigwRouteSearch_forbiden(t *testing.T) { h := newHelper(t) h.clearRoutes() @@ -168,6 +202,7 @@ func TestApigwRouteCreate(t *testing.T) { Assert(helpers.AssertNoErrors). Assert(jsonpath.Present(`$.response.routeID`)). Assert(jsonpath.Equal(`$.response.endpoint`, "/test")). + Assert(jsonpath.Equal(`$.response.enabled`, false)). End() } @@ -206,6 +241,7 @@ func TestApigwRouteUpdate(t *testing.T) { Assert(helpers.AssertNoErrors). Assert(jsonpath.Present(`$.response.routeID`)). Assert(jsonpath.Equal(`$.response.endpoint`, "/test-edited")). + Assert(jsonpath.Equal(`$.response.enabled`, false)). End() } @@ -348,6 +384,29 @@ func TestApigwFilterSearch(t *testing.T) { End() } +func TestApigwFilterSearch_includeDisabled(t *testing.T) { + h := newHelper(t) + h.clearRoutes() + + r, f := h.createRouteWithFilterEnabled("test1", "", false) + + helpers.AllowMe(h, types.ComponentRbacResource(), "apigw-routes.search") + helpers.AllowMe(h, types.ApigwRouteRbacResource(0), "read") + + h.apiInit(). + Get(fmt.Sprintf("/apigw/filter/")). + Query("routeID", strconv.FormatUint(r.ID, 10)). + Query("disabled", "1"). + Header("Accept", "application/json"). + Expect(t). + Status(http.StatusOK). + Assert(helpers.AssertNoErrors). + Assert(jsonpath.Len(`$.response.set`, 1)). + Assert(jsonpath.Equal(`$.response.set[0].filterID`, strconv.FormatUint(f.ID, 10))). + Assert(jsonpath.Equal(`$.response.set[0].routeID`, strconv.FormatUint(r.ID, 10))). + End() +} + func TestApigwFilterSearch_forbiden(t *testing.T) { h := newHelper(t) h.clearRoutes() @@ -384,6 +443,50 @@ func TestApigwFilterCreate(t *testing.T) { End() } +func TestApigwFilterCreate_enabled(t *testing.T) { + h := newHelper(t) + h.clearRoutes() + + r, _ := h.createRouteWithFilter("test1", "") + helpers.AllowMe(h, types.ApigwRouteRbacResource(r.ID), "read") + helpers.AllowMe(h, types.ApigwRouteRbacResource(r.ID), "update") + + h.apiInit(). + Put(fmt.Sprintf("/apigw/filter")). + Header("Accept", "application/json"). + FormData("routeID", strconv.FormatUint(r.ID, 10)). + FormData("enabled", strconv.FormatBool(true)). + Expect(t). + Status(http.StatusOK). + Assert(helpers.AssertNoErrors). + Assert(jsonpath.Present(`$.response.filterID`)). + Assert(jsonpath.Equal(`$.response.routeID`, strconv.FormatUint(r.ID, 10))). + Assert(jsonpath.Equal(`$.response.enabled`, true)). + End() +} + +func TestApigwFilterCreate_disabled(t *testing.T) { + h := newHelper(t) + h.clearRoutes() + + r, _ := h.createRouteWithFilter("test1", "") + helpers.AllowMe(h, types.ApigwRouteRbacResource(r.ID), "read") + helpers.AllowMe(h, types.ApigwRouteRbacResource(r.ID), "update") + + h.apiInit(). + Put(fmt.Sprintf("/apigw/filter")). + Header("Accept", "application/json"). + FormData("routeID", strconv.FormatUint(r.ID, 10)). + FormData("enabled", strconv.FormatBool(false)). + Expect(t). + Status(http.StatusOK). + Assert(helpers.AssertNoErrors). + Assert(jsonpath.Present(`$.response.filterID`)). + Assert(jsonpath.Equal(`$.response.routeID`, strconv.FormatUint(r.ID, 10))). + Assert(jsonpath.NotPresent(`$.response.enabled`)). + End() +} + func TestApigwFilterCreate_forbiden(t *testing.T) { h := newHelper(t) h.clearRoutes() @@ -421,6 +524,50 @@ func TestApigwFilterUpdate(t *testing.T) { End() } +func TestApigwFilterUpdate_enabled(t *testing.T) { + h := newHelper(t) + h.clearRoutes() + + r, f := h.createRouteWithFilterEnabled("test1", "", false) + helpers.AllowMe(h, types.ApigwRouteRbacResource(r.ID), "read") + helpers.AllowMe(h, types.ApigwRouteRbacResource(r.ID), "update") + + h.apiInit(). + Post(fmt.Sprintf("/apigw/filter/%d", f.ID)). + Header("Accept", "application/json"). + FormData("routeID", strconv.FormatUint(r.ID, 10)). + FormData("enabled", strconv.FormatBool(true)). + Expect(t). + Status(http.StatusOK). + Assert(helpers.AssertNoErrors). + Assert(jsonpath.Equal(`$.response.filterID`, strconv.FormatUint(f.ID, 10))). + Assert(jsonpath.Equal(`$.response.routeID`, strconv.FormatUint(r.ID, 10))). + Assert(jsonpath.Equal(`$.response.enabled`, true)). + End() +} + +func TestApigwFilterUpdate_disabled(t *testing.T) { + h := newHelper(t) + h.clearRoutes() + + r, f := h.createRouteWithFilterEnabled("test1", "", true) + helpers.AllowMe(h, types.ApigwRouteRbacResource(r.ID), "read") + helpers.AllowMe(h, types.ApigwRouteRbacResource(r.ID), "update") + + h.apiInit(). + Post(fmt.Sprintf("/apigw/filter/%d", f.ID)). + Header("Accept", "application/json"). + FormData("routeID", strconv.FormatUint(r.ID, 10)). + FormData("enabled", strconv.FormatBool(false)). + Expect(t). + Status(http.StatusOK). + Assert(helpers.AssertNoErrors). + Assert(jsonpath.Equal(`$.response.filterID`, strconv.FormatUint(f.ID, 10))). + Assert(jsonpath.Equal(`$.response.routeID`, strconv.FormatUint(r.ID, 10))). + Assert(jsonpath.NotPresent(`$.response.enabled`)). + End() +} + func TestApigwFilterUpdate_forbiden(t *testing.T) { h := newHelper(t) h.clearRoutes()