forked from openshift-online/ocm-sdk-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconnection_test.go
755 lines (704 loc) · 25.4 KB
/
connection_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
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
/*
Copyright (c) 2019 Red Hat, Inc.
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.
*/
// This file contains tests for the connection.
package sdk
import (
"context"
"io/ioutil"
"net/http"
"os"
"path/filepath"
"time"
"github.com/onsi/gomega/gbytes"
. "github.com/onsi/ginkgo" // nolint
. "github.com/onsi/gomega" // nolint
)
var _ = Describe("Connection", func() {
It("Can be created with access token", func() {
accessToken := DefaultToken("Bearer", 5*time.Minute)
connection, err := NewConnectionBuilder().
Logger(logger).
Tokens(accessToken).
Build()
Expect(err).ToNot(HaveOccurred())
defer connection.Close()
Expect(connection).ToNot(BeNil())
})
It("Can be created with refresh token", func() {
refreshToken := DefaultToken("Refresh", 10*time.Hour)
connection, err := NewConnectionBuilder().
Logger(logger).
Tokens(refreshToken).
Build()
Expect(err).ToNot(HaveOccurred())
defer connection.Close()
Expect(connection).ToNot(BeNil())
})
It("Can be created with offline access token", func() {
offlineToken := DefaultToken("Offline", 0)
connection, err := NewConnectionBuilder().
Logger(logger).
Tokens(offlineToken).
Build()
Expect(err).ToNot(HaveOccurred())
defer connection.Close()
Expect(connection).ToNot(BeNil())
})
It("Can be created with access and refresh tokens", func() {
accessToken := DefaultToken("Bearer", 5*time.Minute)
refreshToken := DefaultToken("Refresh", 10*time.Hour)
connection, err := NewConnectionBuilder().
Logger(logger).
Tokens(accessToken, refreshToken).
Build()
Expect(err).ToNot(HaveOccurred())
defer connection.Close()
Expect(connection).ToNot(BeNil())
})
It("Can be created with access and offline tokens", func() {
accessToken := DefaultToken("Bearer", 5*time.Minute)
offlineToken := DefaultToken("Offline", 10*time.Hour)
connection, err := NewConnectionBuilder().
Logger(logger).
Tokens(accessToken, offlineToken).
Build()
Expect(err).ToNot(HaveOccurred())
defer connection.Close()
Expect(connection).ToNot(BeNil())
})
It("Can be created with user name and password", func() {
connection, err := NewConnectionBuilder().
Logger(logger).
User("myuser", "mypassword").
Build()
Expect(err).ToNot(HaveOccurred())
defer connection.Close()
Expect(connection).ToNot(BeNil())
})
It("Can be created with client identifier and secret", func() {
connection, err := NewConnectionBuilder().
Logger(logger).
Client("myclientid", "myclientsecret").
Build()
Expect(err).ToNot(HaveOccurred())
defer connection.Close()
Expect(connection).ToNot(BeNil())
})
It("Selects default OpenID server with default access token", func() {
accessToken := DefaultToken("Bearer", 5*time.Minute)
connection, err := NewConnectionBuilder().
Logger(logger).
Tokens(accessToken).
Build()
Expect(err).ToNot(HaveOccurred())
defer connection.Close()
tokenURL := connection.TokenURL()
Expect(tokenURL).To(Equal(DefaultTokenURL))
clientID, clientSecret := connection.Client()
Expect(clientID).To(Equal(DefaultClientID))
Expect(clientSecret).To(Equal(DefaultClientSecret))
})
It("Selects default OpenID server with default refresh token", func() {
refreshToken := DefaultToken("Refresh", 10*time.Hour)
connection, err := NewConnectionBuilder().
Logger(logger).
Tokens(refreshToken).
Build()
Expect(err).ToNot(HaveOccurred())
defer connection.Close()
tokenURL := connection.TokenURL()
Expect(tokenURL).To(Equal(DefaultTokenURL))
clientID, clientSecret := connection.Client()
Expect(clientID).To(Equal(DefaultClientID))
Expect(clientSecret).To(Equal(DefaultClientSecret))
})
It("Selects default OpenID server with default offline access token", func() {
offlineToken := DefaultToken("Offline", 0)
connection, err := NewConnectionBuilder().
Logger(logger).
Tokens(offlineToken).
Build()
Expect(err).ToNot(HaveOccurred())
defer connection.Close()
tokenURL := connection.TokenURL()
Expect(tokenURL).To(Equal(DefaultTokenURL))
clientID, clientSecret := connection.Client()
Expect(clientID).To(Equal(DefaultClientID))
Expect(clientSecret).To(Equal(DefaultClientSecret))
})
It("Honours explicitly provided OpenID server with user name and password", func() {
connection, err := NewConnectionBuilder().
Logger(logger).
User("myuser", "mypassword").
TokenURL(DefaultTokenURL).
Client(DefaultClientID, DefaultClientSecret).
Build()
Expect(err).ToNot(HaveOccurred())
defer connection.Close()
tokenURL := connection.TokenURL()
Expect(tokenURL).To(Equal(DefaultTokenURL))
clientID, clientSecret := connection.Client()
Expect(clientID).To(Equal(DefaultClientID))
Expect(clientSecret).To(Equal(DefaultClientSecret))
})
It("Use transport wrapper", func() {
// Create a connection:
transport := NewTestTransport()
connection, err := NewConnectionBuilder().
Logger(logger).
User("test", "test").
TransportWrapper(func(wrapped http.RoundTripper) http.RoundTripper {
return transport
}).
Build()
Expect(err).ToNot(HaveOccurred())
defer connection.Close()
// Try to get the tokens using a explicit and short timeout to make the test run
// faster (by default it takes up to 15 seconds) but give it enough time to retry
// a few times:
ctx, _ := context.WithTimeout(context.Background(), 2*time.Second)
_, _, err = connection.TokensContext(ctx)
// Check that the transport was called at least three times:
Expect(transport.called).To(BeNumerically(">=", 3))
Expect(err).To(HaveOccurred())
})
It("Can be created with one alternative URL", func() {
// Create the connection:
token := DefaultToken("Bearer", 5*time.Minute)
connection, err := NewConnectionBuilder().
Logger(logger).
Tokens(token).
URL("https://my.server.com").
AlternativeURL("/api/clusters_mgmt", "https://your.server.com").
Build()
Expect(err).ToNot(HaveOccurred())
// Check that the URLs are set:
Expect(connection.URL()).To(Equal("https://my.server.com"))
alternativeURLs := connection.AlternativeURLs()
Expect(alternativeURLs).To(HaveLen(1))
Expect(alternativeURLs).To(HaveKeyWithValue(
"/api/clusters_mgmt",
"https://your.server.com",
))
})
It("Can be created with two alternative URLs", func() {
// Create the connection:
token := DefaultToken("Bearer", 5*time.Minute)
connection, err := NewConnectionBuilder().
Logger(logger).
Tokens(token).
URL("https://my.server.com").
AlternativeURL("/api/clusters_mgmt", "https://your.server.com").
AlternativeURL("/api/accounts_mgmt", "https://her.server.com").
Build()
Expect(err).ToNot(HaveOccurred())
// Check that the URLs are set:
Expect(connection.URL()).To(Equal("https://my.server.com"))
alternativeURLs := connection.AlternativeURLs()
Expect(alternativeURLs).To(HaveLen(2))
Expect(alternativeURLs).To(HaveKeyWithValue(
"/api/clusters_mgmt",
"https://your.server.com",
))
Expect(alternativeURLs).To(HaveKeyWithValue(
"/api/accounts_mgmt",
"https://her.server.com",
))
})
It("Can be created with a map of alternative URLs", func() {
// Create the connection:
token := DefaultToken("Bearer", 5*time.Minute)
connection, err := NewConnectionBuilder().
Logger(logger).
Tokens(token).
URL("https://my.server.com").
AlternativeURLs(map[string]string{
"/api/clusters_mgmt": "https://your.server.com",
"/api/accounts_mgmt": "https://her.server.com",
}).
Build()
Expect(err).ToNot(HaveOccurred())
// Check that the URLs are set:
Expect(connection.URL()).To(Equal("https://my.server.com"))
alternativeURLs := connection.AlternativeURLs()
Expect(alternativeURLs).To(HaveLen(2))
Expect(alternativeURLs).To(HaveKeyWithValue(
"/api/clusters_mgmt",
"https://your.server.com",
))
Expect(alternativeURLs).To(HaveKeyWithValue(
"/api/accounts_mgmt",
"https://her.server.com",
))
})
It("Altering returned alternative URLs doesn't affect internal state", func() {
// Create the connection:
token := DefaultToken("Bearer", 5*time.Minute)
connection, err := NewConnectionBuilder().
Logger(logger).
Tokens(token).
URL("https://my.server.com").
AlternativeURLs(map[string]string{
"/api/clusters_mgmt": "https://your.server.com",
"/api/accounts_mgmt": "https://her.server.com",
}).
Build()
Expect(err).ToNot(HaveOccurred())
// Try to modify the returned map of alternative URLs:
alternativeURLs := connection.AlternativeURLs()
alternativeURLs["/api/service_logs"] = "https://his.server.com"
// Check that map used internall hasn't changed:
alternativeURLs = connection.AlternativeURLs()
Expect(alternativeURLs).To(HaveLen(2))
Expect(alternativeURLs).To(HaveKeyWithValue(
"/api/clusters_mgmt",
"https://your.server.com",
))
Expect(alternativeURLs).To(HaveKeyWithValue(
"/api/accounts_mgmt",
"https://her.server.com",
))
})
It("Can't be created with invalid alternative URL prefix", func() {
token := DefaultToken("Bearer", 5*time.Minute)
_, err := NewConnectionBuilder().
Logger(logger).
Tokens(token).
AlternativeURL("junk", "https://api.openshift.com").
Build()
Expect(err).To(HaveOccurred())
})
It("Can't be created with invalid alternative URL", func() {
token := DefaultToken("Bearer", 5*time.Minute)
_, err := NewConnectionBuilder().
Logger(logger).
Tokens(token).
AlternativeURL("/api/clusters_mgmt", ":junk").
Build()
Expect(err).To(HaveOccurred())
})
It("Can be configured with a YAML file", func() {
// Create temporary files for the trusted CAs:
tmp, err := ioutil.TempDir("", "*.test.cas")
Expect(err).ToNot(HaveOccurred())
defer func() {
err = os.RemoveAll(tmp)
Expect(err).ToNot(HaveOccurred())
}()
err = ioutil.WriteFile(filepath.Join(tmp, "myca.pem"), mycaPEM, 0600)
Expect(err).ToNot(HaveOccurred())
err = ioutil.WriteFile(filepath.Join(tmp, "yourca.pem"), yourcaPEM, 0600)
Expect(err).ToNot(HaveOccurred())
// Create a temporary YAML file containing the configuration:
generatedAccess := DefaultToken("Bearer", 5*time.Minute)
generatedRefresh := DefaultToken("Refresh", 10*time.Hour)
content := EvaluateTemplate(
`
url: https://my.server.com
alternative_urls:
/api/clusters_mgmt: https://your.server.com
/api/accounts_mgmt: https://her.server.com
token_url: https://openid.server.com
user: myuser
password: mypassword
client_id: myclient
client_secret: mysecret
tokens:
- {{ .AccessToken }}
- {{ .RefreshToken }}
scopes:
- openid
- myscope
insecure: true
trusted_cas:
- {{ .Tmp }}/myca.pem
- {{ .Tmp }}/yourca.pem
agent: myagent
`,
"Tmp", tmp,
"AccessToken", generatedAccess,
"RefreshToken", generatedRefresh,
)
file, err := ioutil.TempFile("", "*.yaml")
Expect(err).ToNot(HaveOccurred())
path := file.Name()
defer func() {
err = os.Remove(path)
Expect(err).ToNot(HaveOccurred())
}()
_, err = file.WriteString(content)
Expect(err).ToNot(HaveOccurred())
err = file.Close()
Expect(err).ToNot(HaveOccurred())
// Create the connection and verify it has been created with the configuration
// stored in the YAML file:
connection, err := NewConnectionBuilder().
Logger(logger).
Load(path).
Build()
Expect(err).ToNot(HaveOccurred())
Expect(connection.URL()).To(Equal("https://my.server.com"))
alternativeURLs := connection.AlternativeURLs()
Expect(alternativeURLs).To(HaveLen(2))
Expect(alternativeURLs).To(HaveKeyWithValue(
"/api/clusters_mgmt", "https://your.server.com",
))
Expect(alternativeURLs).To(HaveKeyWithValue(
"/api/accounts_mgmt", "https://her.server.com",
))
Expect(connection.TokenURL()).To(Equal("https://openid.server.com"))
user, password := connection.User()
Expect(user).To(Equal("myuser"))
Expect(password).To(Equal("mypassword"))
client, secret := connection.Client()
Expect(client).To(Equal("myclient"))
Expect(secret).To(Equal("mysecret"))
returnedAccess, returnedRefresh, err := connection.Tokens()
Expect(err).ToNot(HaveOccurred())
Expect(returnedAccess).To(Equal(generatedAccess))
Expect(returnedRefresh).To(Equal(generatedRefresh))
defer func() {
err = connection.Close()
Expect(err).ToNot(HaveOccurred())
}()
Expect(connection.Scopes()).To(ConsistOf("openid", "myscope"))
Expect(connection.Insecure()).To(BeTrue())
Expect(connection.Agent()).To(Equal("myagent"))
})
It("Method calls after load override configuration file", func() {
// Create temporary files for the trusted CAs:
tmp, err := ioutil.TempDir("", "*.test.cas")
Expect(err).ToNot(HaveOccurred())
defer func() {
err = os.RemoveAll(tmp)
Expect(err).ToNot(HaveOccurred())
}()
err = ioutil.WriteFile(filepath.Join(tmp, "myca.pem"), mycaPEM, 0600)
Expect(err).ToNot(HaveOccurred())
err = ioutil.WriteFile(filepath.Join(tmp, "yourca.pem"), yourcaPEM, 0600)
Expect(err).ToNot(HaveOccurred())
// Create a temporary YAML file containing the configuration:
generatedAccess := DefaultToken("Bearer", 5*time.Minute)
generatedRefresh := DefaultToken("Refresh", 10*time.Hour)
content := EvaluateTemplate(
`
url: https://my.server.com
alternative_urls:
/api/clusters_mgmt: https://your.server.com
/api/accounts_mgmt: https://her.server.com
token_url: https://openid.server.com
user: myuser
password: mypassword
client_id: myclient
client_secret: mysecret
tokens:
- {{ .AccessToken }}
- {{ .RefreshToken }}
scopes:
- openid
- myscope
insecure: true
trusted_cas:
- {{ .Tmp }}/myca.pem
- {{ .Tmp }}/yourca.pem
agent: myagent
`,
"Tmp", tmp,
"AccessToken", generatedAccess,
"RefreshToken", generatedRefresh,
)
file, err := ioutil.TempFile("", "*.yaml")
Expect(err).ToNot(HaveOccurred())
path := file.Name()
defer func() {
err = os.Remove(path)
Expect(err).ToNot(HaveOccurred())
}()
_, err = file.WriteString(content)
Expect(err).ToNot(HaveOccurred())
err = file.Close()
Expect(err).ToNot(HaveOccurred())
// Load the configuration file and then configure the connection with method
// calls:
overridenAccess := DefaultToken("Bearer", 5*time.Minute)
overridenRefresh := DefaultToken("Refresh", 10*time.Hour)
connection, err := NewConnectionBuilder().
Logger(logger).
Load(path).
URL("https://overriden.my.server.com").
AlternativeURL("/api/clusters_mgmt", "https://overriden.your.server.com").
AlternativeURL("/api/accounts_mgmt", "https://overriden.her.server.com").
TokenURL("https://overriden.openid.server.com").
User("overriden.myuser", "overriden.mypassword").
Client("overriden.myclient", "overriden.mysecret").
Tokens(overridenAccess, overridenRefresh).
Scopes("openid", "overriden.myscope").
Insecure(false).
Agent("overriden.myagent").
Build()
Expect(err).ToNot(HaveOccurred())
// Check that the actual settings are the ones set with the method calls:
Expect(connection.URL()).To(Equal("https://overriden.my.server.com"))
alternativeURLs := connection.AlternativeURLs()
Expect(alternativeURLs).To(HaveLen(2))
Expect(alternativeURLs).To(HaveKeyWithValue(
"/api/clusters_mgmt", "https://overriden.your.server.com",
))
Expect(alternativeURLs).To(HaveKeyWithValue(
"/api/accounts_mgmt", "https://overriden.her.server.com",
))
Expect(connection.TokenURL()).To(Equal("https://overriden.openid.server.com"))
user, password := connection.User()
Expect(user).To(Equal("overriden.myuser"))
Expect(password).To(Equal("overriden.mypassword"))
client, secret := connection.Client()
Expect(client).To(Equal("overriden.myclient"))
Expect(secret).To(Equal("overriden.mysecret"))
returnedAccess, returnedRefresh, err := connection.Tokens()
Expect(err).ToNot(HaveOccurred())
Expect(returnedAccess).To(Equal(overridenAccess))
Expect(returnedRefresh).To(Equal(overridenRefresh))
defer func() {
err = connection.Close()
Expect(err).ToNot(HaveOccurred())
}()
Expect(connection.Scopes()).To(ConsistOf("openid", "overriden.myscope"))
Expect(connection.Insecure()).To(BeFalse())
Expect(connection.Agent()).To(Equal("overriden.myagent"))
})
It("Method calls before load don't override configuration file", func() {
// Create temporary files for the trusted CAs:
tmp, err := ioutil.TempDir("", "*.test.cas")
Expect(err).ToNot(HaveOccurred())
defer func() {
err = os.RemoveAll(tmp)
Expect(err).ToNot(HaveOccurred())
}()
err = ioutil.WriteFile(filepath.Join(tmp, "myca.pem"), mycaPEM, 0600)
Expect(err).ToNot(HaveOccurred())
err = ioutil.WriteFile(filepath.Join(tmp, "yourca.pem"), yourcaPEM, 0600)
Expect(err).ToNot(HaveOccurred())
// Create a temporary YAML file containing the configuration:
generatedAccess := DefaultToken("Bearer", 5*time.Minute)
generatedRefresh := DefaultToken("Refresh", 10*time.Hour)
content := EvaluateTemplate(
`
url: https://my.server.com
alternative_urls:
/api/clusters_mgmt: https://your.server.com
/api/accounts_mgmt: https://her.server.com
token_url: https://openid.server.com
user: myuser
password: mypassword
client_id: myclient
client_secret: mysecret
tokens:
- {{ .AccessToken }}
- {{ .RefreshToken }}
scopes:
- openid
- myscope
insecure: true
trusted_cas:
- {{ .Tmp }}/myca.pem
- {{ .Tmp }}/yourca.pem
agent: myagent
`,
"Tmp", tmp,
"AccessToken", generatedAccess,
"RefreshToken", generatedRefresh,
)
file, err := ioutil.TempFile("", "*.yaml")
Expect(err).ToNot(HaveOccurred())
path := file.Name()
defer func() {
err = os.Remove(path)
Expect(err).ToNot(HaveOccurred())
}()
_, err = file.WriteString(content)
Expect(err).ToNot(HaveOccurred())
err = file.Close()
Expect(err).ToNot(HaveOccurred())
// Configure the connection with methods call and then load the configuration file:
overridenAccess := DefaultToken("Bearer", 5*time.Minute)
overridenRefresh := DefaultToken("Refresh", 10*time.Hour)
connection, err := NewConnectionBuilder().
Logger(logger).
URL("https://overriden.my.server.com").
AlternativeURL("/api/clusters_mgmt", "https://overriden.your.server.com").
AlternativeURL("/api/accounts_mgmt", "https://overriden.her.server.com").
TokenURL("https://overriden.openid.server.com").
User("overriden.myuser", "overriden.mypassword").
Client("overriden.myclient", "overriden.mysecret").
Tokens(overridenAccess, overridenRefresh).
Scopes("openid", "overriden.myscope").
Insecure(false).
Agent("overriden.myagent").
Load(path).
Build()
Expect(err).ToNot(HaveOccurred())
// Check that the actual settings are the ones from the file:
Expect(connection.URL()).To(Equal("https://my.server.com"))
alternativeURLs := connection.AlternativeURLs()
Expect(alternativeURLs).To(HaveLen(2))
Expect(alternativeURLs).To(HaveKeyWithValue(
"/api/clusters_mgmt", "https://your.server.com",
))
Expect(alternativeURLs).To(HaveKeyWithValue(
"/api/accounts_mgmt", "https://her.server.com",
))
Expect(connection.TokenURL()).To(Equal("https://openid.server.com"))
user, password := connection.User()
Expect(user).To(Equal("myuser"))
Expect(password).To(Equal("mypassword"))
client, secret := connection.Client()
Expect(client).To(Equal("myclient"))
Expect(secret).To(Equal("mysecret"))
returnedAccess, returnedRefresh, err := connection.Tokens()
Expect(err).ToNot(HaveOccurred())
Expect(returnedAccess).To(Equal(overridenAccess))
Expect(returnedRefresh).To(Equal(overridenRefresh))
defer func() {
err = connection.Close()
Expect(err).ToNot(HaveOccurred())
}()
Expect(connection.Scopes()).To(ConsistOf("openid", "myscope"))
Expect(connection.Insecure()).To(BeTrue())
Expect(connection.Agent()).To(Equal("myagent"))
})
It("Returns configured URL when there are no alternative URLs", func() {
token := DefaultToken("Bearer", 5*time.Minute)
connection, err := NewConnectionBuilder().
Logger(logger).
URL("https://my.server.com").
Tokens(token).
Build()
Expect(err).ToNot(HaveOccurred())
Expect(connection.URL()).To(Equal("https://my.server.com"))
})
It("Returns configured URL when there are alternative URLs", func() {
token := DefaultToken("Bearer", 5*time.Minute)
connection, err := NewConnectionBuilder().
Logger(logger).
URL("https://my.server.com").
AlternativeURL("/api/clusters_mgmt", "https://your.server.com").
AlternativeURL("/api/accounts_mgmt", "https://her.server.com").
Tokens(token).
Build()
Expect(err).ToNot(HaveOccurred())
Expect(connection.URL()).To(Equal("https://my.server.com"))
})
})
type TestTransport struct {
called int
}
func (t *TestTransport) RoundTrip(request *http.Request) (response *http.Response, err error) {
t.called++
header := http.Header{}
header.Add("Content-type", "application/json")
response = &http.Response{
StatusCode: http.StatusInternalServerError,
Header: header,
Body: gbytes.BufferWithBytes([]byte("{}")),
}
return response, nil
}
func NewTestTransport() *TestTransport {
return &TestTransport{called: 0}
}
// This certificate is used only to verify that the connection can load a certificate from
// a file. It isn't valid for any other thing. It has been generated with the following
// commmad:
//
// openssl req \
// -x509 \
// -newkey rsa:4096 \
// -keyout myca.key \
// -nodes \
// -out myca.crt \
// -subj '/CN=myca.com' \
// -days 3650
var mycaPEM = []byte(`
-----BEGIN CERTIFICATE-----
MIIFCzCCAvOgAwIBAgIUEy+dp9sWPdu59sahER7AxvapYOgwD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6grbe+rcIY55IVcFKe
3ZfE7toa08gWV8kb9VP2KVNt5505jJUVtF8FxRxsu5W0x6b6Kegyotsd5/7tads9
9qMOoiWOyWxdP4FZalNM8PXaF8pspNqeo/cvgFWvDTqtFvgH4vkLehMueAWmDML2
lqYHCaMworpY3e8vfk6jK8b9fRmuXaGMlOTpY7XoF8OOSMI1LdPVSO/lo8DCJbge
RoBHZ97fA8ShB+WRjBuAuh5ST/TEqTha+razhmauVT6CYtw9SSC0SK0ZbNg2oZoG
CzrQhYf9gHXXPnp0qsuPbtHrMm3DPBHBUrfrlvVVmWjKCVipFdPyRXth9FtNguZn
d5NUX3JwGRoez/xHv8nHyjdKmTCu8pmP/9SAoV/HUgpcSaEtXyZdlNd+SgIcgV/A
00eSLesNr9/auzWklxh92oqDnd96IRueamFm6W0BCh64if//BCmAFelPHlWSP1ws
nzNA++GMw2OXJ1cE/9GTU3or0eDGRgB9XIu+T/SLPXW9xBm0hZdt5gyYfEDmppHa
nctMPznTWc+iYCMAwroHzJV40ZrVhllhNYrrOLigA7NfAiXelLmSbLx316TnoZM=
-----END CERTIFICATE-----
`)
// This certificate is used only to verify that the connection can load a certificate from
// a file. It isn't valid for any other thing. It has been generated with the following
// commmad:
//
// openssl req \
// -x509 \
// -newkey rsa:4096 \
// -keyout yourca.key \
// -nodes \
// -out yourca.crt \
// -subj '/CN=yourca.com' \
// -days 3650
var yourcaPEM = []byte(`
-----BEGIN CERTIFICATE-----
MIIFCzCCAvOgAwIBAgIUOBKDkme46UAOif9G7fIfN0FTmAswDQYJKoZIhvcNAQEL
BQAwFTETMBEGA1UEAwwKeW91cmNhLmNvbTAeFw0yMDExMTgxNjU0MTBaFw0zMDEx
MTYxNjU0MTBaMBUxEzARBgNVBAMMCnlvdXJjYS5jb20wggIiMA0GCSqGSIb3DQEB
AQUAA4ICDwAwggIKAoICAQCjCUIsLIE0gp7JQZHvwrl12IjYEQjJWEpsEbp/jpux
ztUAVju5Cq8+V5DYIzHi6WHlottpp6obh4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