-
Notifications
You must be signed in to change notification settings - Fork 56
/
server_test.go
388 lines (358 loc) · 9.02 KB
/
server_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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
package scim_test
import (
"fmt"
"io"
"net/http"
"testing"
"time"
internal "github.com/elimity-com/scim/filter"
"github.com/elimity-com/scim"
"github.com/elimity-com/scim/errors"
"github.com/elimity-com/scim/optional"
"github.com/elimity-com/scim/schema"
"github.com/scim2/filter-parser/v2"
)
func checkBodyNotEmpty(r *http.Request) error {
// Check whether the request body is empty.
data, err := io.ReadAll(r.Body)
if err != nil {
return err
}
if len(data) == 0 {
return fmt.Errorf("passed body is empty")
}
return nil
}
// externalID extracts the external identifier of the given attributes.
func externalID(attributes scim.ResourceAttributes) optional.String {
if eID, ok := attributes["externalId"]; ok {
if externalID, ok := eID.(string); ok {
return optional.NewString(externalID)
}
}
return optional.String{}
}
// Some things that are not checked:
// - Whether a reference to another entity really exists.
// e.g. if a member gets added, does this entity exist?
func newTestServer(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{}{}},
},
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{}{}},
},
schema: schema.CoreGroupSchema(),
},
},
},
},
)
if err != nil {
t.Fatal(err)
}
return s
}
// testData represents a resource entity.
type testData struct {
attributes scim.ResourceAttributes
meta scim.Meta
}
// testResourceHandler is a simple in-memory resource 'database'.
// This is for test/example purposes only! Do NOT use in production.
type testResourceHandler struct {
// nextID is the id of the next resource that gets created.
nextID int
// data is a map[id]resource in which the resources are stored.
data map[string]testData
// schema is the reference schema of the resource handler.
schema schema.Schema
}
func (h *testResourceHandler) Create(r *http.Request, attributes scim.ResourceAttributes) (scim.Resource, error) {
if err := checkBodyNotEmpty(r); err != nil {
return scim.Resource{}, err
}
var (
id = h.createID()
now = time.Now()
meta = scim.Meta{
Created: &now,
LastModified: &now,
Version: fmt.Sprintf("v%d", now.Unix()),
}
)
h.data[id] = testData{
attributes: attributes,
meta: meta,
}
return scim.Resource{
ID: id,
ExternalID: externalID(attributes),
Attributes: attributes,
Meta: meta,
}, nil
}
func (h *testResourceHandler) Delete(r *http.Request, id string) error {
if _, ok := h.data[id]; !ok {
return errors.ScimErrorResourceNotFound(id)
}
delete(h.data, id)
return nil
}
func (h testResourceHandler) Get(r *http.Request, id string) (scim.Resource, error) {
resource, ok := h.data[id]
if !ok {
return scim.Resource{}, errors.ScimErrorResourceNotFound(id)
}
return scim.Resource{
ID: id,
ExternalID: externalID(resource.attributes),
Attributes: resource.attributes,
Meta: resource.meta,
}, nil
}
func (h testResourceHandler) GetAll(r *http.Request, params scim.ListRequestParams) (scim.Page, error) {
if params.Count == 0 {
return scim.Page{
TotalResults: len(h.data),
}, nil
}
var (
resources []scim.Resource
index int
)
for k, v := range h.data {
index++ // 1-indexed
if index < params.StartIndex {
continue
}
if len(resources) == params.Count {
break
}
validator := internal.NewFilterValidator(params.FilterValidator.GetFilter(), h.schema)
if err := validator.PassesFilter(v.attributes); err != nil {
continue
}
resources = append(resources, scim.Resource{
ID: k,
ExternalID: externalID(v.attributes),
Attributes: v.attributes,
Meta: v.meta,
})
}
return scim.Page{
TotalResults: len(h.data),
Resources: resources,
}, nil
}
func (h *testResourceHandler) Patch(r *http.Request, id string, operations []scim.PatchOperation) (scim.Resource, error) {
if err := checkBodyNotEmpty(r); err != nil {
return scim.Resource{}, err
}
if _, ok := h.data[id]; !ok {
return scim.Resource{}, errors.ScimErrorResourceNotFound(id)
}
var changed bool // Whether or not changes where made
for _, op := range operations {
// Target is the root node.
if op.Path == nil {
for k, v := range op.Value.(map[string]interface{}) {
if v == nil {
continue
}
path, _ := filter.ParseAttrPath([]byte(k))
if subAttrName := path.SubAttributeName(); subAttrName != "" {
if old, ok := h.data[id].attributes[path.AttributeName]; ok {
m := old.(map[string]interface{})
if sub, ok := m[subAttrName]; ok {
if sub == v {
continue
}
}
changed = true
m[subAttrName] = v
h.data[id].attributes[path.AttributeName] = m
continue
}
changed = true
h.data[id].attributes[path.AttributeName] = map[string]interface{}{
subAttrName: v,
}
continue
}
old, ok := h.data[id].attributes[k]
if !ok {
changed = true
h.data[id].attributes[k] = v
continue
}
switch v := v.(type) {
case []interface{}:
changed = true
h.data[id].attributes[k] = append(old.([]interface{}), v...)
case map[string]interface{}:
m := old.(map[string]interface{})
var changed_ bool
for attr, value := range v {
if value == nil {
continue
}
if v, ok := m[attr]; ok {
if v == nil || v == value {
continue
}
}
changed = true
changed_ = true
m[attr] = value
}
if changed_ {
h.data[id].attributes[k] = m
}
default:
if old == v {
continue
}
changed = true
h.data[id].attributes[k] = v // replace
}
}
continue
}
var (
attrName = op.Path.AttributePath.AttributeName
subAttrName = op.Path.AttributePath.SubAttributeName()
valueExpr = op.Path.ValueExpression
)
// Attribute does not exist yet.
old, ok := h.data[id].attributes[attrName]
if !ok {
switch {
case subAttrName != "":
changed = true
h.data[id].attributes[attrName] = map[string]interface{}{
subAttrName: op.Value,
}
case valueExpr != nil:
// Do nothing since there is nothing to match the filter?
default:
changed = true
h.data[id].attributes[attrName] = op.Value
}
continue
}
switch op.Op {
case "add":
switch v := op.Value.(type) {
case []interface{}:
changed = true
h.data[id].attributes[attrName] = append(old.([]interface{}), v...)
default:
if subAttrName != "" {
m := old.(map[string]interface{})
if value, ok := old.(map[string]interface{})[subAttrName]; ok {
if v == value {
continue
}
}
changed = true
m[subAttrName] = v
h.data[id].attributes[attrName] = m
continue
}
switch v := v.(type) {
case map[string]interface{}:
m := old.(map[string]interface{})
var changed_ bool
for attr, value := range v {
if value == nil {
continue
}
if v, ok := m[attr]; ok {
if v == nil || v == value {
continue
}
}
changed = true
changed_ = true
m[attr] = value
}
if changed_ {
h.data[id].attributes[attrName] = m
}
default:
if old == v {
continue
}
changed = true
h.data[id].attributes[attrName] = v // replace
}
}
}
}
if !changed {
// StatusNoContent
return scim.Resource{}, nil
}
resource := h.data[id]
return scim.Resource{
ID: id,
ExternalID: externalID(resource.attributes),
Attributes: resource.attributes,
Meta: resource.meta,
}, nil
}
func (h *testResourceHandler) Replace(r *http.Request, id string, attributes scim.ResourceAttributes) (scim.Resource, error) {
if err := checkBodyNotEmpty(r); err != nil {
return scim.Resource{}, err
}
resource, ok := h.data[id]
if !ok {
return scim.Resource{}, errors.ScimErrorResourceNotFound(id)
}
var (
now = time.Now()
meta = scim.Meta{
Created: resource.meta.Created,
LastModified: &now,
Version: fmt.Sprintf("v%d", now.Unix()),
}
)
h.data[id] = testData{
attributes: attributes,
meta: meta,
}
return scim.Resource{
ID: id,
ExternalID: externalID(attributes),
Attributes: attributes,
Meta: meta,
}, nil
}
// createID returns a unique identifier for a resource.
func (h *testResourceHandler) createID() string {
id := fmt.Sprintf("%04d", h.nextID)
h.nextID++
return id
}