Skip to content

Commit

Permalink
Fix default record module field value validation setup
Browse files Browse the repository at this point in the history
  • Loading branch information
tjerman committed Nov 15, 2021
1 parent 5974849 commit aced989
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 2 deletions.
2 changes: 1 addition & 1 deletion compose/service/module.go
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion compose/service/record.go
Expand Up @@ -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()

Expand Down
46 changes: 46 additions & 0 deletions tests/compose/module_test.go
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"net/http"
"net/url"
"strconv"
"testing"
"time"

Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit aced989

Please sign in to comment.