-
Notifications
You must be signed in to change notification settings - Fork 57
/
filter_test.go
174 lines (151 loc) · 4.52 KB
/
filter_test.go
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package scim_test
import (
"encoding/json"
"io"
"net/http"
"net/http/httptest"
"net/url"
"testing"
"github.com/elimity-com/scim"
"github.com/elimity-com/scim/optional"
"github.com/elimity-com/scim/schema"
)
func Test_Group_Filter(t *testing.T) {
s := newTestServerForFilter(t)
tests := []struct {
name string
filter string
expectedDisplayName string
}{
{name: "Happy path", filter: "displayName eq \"testGroup\"", expectedDisplayName: "testGroup"},
{name: "Happy path with plus sign", filter: "displayName eq \"testGroup+test\"", expectedDisplayName: "testGroup+test"},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
r := httptest.NewRequest(http.MethodGet, "/Groups?filter="+url.QueryEscape(tt.filter), nil)
w := httptest.NewRecorder()
s.ServeHTTP(w, r)
bytes, err := io.ReadAll(w.Result().Body)
if err != nil {
t.Fatal(err)
}
if w.Result().StatusCode != http.StatusOK {
t.Fatal(w.Result().StatusCode, string(bytes))
}
var result map[string]interface{}
if err := json.Unmarshal(bytes, &result); err != nil {
t.Fatal(err)
}
resources, ok := result["Resources"].([]interface{})
if !ok {
t.Fatal("Resources is not the right type or missing")
}
if len(resources) != 1 {
t.Fatal("one Resource expected")
}
firstResource, ok := resources[0].(map[string]interface{})
if !ok {
t.Fatal("first Resource is not the right type or missing")
}
displayName, ok := firstResource["displayName"].(string)
if !ok {
t.Fatal("displayName is not the right type or missing")
}
if displayName != tt.expectedDisplayName {
t.Fatal("displayName not eq " + displayName)
}
})
}
}
func Test_User_Filter(t *testing.T) {
s := newTestServerForFilter(t)
tests := []struct {
name string
filter string
expectedUserName string
}{
{name: "Happy path", filter: "userName eq \"testUser\"", expectedUserName: "testUser"},
{name: "Happy path with plus sign", filter: "userName eq \"testUser+test\"", expectedUserName: "testUser+test"},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
r := httptest.NewRequest(http.MethodGet, "/Users?filter="+url.QueryEscape(tt.filter), nil)
w := httptest.NewRecorder()
s.ServeHTTP(w, r)
bytes, err := io.ReadAll(w.Result().Body)
if err != nil {
t.Fatal(err)
}
if w.Result().StatusCode != http.StatusOK {
t.Fatal(w.Result().StatusCode, string(bytes))
}
var result map[string]interface{}
if err := json.Unmarshal(bytes, &result); err != nil {
t.Fatal(err)
}
resources, ok := result["Resources"].([]interface{})
if !ok {
t.Fatal("Resources is not the right type or missing")
}
if len(resources) != 1 {
t.Fatal("one Resource expected")
}
firstResource, ok := resources[0].(map[string]interface{})
if !ok {
t.Fatal("first Resource is not the right type or missing")
}
userName, ok := firstResource["userName"].(string)
if !ok {
t.Fatal("userName is not the right type or missing")
}
if userName != tt.expectedUserName {
t.Fatal("userName not eq " + userName)
}
})
}
}
// newTestServerForFilter creates a new test server with a User and Group resource type
// or fails the test if an error occurs.
func newTestServerForFilter(t *testing.T) scim.Server {
s, err := scim.NewServer(
&scim.ServerArgs{
ServiceProviderConfig: &scim.ServiceProviderConfig{},
ResourceTypes: []scim.ResourceType{
{
ID: optional.NewString("User"),
Name: "User",
Endpoint: "/Users",
Description: optional.NewString("User Account"),
Schema: schema.CoreUserSchema(),
Handler: &testResourceHandler{
data: map[string]testData{
"0001": {attributes: map[string]interface{}{"userName": "testUser"}},
"0002": {attributes: map[string]interface{}{"userName": "testUser+test"}},
},
schema: schema.CoreUserSchema(),
},
},
{
ID: optional.NewString("Group"),
Name: "Group",
Endpoint: "/Groups",
Description: optional.NewString("Group"),
Schema: schema.CoreGroupSchema(),
Handler: &testResourceHandler{
data: map[string]testData{
"0001": {attributes: map[string]interface{}{"displayName": "testGroup"}},
"0002": {attributes: map[string]interface{}{"displayName": "testGroup+test"}},
},
schema: schema.CoreGroupSchema(),
},
},
},
},
)
if err != nil {
t.Fatal(err)
}
return s
}