-
Notifications
You must be signed in to change notification settings - Fork 68
Expand file tree
/
Copy pathresource_handler_test.go
More file actions
328 lines (282 loc) · 7.89 KB
/
resource_handler_test.go
File metadata and controls
328 lines (282 loc) · 7.89 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
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
package scim
import (
"fmt"
"math/rand"
"net/http"
"strings"
"testing"
"time"
"github.com/elimity-com/scim/errors"
"github.com/elimity-com/scim/optional"
"github.com/elimity-com/scim/schema"
)
func ExampleResourceHandler() {
var r interface{} = testResourceHandler{}
_, ok := r.(ResourceHandler)
fmt.Println(ok)
// Output: true
}
func TestValidateFilterForResourceTypes(t *testing.T) {
userSchema := getUserSchema()
groupSchema := schema.CoreGroupSchema()
resourceTypes := []ResourceType{
{
Name: "User",
Endpoint: "/Users",
Schema: userSchema,
},
{
Name: "Group",
Endpoint: "/Groups",
Schema: groupSchema,
},
}
t.Run("filter matching only User", func(t *testing.T) {
results := ValidateFilterForResourceTypes(`userName eq "john"`, resourceTypes)
assertLen(t, results, 1)
assertEqual(t, "User", results[0].ResourceType.Name)
})
t.Run("filter matching only Group", func(t *testing.T) {
results := ValidateFilterForResourceTypes(`members.value eq "123"`, resourceTypes)
assertLen(t, results, 1)
assertEqual(t, "Group", results[0].ResourceType.Name)
})
t.Run("filter matching both", func(t *testing.T) {
results := ValidateFilterForResourceTypes(`displayName eq "test"`, resourceTypes)
assertLen(t, results, 2)
})
t.Run("meta.resourceType filter matches all", func(t *testing.T) {
results := ValidateFilterForResourceTypes(`meta.resourceType eq "User"`, resourceTypes)
assertLen(t, results, 2)
})
t.Run("unparseable filter", func(t *testing.T) {
results := ValidateFilterForResourceTypes(`not a valid ((( filter`, resourceTypes)
assertLen(t, results, 0)
})
t.Run("does not mutate original schema attributes", func(t *testing.T) {
// Create a schema with spare capacity so append can mutate the backing array.
commonAttrs := schema.CommonAttributes()
attrs := make([]schema.CoreAttribute, 1, 1+len(commonAttrs))
attrs[0] = schema.SimpleCoreAttribute(schema.SimpleStringParams(schema.StringParams{
Name: "userName",
}))
// Extend into spare capacity to observe backing array writes.
full := attrs[:cap(attrs)]
rt := []ResourceType{
{
Name: "User",
Endpoint: "/Users",
Schema: schema.Schema{
ID: "urn:ietf:params:scim:schemas:core:2.0:User",
Attributes: attrs,
},
},
}
ValidateFilterForResourceTypes(`userName eq "john"`, rt)
// If append mutated the backing array, full[1] now holds a common attribute.
assertEqual(t, "", full[1].Name())
})
}
type testData struct {
resourceAttributes ResourceAttributes
meta map[string]string
}
// simple in-memory resource database.
type testResourceHandler struct {
data map[string]testData
}
func (h testResourceHandler) Create(r *http.Request, attributes ResourceAttributes) (Resource, error) {
// create unique identifier
rng := rand.New(rand.NewSource(time.Now().UnixNano()))
id := fmt.Sprintf("%04d", rng.Intn(9999))
// store resource
h.data[id] = testData{
resourceAttributes: attributes,
}
now := time.Now()
// return stored resource
return Resource{
ID: id,
ExternalID: h.externalID(attributes),
Attributes: attributes,
Meta: Meta{
Created: &now,
LastModified: &now,
Version: fmt.Sprintf("v%s", id),
},
}, nil
}
func (h testResourceHandler) Delete(r *http.Request, id string) error {
// check if resource exists
_, ok := h.data[id]
if !ok {
return errors.ScimErrorResourceNotFound(id)
}
// delete resource
delete(h.data, id)
return nil
}
func (h testResourceHandler) Get(r *http.Request, id string) (Resource, error) {
// check if resource exists
data, ok := h.data[id]
if !ok {
return Resource{}, errors.ScimErrorResourceNotFound(id)
}
created, _ := time.ParseInLocation(time.RFC3339, fmt.Sprintf("%v", data.meta["created"]), time.UTC)
lastModified, _ := time.Parse(time.RFC3339, fmt.Sprintf("%v", data.meta["lastModified"]))
// return resource with given identifier
return Resource{
ID: id,
ExternalID: h.externalID(data.resourceAttributes),
Attributes: data.resourceAttributes,
Meta: Meta{
Created: &created,
LastModified: &lastModified,
Version: fmt.Sprintf("%v", data.meta["version"]),
},
}, nil
}
func (h testResourceHandler) GetAll(r *http.Request, params ListRequestParams) (Page, error) {
if params.Count == 0 {
return Page{
TotalResults: len(h.data),
}, nil
}
resources := make([]Resource, 0)
i := 1
for k, v := range h.data {
if i > (params.StartIndex + params.Count - 1) {
break
}
if i >= params.StartIndex {
resources = append(resources, Resource{
ID: k,
ExternalID: h.externalID(v.resourceAttributes),
Attributes: v.resourceAttributes,
})
}
i++
}
return Page{
TotalResults: len(h.data),
Resources: resources,
}, nil
}
func (h testResourceHandler) Patch(r *http.Request, id string, operations []PatchOperation) (Resource, error) {
if h.shouldReturnNoContent(id, operations) {
return Resource{}, nil
}
for _, op := range operations {
switch op.Op {
case PatchOperationAdd:
if op.Path != nil {
h.data[id].resourceAttributes[op.Path.String()] = op.Value
} else {
valueMap := op.Value.(map[string]interface{})
for k, v := range valueMap {
if arr, ok := h.data[id].resourceAttributes[k].([]interface{}); ok {
arr = append(arr, v)
h.data[id].resourceAttributes[k] = arr
} else {
h.data[id].resourceAttributes[k] = v
}
}
}
case PatchOperationReplace:
if op.Path != nil {
h.data[id].resourceAttributes[op.Path.String()] = op.Value
} else {
valueMap := op.Value.(map[string]interface{})
for k, v := range valueMap {
h.data[id].resourceAttributes[k] = v
}
}
case PatchOperationRemove:
h.data[id].resourceAttributes[op.Path.String()] = nil
}
}
created, _ := time.ParseInLocation(time.RFC3339, fmt.Sprintf("%v", h.data[id].meta["created"]), time.UTC)
now := time.Now()
// return resource with replaced attributes
return Resource{
ID: id,
ExternalID: h.externalID(h.data[id].resourceAttributes),
Attributes: h.data[id].resourceAttributes,
Meta: Meta{
Created: &created,
LastModified: &now,
Version: fmt.Sprintf("%s.patch", h.data[id].meta["version"]),
},
}, nil
}
func (h testResourceHandler) Replace(r *http.Request, id string, attributes ResourceAttributes) (Resource, error) {
// check if resource exists
_, ok := h.data[id]
if !ok {
return Resource{}, errors.ScimErrorResourceNotFound(id)
}
// replace (all) attributes
h.data[id] = testData{
resourceAttributes: attributes,
}
// return resource with replaced attributes
return Resource{
ID: id,
ExternalID: h.externalID(attributes),
Attributes: attributes,
}, nil
}
func (h testResourceHandler) externalID(attributes ResourceAttributes) optional.String {
if eID, ok := attributes["externalId"]; ok {
externalID, ok := eID.(string)
if !ok {
return optional.String{}
}
return optional.NewString(externalID)
}
return optional.String{}
}
func (h testResourceHandler) noContentOperation(id string, op PatchOperation) bool {
isRemoveOp := strings.EqualFold(op.Op, PatchOperationRemove)
dataValue, ok := h.data[id]
if !ok {
return isRemoveOp
}
var path string
if op.Path != nil {
path = op.Path.String()
}
attrValue, ok := dataValue.resourceAttributes[path]
if ok && attrValue == op.Value {
return true
}
if !ok && isRemoveOp {
return true
}
switch opValue := op.Value.(type) {
case map[string]interface{}:
for k, v := range opValue {
if v == dataValue.resourceAttributes[k] {
return true
}
}
case []map[string]interface{}:
for _, m := range opValue {
for k, v := range m {
if v == dataValue.resourceAttributes[k] {
return true
}
}
}
}
return false
}
func (h testResourceHandler) shouldReturnNoContent(id string, ops []PatchOperation) bool {
for _, op := range ops {
if h.noContentOperation(id, op) {
continue
}
return false
}
return true
}