-
Notifications
You must be signed in to change notification settings - Fork 56
/
resource_handler_test.go
253 lines (219 loc) · 5.73 KB
/
resource_handler_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
package scim
import (
"fmt"
"math/rand"
"net/http"
"strings"
"time"
"github.com/elimity-com/scim/errors"
"github.com/elimity-com/scim/optional"
)
func ExampleResourceHandler() {
var r interface{} = testResourceHandler{}
_, ok := r.(ResourceHandler)
fmt.Println(ok)
// Output: true
}
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
}