-
Notifications
You must be signed in to change notification settings - Fork 28
/
list.go
568 lines (506 loc) · 18.2 KB
/
list.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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
/*
* Copyright 2018 The Service Manager Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package test
import (
"encoding/json"
"fmt"
"strings"
"github.com/gavv/httpexpect"
"github.com/Peripli/service-manager/pkg/query"
. "github.com/onsi/ginkgo/extensions/table"
"net/http"
. "github.com/onsi/ginkgo"
"github.com/Peripli/service-manager/test/common"
)
type listOpEntry struct {
resourcesToExpectBeforeOp []common.Object
queryTemplate string
queryArgs common.Object
resourcesToExpectAfterOp []common.Object
resourcesNotToExpectAfterOp []common.Object
expectedStatusCode int
}
func DescribeListTestsFor(ctx *common.TestContext, t TestCase) bool {
var r []common.Object
var rWithMandatoryFields common.Object
attachLabel := func(obj common.Object) common.Object {
patchLabelsBody := make(map[string]interface{})
patchLabels := []query.LabelChange{
{
Operation: query.AddLabelOperation,
Key: "labelKey1",
Values: []string{"1"},
},
{
Operation: query.AddLabelOperation,
Key: "labelKey2",
Values: []string{"str"},
},
{
Operation: query.AddLabelOperation,
Key: "labelKey3",
Values: []string{`{"key1": "val1", "key2": "val2"}`},
},
}
patchLabelsBody["labels"] = patchLabels
By(fmt.Sprintf("Attempting to patch resource of %s with labels as labels are declared supported", t.API))
ctx.SMWithOAuth.PATCH(t.API + "/" + obj["id"].(string)).WithJSON(patchLabelsBody).
Expect().
Status(http.StatusOK)
result := ctx.SMWithOAuth.GET(t.API + "/" + obj["id"].(string)).
Expect().
Status(http.StatusOK).JSON().Object()
result.ContainsKey("labels")
r := result.Raw()
return r
}
By(fmt.Sprintf("Attempting to create a random resource of %s with mandatory fields only", t.API))
rWithMandatoryFields = t.ResourceWithoutNullableFieldsBlueprint(ctx, ctx.SMWithOAuth)
for i := 0; i < 4; i++ {
By(fmt.Sprintf("Attempting to create a random resource of %s", t.API))
gen := t.ResourceBlueprint(ctx, ctx.SMWithOAuth)
gen = attachLabel(gen)
delete(gen, "created_at")
delete(gen, "updated_at")
r = append(r, gen)
}
entries := []TableEntry{
Entry("returns 200",
listOpEntry{
resourcesToExpectBeforeOp: []common.Object{r[0]},
queryTemplate: "%s = %v",
queryArgs: r[0],
resourcesToExpectAfterOp: []common.Object{r[0]},
expectedStatusCode: http.StatusOK,
},
),
Entry("returns 200",
listOpEntry{
resourcesToExpectBeforeOp: []common.Object{r[0], r[1], r[2], r[3]},
queryTemplate: "%s != %v",
queryArgs: r[0],
resourcesNotToExpectAfterOp: []common.Object{r[0]},
expectedStatusCode: http.StatusOK,
},
),
Entry("returns 200",
listOpEntry{
resourcesToExpectBeforeOp: []common.Object{r[0], r[1], r[2], r[3]},
queryTemplate: "%[1]s in [%[2]v||%[2]v||%[2]v]",
queryArgs: r[0],
resourcesToExpectAfterOp: []common.Object{r[0]},
expectedStatusCode: http.StatusOK,
},
),
Entry("returns 200",
listOpEntry{
resourcesToExpectBeforeOp: []common.Object{r[0], r[1], r[2], r[3]},
queryTemplate: "%s in [%v]",
queryArgs: r[0],
resourcesToExpectAfterOp: []common.Object{r[0]},
expectedStatusCode: http.StatusOK,
},
),
Entry("returns 200",
listOpEntry{
resourcesToExpectBeforeOp: []common.Object{r[0], r[1], r[2], r[3]},
queryTemplate: "%[1]s notin [%[2]v||%[2]v||%[2]v]",
queryArgs: r[0],
resourcesNotToExpectAfterOp: []common.Object{r[0]},
expectedStatusCode: http.StatusOK,
},
),
Entry("returns 200",
listOpEntry{
resourcesToExpectBeforeOp: []common.Object{r[0], r[1], r[2], r[3]},
queryTemplate: "%s notin [%v]",
queryArgs: r[0],
resourcesNotToExpectAfterOp: []common.Object{r[0]},
expectedStatusCode: http.StatusOK,
},
),
Entry("returns 200",
listOpEntry{
resourcesToExpectBeforeOp: []common.Object{r[0], r[1], r[2], r[3]},
queryTemplate: "%s gt %v",
queryArgs: common.RemoveNonNumericArgs(r[0]),
resourcesNotToExpectAfterOp: []common.Object{r[0]},
expectedStatusCode: http.StatusOK,
},
),
Entry("returns 200 for greater than or equal queries",
listOpEntry{
resourcesToExpectBeforeOp: []common.Object{r[0], r[1], r[2], r[3]},
queryTemplate: "%s gte %v",
queryArgs: common.RemoveNonNumericArgs(r[0]),
resourcesToExpectAfterOp: []common.Object{r[0], r[1], r[2], r[3]},
expectedStatusCode: http.StatusOK,
},
),
Entry("returns 400 for greater than or equal queries when query args are non numeric",
listOpEntry{
resourcesToExpectBeforeOp: []common.Object{r[0], r[1], r[2], r[3]},
queryTemplate: "%s gte %v",
queryArgs: common.RemoveNumericArgs(r[0]),
resourcesToExpectAfterOp: []common.Object{r[0], r[1], r[2], r[3]},
expectedStatusCode: http.StatusBadRequest,
},
),
Entry("returns 200 for less than or equal queries",
listOpEntry{
resourcesToExpectBeforeOp: []common.Object{r[0], r[1], r[2], r[3]},
queryTemplate: "%s lte %v",
queryArgs: common.RemoveNonNumericArgs(r[0]),
resourcesToExpectAfterOp: []common.Object{r[0], r[1], r[2], r[3]},
expectedStatusCode: http.StatusOK,
},
),
Entry("returns 400 for less than or equal queries when query args are non numeric",
listOpEntry{
resourcesToExpectBeforeOp: []common.Object{r[0], r[1], r[2], r[3]},
queryTemplate: "%s lte %v",
queryArgs: common.RemoveNumericArgs(r[0]),
resourcesToExpectAfterOp: []common.Object{r[0], r[1], r[2], r[3]},
expectedStatusCode: http.StatusBadRequest,
},
),
Entry("returns 200",
listOpEntry{
resourcesToExpectBeforeOp: []common.Object{r[0], r[1], r[2], r[3]},
queryTemplate: "%s lt %v",
queryArgs: common.RemoveNonNumericArgs(r[0]),
resourcesNotToExpectAfterOp: []common.Object{r[0]},
expectedStatusCode: http.StatusOK,
},
),
Entry("returns 200 for field queries",
listOpEntry{
resourcesToExpectBeforeOp: []common.Object{r[0], rWithMandatoryFields},
queryTemplate: "%s eqornil %v",
queryArgs: common.RemoveNotNullableFieldAndLabels(r[0], rWithMandatoryFields),
resourcesToExpectAfterOp: []common.Object{r[0], rWithMandatoryFields},
expectedStatusCode: http.StatusOK,
},
),
Entry("returns 400 for label queries with operator eqornil",
listOpEntry{
queryTemplate: "%s eqornil %v",
queryArgs: common.Object{
"labels": map[string]interface{}{
"labelKey1": []interface{}{
"str",
},
}},
expectedStatusCode: http.StatusBadRequest,
},
),
Entry("returns 200 for JSON fields with stripped new lines",
listOpEntry{
resourcesToExpectBeforeOp: []common.Object{r[0]},
queryTemplate: "%s = %v",
queryArgs: common.RemoveNonJSONArgs(r[0]),
resourcesToExpectAfterOp: []common.Object{r[0]},
expectedStatusCode: http.StatusOK,
},
),
Entry("returns 400 when query operator is invalid",
listOpEntry{
queryTemplate: "%s @@ %v",
queryArgs: r[0],
expectedStatusCode: http.StatusBadRequest,
},
),
Entry("returns 400 when query is duplicated",
listOpEntry{
queryTemplate: "%[1]s = %[2]v|%[1]s = %[2]v",
queryArgs: r[0],
expectedStatusCode: http.StatusBadRequest,
},
),
Entry("returns 400 when operator is not properly separated with right space from operands",
listOpEntry{
queryTemplate: "%s =%v",
queryArgs: r[0],
expectedStatusCode: http.StatusBadRequest,
},
),
Entry("returns 400 when operator is not properly separated with left space from operands",
listOpEntry{
queryTemplate: "%s= %v",
queryArgs: r[0],
expectedStatusCode: http.StatusBadRequest,
},
),
Entry("returns 400 when field query left operands are unknown",
listOpEntry{
queryTemplate: "%[1]s in [%[2]v||%[2]v]",
queryArgs: common.Object{"unknownkey": "unknownvalue"},
expectedStatusCode: http.StatusBadRequest,
},
),
Entry("returns 200 when label query left operands are unknown",
listOpEntry{
resourcesToExpectBeforeOp: []common.Object{r[0], r[1], r[2], r[3]},
queryTemplate: "%[1]s in [%[2]v||%[2]v]",
queryArgs: common.Object{
"labels": map[string]interface{}{
"unknown": []interface{}{
"unknown",
},
}},
resourcesNotToExpectAfterOp: []common.Object{r[0], r[1], r[2], r[3]},
expectedStatusCode: http.StatusOK,
},
),
Entry("returns 400 when single value operator is used with multiple right value arguments",
listOpEntry{
queryTemplate: "%[1]s != [%[2]v||%[2]v||%[2]v]",
queryArgs: r[0],
expectedStatusCode: http.StatusBadRequest,
},
),
Entry("returns 400 when numeric operator is used with non-numeric operands",
listOpEntry{
queryTemplate: "%s < %v",
queryArgs: common.RemoveNumericArgs(r[0]),
expectedStatusCode: http.StatusBadRequest,
},
),
}
verifyListOpWithAuth := func(listOpEntry listOpEntry, query string, auth *httpexpect.Expect) {
var expectedAfterOpIDs []string
var unexpectedAfterOpIDs []string
expectedAfterOpIDs = common.ExtractResourceIDs(listOpEntry.resourcesToExpectAfterOp)
unexpectedAfterOpIDs = common.ExtractResourceIDs(listOpEntry.resourcesNotToExpectAfterOp)
jsonArrayKey := strings.Replace(t.API, "/v1/", "", 1)
By(fmt.Sprintf("[TEST]: Verifying expected %s before operation after present", t.API))
beforeOpArray := ctx.SMWithOAuth.GET(t.API).
Expect().
Status(http.StatusOK).JSON().Object().Value(jsonArrayKey).Array()
for _, v := range beforeOpArray.Iter() {
obj := v.Object().Raw()
delete(obj, "created_at")
delete(obj, "updated_at")
}
for _, entity := range listOpEntry.resourcesToExpectBeforeOp {
delete(entity, "created_at")
delete(entity, "updated_at")
beforeOpArray.Contains(entity)
}
By("[TEST]: ======= Expectations Summary =======")
By(fmt.Sprintf("[TEST]: Listing %s with %s", t.API, query))
By(fmt.Sprintf("[TEST]: Currently present resources: %v", r))
By(fmt.Sprintf("[TEST]: Expected %s ids after operations: %s", t.API, expectedAfterOpIDs))
By(fmt.Sprintf("[TEST]: Unexpected %s ids after operations: %s", t.API, unexpectedAfterOpIDs))
By(fmt.Sprintf("[TEST]: Expected status code %d", listOpEntry.expectedStatusCode))
By("[TEST]: ====================================")
req := ctx.SMWithOAuth.GET(t.API)
if query != "" {
req = req.WithQueryString(query)
}
By(fmt.Sprintf("[TEST]: Verifying expected status code %d is returned from list operation", listOpEntry.expectedStatusCode))
resp := req.
Expect().
Status(listOpEntry.expectedStatusCode)
if listOpEntry.expectedStatusCode != http.StatusOK {
By(fmt.Sprintf("[TEST]: Verifying error and description fields are returned after list operation"))
resp.JSON().Object().Keys().Contains("error", "description")
} else {
array := resp.JSON().Object().Value(jsonArrayKey).Array()
for _, v := range array.Iter() {
obj := v.Object().Raw()
delete(obj, "created_at")
delete(obj, "updated_at")
}
if listOpEntry.resourcesToExpectAfterOp != nil {
By(fmt.Sprintf("[TEST]: Verifying expected %s are returned after list operation", t.API))
for _, entity := range listOpEntry.resourcesToExpectAfterOp {
delete(entity, "created_at")
delete(entity, "updated_at")
array.Contains(entity)
}
}
if listOpEntry.resourcesNotToExpectAfterOp != nil {
By(fmt.Sprintf("[TEST]: Verifying unexpected %s are NOT returned after list operation", t.API))
for _, entity := range listOpEntry.resourcesNotToExpectAfterOp {
delete(entity, "created_at")
delete(entity, "updated_at")
array.NotContains(entity)
}
}
}
}
verifyListOp := func(listOpEntry listOpEntry, query string) {
verifyListOpWithAuth(listOpEntry, query, ctx.SMWithOAuth)
}
return Describe("List", func() {
Context("with basic auth", func() {
It("returns 200", func() {
ctx.SMWithBasic.GET(t.API).
Expect().
Status(http.StatusOK)
})
})
Context("with bearer auth", func() {
if !t.DisableTenantResources {
Context("when authenticating with tenant scoped token", func() {
var rForTenant common.Object
BeforeEach(func() {
rForTenant = t.ResourceBlueprint(ctx, ctx.SMWithOAuthForTenant)
})
It("returns only tenant specific resources", func() {
verifyListOpWithAuth(listOpEntry{
resourcesToExpectBeforeOp: []common.Object{r[0], r[1], rForTenant},
resourcesToExpectAfterOp: []common.Object{rForTenant},
expectedStatusCode: http.StatusOK,
}, "", ctx.SMWithOAuthForTenant)
})
Context("when authenticating with global token", func() {
It("it returns all resources", func() {
verifyListOpWithAuth(listOpEntry{
resourcesToExpectBeforeOp: []common.Object{r[0], r[1], rForTenant},
resourcesToExpectAfterOp: []common.Object{r[0], r[1], rForTenant},
expectedStatusCode: http.StatusOK,
}, "", ctx.SMWithOAuth)
})
})
})
}
Context("with no field query", func() {
It("it returns all resources", func() {
verifyListOpWithAuth(listOpEntry{
resourcesToExpectBeforeOp: []common.Object{r[0], r[1]},
resourcesToExpectAfterOp: []common.Object{r[0], r[1]},
expectedStatusCode: http.StatusOK,
}, "", ctx.SMWithOAuth)
})
})
Context("with empty field query", func() {
It("returns 200", func() {
verifyListOp(listOpEntry{
resourcesToExpectBeforeOp: []common.Object{r[0], r[1]},
resourcesToExpectAfterOp: []common.Object{r[0], r[1]},
expectedStatusCode: http.StatusOK,
}, "fieldQuery=")
})
})
Context("with empty label query", func() {
It("returns 200", func() {
verifyListOp(listOpEntry{
resourcesToExpectBeforeOp: []common.Object{r[0], r[1]},
resourcesToExpectAfterOp: []common.Object{r[0], r[1]},
expectedStatusCode: http.StatusOK,
}, "labelQuery=")
})
})
Context("with empty label query and field query", func() {
It("returns 200", func() {
verifyListOp(listOpEntry{
resourcesToExpectBeforeOp: []common.Object{r[0], r[1]},
resourcesToExpectAfterOp: []common.Object{r[0], r[1]},
expectedStatusCode: http.StatusOK,
}, "labelQuery=&fieldQuery=")
})
})
// expand all field and label query test enties into Its wrapped by descriptive Contexts
for i := 0; i < len(entries); i++ {
params := entries[i].Parameters[0].(listOpEntry)
if len(params.queryTemplate) == 0 {
panic("query templates missing")
}
var multiQueryValue string
var queryValues []string
fields := common.CopyObject(params.queryArgs)
delete(fields, "labels")
multiQueryValue, queryValues = expandFieldQuery(fields, params.queryTemplate)
fquery := "fieldQuery" + "=" + multiQueryValue
Context("with field query=", func() {
for _, queryValue := range queryValues {
query := "fieldQuery" + "=" + queryValue
DescribeTable(fmt.Sprintf("%s", queryValue), func(test listOpEntry) {
verifyListOp(test, query)
}, entries[i])
}
if len(queryValues) > 1 {
DescribeTable(fmt.Sprintf("%s", multiQueryValue), func(test listOpEntry) {
verifyListOp(test, fquery)
}, entries[i])
}
})
labels := params.queryArgs["labels"]
if labels != nil {
multiQueryValue, queryValues = expandLabelQuery(labels.(map[string]interface{}), params.queryTemplate)
lquery := "labelQuery" + "=" + multiQueryValue
Context("with label query=", func() {
for _, queryValue := range queryValues {
query := "labelQuery" + "=" + queryValue
DescribeTable(fmt.Sprintf("%s", queryValue), func(test listOpEntry) {
verifyListOp(test, query)
}, entries[i])
}
if len(queryValues) > 1 {
DescribeTable(fmt.Sprintf("%s", multiQueryValue), func(test listOpEntry) {
verifyListOp(test, lquery)
}, entries[i])
}
})
Context("with multiple field and label queries", func() {
DescribeTable(fmt.Sprintf("%s", fquery+"&"+lquery), func(test listOpEntry) {
verifyListOp(test, fquery+"&"+lquery)
}, entries[i])
})
}
}
})
})
}
func expandFieldQuery(fieldQueryArgs common.Object, queryTemplate string) (string, []string) {
var expandedMultiQuery string
var expandedQueries []string
for k, v := range fieldQueryArgs {
if v == nil {
continue
}
if m, ok := v.(map[string]interface{}); ok {
bytes, err := json.Marshal(m)
if err != nil {
panic(err)
}
v = string(bytes)
}
if a, ok := v.([]interface{}); ok {
bytes, err := json.Marshal(a)
if err != nil {
panic(err)
}
v = string(bytes)
}
expandedQueries = append(expandedQueries, fmt.Sprintf(queryTemplate, k, v))
}
expandedMultiQuery = strings.Join(expandedQueries, "|")
return expandedMultiQuery, expandedQueries
}
func expandLabelQuery(labelQueryArgs map[string]interface{}, queryTemplate string) (string, []string) {
var expandedMultiQuery string
var expandedQueries []string
for key, values := range labelQueryArgs {
for _, value := range values.([]interface{}) {
expandedQueries = append(expandedQueries, fmt.Sprintf(queryTemplate, key, value))
}
}
expandedMultiQuery = strings.Join(expandedQueries, "|")
return expandedMultiQuery, expandedQueries
}