From 62e43f19d3acc017548549ed120edbc52987eac6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Toma=C5=BE=20Jerman?= Date: Tue, 31 May 2022 16:53:01 +0200 Subject: [PATCH] Add base DAL crud integration tests --- compose/service/record_test.go | 2 - store/adapters/rdbms/filters.gen.go | 4 + system/dal_connection.cue | 2 +- system/rest/dal_connection.go | 9 +- system/service/dal_connection.go | 4 +- system/service/dal_sensitivity_level.go | 2 +- tests/system/dal_connection_crud_test.go | 349 ++++++++++++++++++ tests/system/dal_driver_crud_test.go | 22 ++ .../system/dal_sensitivity_level_crud_test.go | 251 +++++++++++++ tests/system/main_test.go | 14 + .../dal_connection_create/generic.json | 43 +++ .../generic.json | 43 +++ .../generic.json | 43 +++ .../dal_connection_update/generic.json | 43 +++ .../generic.json | 43 +++ .../generic.json | 43 +++ .../dal_sensitivity_level_create/generic.json | 8 + .../generic.json | 8 + .../dal_sensitivity_level_update/generic.json | 8 + .../generic.json | 8 + 20 files changed, 940 insertions(+), 9 deletions(-) create mode 100644 tests/system/dal_connection_crud_test.go create mode 100644 tests/system/dal_driver_crud_test.go create mode 100644 tests/system/dal_sensitivity_level_crud_test.go create mode 100644 tests/system/testdata/dal_connection_create/generic.json create mode 100644 tests/system/testdata/dal_connection_create_forbidden/generic.json create mode 100644 tests/system/testdata/dal_connection_create_invalid_type/generic.json create mode 100644 tests/system/testdata/dal_connection_update/generic.json create mode 100644 tests/system/testdata/dal_connection_update_forbidden/generic.json create mode 100644 tests/system/testdata/dal_connection_update_primary/generic.json create mode 100644 tests/system/testdata/dal_sensitivity_level_create/generic.json create mode 100644 tests/system/testdata/dal_sensitivity_level_create_forbidden/generic.json create mode 100644 tests/system/testdata/dal_sensitivity_level_update/generic.json create mode 100644 tests/system/testdata/dal_sensitivity_level_update_forbidden/generic.json diff --git a/compose/service/record_test.go b/compose/service/record_test.go index 7840dc11f..aece5bc84 100644 --- a/compose/service/record_test.go +++ b/compose/service/record_test.go @@ -3,7 +3,6 @@ package service import ( "context" "fmt" - "github.com/davecgh/go-spew/spew" "testing" "github.com/cortezaproject/corteza-server/compose/service/values" @@ -898,7 +897,6 @@ func TestSetRecordOwner(t *testing.T) { ) req.NoError(rbacService.Grant(ctx, rbac.AllowRule(role.ID, types.RecordRbacResource(0, 0, 0), "owner.manage"))) - spew.Dump(rbacService.Rules()) req.NoError(store.CreateUser(ctx, s, invoker, originalOwner, alternativeOwner)) t.Run("invalid input", func(t *testing.T) { diff --git a/store/adapters/rdbms/filters.gen.go b/store/adapters/rdbms/filters.gen.go index 4b6b75ba4..4102b95ff 100644 --- a/store/adapters/rdbms/filters.gen.go +++ b/store/adapters/rdbms/filters.gen.go @@ -740,6 +740,10 @@ func DalConnectionFilter(f systemType.DalConnectionFilter) (ee []goqu.Expression ee = append(ee, goqu.C("handle").Eq(f.Handle)) } + if val := strings.TrimSpace(f.Type); len(val) > 0 { + ee = append(ee, goqu.C("type").Eq(f.Type)) + } + return ee, f, err } diff --git a/system/dal_connection.cue b/system/dal_connection.cue index 720e2975e..a2ae939dc 100644 --- a/system/dal_connection.cue +++ b/system/dal_connection.cue @@ -37,7 +37,7 @@ dal_connection: schema.#Resource & { deleted: {goType: "filter.State", storeIdent: "deleted_at"} } - byValue: ["connection_id", "handle"] + byValue: ["connection_id", "handle", "type"] byNilState: ["deleted"] } diff --git a/system/rest/dal_connection.go b/system/rest/dal_connection.go index f5f147126..b3e96358b 100644 --- a/system/rest/dal_connection.go +++ b/system/rest/dal_connection.go @@ -146,6 +146,10 @@ func (ctrl DalConnection) Undelete(ctx context.Context, r *request.DalConnection } func (ctrl DalConnection) makeFilterPayload(ctx context.Context, connections types.DalConnectionSet, f types.DalConnectionFilter) (out *connectionSetPayload, err error) { + out = &connectionSetPayload{ + Filter: f, + Set: make(types.DalConnectionSet, 0), + } for _, c := range connections { if c.Capabilities.Enforced == nil { c.Capabilities.Enforced = capabilities.Set{} @@ -160,10 +164,7 @@ func (ctrl DalConnection) makeFilterPayload(ctx context.Context, connections typ c.Capabilities.Enabled = capabilities.Set{} } } - out = &connectionSetPayload{ - Set: connections, - Filter: f, - } + out.Set = append(out.Set, connections...) return } diff --git a/system/service/dal_connection.go b/system/service/dal_connection.go index 1d62cf8ca..596b5caf1 100644 --- a/system/service/dal_connection.go +++ b/system/service/dal_connection.go @@ -297,7 +297,9 @@ func (svc *dalConnection) Search(ctx context.Context, filter types.DalConnection func (svc *dalConnection) ReloadConnections(ctx context.Context) (err error) { // Get all available connections - cc, _, err := store.SearchDalConnections(ctx, svc.store, types.DalConnectionFilter{}) + cc, _, err := store.SearchDalConnections(ctx, svc.store, types.DalConnectionFilter{ + Type: types.DalConnectionResourceType, + }) if err != nil { return } diff --git a/system/service/dal_sensitivity_level.go b/system/service/dal_sensitivity_level.go index 81fd7aa71..5e591098c 100644 --- a/system/service/dal_sensitivity_level.go +++ b/system/service/dal_sensitivity_level.go @@ -277,7 +277,7 @@ func (svc *dalSensitivityLevel) prepare(ctx context.Context, s store.Storer, sl { // Assure unique level for _, crt := range set { - if crt.Level == sl.Level { + if crt.Level == sl.Level && crt.ID != sl.ID { return nil, fmt.Errorf("invalid sensitivity level: duplicated level value %d", sl.Level) } } diff --git a/tests/system/dal_connection_crud_test.go b/tests/system/dal_connection_crud_test.go new file mode 100644 index 000000000..4894395f5 --- /dev/null +++ b/tests/system/dal_connection_crud_test.go @@ -0,0 +1,349 @@ +package system + +import ( + "context" + "fmt" + "net/http" + "testing" + "time" + + "github.com/cortezaproject/corteza-server/pkg/dal" + "github.com/cortezaproject/corteza-server/pkg/dal/capabilities" + "github.com/cortezaproject/corteza-server/pkg/id" + "github.com/cortezaproject/corteza-server/store" + "github.com/cortezaproject/corteza-server/system/service" + "github.com/cortezaproject/corteza-server/system/types" + "github.com/cortezaproject/corteza-server/tests/helpers" + jsonpath "github.com/steinfletcher/apitest-jsonpath" +) + +func (h helper) clearDalConnections() { + h.noError(store.TruncateDalConnections(context.Background(), service.DefaultStore)) +} + +func (h helper) getPrimaryConnection() *types.DalConnection { + cc, _, err := store.SearchDalConnections(context.Background(), service.DefaultStore, types.DalConnectionFilter{Type: types.DalPrimaryConnectionResourceType}) + h.a.NoError(err) + + if len(cc) != 1 { + h.a.FailNow("invalid state: no or too many primary connections") + } + + return cc[0] +} + +func (h helper) createDalConnection(res *types.DalConnection) *types.DalConnection { + if res.ID == 0 { + res.ID = id.Next() + } + + if res.Name == "" { + res.Name = "Test Connection" + } + if res.Handle == "" { + res.Handle = "test_connection" + } + if res.Type == "" { + res.Type = types.DalConnectionResourceType + } + if res.Ownership == "" { + res.Ownership = "tester" + } + + if res.Config.DefaultModelIdent == "" { + res.Config.DefaultModelIdent = "compose_records" + } + if res.Config.DefaultAttributeIdent == "" { + res.Config.DefaultAttributeIdent = "values" + } + if res.Config.DefaultPartitionFormat == "" { + res.Config.DefaultPartitionFormat = "compose_records_{{namespace}}_{{module}}" + } + if res.Config.PartitionFormatValidator == "" { + res.Config.PartitionFormatValidator = "" + } + if res.Config.Connection.Params == nil { + res.Config.Connection = dal.NewDSNConnection("sqlite3://file::memory:?cache=shared&mode=memory") + } + + if len(res.Capabilities.Enforced) == 0 { + res.Capabilities.Enforced = capabilities.FullCapabilities() + } + + if len(res.Capabilities.Supported) == 0 { + res.Capabilities.Supported = capabilities.Set{} + } + + if len(res.Capabilities.Unsupported) == 0 { + res.Capabilities.Unsupported = capabilities.Set{} + } + + if len(res.Capabilities.Enabled) == 0 { + res.Capabilities.Enabled = capabilities.Set{} + } + + if res.CreatedAt.IsZero() { + res.CreatedAt = time.Now() + } + if res.CreatedBy == 0 { + res.CreatedBy = h.cUser.ID + } + + h.a.NoError(service.DefaultStore.CreateDalConnection(context.Background(), res)) + h.a.NoError(service.DefaultDalConnection.ReloadConnections(context.Background())) + return res +} + +func Test_dal_connection_list(t *testing.T) { + h := newHelper(t) + defer h.clearDalConnections() + + helpers.AllowMe(h, types.ComponentRbacResource(), "dal-connections.search") + helpers.AllowMe(h, types.DalConnectionRbacResource(0), "read") + + h.apiInit(). + Get("/dal/connections/"). + Header("Accept", "application/json"). + Expect(t). + Status(http.StatusOK). + Assert(helpers.AssertNoErrors). + Assert(jsonpath.Len("$.response.set", 1)). + End() +} + +func Test_dal_connection_list_forbidden(t *testing.T) { + h := newHelper(t) + defer h.clearDalConnections() + + h.apiInit(). + Get("/dal/connections/"). + Header("Accept", "application/json"). + Expect(t). + Status(http.StatusOK). + Assert(helpers.AssertError("dalConnection.errors.notAllowedToSearch")). + End() +} + +func Test_dal_connection_list_forbidden_read(t *testing.T) { + h := newHelper(t) + defer h.clearDalConnections() + + helpers.AllowMe(h, types.ComponentRbacResource(), "dal-connections.search") + + h.apiInit(). + Get("/dal/connections/"). + Header("Accept", "application/json"). + Expect(t). + Status(http.StatusOK). + Assert(jsonpath.Len("$.response.set", 0)). + End() +} + +func Test_dal_connection_create(t *testing.T) { + h := newHelper(t) + defer h.clearDalConnections() + + helpers.AllowMe(h, types.ComponentRbacResource(), "dal-connection.create") + + h.apiInit(). + Post("/dal/connections/"). + Body(loadScenarioRequest(t, "generic.json")). + Header("Accept", "application/json"). + Header("Content-Type", "application/json"). + Expect(t). + Status(http.StatusOK). + Assert(helpers.AssertNoErrors). + Assert(jsonpath.Present("$.response.connectionID")). + End() +} + +func Test_dal_connection_create_forbidden(t *testing.T) { + h := newHelper(t) + defer h.clearDalConnections() + + h.apiInit(). + Post("/dal/connections/"). + Body(loadScenarioRequest(t, "generic.json")). + Header("Accept", "application/json"). + Header("Content-Type", "application/json"). + Expect(t). + Status(http.StatusOK). + Assert(helpers.AssertError("dalConnection.errors.notAllowedToCreate")). + End() +} + +func Test_dal_connection_update(t *testing.T) { + h := newHelper(t) + defer h.clearDalConnections() + + sl := h.createDalConnection(&types.DalConnection{ + Handle: "test_connection", + }) + + helpers.AllowMe(h, types.DalConnectionRbacResource(0), "update") + + h.apiInit(). + Put(fmt.Sprintf("/dal/connections/%d", sl.ID)). + Header("Accept", "application/json"). + Header("Content-Type", "application/json"). + Body(loadScenarioRequest(t, "generic.json")). + Expect(t). + Status(http.StatusOK). + Assert(helpers.AssertNoErrors). + Assert(jsonpath.Equal("$.response.handle", "test_connection_edited")). + End() +} + +func Test_dal_connection_update_primary(t *testing.T) { + h := newHelper(t) + defer h.clearDalConnections() + + sl := h.getPrimaryConnection() + + helpers.AllowMe(h, types.DalConnectionRbacResource(0), "update") + + h.apiInit(). + Put(fmt.Sprintf("/dal/connections/%d", sl.ID)). + Header("Accept", "application/json"). + Header("Content-Type", "application/json"). + Body(loadScenarioRequest(t, "generic.json")). + Expect(t). + Status(http.StatusOK). + Assert(helpers.AssertNoErrors). + Assert(jsonpath.Equal("$.response.name", "Primary Connection EDITED")). + End() +} + +func Test_dal_connection_update_forbidden(t *testing.T) { + h := newHelper(t) + defer h.clearDalConnections() + + sl := h.createDalConnection(&types.DalConnection{ + Handle: "test_connection", + }) + + h.apiInit(). + Put(fmt.Sprintf("/dal/connections/%d", sl.ID)). + Header("Accept", "application/json"). + Header("Content-Type", "application/json"). + Body(loadScenarioRequest(t, "generic.json")). + Expect(t). + Status(http.StatusOK). + Assert(helpers.AssertError("dalConnection.errors.notAllowedToUpdate")). + End() +} + +func Test_dal_connection_read(t *testing.T) { + h := newHelper(t) + defer h.clearDalConnections() + + sl := h.createDalConnection(&types.DalConnection{ + Handle: "test_connection", + }) + + helpers.AllowMe(h, types.DalConnectionRbacResource(0), "read") + + h.apiInit(). + Get(fmt.Sprintf("/dal/connections/%d", sl.ID)). + Header("Accept", "application/json"). + Expect(t). + Status(http.StatusOK). + Assert(helpers.AssertNoErrors). + Assert(jsonpath.Present("$.response.connectionID")). + End() +} + +func Test_dal_connection_read_forbiden(t *testing.T) { + h := newHelper(t) + defer h.clearDalConnections() + + sl := h.createDalConnection(&types.DalConnection{ + Handle: "test_connection", + }) + + h.apiInit(). + Get(fmt.Sprintf("/dal/connections/%d", sl.ID)). + Header("Accept", "application/json"). + Expect(t). + Status(http.StatusOK). + Assert(helpers.AssertError("dalConnection.errors.notAllowedToRead")). + End() +} + +func Test_dal_connection_delete(t *testing.T) { + h := newHelper(t) + defer h.clearDalConnections() + + sl := h.createDalConnection(&types.DalConnection{ + Handle: "test_connection", + }) + + helpers.AllowMe(h, types.DalConnectionRbacResource(0), "delete") + + h.apiInit(). + Delete(fmt.Sprintf("/dal/connections/%d", sl.ID)). + Header("Accept", "application/json"). + Expect(t). + Status(http.StatusOK). + Assert(helpers.AssertNoErrors). + Assert(jsonpath.Equal("$.success.message", "OK")). + End() +} + +func Test_dal_connection_delete_forbidden(t *testing.T) { + h := newHelper(t) + defer h.clearDalConnections() + + sl := h.createDalConnection(&types.DalConnection{ + Handle: "test_connection", + }) + + h.apiInit(). + Delete(fmt.Sprintf("/dal/connections/%d", sl.ID)). + Header("Accept", "application/json"). + Expect(t). + Status(http.StatusOK). + Assert(helpers.AssertError("dalConnection.errors.notAllowedToDelete")). + End() +} + +func Test_dal_connection_undelete(t *testing.T) { + h := newHelper(t) + defer h.clearDalConnections() + + sl := h.createDalConnection(&types.DalConnection{ + Handle: "test_connection", + DeletedAt: &h.cUser.CreatedAt, + DeletedBy: h.cUser.ID, + }) + + helpers.AllowMe(h, types.DalConnectionRbacResource(0), "delete") + + h.apiInit(). + Post(fmt.Sprintf("/dal/connections/%d/undelete", sl.ID)). + Header("Accept", "application/json"). + Expect(t). + Status(http.StatusOK). + Assert(helpers.AssertNoErrors). + Assert(jsonpath.Equal("$.success.message", "OK")). + End() +} + +func Test_dal_connection_undelete_forbidden(t *testing.T) { + h := newHelper(t) + defer h.clearDalConnections() + + sl := h.createDalConnection(&types.DalConnection{ + Handle: "test_connection", + DeletedAt: &h.cUser.CreatedAt, + DeletedBy: h.cUser.ID, + }) + + h.apiInit(). + Post(fmt.Sprintf("/dal/connections/%d/undelete", sl.ID)). + Header("Accept", "application/json"). + Expect(t). + Status(http.StatusOK). + Assert(helpers.AssertError("dalConnection.errors.notAllowedToUndelete")). + End() +} diff --git a/tests/system/dal_driver_crud_test.go b/tests/system/dal_driver_crud_test.go new file mode 100644 index 000000000..dc6971a45 --- /dev/null +++ b/tests/system/dal_driver_crud_test.go @@ -0,0 +1,22 @@ +package system + +import ( + "net/http" + "testing" + + "github.com/cortezaproject/corteza-server/tests/helpers" + jsonpath "github.com/steinfletcher/apitest-jsonpath" +) + +func Test_dal_driver_list(t *testing.T) { + h := newHelper(t) + + h.apiInit(). + Get("/dal/drivers/"). + Expect(t). + Status(http.StatusOK). + Assert(helpers.AssertNoErrors). + Assert(jsonpath.Len("$.response.set", 1)). + Assert(jsonpath.Present("$.response.set[0].capabilities")). + End() +} diff --git a/tests/system/dal_sensitivity_level_crud_test.go b/tests/system/dal_sensitivity_level_crud_test.go new file mode 100644 index 000000000..54daede73 --- /dev/null +++ b/tests/system/dal_sensitivity_level_crud_test.go @@ -0,0 +1,251 @@ +package system + +import ( + "context" + "fmt" + "net/http" + "testing" + "time" + + "github.com/cortezaproject/corteza-server/pkg/id" + "github.com/cortezaproject/corteza-server/store" + "github.com/cortezaproject/corteza-server/system/service" + "github.com/cortezaproject/corteza-server/system/types" + "github.com/cortezaproject/corteza-server/tests/helpers" + jsonpath "github.com/steinfletcher/apitest-jsonpath" +) + +func (h helper) clearSensitivityLevels() { + h.noError(store.TruncateDalSensitivityLevels(context.Background(), service.DefaultStore)) +} + +func (h helper) createSensitivityLevel(res *types.DalSensitivityLevel) *types.DalSensitivityLevel { + if res.ID == 0 { + res.ID = id.Next() + } + + if res.CreatedAt.IsZero() { + res.CreatedAt = time.Now() + } + + h.a.NoError(service.DefaultStore.CreateDalSensitivityLevel(context.Background(), res)) + return res +} + +func Test_dal_sensitivity_level_list(t *testing.T) { + h := newHelper(t) + defer h.clearSensitivityLevels() + + helpers.AllowMe(h, types.ComponentRbacResource(), "dal-sensitivity-level.manage") + + h.apiInit(). + Get("/dal/sensitivity-levels/"). + Header("Accept", "application/json"). + Expect(t). + Status(http.StatusOK). + Assert(helpers.AssertNoErrors). + End() +} + +func Test_dal_sensitivity_level_list_forbidden(t *testing.T) { + h := newHelper(t) + defer h.clearSensitivityLevels() + + h.apiInit(). + Get("/dal/sensitivity-levels/"). + Header("Accept", "application/json"). + Expect(t). + Status(http.StatusOK). + Assert(helpers.AssertError("dalSensitivityLevel.errors.notAllowedToManage")). + End() +} + +func Test_dal_sensitivity_level_create(t *testing.T) { + h := newHelper(t) + defer h.clearSensitivityLevels() + + helpers.AllowMe(h, types.ComponentRbacResource(), "dal-sensitivity-level.manage") + + h.apiInit(). + Post("/dal/sensitivity-levels/"). + Body(loadScenarioRequest(t, "generic.json")). + Header("Accept", "application/json"). + Header("Content-Type", "application/json"). + Expect(t). + Status(http.StatusOK). + Assert(helpers.AssertNoErrors). + Assert(jsonpath.Present("$.response.sensitivityLevelID")). + End() +} + +func Test_dal_sensitivity_level_create_forbidden(t *testing.T) { + h := newHelper(t) + defer h.clearSensitivityLevels() + + h.apiInit(). + Post("/dal/sensitivity-levels/"). + Body(loadScenarioRequest(t, "generic.json")). + Header("Accept", "application/json"). + Header("Content-Type", "application/json"). + Expect(t). + Status(http.StatusOK). + Assert(helpers.AssertError("dalSensitivityLevel.errors.notAllowedToManage")). + End() +} + +func Test_dal_sensitivity_level_update(t *testing.T) { + h := newHelper(t) + defer h.clearSensitivityLevels() + + sl := h.createSensitivityLevel(&types.DalSensitivityLevel{ + Handle: "test_sl", + }) + + helpers.AllowMe(h, types.ComponentRbacResource(), "dal-sensitivity-level.manage") + + h.apiInit(). + Put(fmt.Sprintf("/dal/sensitivity-levels/%d", sl.ID)). + Header("Accept", "application/json"). + Header("Content-Type", "application/json"). + Body(loadScenarioRequest(t, "generic.json")). + Expect(t). + Status(http.StatusOK). + Assert(helpers.AssertNoErrors). + Assert(jsonpath.Present("$.response.sensitivityLevelID")). + Assert(jsonpath.Equal("$.response.handle", "private_edited")). + End() +} + +func Test_dal_sensitivity_level_update_forbidden(t *testing.T) { + h := newHelper(t) + defer h.clearSensitivityLevels() + + sl := h.createSensitivityLevel(&types.DalSensitivityLevel{ + Handle: "test_sl", + }) + + h.apiInit(). + Put(fmt.Sprintf("/dal/sensitivity-levels/%d", sl.ID)). + Header("Accept", "application/json"). + Header("Content-Type", "application/json"). + Body(loadScenarioRequest(t, "generic.json")). + Expect(t). + Status(http.StatusOK). + Assert(helpers.AssertError("dalSensitivityLevel.errors.notAllowedToManage")). + End() +} + +func Test_dal_sensitivity_level_read(t *testing.T) { + h := newHelper(t) + defer h.clearSensitivityLevels() + + sl := h.createSensitivityLevel(&types.DalSensitivityLevel{ + Handle: "test_sl", + }) + + helpers.AllowMe(h, types.ComponentRbacResource(), "dal-sensitivity-level.manage") + + h.apiInit(). + Get(fmt.Sprintf("/dal/sensitivity-levels/%d", sl.ID)). + Header("Accept", "application/json"). + Expect(t). + Status(http.StatusOK). + Assert(helpers.AssertNoErrors). + Assert(jsonpath.Present("$.response.sensitivityLevelID")). + End() +} + +func Test_dal_sensitivity_level_read_forbiden(t *testing.T) { + h := newHelper(t) + defer h.clearSensitivityLevels() + + sl := h.createSensitivityLevel(&types.DalSensitivityLevel{ + Handle: "test_sl", + }) + + h.apiInit(). + Get(fmt.Sprintf("/dal/sensitivity-levels/%d", sl.ID)). + Header("Accept", "application/json"). + Expect(t). + Status(http.StatusOK). + Assert(helpers.AssertError("dalSensitivityLevel.errors.notAllowedToManage")). + End() +} + +func Test_dal_sensitivity_level_delete(t *testing.T) { + h := newHelper(t) + defer h.clearSensitivityLevels() + + sl := h.createSensitivityLevel(&types.DalSensitivityLevel{ + Handle: "test_sl", + }) + + helpers.AllowMe(h, types.ComponentRbacResource(), "dal-sensitivity-level.manage") + + h.apiInit(). + Delete(fmt.Sprintf("/dal/sensitivity-levels/%d", sl.ID)). + Header("Accept", "application/json"). + Expect(t). + Status(http.StatusOK). + Assert(helpers.AssertNoErrors). + Assert(jsonpath.Equal("$.success.message", "OK")). + End() +} + +func Test_dal_sensitivity_level_delete_forbidden(t *testing.T) { + h := newHelper(t) + defer h.clearSensitivityLevels() + + sl := h.createSensitivityLevel(&types.DalSensitivityLevel{ + Handle: "test_sl", + }) + + h.apiInit(). + Delete(fmt.Sprintf("/dal/sensitivity-levels/%d", sl.ID)). + Header("Accept", "application/json"). + Expect(t). + Status(http.StatusOK). + Assert(helpers.AssertError("dalSensitivityLevel.errors.notAllowedToManage")). + End() +} + +func Test_dal_sensitivity_level_undelete(t *testing.T) { + h := newHelper(t) + defer h.clearSensitivityLevels() + + sl := h.createSensitivityLevel(&types.DalSensitivityLevel{ + Handle: "test_sl", + DeletedAt: &h.cUser.CreatedAt, + DeletedBy: h.cUser.ID, + }) + + helpers.AllowMe(h, types.ComponentRbacResource(), "dal-sensitivity-level.manage") + + h.apiInit(). + Post(fmt.Sprintf("/dal/sensitivity-levels/%d/undelete", sl.ID)). + Header("Accept", "application/json"). + Expect(t). + Status(http.StatusOK). + Assert(helpers.AssertNoErrors). + Assert(jsonpath.Equal("$.success.message", "OK")). + End() +} + +func Test_dal_sensitivity_level_undelete_forbidden(t *testing.T) { + h := newHelper(t) + defer h.clearSensitivityLevels() + + sl := h.createSensitivityLevel(&types.DalSensitivityLevel{ + Handle: "test_sl", + DeletedAt: &h.cUser.CreatedAt, + DeletedBy: h.cUser.ID, + }) + + h.apiInit(). + Post(fmt.Sprintf("/dal/sensitivity-levels/%d/undelete", sl.ID)). + Header("Accept", "application/json"). + Expect(t). + Status(http.StatusOK). + Assert(helpers.AssertError("dalSensitivityLevel.errors.notAllowedToManage")). + End() +} diff --git a/tests/system/main_test.go b/tests/system/main_test.go index 749ffd36a..21028d24c 100644 --- a/tests/system/main_test.go +++ b/tests/system/main_test.go @@ -5,7 +5,9 @@ import ( "embed" "errors" "io" + "io/ioutil" "os" + "path" "testing" "github.com/cortezaproject/corteza-server/app" @@ -24,6 +26,7 @@ import ( "github.com/cortezaproject/corteza-server/pkg/rand" "github.com/cortezaproject/corteza-server/pkg/rbac" "github.com/cortezaproject/corteza-server/store" + "github.com/cortezaproject/corteza-server/store/adapters/rdbms/drivers/sqlite" "github.com/cortezaproject/corteza-server/system/rest" "github.com/cortezaproject/corteza-server/system/service" "github.com/cortezaproject/corteza-server/system/types" @@ -214,3 +217,14 @@ func readStaticFile(f string) []byte { c, _ := mockData.ReadFile(f) return c } + +func loadScenarioRequest(t *testing.T, req string) string { + f, err := os.Open(path.Join("testdata", t.Name()[5:], req)) + require.NoError(t, err) + defer f.Close() + + bb, err := ioutil.ReadAll(f) + require.NoError(t, err) + + return string(bb) +} diff --git a/tests/system/testdata/dal_connection_create/generic.json b/tests/system/testdata/dal_connection_create/generic.json new file mode 100644 index 000000000..613253294 --- /dev/null +++ b/tests/system/testdata/dal_connection_create/generic.json @@ -0,0 +1,43 @@ +{ + "name": "Test Connection", + "handle": "test_connection", + "type": "corteza::system:dal_connection", + "location": { + "geometry": { + "type": "", + "coordinates": null + }, + "properties": { + "name": "" + } + }, + "ownership": "", + "sensitivityLevel": "0", + "config": { + "defaultModelIdent": "compose_record", + "defaultAttributeIdent": "values", + "defaultPartitionFormat": "compose_record_{{namespace}}_{{module}}", + "partitionFormatValidator": "", + "connection": { + "type": "corteza::dal:connection:dsn", + "params": { + "dsn": "mysql://user:pass@tcp(localhost:3308)/corteza?collation=utf8mb4_general_ci" + } + } + }, + "capabilities": { + "enforced": [], + "supported": [ + "corteza::dal:capability:create", + "corteza::dal:capability:update", + "corteza::dal:capability:search", + "corteza::dal:capability:lookup", + "corteza::dal:capability:paging", + "corteza::dal:capability:stats", + "corteza::dal:capability:sorting", + "corteza::dal:capability:RBAC" + ], + "unsupported": [], + "enabled": [] + } +} diff --git a/tests/system/testdata/dal_connection_create_forbidden/generic.json b/tests/system/testdata/dal_connection_create_forbidden/generic.json new file mode 100644 index 000000000..613253294 --- /dev/null +++ b/tests/system/testdata/dal_connection_create_forbidden/generic.json @@ -0,0 +1,43 @@ +{ + "name": "Test Connection", + "handle": "test_connection", + "type": "corteza::system:dal_connection", + "location": { + "geometry": { + "type": "", + "coordinates": null + }, + "properties": { + "name": "" + } + }, + "ownership": "", + "sensitivityLevel": "0", + "config": { + "defaultModelIdent": "compose_record", + "defaultAttributeIdent": "values", + "defaultPartitionFormat": "compose_record_{{namespace}}_{{module}}", + "partitionFormatValidator": "", + "connection": { + "type": "corteza::dal:connection:dsn", + "params": { + "dsn": "mysql://user:pass@tcp(localhost:3308)/corteza?collation=utf8mb4_general_ci" + } + } + }, + "capabilities": { + "enforced": [], + "supported": [ + "corteza::dal:capability:create", + "corteza::dal:capability:update", + "corteza::dal:capability:search", + "corteza::dal:capability:lookup", + "corteza::dal:capability:paging", + "corteza::dal:capability:stats", + "corteza::dal:capability:sorting", + "corteza::dal:capability:RBAC" + ], + "unsupported": [], + "enabled": [] + } +} diff --git a/tests/system/testdata/dal_connection_create_invalid_type/generic.json b/tests/system/testdata/dal_connection_create_invalid_type/generic.json new file mode 100644 index 000000000..0598fc0c4 --- /dev/null +++ b/tests/system/testdata/dal_connection_create_invalid_type/generic.json @@ -0,0 +1,43 @@ +{ + "name": "Test Connection", + "handle": "test_connection", + "type": "corteza::system:primary_dal_connection", + "location": { + "geometry": { + "type": "", + "coordinates": null + }, + "properties": { + "name": "" + } + }, + "ownership": "", + "sensitivityLevel": "0", + "config": { + "defaultModelIdent": "compose_record", + "defaultAttributeIdent": "values", + "defaultPartitionFormat": "compose_record_{{namespace}}_{{module}}", + "partitionFormatValidator": "", + "connection": { + "type": "corteza::dal:connection:dsn", + "params": { + "dsn": "mysql://user:pass@tcp(localhost:3308)/corteza?collation=utf8mb4_general_ci" + } + } + }, + "capabilities": { + "enforced": [], + "supported": [ + "corteza::dal:capability:create", + "corteza::dal:capability:update", + "corteza::dal:capability:search", + "corteza::dal:capability:lookup", + "corteza::dal:capability:paging", + "corteza::dal:capability:stats", + "corteza::dal:capability:sorting", + "corteza::dal:capability:RBAC" + ], + "unsupported": [], + "enabled": [] + } +} diff --git a/tests/system/testdata/dal_connection_update/generic.json b/tests/system/testdata/dal_connection_update/generic.json new file mode 100644 index 000000000..5a809465e --- /dev/null +++ b/tests/system/testdata/dal_connection_update/generic.json @@ -0,0 +1,43 @@ +{ + "name": "Test Connection EDITED", + "handle": "test_connection_edited", + "type": "corteza::system:dal_connection", + "location": { + "geometry": { + "type": "", + "coordinates": null + }, + "properties": { + "name": "" + } + }, + "ownership": "", + "sensitivityLevel": "0", + "config": { + "defaultModelIdent": "compose_record", + "defaultAttributeIdent": "values", + "defaultPartitionFormat": "compose_record_{{namespace}}_{{module}}", + "partitionFormatValidator": "", + "connection": { + "type": "corteza::dal:connection:dsn", + "params": { + "dsn": "sqlite3://file::memory:?cache=shared&mode=memory" + } + } + }, + "capabilities": { + "enforced": [], + "supported": [ + "corteza::dal:capability:create", + "corteza::dal:capability:update", + "corteza::dal:capability:search", + "corteza::dal:capability:lookup", + "corteza::dal:capability:paging", + "corteza::dal:capability:stats", + "corteza::dal:capability:sorting", + "corteza::dal:capability:RBAC" + ], + "unsupported": [], + "enabled": [] + } +} diff --git a/tests/system/testdata/dal_connection_update_forbidden/generic.json b/tests/system/testdata/dal_connection_update_forbidden/generic.json new file mode 100644 index 000000000..5a809465e --- /dev/null +++ b/tests/system/testdata/dal_connection_update_forbidden/generic.json @@ -0,0 +1,43 @@ +{ + "name": "Test Connection EDITED", + "handle": "test_connection_edited", + "type": "corteza::system:dal_connection", + "location": { + "geometry": { + "type": "", + "coordinates": null + }, + "properties": { + "name": "" + } + }, + "ownership": "", + "sensitivityLevel": "0", + "config": { + "defaultModelIdent": "compose_record", + "defaultAttributeIdent": "values", + "defaultPartitionFormat": "compose_record_{{namespace}}_{{module}}", + "partitionFormatValidator": "", + "connection": { + "type": "corteza::dal:connection:dsn", + "params": { + "dsn": "sqlite3://file::memory:?cache=shared&mode=memory" + } + } + }, + "capabilities": { + "enforced": [], + "supported": [ + "corteza::dal:capability:create", + "corteza::dal:capability:update", + "corteza::dal:capability:search", + "corteza::dal:capability:lookup", + "corteza::dal:capability:paging", + "corteza::dal:capability:stats", + "corteza::dal:capability:sorting", + "corteza::dal:capability:RBAC" + ], + "unsupported": [], + "enabled": [] + } +} diff --git a/tests/system/testdata/dal_connection_update_primary/generic.json b/tests/system/testdata/dal_connection_update_primary/generic.json new file mode 100644 index 000000000..7de858cf6 --- /dev/null +++ b/tests/system/testdata/dal_connection_update_primary/generic.json @@ -0,0 +1,43 @@ +{ + "name": "Primary Connection EDITED", + "handle": "primary_connection", + "type": "corteza::system:primary_dal_connection", + "location": { + "geometry": { + "type": "", + "coordinates": null + }, + "properties": { + "name": "" + } + }, + "ownership": "", + "sensitivityLevel": "0", + "config": { + "defaultModelIdent": "compose_record", + "defaultAttributeIdent": "values", + "defaultPartitionFormat": "compose_record_{{namespace}}_{{module}}", + "partitionFormatValidator": "", + "connection": { + "type": "corteza::dal:connection:dsn", + "params": { + "dsn": "sqlite3://file::memory:?cache=shared&mode=memory" + } + } + }, + "capabilities": { + "enforced": [], + "supported": [ + "corteza::dal:capability:create", + "corteza::dal:capability:update", + "corteza::dal:capability:search", + "corteza::dal:capability:lookup", + "corteza::dal:capability:paging", + "corteza::dal:capability:stats", + "corteza::dal:capability:sorting", + "corteza::dal:capability:RBAC" + ], + "unsupported": [], + "enabled": [] + } +} diff --git a/tests/system/testdata/dal_sensitivity_level_create/generic.json b/tests/system/testdata/dal_sensitivity_level_create/generic.json new file mode 100644 index 000000000..5951028bd --- /dev/null +++ b/tests/system/testdata/dal_sensitivity_level_create/generic.json @@ -0,0 +1,8 @@ +{ + "handle": "private", + "level": 1, + "meta": { + "name": "private sensitivity", + "description": "data is private to the owner" + } +} diff --git a/tests/system/testdata/dal_sensitivity_level_create_forbidden/generic.json b/tests/system/testdata/dal_sensitivity_level_create_forbidden/generic.json new file mode 100644 index 000000000..5951028bd --- /dev/null +++ b/tests/system/testdata/dal_sensitivity_level_create_forbidden/generic.json @@ -0,0 +1,8 @@ +{ + "handle": "private", + "level": 1, + "meta": { + "name": "private sensitivity", + "description": "data is private to the owner" + } +} diff --git a/tests/system/testdata/dal_sensitivity_level_update/generic.json b/tests/system/testdata/dal_sensitivity_level_update/generic.json new file mode 100644 index 000000000..76a3c92db --- /dev/null +++ b/tests/system/testdata/dal_sensitivity_level_update/generic.json @@ -0,0 +1,8 @@ +{ + "handle": "private_edited", + "level": 1, + "meta": { + "name": "private sensitivity", + "description": "data is private to the owner" + } +} diff --git a/tests/system/testdata/dal_sensitivity_level_update_forbidden/generic.json b/tests/system/testdata/dal_sensitivity_level_update_forbidden/generic.json new file mode 100644 index 000000000..76a3c92db --- /dev/null +++ b/tests/system/testdata/dal_sensitivity_level_update_forbidden/generic.json @@ -0,0 +1,8 @@ +{ + "handle": "private_edited", + "level": 1, + "meta": { + "name": "private sensitivity", + "description": "data is private to the owner" + } +}