From ea9fc955b88dfad6779d760d1eab9402e4ab11bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Toma=C5=BE=20Jerman?= Date: Wed, 16 Oct 2019 16:24:51 +0200 Subject: [PATCH] Add tests for compose/settings --- tests/compose/settings_test.go | 128 +++++++++++++++++++++++++++++++++ 1 file changed, 128 insertions(+) create mode 100644 tests/compose/settings_test.go diff --git a/tests/compose/settings_test.go b/tests/compose/settings_test.go new file mode 100644 index 000000000..cb8419802 --- /dev/null +++ b/tests/compose/settings_test.go @@ -0,0 +1,128 @@ +package compose + +import ( + "net/http" + "testing" + + "github.com/cortezaproject/corteza-server/compose/service" + tt "github.com/cortezaproject/corteza-server/compose/types" + "github.com/cortezaproject/corteza-server/pkg/settings" + "github.com/cortezaproject/corteza-server/tests/helpers" + "github.com/jmoiron/sqlx/types" + jsonpath "github.com/steinfletcher/apitest-jsonpath" +) + +func TestSettingsList(t *testing.T) { + h := newHelper(t) + h.allow(tt.ComposePermissionResource, "settings.read") + h.allow(tt.ComposePermissionResource, "settings.manage") + + err := service.DefaultSettings.With(h.secCtx()).BulkSet(settings.ValueSet{ + &settings.Value{Name: "t_cmp_k1.s1", Value: types.JSONText(`"t_cmp_v1"`)}, + &settings.Value{Name: "t_cmp_k1.s2", Value: types.JSONText(`"t_cmp_v2"`)}, + }) + h.a.NoError(err) + + h.apiInit(). + Get("/settings/"). + Expect(t). + Status(http.StatusOK). + Assert(helpers.AssertNoErrors). + Assert(jsonpath.Present(`$.response[? @.name=="t_cmp_k1.s1"]`)). + Assert(jsonpath.Present(`$.response[? @.value=="t_cmp_v1"]`)). + Assert(jsonpath.Present(`$.response[? @.name=="t_cmp_k1.s2"]`)). + Assert(jsonpath.Present(`$.response[? @.value=="t_cmp_v2"]`)). + End() +} + +func TestSettingsList_noPermissions(t *testing.T) { + h := newHelper(t) + h.deny(tt.ComposePermissionResource, "settings.read") + + h.apiInit(). + Get("/settings/"). + Expect(t). + Status(http.StatusOK). + Assert(helpers.AssertError("not allowed to read settings")). + End() +} + +func TestSettingsUpdate(t *testing.T) { + h := newHelper(t) + h.allow(tt.ComposePermissionResource, "settings.manage") + h.allow(tt.ComposePermissionResource, "settings.read") + + err := service.DefaultSettings.With(h.secCtx()).BulkSet(settings.ValueSet{ + &settings.Value{Name: "t_cmp_k1.s1", Value: types.JSONText(`"t_cmp_v1"`)}, + }) + h.a.NoError(err) + + h.apiInit(). + Patch("/settings/"). + JSON(`{"values":[{"name":"t_cmp_k1.s1","value":"t_cmp_v1_edited"},{"name":"t_cmp_k2.s1","value":"t_cmp_v2_new"}]}`). + Expect(t). + Status(http.StatusOK). + Assert(helpers.AssertNoErrors). + End() + + s, err := service.DefaultSettings.With(h.secCtx()).Get("t_cmp_k1.s1", 0) + h.a.NoError(err) + h.a.Equal(`"t_cmp_v1_edited"`, s.Value.String(), "existing key should be updated") + + s, err = service.DefaultSettings.With(h.secCtx()).Get("t_cmp_k2.s1", 0) + h.a.NoError(err) + h.a.Equal(`"t_cmp_v2_new"`, s.Value.String(), "new key should be added") +} + +func TestSettingsUpdate_noPermissions(t *testing.T) { + h := newHelper(t) + h.deny(tt.ComposePermissionResource, "settings.manage") + + h.apiInit(). + Patch("/settings/"). + JSON(`{"values":[]}`). + Expect(t). + Status(http.StatusOK). + Assert(helpers.AssertError("not allowed to manage settings")). + End() +} + +func TestSettingsGet(t *testing.T) { + h := newHelper(t) + h.allow(tt.ComposePermissionResource, "settings.read") + h.allow(tt.ComposePermissionResource, "settings.manage") + + err := service.DefaultSettings.With(h.secCtx()).BulkSet(settings.ValueSet{ + &settings.Value{Name: "t_cmp_k1.s1", Value: types.JSONText(`"t_cmp_v1"`)}, + }) + h.a.NoError(err) + + h.apiInit(). + Get("/settings/t_cmp_k1.s1"). + Expect(t). + Status(http.StatusOK). + Assert(helpers.AssertNoErrors). + Assert(jsonpath.Present(`$.response.name=="t_cmp_k1.s1"`)). + Assert(jsonpath.Present(`$.response.value=="t_cmp_v1"`)). + End() + + h.apiInit(). + Get("/settings/t_cmp_k1.missing"). + Expect(t). + Status(http.StatusOK). + Assert(helpers.AssertNoErrors). + Assert(jsonpath.Equal(`$.response`, nil)). + End() +} + +func TestSettingsGet_noPermissions(t *testing.T) { + h := newHelper(t) + h.deny(tt.ComposePermissionResource, "settings.read") + + h.apiInit(). + Get("/settings/t_cmp_k1.s1"). + Expect(t). + Status(http.StatusOK). + Assert(helpers.AssertError("not allowed to read settings")). + End() +}