-
Notifications
You must be signed in to change notification settings - Fork 1
/
users_service_test.go
702 lines (573 loc) · 22.4 KB
/
users_service_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
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
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
package warrant_test
import (
"fmt"
"net/http"
"net/http/httptest"
"time"
"github.com/pivotal-cf-experimental/warrant"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
var _ = Describe("UsersService", func() {
var (
service warrant.UsersService
clientsService warrant.ClientsService
token string
config warrant.Config
)
BeforeEach(func() {
config = warrant.Config{
Host: fakeUAA.URL(),
SkipVerifySSL: true,
TraceWriter: TraceWriter,
}
service = warrant.NewUsersService(config)
clientsService = warrant.NewClientsService(config)
var err error
token, err = clientsService.GetToken("admin", "admin")
Expect(err).NotTo(HaveOccurred())
})
Describe("Create", func() {
It("creates a new user", func() {
user, err := service.Create("created-user", "[email protected]", token)
Expect(err).NotTo(HaveOccurred())
Expect(user.ID).NotTo(BeEmpty())
Expect(user.UserName).To(Equal("created-user"))
Expect(user.CreatedAt).To(BeTemporally("~", time.Now().UTC(), 2*time.Millisecond))
Expect(user.UpdatedAt).To(BeTemporally("~", time.Now().UTC(), 2*time.Millisecond))
Expect(user.Version).To(Equal(0))
Expect(user.Emails).To(ConsistOf([]string{"[email protected]"}))
Expect(user.Groups).To(ConsistOf([]warrant.Group{}))
Expect(user.Active).To(BeTrue())
Expect(user.Verified).To(BeFalse())
Expect(user.Origin).To(Equal("uaa"))
Expect(user.ExternalID).To(Equal(""))
Expect(user.FormattedName).To(Equal(""))
Expect(user.FamilyName).To(Equal(""))
Expect(user.GivenName).To(Equal(""))
Expect(user.MiddleName).To(Equal(""))
fetchedUser, err := service.Get(user.ID, token)
Expect(err).NotTo(HaveOccurred())
Expect(fetchedUser).To(Equal(user))
})
Context("when the client does not have the scim.write scope", func() {
It("returns an unauthorized error", func() {
c := warrant.Client{
ID: "unauthorized",
ResourceIDs: []string{"scim"},
Authorities: []string{"scim.read"},
}
err := clientsService.Create(c, "secret", token)
Expect(err).NotTo(HaveOccurred())
t, err := clientsService.GetToken(c.ID, "secret")
Expect(err).NotTo(HaveOccurred())
_, err = service.Create("created-user", "[email protected]", t)
Expect(err).To(BeAssignableToTypeOf(warrant.UnauthorizedError{}))
})
})
Context("when the client does not have the scim audience", func() {
It("returns an unauthorized error", func() {
c := warrant.Client{
ID: "unauthorized",
ResourceIDs: []string{"banana"},
Authorities: []string{"scim.write"},
}
err := clientsService.Create(c, "secret", token)
Expect(err).NotTo(HaveOccurred())
t, err := clientsService.GetToken(c.ID, "secret")
Expect(err).NotTo(HaveOccurred())
_, err = service.Create("created-user", "[email protected]", t)
Expect(err).To(BeAssignableToTypeOf(warrant.UnauthorizedError{}))
})
})
It("requires an email address", func() {
_, err := service.Create("created-user", "", token)
Expect(err).To(BeAssignableToTypeOf(warrant.BadRequestError{}))
Expect(err.Error()).To(Equal(`bad request: {"error_description":"[Assertion failed] - this String argument must have text; it must not be null, empty, or blank","error":"invalid_scim_resource"}`))
})
Context("failure cases", func() {
It("returns an error when a user with the given username already exists", func() {
_, err := service.Create("username", "[email protected]", token)
Expect(err).NotTo(HaveOccurred())
_, err = service.Create("username", "[email protected]", token)
Expect(err).To(BeAssignableToTypeOf(warrant.DuplicateResourceError{}))
Expect(err.Error()).To(Equal("duplicate resource: {\"error_description\":\"Username already in use: username\",\"error\":\"scim_resource_already_exists\"}"))
})
It("returns an error when the json response is malformed", func() {
malformedJSONServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
w.WriteHeader(http.StatusCreated)
w.Write([]byte("this is not JSON"))
}))
service = warrant.NewUsersService(warrant.Config{
Host: malformedJSONServer.URL,
SkipVerifySSL: true,
TraceWriter: TraceWriter,
})
_, err := service.Create("some-user", "[email protected]", "some-token")
Expect(err).To(BeAssignableToTypeOf(warrant.MalformedResponseError{}))
Expect(err).To(MatchError("malformed response: invalid character 'h' in literal true (expecting 'r')"))
})
})
})
Describe("Get", func() {
var createdUser warrant.User
BeforeEach(func() {
var err error
createdUser, err = service.Create("created-user", "[email protected]", token)
Expect(err).NotTo(HaveOccurred())
})
It("returns the found user", func() {
user, err := service.Get(createdUser.ID, token)
Expect(err).NotTo(HaveOccurred())
Expect(user).To(Equal(createdUser))
})
Context("when the client does not have the scim.read scope", func() {
It("returns an unauthorized error", func() {
c := warrant.Client{
ID: "unauthorized",
ResourceIDs: []string{"scim"},
Authorities: []string{"scim.write"},
}
err := clientsService.Create(c, "secret", token)
Expect(err).NotTo(HaveOccurred())
t, err := clientsService.GetToken(c.ID, "secret")
Expect(err).NotTo(HaveOccurred())
_, err = service.Get(createdUser.ID, t)
Expect(err).To(BeAssignableToTypeOf(warrant.UnauthorizedError{}))
})
})
Context("when the client does not have the scim audience", func() {
It("returns an unauthorized error", func() {
c := warrant.Client{
ID: "unauthorized",
ResourceIDs: []string{"banana"},
Authorities: []string{"scim.read"},
}
err := clientsService.Create(c, "secret", token)
Expect(err).NotTo(HaveOccurred())
t, err := clientsService.GetToken(c.ID, "secret")
Expect(err).NotTo(HaveOccurred())
_, err = service.Get(createdUser.ID, t)
Expect(err).To(BeAssignableToTypeOf(warrant.UnauthorizedError{}))
})
})
Context("failure cases", func() {
It("returns an error when the user cannot be found", func() {
_, err := service.Get("non-existent-user-id", token)
Expect(err).To(BeAssignableToTypeOf(warrant.NotFoundError{}))
})
It("returns an error when the json response is malformed", func() {
malformedJSONServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
w.Write([]byte("this is not JSON"))
}))
service = warrant.NewUsersService(warrant.Config{
Host: malformedJSONServer.URL,
SkipVerifySSL: true,
TraceWriter: TraceWriter,
})
_, err := service.Get("some-user-id", "some-token")
Expect(err).To(BeAssignableToTypeOf(warrant.MalformedResponseError{}))
Expect(err).To(MatchError("malformed response: invalid character 'h' in literal true (expecting 'r')"))
})
})
})
Describe("Delete", func() {
var user warrant.User
BeforeEach(func() {
var err error
user, err = service.Create("deleted-user", "[email protected]", token)
Expect(err).NotTo(HaveOccurred())
})
It("deletes the user", func() {
err := service.Delete(user.ID, token)
Expect(err).NotTo(HaveOccurred())
_, err = service.Get(user.ID, token)
Expect(err).To(BeAssignableToTypeOf(warrant.NotFoundError{}))
})
Context("when the client does not have the scim.write scope", func() {
It("returns an unauthorized error", func() {
c := warrant.Client{
ID: "unauthorized",
ResourceIDs: []string{"scim"},
Authorities: []string{"scim.read"},
}
err := clientsService.Create(c, "secret", token)
Expect(err).NotTo(HaveOccurred())
t, err := clientsService.GetToken(c.ID, "secret")
Expect(err).NotTo(HaveOccurred())
err = service.Delete(user.ID, t)
Expect(err).To(BeAssignableToTypeOf(warrant.UnauthorizedError{}))
})
})
Context("when the client does not have the scim audience", func() {
It("returns an unauthorized error", func() {
c := warrant.Client{
ID: "unauthorized",
ResourceIDs: []string{"banana"},
Authorities: []string{"scim.write"},
}
err := clientsService.Create(c, "secret", token)
Expect(err).NotTo(HaveOccurred())
t, err := clientsService.GetToken(c.ID, "secret")
Expect(err).NotTo(HaveOccurred())
err = service.Delete(user.ID, t)
Expect(err).To(BeAssignableToTypeOf(warrant.UnauthorizedError{}))
})
})
It("returns an error when the user does not exist", func() {
err := service.Delete("non-existant-user-guid", token)
Expect(err).To(BeAssignableToTypeOf(warrant.NotFoundError{}))
})
})
Describe("Update", func() {
var user warrant.User
BeforeEach(func() {
var err error
user, err = service.Create("new-user", "[email protected]", token)
Expect(err).NotTo(HaveOccurred())
})
It("updates an existing user", func() {
user.UserName = "updated-user"
updatedUser, err := service.Update(user, token)
Expect(err).NotTo(HaveOccurred())
fetchedUser, err := service.Get(user.ID, token)
Expect(err).NotTo(HaveOccurred())
Expect(fetchedUser).To(Equal(updatedUser))
})
It("allows fields to be updated", func() {
user.ExternalID = "external-id"
user.FormattedName = "James Tiberius Kirk"
user.FamilyName = "Kirk"
user.GivenName = "James"
user.MiddleName = "Tiberius"
updatedUser, err := service.Update(user, token)
Expect(err).NotTo(HaveOccurred())
Expect(updatedUser.ExternalID).To(Equal(user.ExternalID))
Expect(updatedUser.FormattedName).To(Equal(user.FormattedName))
Expect(updatedUser.FamilyName).To(Equal(user.FamilyName))
Expect(updatedUser.GivenName).To(Equal(user.GivenName))
Expect(updatedUser.MiddleName).To(Equal(user.MiddleName))
fetchedUser, err := service.Get(user.ID, token)
Expect(err).NotTo(HaveOccurred())
Expect(fetchedUser).To(Equal(updatedUser))
})
Context("when the client does not have the scim.write scope", func() {
It("returns an unauthorized error", func() {
c := warrant.Client{
ID: "unauthorized",
ResourceIDs: []string{"scim"},
Authorities: []string{"scim.read"},
}
err := clientsService.Create(c, "secret", token)
Expect(err).NotTo(HaveOccurred())
t, err := clientsService.GetToken(c.ID, "secret")
Expect(err).NotTo(HaveOccurred())
_, err = service.Update(user, t)
Expect(err).To(BeAssignableToTypeOf(warrant.UnauthorizedError{}))
})
})
Context("when the client does not have the scim audience", func() {
It("returns an unauthorized error", func() {
c := warrant.Client{
ID: "unauthorized",
ResourceIDs: []string{"banana"},
Authorities: []string{"scim.write"},
}
err := clientsService.Create(c, "secret", token)
Expect(err).NotTo(HaveOccurred())
t, err := clientsService.GetToken(c.ID, "secret")
Expect(err).NotTo(HaveOccurred())
_, err = service.Update(user, t)
Expect(err).To(BeAssignableToTypeOf(warrant.UnauthorizedError{}))
})
})
It("must match the 'If-Match' header value", func() {
user.Version = 24
_, err := service.Update(user, token)
Expect(err).To(BeAssignableToTypeOf(warrant.BadRequestError{}))
Expect(err).To(MatchError(`bad request: {"error_description":"Missing If-Match for PUT","error":"scim"}`))
})
It("returns an error if the user does not exist", func() {
user.ID = "non-existant-guid"
_, err := service.Update(user, token)
Expect(err).To(BeAssignableToTypeOf(warrant.NotFoundError{}))
})
Context("failure cases", func() {
It("returns an error when the json response is malformed", func() {
malformedJSONServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
w.Write([]byte("this is not JSON"))
}))
service = warrant.NewUsersService(warrant.Config{
Host: malformedJSONServer.URL,
SkipVerifySSL: true,
TraceWriter: TraceWriter,
})
_, err := service.Update(warrant.User{ID: "some-user-id"}, "some-token")
Expect(err).To(BeAssignableToTypeOf(warrant.MalformedResponseError{}))
Expect(err).To(MatchError("malformed response: invalid character 'h' in literal true (expecting 'r')"))
})
})
})
Describe("SetPassword", func() {
var user warrant.User
BeforeEach(func() {
var err error
user, err = service.Create("password-user", "[email protected]", token)
Expect(err).NotTo(HaveOccurred())
})
It("sets the password belonging to the given user guid", func() {
err := service.SetPassword(user.ID, "password", token)
Expect(err).NotTo(HaveOccurred())
})
Context("when the client does not have the password.write scope", func() {
It("returns an unauthorized error", func() {
c := warrant.Client{
ID: "unauthorized",
ResourceIDs: []string{"password"},
Authorities: []string{"password.read"},
}
err := clientsService.Create(c, "secret", token)
Expect(err).NotTo(HaveOccurred())
t, err := clientsService.GetToken(c.ID, "secret")
Expect(err).NotTo(HaveOccurred())
err = service.SetPassword(user.ID, "password", t)
Expect(err).To(BeAssignableToTypeOf(warrant.UnauthorizedError{}))
})
})
Context("when the client does not have the password audience", func() {
It("returns an unauthorized error", func() {
c := warrant.Client{
ID: "unauthorized",
ResourceIDs: []string{"banana"},
Authorities: []string{"password.write"},
}
err := clientsService.Create(c, "secret", token)
Expect(err).NotTo(HaveOccurred())
t, err := clientsService.GetToken(c.ID, "secret")
Expect(err).NotTo(HaveOccurred())
err = service.SetPassword(user.ID, "password", t)
Expect(err).To(BeAssignableToTypeOf(warrant.UnauthorizedError{}))
})
})
It("returns an error if the user does not exist", func() {
err := service.SetPassword("non-existant-guid", "password", token)
Expect(err).To(BeAssignableToTypeOf(warrant.UnauthorizedError{}))
})
})
Describe("ChangePassword", func() {
var (
user warrant.User
userToken string
)
BeforeEach(func() {
var err error
user, err = service.Create("change-password-user", "[email protected]", token)
Expect(err).NotTo(HaveOccurred())
err = service.SetPassword(user.ID, "old-password", token)
Expect(err).NotTo(HaveOccurred())
userToken = fakeUAA.UserTokenFor(user.ID, []string{}, []string{})
})
Context("with a user token", func() {
It("changes the password given the old password", func() {
err := service.ChangePassword(user.ID, "old-password", "new-password", userToken)
Expect(err).NotTo(HaveOccurred())
})
It("does not change password if the old password does not match", func() {
err := service.ChangePassword(user.ID, "bad-password", "new-password", userToken)
Expect(err).To(BeAssignableToTypeOf(warrant.UnauthorizedError{}))
})
})
Context("with a client token", func() {
Context("when it has the password.write scope and password audience", func() {
It("changes the password regardless of the old password", func() {
c := warrant.Client{
ID: "authorized",
ResourceIDs: []string{"password"},
Authorities: []string{"password.write"},
}
err := clientsService.Create(c, "secret", token)
Expect(err).NotTo(HaveOccurred())
t, err := clientsService.GetToken(c.ID, "secret")
Expect(err).NotTo(HaveOccurred())
err = service.ChangePassword(user.ID, "bad-password", "new-password", t)
Expect(err).NotTo(HaveOccurred())
})
})
Context("when the client does not have the password.write scope", func() {
It("returns an unauthorized error", func() {
c := warrant.Client{
ID: "authorized",
ResourceIDs: []string{"password"},
Authorities: []string{"password.read"},
}
err := clientsService.Create(c, "secret", token)
Expect(err).NotTo(HaveOccurred())
t, err := clientsService.GetToken(c.ID, "secret")
Expect(err).NotTo(HaveOccurred())
err = service.ChangePassword(user.ID, "old-password", "new-password", t)
Expect(err).To(BeAssignableToTypeOf(warrant.UnauthorizedError{}))
})
})
Context("when the client does not have the password audience", func() {
It("returns an unauthorized error", func() {
c := warrant.Client{
ID: "authorized",
ResourceIDs: []string{"banana"},
Authorities: []string{"password.write"},
}
err := clientsService.Create(c, "secret", token)
Expect(err).NotTo(HaveOccurred())
t, err := clientsService.GetToken(c.ID, "secret")
Expect(err).NotTo(HaveOccurred())
err = service.ChangePassword(user.ID, "old-password", "new-password", t)
Expect(err).To(BeAssignableToTypeOf(warrant.UnauthorizedError{}))
})
})
})
})
Describe("GetToken", func() {
var (
user warrant.User
client warrant.Client
scopes []string
)
BeforeEach(func() {
var err error
user, err = service.Create("username", "[email protected]", token)
Expect(err).NotTo(HaveOccurred())
err = service.SetPassword(user.ID, "password", token)
Expect(err).NotTo(HaveOccurred())
clientsService := warrant.NewClientsService(config)
scopes = []string{"notification_preferences.read", "notification_preferences.write"}
client = warrant.Client{
ID: "some-client-id",
Scope: scopes,
ResourceIDs: []string{""},
Authorities: []string{"scim.read", "scim.write"},
AuthorizedGrantTypes: []string{"implicit"},
AccessTokenValidity: 24 * time.Hour,
RedirectURI: []string{"https://redirect.example.com"},
Autoapprove: scopes,
}
err = clientsService.Create(client, "", token)
Expect(err).NotTo(HaveOccurred())
})
It("returns a valid token given a username and password", func() {
token, err := service.GetToken("username", "password", client)
Expect(err).NotTo(HaveOccurred())
Expect(token).NotTo(BeEmpty())
tokensService := warrant.NewTokensService(config)
decodedToken, err := tokensService.Decode(token)
Expect(err).NotTo(HaveOccurred())
Expect(decodedToken.UserID).To(Equal(user.ID))
Expect(decodedToken.Scopes).To(Equal(scopes))
})
Context("failure cases", func() {
It("returns an error when the request does not succeed", func() {
_, err := service.GetToken("unknown-user", "password", client)
Expect(err).To(BeAssignableToTypeOf(warrant.NotFoundError{}))
})
It("returns an error when the response is not parsable", func() {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte(`%%%%%`))
}))
config.Host = server.URL
service = warrant.NewUsersService(config)
_, err := service.GetToken("username", "password", client)
Expect(err).To(BeAssignableToTypeOf(warrant.MalformedResponseError{}))
Expect(err).To(MatchError(ContainSubstring(`invalid character '%' looking for beginning of value`)))
})
It("returns an error when the client requesting the token does not exist", func() {
client.ID = "missing-client"
_, err := service.GetToken("username", "password", client)
Expect(err).To(BeAssignableToTypeOf(warrant.UnauthorizedError{}))
Expect(err).To(MatchError(`Warrant UnauthorizedError: {"error_description":"No client with requested id: missing-client","error":"invalid_client"}`))
})
})
})
Describe("List", func() {
var (
user warrant.User
otherUser warrant.User
)
BeforeEach(func() {
var err error
user, err = service.Create("username", "[email protected]", token)
Expect(err).NotTo(HaveOccurred())
otherUser, err = service.Create("other", "[email protected]", token)
Expect(err).NotTo(HaveOccurred())
})
It("finds users that match a filter", func() {
users, err := service.List(warrant.Query{
Filter: fmt.Sprintf("id eq '%s'", user.ID),
}, token)
Expect(err).NotTo(HaveOccurred())
Expect(users).To(HaveLen(1))
Expect(users[0].ID).To(Equal(user.ID))
})
It("returns an empty list of users if nothing matches the filter", func() {
users, err := service.List(warrant.Query{
Filter: "id eq ''",
}, token)
Expect(err).NotTo(HaveOccurred())
Expect(users).To(HaveLen(0))
})
It("ignores the case of the parameter in the filter", func() {
users, err := service.List(warrant.Query{
Filter: fmt.Sprintf("ID eq '%s'", user.ID),
}, token)
Expect(err).NotTo(HaveOccurred())
Expect(users).To(HaveLen(1))
Expect(users[0].ID).To(Equal(user.ID))
})
It("ignores the case of the operator in the filter", func() {
users, err := service.List(warrant.Query{
Filter: fmt.Sprintf("id EQ '%s'", user.ID),
}, token)
Expect(err).NotTo(HaveOccurred())
Expect(users).To(HaveLen(1))
Expect(users[0].ID).To(Equal(user.ID))
})
It("defaults to sorting users by date created", func() {
users, err := service.List(warrant.Query{}, token)
Expect(err).NotTo(HaveOccurred())
Expect(users).To(HaveLen(2))
Expect(users[0].ID).To(Equal(user.ID))
Expect(users[1].ID).To(Equal(otherUser.ID))
})
It("returns a list of users sorted by email when sortBy is given", func() {
users, err := service.List(warrant.Query{
SortBy: "email",
}, token)
Expect(err).NotTo(HaveOccurred())
Expect(users).To(HaveLen(2))
Expect(users[0].Emails[0]).To(Equal(otherUser.Emails[0]))
Expect(users[1].Emails[0]).To(Equal(user.Emails[0]))
})
Context("failure cases", func() {
It("returns an error when the query is malformed", func() {
_, err := service.List(warrant.Query{
Filter: fmt.Sprintf("invalid-parameter eq '%s'", user.ID),
}, token)
Expect(err).To(BeAssignableToTypeOf(warrant.BadRequestError{}))
Expect(err.Error()).To(Equal(`bad request: {"error_description":"Invalid filter expression: [invalid-parameter eq '` + user.ID + `']","error":"scim"}`))
})
It("returns an error when the JSON is malformed", func() {
malformedJSONServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
w.Write([]byte("this is not JSON"))
}))
service = warrant.NewUsersService(warrant.Config{
Host: malformedJSONServer.URL,
SkipVerifySSL: true,
TraceWriter: TraceWriter,
})
_, err := service.List(warrant.Query{
Filter: fmt.Sprintf("id eq '%s'", user.ID),
}, token)
Expect(err).To(BeAssignableToTypeOf(warrant.MalformedResponseError{}))
Expect(err).To(MatchError("malformed response: invalid character 'h' in literal true (expecting 'r')"))
})
})
})
})