From aced989ae6a87728e0573b32b69fc1b65b1ab90b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Toma=C5=BE=20Jerman?= Date: Mon, 15 Nov 2021 12:45:24 +0100 Subject: [PATCH] Fix default record module field value validation setup --- compose/service/module.go | 2 +- compose/service/record.go | 2 +- tests/compose/module_test.go | 46 ++++++++++++++++++++++++++++++++++++ 3 files changed, 48 insertions(+), 2 deletions(-) diff --git a/compose/service/module.go b/compose/service/module.go index 8337d70b9..3539d8bb6 100644 --- a/compose/service/module.go +++ b/compose/service/module.go @@ -729,7 +729,7 @@ func moduleFieldDefaultPreparer(ctx context.Context, s store.Storer, m *types.Mo Values: vv, } - rve := defaultValidator(nil).Run(ctx, s, auxm, r) + rve := defaultValidator(DefaultRecord).Run(ctx, s, auxm, r) if !rve.IsValid() { return nil, rve } diff --git a/compose/service/record.go b/compose/service/record.go index 159d9ae8c..2d2c0de81 100644 --- a/compose/service/record.go +++ b/compose/service/record.go @@ -163,7 +163,7 @@ func Record() RecordService { return svc } -func defaultValidator(svc *record) recordValuesValidator { +func defaultValidator(svc RecordService) recordValuesValidator { // Initialize validator and setup all checkers it needs validator := values.Validator() diff --git a/tests/compose/module_test.go b/tests/compose/module_test.go index a68d9a350..22422257f 100644 --- a/tests/compose/module_test.go +++ b/tests/compose/module_test.go @@ -5,6 +5,7 @@ import ( "fmt" "net/http" "net/url" + "strconv" "testing" "time" @@ -464,6 +465,51 @@ func TestModuleFieldsDefaultValue(t *testing.T) { h.a.Len(m.Fields[0].DefaultValue, 1) h.a.Equal("1", m.Fields[0].DefaultValue[0].Value) }) + + t.Run("record; valid", func(t *testing.T) { + prep() + + // Prep the related module + refM := h.makeModule(ns, "ref-mod", &types.ModuleField{ + ID: id.Next(), + Kind: "String", + Name: "string", + }) + rec := h.makeRecord(refM, &types.RecordValue{ + Name: "string", + Value: "val", + }) + + // Prep the module and a field + m := h.makeModule(ns, "some-module", &types.ModuleField{ + ID: id.Next(), + Kind: "String", + Name: "string", + }) + + // RBAC (make sure to allow record read) + helpers.AllowMe(h, types.ModuleRbacResource(0, 0), "update") + helpers.AllowMe(h, types.RecordRbacResource(0, 0, 0), "read") + + f := m.Fields[0] + fjs := fmt.Sprintf(`{ "name": "%s", "fields": [{ "fieldID": "%d", "name": "record", "kind": "Record", "options": {"moduleID": "%d"}, "defaultValue": [{"value": "%d"}] }] }`, m.Name, f.ID, refM.ID, rec.ID) + h.apiInit(). + Post(fmt.Sprintf("/namespace/%d/module/%d", ns.ID, m.ID)). + JSON(fjs). + Expect(t). + Status(http.StatusOK). + Assert(helpers.AssertNoErrors). + End() + + m = h.lookupModuleByID(m.ID) + h.a.NotNil(m) + h.a.NotNil(m.Fields) + h.a.Len(m.Fields, 1) + + h.a.NotNil(m.Fields[0].DefaultValue) + h.a.Len(m.Fields[0].DefaultValue, 1) + h.a.Equal(strconv.FormatUint(rec.ID, 10), m.Fields[0].DefaultValue[0].Value) + }) } func TestModuleFieldsUpdate_removed(t *testing.T) {