Skip to content

Commit e126f78

Browse files
committed
add unit test for marshal json
1 parent 360cb73 commit e126f78

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

coderd/idpsync/idpsync_test.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,58 @@ package idpsync_test
22

33
import (
44
"encoding/json"
5+
"regexp"
56
"testing"
67

78
"github.com/stretchr/testify/require"
89

910
"github.com/coder/coder/v2/coderd/idpsync"
1011
)
1112

13+
// TestMarshalJSONEmpty ensures no empty maps are marshaled as `null` in JSON.
14+
func TestMarshalJSONEmpty(t *testing.T) {
15+
t.Parallel()
16+
17+
t.Run("Group", func(t *testing.T) {
18+
t.Parallel()
19+
20+
output, err := json.Marshal(&idpsync.GroupSyncSettings{
21+
RegexFilter: regexp.MustCompile(".*"),
22+
})
23+
require.NoError(t, err, "marshal empty group settings")
24+
require.NotContains(t, string(output), "null")
25+
26+
require.JSONEq(t,
27+
`{"field":"","mapping":{},"regex_filter":".*","auto_create_missing_groups":false}`,
28+
string(output))
29+
})
30+
31+
t.Run("Role", func(t *testing.T) {
32+
t.Parallel()
33+
34+
output, err := json.Marshal(&idpsync.RoleSyncSettings{})
35+
require.NoError(t, err, "marshal empty group settings")
36+
require.NotContains(t, string(output), "null")
37+
38+
require.JSONEq(t,
39+
`{"field":"","mapping":{}}`,
40+
string(output))
41+
})
42+
43+
t.Run("Organization", func(t *testing.T) {
44+
t.Parallel()
45+
46+
output, err := json.Marshal(&idpsync.OrganizationSyncSettings{})
47+
require.NoError(t, err, "marshal empty group settings")
48+
require.NotContains(t, string(output), "null")
49+
50+
require.JSONEq(t,
51+
`{"field":"","mapping":{},"assign_default":false}`,
52+
string(output))
53+
})
54+
55+
}
56+
1257
func TestParseStringSliceClaim(t *testing.T) {
1358
t.Parallel()
1459

0 commit comments

Comments
 (0)