Skip to content

Commit 63fe61e

Browse files
committed
unit tests
1 parent 0bd312e commit 63fe61e

File tree

6 files changed

+92
-3
lines changed

6 files changed

+92
-3
lines changed

internal/cac/storage/reader.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"path/filepath"
66

77
"github.com/cloudentity/cac/internal/cac/templates"
8+
"github.com/cloudentity/cac/internal/cac/utils"
89
ccyaml "github.com/goccy/go-yaml"
910
"github.com/pkg/errors"
1011
"golang.org/x/exp/slog"
@@ -44,7 +45,7 @@ func readFile(path string, opts ...ReadFileOpt) (map[string]any, error) {
4445
slog.Debug("read template", "path", path, "data", bts)
4546

4647

47-
if err = ccyaml.Unmarshal(bts, &out); err != nil {
48+
if out, err = utils.FromYaml(bts); err != nil {
4849
return out, errors.Wrapf(err, "failed to unmarshal template %s", path)
4950
}
5051

internal/cac/storage/writer.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ func createMultilineIncludeTemplate(str string, indent int) string {
170170

171171
var multilineTemplateRegexp = regexp.MustCompile(`⌘⌘(\d+) ([^⌘]+)⌘⌘`)
172172

173-
func postProcessMultilineTemplates(bts []byte) []byte {
173+
func postProcessMultilineTemplates(bts []byte) []byte {
174174
bts = multilineTemplateRegexp.ReplaceAll(bts, []byte("{{ $2 | nindent $1 }}"))
175175

176176
return bts

internal/cac/utils/decoder.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ func Decoder(result any) (*mapstructure.Decoder, error) {
2121

2222
func ConfigureDecoder(config *mapstructure.DecoderConfig) {
2323
config.TagName = "json"
24-
config.WeaklyTypedInput = true
24+
config.WeaklyTypedInput = false
2525
config.DecodeHook = mapstructure.ComposeDecodeHookFunc(urlDecoder(), timeDecoder(), stringToSlice())
2626
}
2727

internal/cac/utils/model_test.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,3 +81,44 @@ func TestFilterPatch(t *testing.T) {
8181
})
8282
}
8383
}
84+
85+
func TestMappingNumbersToModel(t *testing.T) {
86+
patch := models.Rfc7396PatchOperation{
87+
"policies": map[string]any{
88+
"p1": map[string]any{
89+
"validators": []any{
90+
map[string]any{
91+
"conf": map[string]any{
92+
"max_x": 3,
93+
},
94+
},
95+
},
96+
},
97+
},
98+
}
99+
100+
result, err := utils.FromPatchToModel[models.TreeServer](patch)
101+
102+
require.NoError(t, err)
103+
require.Equal(t, float64(3), result.Policies["p1"].Validators[0].Conf["max_x"])
104+
}
105+
106+
func TestMappingNumbersToYaml(t *testing.T) {
107+
model := models.TreeServer{
108+
Policies: models.TreePolicies{
109+
"p1": models.TreePolicy{
110+
Validators: []*models.ValidatorConfig{
111+
{
112+
Conf: map[string]any{
113+
"max_x": 3,
114+
},
115+
},
116+
},
117+
},
118+
},
119+
}
120+
patch, err := utils.FromModelToPatch(&model)
121+
122+
require.NoError(t, err)
123+
require.Equal(t, float64(3), patch["policies"].(map[string]any)["p1"].(map[string]any)["validators"].([]any)[0].(map[string]any)["conf"].(map[string]any)["max_x"])
124+
}

internal/cac/utils/yaml.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"github.com/go-json-experiment/json"
77
"github.com/go-json-experiment/json/jsontext"
88
ccyaml "github.com/goccy/go-yaml"
9+
"github.com/pkg/errors"
910
)
1011

1112
func ToYaml(it any) ([]byte, error) {
@@ -18,6 +19,7 @@ func ToYaml(it any) ([]byte, error) {
1819
enc := jsontext.NewEncoder(buffer,
1920
json.FormatNilMapAsNull(true),
2021
json.FormatNilSliceAsNull(true),
22+
json.StringifyNumbers(true),
2123
)
2224

2325
if err = json.MarshalEncode(enc, it); err != nil {
@@ -32,3 +34,16 @@ func ToYaml(it any) ([]byte, error) {
3234

3335
return bts, nil
3436
}
37+
38+
func FromYaml(bts []byte) (map[string]any, error) {
39+
var (
40+
out map[string]any
41+
err error
42+
)
43+
44+
if err = ccyaml.Unmarshal(bts, &out); err != nil {
45+
return out, errors.Wrapf(err, "failed to unmarshal template %s", string(bts))
46+
}
47+
48+
return out, nil
49+
}

internal/cac/utils/yaml_test.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package utils_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/cloudentity/cac/internal/cac/utils"
7+
"github.com/stretchr/testify/require"
8+
)
9+
10+
func TestDecodingNumbers(t *testing.T) {
11+
patchWithNumbers := []byte(`something:
12+
else: 3`)
13+
14+
yml, err := utils.FromYaml(patchWithNumbers)
15+
16+
require.NoError(t, err)
17+
something, ok := yml["something"].(map[string]any)
18+
require.True(t, ok)
19+
require.Equal(t, uint64(3), something["else"])
20+
}
21+
22+
func TestEncodingNumbers(t *testing.T) {
23+
yml, err := utils.ToYaml(map[string]any{
24+
"something": map[string]any{
25+
"else": 3,
26+
},
27+
})
28+
29+
require.NoError(t, err)
30+
require.YAMLEq(t, `something:
31+
else: 3`, string(yml))
32+
}

0 commit comments

Comments
 (0)