forked from coder/coder
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathidpsync_test.go
More file actions
158 lines (144 loc) · 3.34 KB
/
idpsync_test.go
File metadata and controls
158 lines (144 loc) · 3.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package idpsync_test
import (
"encoding/json"
"testing"
"github.com/stretchr/testify/require"
"github.com/coder/coder/v2/coderd/idpsync"
)
func TestParseStringSliceClaim(t *testing.T) {
t.Parallel()
cases := []struct {
Name string
GoClaim interface{}
// JSON Claim allows testing the json -> go conversion
// of some strings.
JSONClaim string
ErrorExpected bool
ExpectedSlice []string
}{
{
Name: "Nil",
GoClaim: nil,
ExpectedSlice: []string{},
},
// Go Slices
{
Name: "EmptySlice",
GoClaim: []string{},
ExpectedSlice: []string{},
},
{
Name: "StringSlice",
GoClaim: []string{"a", "b", "c"},
ExpectedSlice: []string{"a", "b", "c"},
},
{
Name: "InterfaceSlice",
GoClaim: []interface{}{"a", "b", "c"},
ExpectedSlice: []string{"a", "b", "c"},
},
{
Name: "MixedSlice",
GoClaim: []interface{}{"a", string("b"), interface{}("c")},
ExpectedSlice: []string{"a", "b", "c"},
},
{
Name: "StringSliceOneElement",
GoClaim: []string{"a"},
ExpectedSlice: []string{"a"},
},
// Json Slices
{
Name: "JSONEmptySlice",
JSONClaim: `[]`,
ExpectedSlice: []string{},
},
{
Name: "JSONStringSlice",
JSONClaim: `["a", "b", "c"]`,
ExpectedSlice: []string{"a", "b", "c"},
},
{
Name: "JSONStringSliceOneElement",
JSONClaim: `["a"]`,
ExpectedSlice: []string{"a"},
},
// Go string
{
Name: "String",
GoClaim: "a",
ExpectedSlice: []string{"a"},
},
{
Name: "EmptyString",
GoClaim: "",
ExpectedSlice: []string{},
},
{
Name: "Interface",
GoClaim: interface{}("a"),
ExpectedSlice: []string{"a"},
},
// JSON string
{
Name: "JSONString",
JSONClaim: `"a"`,
ExpectedSlice: []string{"a"},
},
{
Name: "JSONEmptyString",
JSONClaim: `""`,
ExpectedSlice: []string{},
},
// Go Errors
{
Name: "IntegerInSlice",
GoClaim: []interface{}{"a", "b", 1},
ErrorExpected: true,
},
// Json Errors
{
Name: "JSONIntegerInSlice",
JSONClaim: `["a", "b", 1]`,
ErrorExpected: true,
},
{
Name: "JSON_CSV",
JSONClaim: `"a,b,c"`,
ErrorExpected: true,
},
}
for _, c := range cases {
c := c
t.Run(c.Name, func(t *testing.T) {
t.Parallel()
if len(c.JSONClaim) > 0 {
require.Nil(t, c.GoClaim, "go claim should be nil if json set")
err := json.Unmarshal([]byte(c.JSONClaim), &c.GoClaim)
require.NoError(t, err, "unmarshal json claim")
}
found, err := idpsync.ParseStringSliceClaim(c.GoClaim)
if c.ErrorExpected {
require.Error(t, err)
} else {
require.NoError(t, err)
require.ElementsMatch(t, c.ExpectedSlice, found, "expected groups")
}
})
}
}
func TestParseStringSliceClaimReference(t *testing.T) {
t.Parallel()
var val any = []string{"a", "b", "c"}
parsed, err := idpsync.ParseStringSliceClaim(val)
require.NoError(t, err)
parsed[0] = ""
require.Equal(t, "a", val.([]string)[0], "should not modify original value")
}
func TestIsHTTPError(t *testing.T) {
t.Parallel()
herr := idpsync.HTTPError{}
require.NotNil(t, idpsync.IsHTTPError(herr))
require.NotNil(t, idpsync.IsHTTPError(&herr))
require.Nil(t, error(nil))
}