diff --git a/compose/importer/module_test.go b/compose/importer/module_test.go index e3cc0c63c..38eda268b 100644 --- a/compose/importer/module_test.go +++ b/compose/importer/module_test.go @@ -9,7 +9,7 @@ import ( ) func TestModuleImport_CastSet(t *testing.T) { - stdFieldsAsTest := func(t *testing.T, module *Module) { + stdFieldsAsTest := func(t *testing.T, module *Module, testOrder bool) { // map definition's sort order is unreliable req := require.New(t) @@ -18,17 +18,35 @@ func TestModuleImport_CastSet(t *testing.T) { tc := module.set.FindByHandle("mod1") req.Equal(tc.Name, "Module with fields") req.Len(tc.Fields, 2) - req.Equal(tc.Fields.FindByName("f1"), &types.ModuleField{ + req.NotNil(tc.Fields.FindByName("f1")) + req.NotNil(tc.Fields.FindByName("f2")) + + f1 := tc.Fields.FindByName("f1") + f2 := tc.Fields.FindByName("f2") + + if testOrder { + req.Equal(0, f1.Place) + req.Equal(1, f2.Place) + } else { + // Check if field's place property has been set + req.Equal(1, f1.Place+f2.Place) + } + + // due to "module_fields_as_map" & unstable order of map[..].. + // we need to reset order here + f1.Place = 0 + f2.Place = 0 + + req.Equal(f1, &types.ModuleField{ Name: "f1", Kind: "Number", Label: "f1", }) - req.Equal(tc.Fields.FindByName("f2"), &types.ModuleField{ + req.Equal(f2, &types.ModuleField{ Name: "f2", Kind: "String", Label: "F2", - Place: 1, Required: true, Options: map[string]interface{}{ "multiLine": true, @@ -38,6 +56,12 @@ func TestModuleImport_CastSet(t *testing.T) { }) } - impFixTester(t, "module_fields_as_slice", stdFieldsAsTest) - impFixTester(t, "module_fields_as_map", stdFieldsAsTest) + impFixTester(t, "module_fields_as_slice", func(t *testing.T, module *Module) { + stdFieldsAsTest(t, module, true) + + }) + + impFixTester(t, "module_fields_as_map", func(t *testing.T, module *Module) { + stdFieldsAsTest(t, module, false) + }) } diff --git a/compose/rest/chart.go b/compose/rest/chart.go index 577613dc8..1c2419d74 100644 --- a/compose/rest/chart.go +++ b/compose/rest/chart.go @@ -70,10 +70,11 @@ func (ctrl Chart) Create(ctx context.Context, r *request.ChartCreate) (interface Handle: r.Handle, } - if err = r.Config.Unmarshal(&mod.Config); err != nil { - return nil, err + if len(r.Config) > 2 { + if err = r.Config.Unmarshal(&mod.Config); err != nil { + return nil, err + } } - mod, err = ctrl.chart.With(ctx).Create(mod) return ctrl.makePayload(ctx, mod, err) } @@ -96,10 +97,11 @@ func (ctrl Chart) Update(ctx context.Context, r *request.ChartUpdate) (interface } ) - if err = r.Config.Unmarshal(&mod.Config); err != nil { - return nil, err + if len(r.Config) > 2 { + if err = r.Config.Unmarshal(&mod.Config); err != nil { + return nil, err + } } - mod, err = ctrl.chart.With(ctx).Update(mod) return ctrl.makePayload(ctx, mod, err) } diff --git a/compose/rest/page.go b/compose/rest/page.go index 468980d25..b202ed579 100644 --- a/compose/rest/page.go +++ b/compose/rest/page.go @@ -83,10 +83,11 @@ func (ctrl *Page) Create(ctx context.Context, r *request.PageCreate) (interface{ } ) - if err = r.Blocks.Unmarshal(&mod.Blocks); err != nil { - return nil, err + if len(r.Blocks) > 2 { + if err = r.Blocks.Unmarshal(&mod.Blocks); err != nil { + return nil, err + } } - mod, err = ctrl.page.With(ctx).Create(mod) return ctrl.makePayload(ctx, mod, err) } @@ -116,8 +117,10 @@ func (ctrl *Page) Update(ctx context.Context, r *request.PageUpdate) (interface{ } ) - if err = r.Blocks.Unmarshal(&mod.Blocks); err != nil { - return nil, err + if len(r.Blocks) > 2 { + if err = r.Blocks.Unmarshal(&mod.Blocks); err != nil { + return nil, err + } } mod, err = ctrl.page.With(ctx).Update(mod) diff --git a/compose/service/access_control.go b/compose/service/access_control.go index ca882399b..af91a4785 100644 --- a/compose/service/access_control.go +++ b/compose/service/access_control.go @@ -190,60 +190,6 @@ func (svc accessControl) FindRulesByRoleID(ctx context.Context, roleID uint64) ( return svc.permissions.FindRulesByRoleID(roleID), nil } -// DefaultRules returns list of default rules for this compose service -func (svc accessControl) DefaultRules() permissions.RuleSet { - var ( - compose = types.ComposePermissionResource - namespaces = types.NamespacePermissionResource.AppendWildcard() - modules = types.ModulePermissionResource.AppendWildcard() - charts = types.ChartPermissionResource.AppendWildcard() - pages = types.PagePermissionResource.AppendWildcard() - ascripts = types.AutomationScriptPermissionResource.AppendWildcard() - - allowAdm = func(res permissions.Resource, op permissions.Operation) *permissions.Rule { - return permissions.AllowRule(permissions.AdminRoleID, res, op) - } - ) - - return permissions.RuleSet{ - permissions.AllowRule(permissions.EveryoneRoleID, compose, "access"), - - allowAdm(compose, "namespace.create"), - allowAdm(compose, "access"), - allowAdm(compose, "grant"), - - allowAdm(namespaces, "read"), - allowAdm(namespaces, "update"), - allowAdm(namespaces, "delete"), - allowAdm(namespaces, "manage"), - allowAdm(namespaces, "page.create"), - allowAdm(namespaces, "module.create"), - allowAdm(namespaces, "chart.create"), - allowAdm(namespaces, "automation-script.create"), - - allowAdm(modules, "read"), - allowAdm(modules, "update"), - allowAdm(modules, "delete"), - allowAdm(modules, "record.create"), - allowAdm(modules, "record.read"), - allowAdm(modules, "record.update"), - allowAdm(modules, "record.delete"), - allowAdm(modules, "automation-trigger.manage"), - - allowAdm(charts, "read"), - allowAdm(charts, "update"), - allowAdm(charts, "delete"), - - allowAdm(pages, "read"), - allowAdm(pages, "update"), - allowAdm(pages, "delete"), - - allowAdm(ascripts, "read"), - allowAdm(ascripts, "update"), - allowAdm(ascripts, "delete"), - } -} - func (svc accessControl) Whitelist() permissions.Whitelist { var wl = permissions.Whitelist{} diff --git a/messaging/service/access_control.go b/messaging/service/access_control.go index 3e17466b5..b981bda9e 100644 --- a/messaging/service/access_control.go +++ b/messaging/service/access_control.go @@ -230,52 +230,6 @@ func (svc accessControl) FindRulesByRoleID(ctx context.Context, roleID uint64) ( return svc.permissions.FindRulesByRoleID(roleID), nil } -// DefaultRules returns list of default rules for this compose service -func (svc accessControl) DefaultRules() permissions.RuleSet { - var ( - messaging = types.MessagingPermissionResource - channels = types.ChannelPermissionResource.AppendWildcard() - - allowAdm = func(res permissions.Resource, op permissions.Operation) *permissions.Rule { - return permissions.AllowRule(permissions.AdminRoleID, res, op) - } - ) - - return permissions.RuleSet{ - permissions.AllowRule(permissions.EveryoneRoleID, messaging, "access"), - - allowAdm(messaging, "access"), - allowAdm(messaging, "grant"), - allowAdm(messaging, "channel.public.create"), - allowAdm(messaging, "channel.private.create"), - allowAdm(messaging, "channel.group.create"), - allowAdm(messaging, "webhook.create"), - allowAdm(messaging, "webhook.manage.all"), - allowAdm(messaging, "webhook.manage.own"), - - allowAdm(channels, "update"), - allowAdm(channels, "leave"), - allowAdm(channels, "read"), - allowAdm(channels, "join"), - allowAdm(channels, "delete"), - allowAdm(channels, "undelete"), - allowAdm(channels, "archive"), - allowAdm(channels, "unarchive"), - allowAdm(channels, "members.manage"), - allowAdm(channels, "webhooks.manage"), - allowAdm(channels, "attachments.manage"), - allowAdm(channels, "message.attach"), - allowAdm(channels, "message.update.all"), - allowAdm(channels, "message.update.own"), - allowAdm(channels, "message.delete.all"), - allowAdm(channels, "message.delete.own"), - allowAdm(channels, "message.embed"), - allowAdm(channels, "message.send"), - allowAdm(channels, "message.reply"), - allowAdm(channels, "message.react"), - } -} - func (svc accessControl) Whitelist() permissions.Whitelist { var wl = permissions.Whitelist{} diff --git a/system/service/access_control.go b/system/service/access_control.go index 1436fc670..881a39f82 100644 --- a/system/service/access_control.go +++ b/system/service/access_control.go @@ -165,56 +165,6 @@ func (svc accessControl) FindRulesByRoleID(ctx context.Context, roleID uint64) ( return svc.permissions.FindRulesByRoleID(roleID), nil } -// DefaultRules returns list of default rules for this compose service -func (svc accessControl) DefaultRules() permissions.RuleSet { - var ( - sys = types.SystemPermissionResource - applications = types.ApplicationPermissionResource.AppendWildcard() - organisations = types.OrganisationPermissionResource.AppendWildcard() - roles = types.RolePermissionResource.AppendWildcard() - users = types.UserPermissionResource.AppendWildcard() - ascripts = types.AutomationScriptPermissionResource.AppendWildcard() - - allowAdm = func(res permissions.Resource, op permissions.Operation) *permissions.Rule { - return permissions.AllowRule(permissions.AdminRoleID, res, op) - } - ) - - return permissions.RuleSet{ - permissions.AllowRule(permissions.EveryoneRoleID, sys, "user.create"), - - allowAdm(sys, "access"), - allowAdm(sys, "grant"), - allowAdm(sys, "settings.read"), - allowAdm(sys, "settings.manage"), - allowAdm(sys, "organisation.create"), - allowAdm(sys, "application.create"), - allowAdm(sys, "user.create"), - allowAdm(sys, "role.create"), - allowAdm(sys, "automation-script.create"), - - allowAdm(organisations, "access"), - allowAdm(applications, "read"), - allowAdm(applications, "update"), - allowAdm(applications, "delete"), - - allowAdm(users, "read"), - allowAdm(users, "update"), - allowAdm(users, "suspend"), - allowAdm(users, "unsuspend"), - allowAdm(users, "delete"), - - allowAdm(roles, "read"), - allowAdm(roles, "update"), - allowAdm(roles, "delete"), - allowAdm(roles, "members.manage"), - - allowAdm(ascripts, "read"), - allowAdm(ascripts, "update"), - allowAdm(ascripts, "delete"), - } -} - func (svc accessControl) Whitelist() permissions.Whitelist { var wl = permissions.Whitelist{} diff --git a/tests/compose/automation_script_test.go b/tests/compose/automation_script_test.go index 719c61d59..746df2ab6 100644 --- a/tests/compose/automation_script_test.go +++ b/tests/compose/automation_script_test.go @@ -36,7 +36,7 @@ func TestAutomationScriptRead(t *testing.T) { h.allow(types.AutomationScriptPermissionResource.AppendWildcard(), "read") ns := h.repoMakeNamespace("some-namespace") - script := h.svcMakeAutomationScript(ns, "my little automation script") + script := h.svcMakeAutomationScript(ns, "myLittleAutomationScript"+rs()) h.apiInit(). Get(fmt.Sprintf("/namespace/%d/automation/script/%d", ns.ID, script.ID)). @@ -55,8 +55,8 @@ func TestAutomationScriptList(t *testing.T) { ns := h.repoMakeNamespace("some-namespace") - h.svcMakeAutomationScript(ns, "app") - h.svcMakeAutomationScript(ns, "app") + h.svcMakeAutomationScript(ns, "myLittleAutomationScript"+rs()) + h.svcMakeAutomationScript(ns, "myLittleAutomationScript"+rs()) h.apiInit(). Get(fmt.Sprintf("/namespace/%d/automation/script/", ns.ID)). @@ -73,7 +73,7 @@ func TestAutomationScriptCreateForbidden(t *testing.T) { h.apiInit(). Post(fmt.Sprintf("/namespace/%d/automation/script/", ns.ID)). - FormData("name", "my little automation script"). + FormData("name", "myLittleAutomationScript"+rs()). Expect(t). Status(http.StatusOK). Assert(helpers.AssertError("compose.service.NoCreatePermissions")). @@ -90,7 +90,7 @@ func TestAutomationScriptCreate(t *testing.T) { h.apiInit(). Post(fmt.Sprintf("/namespace/%d/automation/script/", ns.ID)). - FormData("name", "my little automation script"). + FormData("name", "myLittleAutomationScript"+rs()). Expect(t). Status(http.StatusOK). Assert(helpers.AssertNoErrors). @@ -103,7 +103,7 @@ func TestAutomationScriptUpdateForbidden(t *testing.T) { h.allow(types.NamespacePermissionResource.AppendWildcard(), "read") ns := h.repoMakeNamespace("some-namespace") - script := h.svcMakeAutomationScript(ns, "my little automation script") + script := h.svcMakeAutomationScript(ns, "myLittleAutomationScript"+rs()) h.apiInit(). Post(fmt.Sprintf("/namespace/%d/automation/script/%d", ns.ID, script.ID)). @@ -121,7 +121,7 @@ func TestAutomationScriptUpdate(t *testing.T) { h.allow(types.AutomationScriptPermissionResource.AppendWildcard(), "update") ns := h.repoMakeNamespace("some-namespace") - script := h.svcMakeAutomationScript(ns, "my little automation script") + script := h.svcMakeAutomationScript(ns, "myLittleAutomationScript"+rs()) h.apiInit(). Post(fmt.Sprintf("/namespace/%d/automation/script/%d", ns.ID, script.ID)). @@ -143,7 +143,7 @@ func TestAutomationScriptDeleteForbidden(t *testing.T) { h.allow(types.NamespacePermissionResource.AppendWildcard(), "read") ns := h.repoMakeNamespace("some-namespace") - script := h.svcMakeAutomationScript(ns, "my little automation script") + script := h.svcMakeAutomationScript(ns, "myLittleAutomationScript"+rs()) h.apiInit(). Delete(fmt.Sprintf("/namespace/%d/automation/script/%d", ns.ID, script.ID)). @@ -160,7 +160,7 @@ func TestAutomationScriptDelete(t *testing.T) { h.allow(types.AutomationScriptPermissionResource.AppendWildcard(), "delete") ns := h.repoMakeNamespace("some-namespace") - script := h.svcMakeAutomationScript(ns, "my little automation script") + script := h.svcMakeAutomationScript(ns, "myLittleAutomationScript"+rs()) h.apiInit(). Delete(fmt.Sprintf("/namespace/%d/automation/script/%d", ns.ID, script.ID)). diff --git a/tests/compose/automation_trigger_test.go b/tests/compose/automation_trigger_test.go index 3c269abf9..87fc014c5 100644 --- a/tests/compose/automation_trigger_test.go +++ b/tests/compose/automation_trigger_test.go @@ -112,9 +112,9 @@ func TestAutomationTriggerUpdateForbidden(t *testing.T) { h.allow(types.NamespacePermissionResource.AppendWildcard(), "read") h.allow(types.AutomationScriptPermissionResource.AppendWildcard(), "read") - ns := h.repoMakeNamespace("some-namespace") - script := h.svcMakeAutomationScript(ns, "dummy") - trigger := h.svcMakeAutomationTrigger(script, "my little automation trigger") + ns := h.repoMakeNamespace("some-namespace" + rs()) + script := h.svcMakeAutomationScript(ns, "dummy"+rs()) + trigger := h.svcMakeAutomationTrigger(script, "manual") h.apiInit(). Post(fmt.Sprintf("/namespace/%d/automation/script/%d/trigger/%d", ns.ID, script.ID, trigger.ID)). @@ -132,9 +132,9 @@ func TestAutomationTriggerUpdate(t *testing.T) { h.allow(types.AutomationScriptPermissionResource.AppendWildcard(), "read") h.allow(types.AutomationScriptPermissionResource.AppendWildcard(), "update") - ns := h.repoMakeNamespace("some-namespace") - script := h.svcMakeAutomationScript(ns, "dummy") - trigger := h.svcMakeAutomationTrigger(script, "my little automation trigger") + ns := h.repoMakeNamespace("some-namespace" + rs()) + script := h.svcMakeAutomationScript(ns, "dummy"+rs()) + trigger := h.svcMakeAutomationTrigger(script, "manual") h.apiInit(). Post(fmt.Sprintf("/namespace/%d/automation/script/%d/trigger/%d", ns.ID, script.ID, trigger.ID)). @@ -154,9 +154,9 @@ func TestAutomationTriggerDeleteForbidden(t *testing.T) { h.allow(types.NamespacePermissionResource.AppendWildcard(), "read") h.allow(types.AutomationScriptPermissionResource.AppendWildcard(), "read") - ns := h.repoMakeNamespace("some-namespace") - script := h.svcMakeAutomationScript(ns, "dummy") - trigger := h.svcMakeAutomationTrigger(script, "my little automation trigger") + ns := h.repoMakeNamespace("some-namespace" + rs()) + script := h.svcMakeAutomationScript(ns, "dummy"+rs()) + trigger := h.svcMakeAutomationTrigger(script, "manual") h.apiInit(). Delete(fmt.Sprintf("/namespace/%d/automation/script/%d/trigger/%d", ns.ID, script.ID, trigger.ID)). @@ -173,9 +173,9 @@ func TestAutomationTriggerDelete(t *testing.T) { h.allow(types.AutomationScriptPermissionResource.AppendWildcard(), "read") h.allow(types.AutomationScriptPermissionResource.AppendWildcard(), "update") - ns := h.repoMakeNamespace("some-namespace") - script := h.svcMakeAutomationScript(ns, "dummy") - trigger := h.svcMakeAutomationTrigger(script, "my little automation trigger") + ns := h.repoMakeNamespace("some-namespace" + rs()) + script := h.svcMakeAutomationScript(ns, "dummy"+rs()) + trigger := h.svcMakeAutomationTrigger(script, "manual") h.apiInit(). Delete(fmt.Sprintf("/namespace/%d/automation/script/%d/trigger/%d", ns.ID, script.ID, trigger.ID)). diff --git a/tests/compose/main_test.go b/tests/compose/main_test.go index 5c2e17ae8..337e63ba1 100644 --- a/tests/compose/main_test.go +++ b/tests/compose/main_test.go @@ -50,6 +50,16 @@ func db() *factory.DB { return factory.Database.MustGet("compose").With(context.Background()) } +// random string, 10 chars long by default +func rs(a ...int) string { + var l = 10 + if len(a) > 0 { + l = a[0] + } + + return string(rand.Bytes(l)) +} + func InitConfig() { var err error diff --git a/tests/compose/provision_test.go b/tests/compose/provision_test.go index 520e08e2e..f7efde223 100644 --- a/tests/compose/provision_test.go +++ b/tests/compose/provision_test.go @@ -1,54 +1,19 @@ package compose import ( - "os" "testing" - "gopkg.in/yaml.v2" - "github.com/cortezaproject/corteza-server/compose/importer" - "github.com/cortezaproject/corteza-server/compose/service" - "github.com/cortezaproject/corteza-server/compose/types" - "github.com/cortezaproject/corteza-server/internal/permissions" - sysTypes "github.com/cortezaproject/corteza-server/system/types" + "github.com/cortezaproject/corteza-server/internal/auth" + impAux "github.com/cortezaproject/corteza-server/pkg/importer" + provision "github.com/cortezaproject/corteza-server/provision/compose" ) func TestProvisioning(t *testing.T) { h := newHelper(t) + ctx := auth.SetSuperUserContext(h.secCtx()) - var ( - aux interface{} - - ctx = h.secCtx() - imp = importer.NewImporter( - service.DefaultNamespace.With(ctx), - service.DefaultModule.With(ctx), - service.DefaultChart.With(ctx), - service.DefaultPage.With(ctx), - service.DefaultInternalAutomationManager, - permissions.NewImporter(service.DefaultAccessControl.Whitelist()), - ) - ) - - h.allow(types.ComposePermissionResource, "grant") - - var f, err = os.Open("../../provision/compose/001_permission_rules.yaml") + readers, err := impAux.ReadStatic(provision.Asset) h.a.NoError(err) - h.a.NoError(yaml.NewDecoder(f).Decode(&aux)) - - h.a.NoError(imp.Cast(aux)) - - h.a.NoError(imp.Store( - ctx, - service.DefaultNamespace.With(ctx), - service.DefaultModule.With(ctx), - service.DefaultChart.With(ctx), - service.DefaultPage.With(ctx), - service.DefaultInternalAutomationManager, - service.DefaultAccessControl, - sysTypes.RoleSet{ - &sysTypes.Role{ID: 1, Handle: "everyone"}, - &sysTypes.Role{ID: 2, Handle: "admins"}, - }, - )) + h.a.NoError(importer.Import(ctx, nil, readers...)) } diff --git a/tests/messaging/provision_test.go b/tests/messaging/provision_test.go index 513ca77c5..48c05c121 100644 --- a/tests/messaging/provision_test.go +++ b/tests/messaging/provision_test.go @@ -1,46 +1,19 @@ package messaging import ( - "os" "testing" - "gopkg.in/yaml.v2" - - "github.com/cortezaproject/corteza-server/internal/permissions" + "github.com/cortezaproject/corteza-server/internal/auth" "github.com/cortezaproject/corteza-server/messaging/importer" - "github.com/cortezaproject/corteza-server/messaging/service" - "github.com/cortezaproject/corteza-server/messaging/types" - sysTypes "github.com/cortezaproject/corteza-server/system/types" + impAux "github.com/cortezaproject/corteza-server/pkg/importer" + provision "github.com/cortezaproject/corteza-server/provision/messaging" ) func TestProvisioning(t *testing.T) { h := newHelper(t) + ctx := auth.SetSuperUserContext(h.secCtx()) - var ( - aux interface{} - - roles = sysTypes.RoleSet{ - &sysTypes.Role{ID: permissions.EveryoneRoleID, Handle: "everyone"}, - &sysTypes.Role{ID: permissions.AdminRoleID, Handle: "admins"}, - } - - ctx = h.secCtx() - pi = permissions.NewImporter(service.DefaultAccessControl.Whitelist()) - imp = importer.NewImporter(pi, importer.NewChannelImport(pi, nil)) - ) - - h.allow(types.MessagingPermissionResource, "grant") - - var f, err = os.Open("../../provision/messaging/001_permission_rules.yaml") + readers, err := impAux.ReadStatic(provision.Asset) h.a.NoError(err) - h.a.NoError(yaml.NewDecoder(f).Decode(&aux)) - - h.a.NoError(imp.Cast(aux)) - - h.a.NoError(imp.Store( - ctx, - service.DefaultChannel.With(ctx), - service.DefaultAccessControl, - roles, - )) + h.a.NoError(importer.Import(ctx, readers...)) } diff --git a/tests/system/automation_script_test.go b/tests/system/automation_script_test.go index f39f26e0b..1210e997e 100644 --- a/tests/system/automation_script_test.go +++ b/tests/system/automation_script_test.go @@ -33,7 +33,7 @@ func TestAutomationScriptRead(t *testing.T) { h.allow(types.AutomationScriptPermissionResource.AppendWildcard(), "read") - script := h.svcMakeAutomationScript("my little automation script") + script := h.svcMakeAutomationScript("myLittleAutomationScript" + rs()) h.apiInit(). Get(fmt.Sprintf("/automation/script/%d", script.ID)). @@ -48,8 +48,8 @@ func TestAutomationScriptRead(t *testing.T) { func TestAutomationScriptList(t *testing.T) { h := newHelper(t) - h.svcMakeAutomationScript("app") - h.svcMakeAutomationScript("app") + h.svcMakeAutomationScript("myLittleAutomationScript" + rs()) + h.svcMakeAutomationScript("myLittleAutomationScript" + rs()) h.apiInit(). Get(fmt.Sprintf("/automation/script/")). @@ -66,7 +66,7 @@ func TestAutomationScriptCreateForbidden(t *testing.T) { h.apiInit(). Post(fmt.Sprintf("/automation/script/")). - FormData("name", "my little automation script"). + FormData("name", "myLittleAutomationScript"+rs()). Expect(t). Status(http.StatusOK). Assert(helpers.AssertError("system.service.NoCreatePermissions")). @@ -80,7 +80,7 @@ func TestAutomationScriptCreate(t *testing.T) { h.apiInit(). Post(fmt.Sprintf("/automation/script/")). - FormData("name", "my little automation script"). + FormData("name", "myLittleAutomationScript"+rs()). Expect(t). Status(http.StatusOK). Assert(helpers.AssertNoErrors). @@ -92,11 +92,11 @@ func TestAutomationScriptUpdateForbidden(t *testing.T) { h.allow(types.AutomationScriptPermissionResource.AppendWildcard(), "read") - script := h.svcMakeAutomationScript("my little automation script") + script := h.svcMakeAutomationScript("myLittleAutomationScript" + rs()) h.apiInit(). Post(fmt.Sprintf("/automation/script/%d", script.ID)). - FormData("name", "changed-name"). + FormData("name", "changed-name"+rs()). Expect(t). Status(http.StatusOK). Assert(helpers.AssertError("system.service.NoUpdatePermissions")). @@ -109,11 +109,11 @@ func TestAutomationScriptUpdate(t *testing.T) { h.allow(types.AutomationScriptPermissionResource.AppendWildcard(), "read") h.allow(types.AutomationScriptPermissionResource.AppendWildcard(), "update") - script := h.svcMakeAutomationScript("my little automation script") - + script := h.svcMakeAutomationScript("myLittleAutomationScript" + rs()) + newName := "changed-name" + rs() h.apiInit(). Post(fmt.Sprintf("/automation/script/%d", script.ID)). - FormData("name", "changed-name"). + FormData("name", newName). Expect(t). Status(http.StatusOK). Assert(helpers.AssertNoErrors). @@ -122,7 +122,7 @@ func TestAutomationScriptUpdate(t *testing.T) { script, err := service.DefaultInternalAutomationManager.FindScriptByID(context.Background(), script.ID) h.a.NoError(err) h.a.NotNil(script) - h.a.Equal("changed-name", script.Name) + h.a.Equal(newName, script.Name) } func TestAutomationScriptDeleteForbidden(t *testing.T) { @@ -130,7 +130,7 @@ func TestAutomationScriptDeleteForbidden(t *testing.T) { h.allow(types.AutomationScriptPermissionResource.AppendWildcard(), "read") - script := h.svcMakeAutomationScript("my little automation script") + script := h.svcMakeAutomationScript("myLittleAutomationScript" + rs()) h.apiInit(). Delete(fmt.Sprintf("/automation/script/%d", script.ID)). @@ -146,7 +146,7 @@ func TestAutomationScriptDelete(t *testing.T) { h.allow(types.AutomationScriptPermissionResource.AppendWildcard(), "read") h.allow(types.AutomationScriptPermissionResource.AppendWildcard(), "delete") - script := h.svcMakeAutomationScript("my little automation script") + script := h.svcMakeAutomationScript("myLittleAutomationScript" + rs()) h.apiInit(). Delete(fmt.Sprintf("/automation/script/%d", script.ID)). diff --git a/tests/system/automation_trigger_test.go b/tests/system/automation_trigger_test.go index f5ad25b99..e94cfa429 100644 --- a/tests/system/automation_trigger_test.go +++ b/tests/system/automation_trigger_test.go @@ -35,7 +35,7 @@ func TestAutomationTriggerRead(t *testing.T) { h.allow(types.AutomationScriptPermissionResource.AppendWildcard(), "read") - script := h.svcMakeAutomationScript("dummy") + script := h.svcMakeAutomationScript("dummy" + rs()) trigger := h.svcMakeAutomationTrigger(script, "manual") h.apiInit(). @@ -51,7 +51,7 @@ func TestAutomationTriggerRead(t *testing.T) { func TestAutomationTriggerList(t *testing.T) { h := newHelper(t) - script := h.svcMakeAutomationScript("dummy") + script := h.svcMakeAutomationScript("dummy" + rs()) h.svcMakeAutomationTrigger(script, "app") h.svcMakeAutomationTrigger(script, "app") @@ -69,7 +69,7 @@ func TestAutomationTriggerCreateForbidden(t *testing.T) { h.allow(types.AutomationScriptPermissionResource.AppendWildcard(), "read") - script := h.svcMakeAutomationScript("dummy") + script := h.svcMakeAutomationScript("dummy" + rs()) h.apiInit(). Post(fmt.Sprintf("/automation/script/%d/trigger/", script.ID)). @@ -86,7 +86,7 @@ func TestAutomationTriggerCreate(t *testing.T) { h.allow(types.AutomationScriptPermissionResource.AppendWildcard(), "read") h.allow(types.AutomationScriptPermissionResource.AppendWildcard(), "update") - script := h.svcMakeAutomationScript("dummy") + script := h.svcMakeAutomationScript("dummy" + rs()) h.apiInit(). Post(fmt.Sprintf("/automation/script/%d/trigger/", script.ID)). @@ -102,8 +102,8 @@ func TestAutomationTriggerUpdateForbidden(t *testing.T) { h.allow(types.AutomationScriptPermissionResource.AppendWildcard(), "read") - script := h.svcMakeAutomationScript("dummy") - trigger := h.svcMakeAutomationTrigger(script, "my little automation trigger") + script := h.svcMakeAutomationScript("dummy" + rs()) + trigger := h.svcMakeAutomationTrigger(script, "manual") h.apiInit(). Post(fmt.Sprintf("/automation/script/%d/trigger/%d", script.ID, trigger.ID)). @@ -120,8 +120,8 @@ func TestAutomationTriggerUpdate(t *testing.T) { h.allow(types.AutomationScriptPermissionResource.AppendWildcard(), "read") h.allow(types.AutomationScriptPermissionResource.AppendWildcard(), "update") - script := h.svcMakeAutomationScript("dummy") - trigger := h.svcMakeAutomationTrigger(script, "my little automation trigger") + script := h.svcMakeAutomationScript("dummy" + rs()) + trigger := h.svcMakeAutomationTrigger(script, "manual") h.apiInit(). Post(fmt.Sprintf("/automation/script/%d/trigger/%d", script.ID, trigger.ID)). @@ -140,8 +140,8 @@ func TestAutomationTriggerDeleteForbidden(t *testing.T) { h.allow(types.AutomationScriptPermissionResource.AppendWildcard(), "read") - script := h.svcMakeAutomationScript("dummy") - trigger := h.svcMakeAutomationTrigger(script, "my little automation trigger") + script := h.svcMakeAutomationScript("dummy" + rs()) + trigger := h.svcMakeAutomationTrigger(script, "manual") h.apiInit(). Delete(fmt.Sprintf("/automation/script/%d/trigger/%d", script.ID, trigger.ID)). @@ -157,8 +157,8 @@ func TestAutomationTriggerDelete(t *testing.T) { h.allow(types.AutomationScriptPermissionResource.AppendWildcard(), "read") h.allow(types.AutomationScriptPermissionResource.AppendWildcard(), "update") - script := h.svcMakeAutomationScript("dummy") - trigger := h.svcMakeAutomationTrigger(script, "my little automation trigger") + script := h.svcMakeAutomationScript("dummy" + rs()) + trigger := h.svcMakeAutomationTrigger(script, "manual") h.apiInit(). Delete(fmt.Sprintf("/automation/script/%d/trigger/%d", script.ID, trigger.ID)). diff --git a/tests/system/provision_test.go b/tests/system/provision_test.go index 7fd9ddbf3..e7f85d4f6 100644 --- a/tests/system/provision_test.go +++ b/tests/system/provision_test.go @@ -1,47 +1,19 @@ package system import ( - "os" "testing" - "gopkg.in/yaml.v2" - - "github.com/cortezaproject/corteza-server/internal/permissions" + "github.com/cortezaproject/corteza-server/internal/auth" + impAux "github.com/cortezaproject/corteza-server/pkg/importer" + provision "github.com/cortezaproject/corteza-server/provision/system" "github.com/cortezaproject/corteza-server/system/importer" - "github.com/cortezaproject/corteza-server/system/service" - "github.com/cortezaproject/corteza-server/system/types" ) func TestProvisioning(t *testing.T) { h := newHelper(t) + ctx := auth.SetSuperUserContext(h.secCtx()) - var ( - aux interface{} - - roles = types.RoleSet{ - &types.Role{ID: permissions.EveryoneRoleID, Handle: "everyone"}, - &types.Role{ID: permissions.AdminRoleID, Handle: "admins"}, - } - - ctx = h.secCtx() - pi = permissions.NewImporter(service.DefaultAccessControl.Whitelist()) - imp = importer.NewImporter(pi, importer.NewRoleImport(pi, roles)) - ) - - h.allow(types.SystemPermissionResource, "grant") - h.allow(types.SystemPermissionResource, "role.create") - h.allow(types.RolePermissionResource.AppendWildcard(), "update") - - var f, err = os.Open("../../provision/system/001_permission_rules.yaml") + readers, err := impAux.ReadStatic(provision.Asset) h.a.NoError(err) - h.a.NoError(yaml.NewDecoder(f).Decode(&aux)) - - h.a.NoError(imp.Cast(aux)) - - h.a.NoError(imp.Store( - ctx, - service.DefaultRole.With(ctx), - service.DefaultAccessControl, - roles, - )) + h.a.NoError(importer.Import(ctx, readers...)) } diff --git a/tests/system/role_test.go b/tests/system/role_test.go index 6ec6c4641..35802ad55 100644 --- a/tests/system/role_test.go +++ b/tests/system/role_test.go @@ -97,7 +97,7 @@ func TestRoleCreateNotUnique(t *testing.T) { h.apiInit(). Post("/roles/"). FormData("name", role.Name). - FormData("handle", rs()). + FormData("handle", "handle_"+rs()). Expect(t). Status(http.StatusOK). Assert(helpers.AssertError("system.service.RoleNameNotUnique")). @@ -112,7 +112,7 @@ func TestRoleCreate(t *testing.T) { h.apiInit(). Post("/roles/"). FormData("name", rs()). - FormData("handle", rs()). + FormData("handle", "handle_"+rs()). Expect(t). Status(http.StatusOK). Assert(helpers.AssertNoErrors).