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
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
|
# Copyright 2014 IBM Corp.
# All Rights Reserved.
#
# 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.
import copy
import datetime
import fixtures
import testtools
from tempest.lib import auth
from tempest.lib import exceptions
from tempest.lib.services.identity.v2 import token_client as v2_client
from tempest.lib.services.identity.v3 import token_client as v3_client
from tempest.tests import base
from tempest.tests.lib import fake_credentials
from tempest.tests.lib import fake_identity
def fake_get_credentials(fill_in=True, identity_version='v2', **kwargs):
return fake_credentials.FakeCredentials()
class BaseAuthTestsSetUp(base.TestCase):
_auth_provider_class = None
credentials = fake_credentials.FakeCredentials()
def _auth(self, credentials, auth_url, **params):
"""returns auth method according to keystone"""
return self._auth_provider_class(credentials, auth_url, **params)
def setUp(self):
super(BaseAuthTestsSetUp, self).setUp()
self.patchobject(auth, 'get_credentials', fake_get_credentials)
self.auth_provider = self._auth(self.credentials,
fake_identity.FAKE_AUTH_URL)
class TestBaseAuthProvider(BaseAuthTestsSetUp):
"""Tests for base AuthProvider
This tests auth.AuthProvider class which is base for the other so we
obviously don't test not implemented method or the ones which strongly
depends on them.
"""
class FakeAuthProviderImpl(auth.AuthProvider):
def _decorate_request(self):
pass
def _fill_credentials(self):
pass
def _get_auth(self):
pass
def base_url(self):
pass
def is_expired(self):
pass
_auth_provider_class = FakeAuthProviderImpl
def _auth(self, credentials, auth_url, **params):
"""returns auth method according to keystone"""
return self._auth_provider_class(credentials, **params)
def test_check_credentials_bad_type(self):
self.assertFalse(self.auth_provider.check_credentials([]))
def test_auth_data_property_when_cache_exists(self):
self.auth_provider.cache = 'foo'
self.useFixture(fixtures.MockPatchObject(self.auth_provider,
'is_expired',
return_value=False))
self.assertEqual('foo', getattr(self.auth_provider, 'auth_data'))
def test_delete_auth_data_property_through_deleter(self):
self.auth_provider.cache = 'foo'
del self.auth_provider.auth_data
self.assertIsNone(self.auth_provider.cache)
def test_delete_auth_data_property_through_clear_auth(self):
self.auth_provider.cache = 'foo'
self.auth_provider.clear_auth()
self.assertIsNone(self.auth_provider.cache)
def test_set_and_reset_alt_auth_data(self):
self.auth_provider.set_alt_auth_data('foo', 'bar')
self.assertEqual(self.auth_provider.alt_part, 'foo')
self.assertEqual(self.auth_provider.alt_auth_data, 'bar')
self.auth_provider.reset_alt_auth_data()
self.assertIsNone(self.auth_provider.alt_part)
self.assertIsNone(self.auth_provider.alt_auth_data)
def test_auth_class(self):
self.assertRaises(TypeError,
auth.AuthProvider,
fake_credentials.FakeCredentials)
class TestKeystoneV2AuthProvider(BaseAuthTestsSetUp):
_endpoints = fake_identity.IDENTITY_V2_RESPONSE['access']['serviceCatalog']
_auth_provider_class = auth.KeystoneV2AuthProvider
credentials = fake_credentials.FakeKeystoneV2Credentials()
def setUp(self):
super(TestKeystoneV2AuthProvider, self).setUp()
self.patchobject(v2_client.TokenClient, 'raw_request',
fake_identity._fake_v2_response)
self.target_url = 'test_api'
def _get_fake_identity(self):
return fake_identity.IDENTITY_V2_RESPONSE['access']
def _get_fake_alt_identity(self):
return fake_identity.ALT_IDENTITY_V2_RESPONSE['access']
def _get_result_url_from_endpoint(self, ep, endpoint_type='publicURL',
replacement=None):
if replacement:
return ep[endpoint_type].replace('v2', replacement)
return ep[endpoint_type]
def _get_token_from_fake_identity(self):
return fake_identity.TOKEN
def _get_from_fake_identity(self, attr):
access = fake_identity.IDENTITY_V2_RESPONSE['access']
if attr == 'user_id':
return access['user']['id']
elif attr == 'tenant_id':
return access['token']['tenant']['id']
def _test_request_helper(self, filters, expected):
url, headers, body = self.auth_provider.auth_request('GET',
self.target_url,
filters=filters)
self.assertEqual(expected['url'], url)
self.assertEqual(expected['token'], headers['X-Auth-Token'])
self.assertEqual(expected['body'], body)
def _auth_data_with_expiry(self, date_as_string):
token, access = self.auth_provider.auth_data
access['token']['expires'] = date_as_string
return token, access
def test_request(self):
filters = {
'service': 'compute',
'endpoint_type': 'publicURL',
'region': 'FakeRegion'
}
url = self._get_result_url_from_endpoint(
self._endpoints[0]['endpoints'][1]) + '/' + self.target_url
expected = {
'body': None,
'url': url,
'token': self._get_token_from_fake_identity(),
}
self._test_request_helper(filters, expected)
def test_request_with_alt_auth_cleans_alt(self):
"""Test alternate auth data for headers
Assert that when the alt data is provided for headers, after an
auth_request the data alt_data is cleaned-up.
"""
self.auth_provider.set_alt_auth_data(
'headers',
(fake_identity.ALT_TOKEN, self._get_fake_alt_identity()))
filters = {
'service': 'compute',
'endpoint_type': 'publicURL',
'region': 'fakeRegion'
}
self.auth_provider.auth_request('GET', self.target_url,
filters=filters)
# Assert alt auth data is clear after it
self.assertIsNone(self.auth_provider.alt_part)
self.assertIsNone(self.auth_provider.alt_auth_data)
def _test_request_with_identical_alt_auth(self, part):
"""Test alternate but identical auth data for headers
Assert that when the alt data is provided, but it's actually
identical, an exception is raised.
"""
self.auth_provider.set_alt_auth_data(
part,
(fake_identity.TOKEN, self._get_fake_identity()))
filters = {
'service': 'compute',
'endpoint_type': 'publicURL',
'region': 'fakeRegion'
}
self.assertRaises(exceptions.BadAltAuth,
self.auth_provider.auth_request,
'GET', self.target_url, filters=filters)
def test_request_with_identical_alt_auth_headers(self):
self._test_request_with_identical_alt_auth('headers')
def test_request_with_identical_alt_auth_url(self):
self._test_request_with_identical_alt_auth('url')
def test_request_with_identical_alt_auth_body(self):
self._test_request_with_identical_alt_auth('body')
def test_request_with_alt_part_without_alt_data(self):
"""Test empty alternate auth data
Assert that when alt_part is defined, the corresponding original
request element is kept the same.
"""
filters = {
'service': 'compute',
'endpoint_type': 'publicURL',
'region': 'fakeRegion'
}
self.auth_provider.set_alt_auth_data('headers', None)
url, headers, body = self.auth_provider.auth_request('GET',
self.target_url,
filters=filters)
# The original headers where empty
self.assertNotEqual(url, self.target_url)
self.assertIsNone(headers)
self.assertIsNone(body)
def _test_request_with_alt_part_without_alt_data_no_change(self, body):
"""Test empty alternate auth data with no effect
Assert that when alt_part is defined, no auth_data is provided,
and the corresponding original request element was not going to
be changed anyways, and exception is raised
"""
filters = {
'service': 'compute',
'endpoint_type': 'publicURL',
'region': 'fakeRegion'
}
self.auth_provider.set_alt_auth_data('body', None)
self.assertRaises(exceptions.BadAltAuth,
self.auth_provider.auth_request,
'GET', self.target_url, filters=filters)
def test_request_with_alt_part_without_alt_data_no_change_headers(self):
self._test_request_with_alt_part_without_alt_data_no_change('headers')
def test_request_with_alt_part_without_alt_data_no_change_url(self):
self._test_request_with_alt_part_without_alt_data_no_change('url')
def test_request_with_alt_part_without_alt_data_no_change_body(self):
self._test_request_with_alt_part_without_alt_data_no_change('body')
def test_request_with_bad_service(self):
filters = {
'service': 'BAD_SERVICE',
'endpoint_type': 'publicURL',
'region': 'fakeRegion'
}
self.assertRaises(exceptions.EndpointNotFound,
self.auth_provider.auth_request, 'GET',
self.target_url, filters=filters)
def test_request_without_service(self):
filters = {
'service': None,
'endpoint_type': 'publicURL',
'region': 'fakeRegion'
}
self.assertRaises(exceptions.EndpointNotFound,
self.auth_provider.auth_request, 'GET',
self.target_url, filters=filters)
def test_check_credentials_missing_attribute(self):
for attr in ['username', 'password']:
cred = copy.copy(self.credentials)
del cred[attr]
self.assertFalse(self.auth_provider.check_credentials(cred))
def test_fill_credentials(self):
self.auth_provider.fill_credentials()
creds = self.auth_provider.credentials
for attr in ['user_id', 'tenant_id']:
self.assertEqual(self._get_from_fake_identity(attr),
getattr(creds, attr))
def _test_base_url_helper(self, expected_url, filters,
auth_data=None):
url = self.auth_provider.base_url(filters, auth_data)
self.assertEqual(url, expected_url)
def test_base_url(self):
self.filters = {
'service': 'compute',
'endpoint_type': 'publicURL',
'region': 'FakeRegion'
}
expected = self._get_result_url_from_endpoint(
self._endpoints[0]['endpoints'][1])
self._test_base_url_helper(expected, self.filters)
def test_base_url_to_get_admin_endpoint(self):
self.filters = {
'service': 'compute',
'endpoint_type': 'adminURL',
'region': 'FakeRegion'
}
expected = self._get_result_url_from_endpoint(
self._endpoints[0]['endpoints'][1], endpoint_type='adminURL')
self._test_base_url_helper(expected, self.filters)
def test_base_url_unknown_region(self):
"""If the region is unknown, the first endpoint is returned."""
self.filters = {
'service': 'compute',
'endpoint_type': 'publicURL',
'region': 'AintNoBodyKnowThisRegion'
}
expected = self._get_result_url_from_endpoint(
self._endpoints[0]['endpoints'][0])
self._test_base_url_helper(expected, self.filters)
def test_base_url_with_non_existent_service(self):
self.filters = {
'service': 'BAD_SERVICE',
'endpoint_type': 'publicURL',
'region': 'FakeRegion'
}
self.assertRaises(exceptions.EndpointNotFound,
self._test_base_url_helper, None, self.filters)
def test_base_url_without_service(self):
self.filters = {
'endpoint_type': 'publicURL',
'region': 'FakeRegion'
}
self.assertRaises(exceptions.EndpointNotFound,
self._test_base_url_helper, None, self.filters)
def test_base_url_with_known_name(self):
"""If name and service is known, return the endpoint."""
self.filters = {
'service': 'compute',
'endpoint_type': 'publicURL',
'region': 'FakeRegion',
'name': 'nova'
}
expected = self._get_result_url_from_endpoint(
self._endpoints[0]['endpoints'][1])
self._test_base_url_helper(expected, self.filters)
def test_base_url_with_known_name_and_unknown_servce(self):
"""Test with Known Name and Unknown service
If the name is known but the service is unknown, raise an exception.
"""
self.filters = {
'service': 'AintNoBodyKnowThatService',
'endpoint_type': 'publicURL',
'region': 'FakeRegion',
'name': 'AintNoBodyKnowThatName'
}
self.assertRaises(exceptions.EndpointNotFound,
self._test_base_url_helper, None, self.filters)
def test_base_url_with_unknown_name_and_known_service(self):
"""Test with Unknown Name and Known Service
If the name is unknown, raise an exception. Note that filtering by
name is only successful service exists.
"""
self.filters = {
'service': 'compute',
'endpoint_type': 'publicURL',
'region': 'FakeRegion',
'name': 'AintNoBodyKnowThatName'
}
self.assertRaises(exceptions.EndpointNotFound,
self._test_base_url_helper, None, self.filters)
def test_base_url_without_name(self):
self.filters = {
'service': 'compute',
'endpoint_type': 'publicURL',
'region': 'FakeRegion',
}
expected = self._get_result_url_from_endpoint(
self._endpoints[0]['endpoints'][1])
self._test_base_url_helper(expected, self.filters)
def test_base_url_with_api_version_filter(self):
self.filters = {
'service': 'compute',
'endpoint_type': 'publicURL',
'region': 'FakeRegion',
'api_version': 'v12'
}
expected = self._get_result_url_from_endpoint(
self._endpoints[0]['endpoints'][1], replacement='v12')
self._test_base_url_helper(expected, self.filters)
def test_base_url_with_skip_path_filter(self):
self.filters = {
'service': 'compute',
'endpoint_type': 'publicURL',
'region': 'FakeRegion',
'skip_path': True
}
expected = 'http://fake_url/'
self._test_base_url_helper(expected, self.filters)
def test_base_url_with_unversioned_endpoint(self):
auth_data = {
'serviceCatalog': [
{
'type': 'identity',
'endpoints': [
{
'region': 'FakeRegion',
'publicURL': 'http://fake_url'
}
]
}
]
}
filters = {
'service': 'identity',
'endpoint_type': 'publicURL',
'region': 'FakeRegion',
'api_version': 'v2.0'
}
expected = 'http://fake_url/v2.0'
self._test_base_url_helper(expected, filters, ('token', auth_data))
def test_base_url_with_extra_path_endpoint(self):
auth_data = {
'serviceCatalog': [
{
'type': 'compute',
'endpoints': [
{
'region': 'FakeRegion',
'publicURL': 'http://fake_url/some_path/v2.0'
}
]
}
]
}
filters = {
'service': 'compute',
'endpoint_type': 'publicURL',
'region': 'FakeRegion',
'api_version': 'v2.0'
}
expected = 'http://fake_url/some_path/v2.0'
self._test_base_url_helper(expected, filters, ('token', auth_data))
def test_base_url_with_unversioned_extra_path_endpoint(self):
auth_data = {
'serviceCatalog': [
{
'type': 'compute',
'endpoints': [
{
'region': 'FakeRegion',
'publicURL': 'http://fake_url/some_path'
}
]
}
]
}
filters = {
'service': 'compute',
'endpoint_type': 'publicURL',
'region': 'FakeRegion',
'api_version': 'v2.0'
}
expected = 'http://fake_url/some_path/v2.0'
self._test_base_url_helper(expected, filters, ('token', auth_data))
def test_token_not_expired(self):
expiry_data = datetime.datetime.utcnow() + datetime.timedelta(days=1)
self._verify_expiry(expiry_data=expiry_data, should_be_expired=False)
def test_token_expired(self):
expiry_data = datetime.datetime.utcnow() - datetime.timedelta(hours=1)
self._verify_expiry(expiry_data=expiry_data, should_be_expired=True)
def test_token_not_expired_to_be_renewed(self):
expiry_data = (datetime.datetime.utcnow() +
self.auth_provider.token_expiry_threshold / 2)
self._verify_expiry(expiry_data=expiry_data, should_be_expired=True)
def _verify_expiry(self, expiry_data, should_be_expired):
for expiry_format in self.auth_provider.EXPIRY_DATE_FORMATS:
auth_data = self._auth_data_with_expiry(
expiry_data.strftime(expiry_format))
self.assertEqual(self.auth_provider.is_expired(auth_data),
should_be_expired)
def test_set_scope_all_valid(self):
for scope in self.auth_provider.SCOPES:
self.auth_provider.scope = scope
self.assertEqual(scope, self.auth_provider.scope)
def test_set_scope_invalid(self):
with testtools.ExpectedException(exceptions.InvalidScope,
'.* invalid_scope .*'):
self.auth_provider.scope = 'invalid_scope'
class TestKeystoneV3AuthProvider(TestKeystoneV2AuthProvider):
_endpoints = fake_identity.IDENTITY_V3_RESPONSE['token']['catalog']
_auth_provider_class = auth.KeystoneV3AuthProvider
credentials = fake_credentials.FakeKeystoneV3Credentials()
def setUp(self):
super(TestKeystoneV3AuthProvider, self).setUp()
self.patchobject(v3_client.V3TokenClient, 'raw_request',
fake_identity._fake_v3_response)
def _get_fake_identity(self):
return fake_identity.IDENTITY_V3_RESPONSE['token']
def _get_fake_alt_identity(self):
return fake_identity.ALT_IDENTITY_V3['token']
def _get_result_url_from_endpoint(self, ep, replacement=None):
if replacement:
return ep['url'].replace('v3', replacement)
return ep['url']
def _auth_data_with_expiry(self, date_as_string):
token, access = self.auth_provider.auth_data
access['expires_at'] = date_as_string
return token, access
def _get_from_fake_identity(self, attr):
token = fake_identity.IDENTITY_V3_RESPONSE['token']
if attr == 'user_id':
return token['user']['id']
elif attr == 'project_id':
return token['project']['id']
elif attr == 'user_domain_id':
return token['user']['domain']['id']
elif attr == 'project_domain_id':
return token['project']['domain']['id']
def test_check_credentials_missing_attribute(self):
# reset credentials to fresh ones
self.credentials.reset()
for attr in ['username', 'password', 'user_domain_name',
'project_domain_name']:
cred = copy.copy(self.credentials)
del cred[attr]
self.assertFalse(self.auth_provider.check_credentials(cred),
"Credentials should be invalid without %s" % attr)
def test_check_domain_credentials_missing_attribute(self):
# reset credentials to fresh ones
self.credentials.reset()
domain_creds = fake_credentials.FakeKeystoneV3DomainCredentials()
for attr in ['username', 'password', 'user_domain_name']:
cred = copy.copy(domain_creds)
del cred[attr]
self.assertFalse(self.auth_provider.check_credentials(cred),
"Credentials should be invalid without %s" % attr)
def test_fill_credentials(self):
self.auth_provider.fill_credentials()
creds = self.auth_provider.credentials
for attr in ['user_id', 'project_id', 'user_domain_id',
'project_domain_id']:
self.assertEqual(self._get_from_fake_identity(attr),
getattr(creds, attr))
# Overwrites v2 test
def test_base_url_to_get_admin_endpoint(self):
self.filters = {
'service': 'compute',
'endpoint_type': 'admin',
'region': 'MiddleEarthRegion'
}
expected = self._get_result_url_from_endpoint(
self._endpoints[0]['endpoints'][2])
self._test_base_url_helper(expected, self.filters)
# Overwrites v2 test
def test_base_url_with_unversioned_endpoint(self):
auth_data = {
'catalog': [
{
'type': 'identity',
'endpoints': [
{
'region': 'FakeRegion',
'url': 'http://fake_url',
'interface': 'public'
}
]
}
]
}
filters = {
'service': 'identity',
'endpoint_type': 'publicURL',
'region': 'FakeRegion',
'api_version': 'v3'
}
expected = 'http://fake_url/v3'
self._test_base_url_helper(expected, filters, ('token', auth_data))
def test_base_url_with_extra_path_endpoint(self):
auth_data = {
'catalog': [
{
'type': 'compute',
'endpoints': [
{
'region': 'FakeRegion',
'url': 'http://fake_url/some_path/v2.0',
'interface': 'public'
}
]
}
]
}
filters = {
'service': 'compute',
'endpoint_type': 'publicURL',
'region': 'FakeRegion',
'api_version': 'v2.0'
}
expected = 'http://fake_url/some_path/v2.0'
self._test_base_url_helper(expected, filters, ('token', auth_data))
def test_base_url_with_unversioned_extra_path_endpoint(self):
auth_data = {
'catalog': [
{
'type': 'compute',
'endpoints': [
{
'region': 'FakeRegion',
'url': 'http://fake_url/some_path',
'interface': 'public'
}
]
}
]
}
filters = {
'service': 'compute',
'endpoint_type': 'publicURL',
'region': 'FakeRegion',
'api_version': 'v2.0'
}
expected = 'http://fake_url/some_path/v2.0'
self._test_base_url_helper(expected, filters, ('token', auth_data))
# Base URL test with scope only for V3
def test_base_url_scope_project(self):
self.auth_provider.scope = 'project'
self.filters = {
'service': 'compute',
'endpoint_type': 'publicURL',
'region': 'FakeRegion'
}
expected = self._get_result_url_from_endpoint(
self._endpoints[0]['endpoints'][1])
self._test_base_url_helper(expected, self.filters)
# Base URL test with scope only for V3
def test_base_url_unscoped_identity(self):
self.auth_provider.scope = 'unscoped'
self.patchobject(v3_client.V3TokenClient, 'raw_request',
fake_identity._fake_v3_response_no_scope)
self.filters = {
'service': 'identity',
'endpoint_type': 'publicURL',
'region': 'FakeRegion'
}
expected = fake_identity.FAKE_AUTH_URL
self._test_base_url_helper(expected, self.filters)
# Base URL test with scope only for V3
def test_base_url_unscoped_other(self):
self.auth_provider.scope = 'unscoped'
self.patchobject(v3_client.V3TokenClient, 'raw_request',
fake_identity._fake_v3_response_no_scope)
self.filters = {
'service': 'compute',
'endpoint_type': 'publicURL',
'region': 'FakeRegion'
}
self.assertRaises(exceptions.EndpointNotFound,
self.auth_provider.base_url,
auth_data=self.auth_provider.auth_data,
filters=self.filters)
def test_auth_parameters_with_scope_unset(self):
# No scope defaults to 'project'
all_creds = fake_credentials.FakeKeystoneV3AllCredentials()
self.auth_provider.credentials = all_creds
auth_params = self.auth_provider._auth_params()
self.assertNotIn('scope', auth_params.keys())
for attr in all_creds.get_init_attributes():
if attr.startswith('domain_'):
self.assertNotIn(attr, auth_params.keys())
else:
self.assertIn(attr, auth_params.keys())
self.assertEqual(getattr(all_creds, attr), auth_params[attr])
def test_auth_parameters_with_project_scope(self):
all_creds = fake_credentials.FakeKeystoneV3AllCredentials()
self.auth_provider.credentials = all_creds
self.auth_provider.scope = 'project'
auth_params = self.auth_provider._auth_params()
self.assertNotIn('scope', auth_params.keys())
for attr in all_creds.get_init_attributes():
if attr.startswith('domain_'):
self.assertNotIn(attr, auth_params.keys())
else:
self.assertIn(attr, auth_params.keys())
self.assertEqual(getattr(all_creds, attr), auth_params[attr])
def test_auth_parameters_with_domain_scope(self):
all_creds = fake_credentials.FakeKeystoneV3AllCredentials()
self.auth_provider.credentials = all_creds
self.auth_provider.scope = 'domain'
auth_params = self.auth_provider._auth_params()
self.assertNotIn('scope', auth_params.keys())
for attr in all_creds.get_init_attributes():
if attr.startswith('project_'):
self.assertNotIn(attr, auth_params.keys())
else:
self.assertIn(attr, auth_params.keys())
self.assertEqual(getattr(all_creds, attr), auth_params[attr])
def test_auth_parameters_unscoped(self):
all_creds = fake_credentials.FakeKeystoneV3AllCredentials()
self.auth_provider.credentials = all_creds
self.auth_provider.scope = 'unscoped'
auth_params = self.auth_provider._auth_params()
self.assertNotIn('scope', auth_params.keys())
for attr in all_creds.get_init_attributes():
if attr.startswith('project_') or attr.startswith('domain_'):
self.assertNotIn(attr, auth_params.keys())
else:
self.assertIn(attr, auth_params.keys())
self.assertEqual(getattr(all_creds, attr), auth_params[attr])
def test_auth_parameters_with_system_scope(self):
all_creds = fake_credentials.FakeKeystoneV3AllCredentials()
self.auth_provider.credentials = all_creds
self.auth_provider.scope = 'system'
auth_params = self.auth_provider._auth_params()
self.assertNotIn('scope', auth_params.keys())
for attr in all_creds.get_init_attributes():
if attr.startswith('project_') or attr.startswith('domain_'):
self.assertNotIn(attr, auth_params.keys())
else:
self.assertIn(attr, auth_params.keys())
self.assertEqual(getattr(all_creds, attr), auth_params[attr])
class TestKeystoneV3Credentials(base.TestCase):
def testSetAttrUserDomain(self):
creds = auth.KeystoneV3Credentials()
creds.user_domain_name = 'user_domain'
creds.domain_name = 'domain'
self.assertEqual('user_domain', creds.user_domain_name)
creds = auth.KeystoneV3Credentials()
creds.domain_name = 'domain'
creds.user_domain_name = 'user_domain'
self.assertEqual('user_domain', creds.user_domain_name)
def testSetAttrProjectDomain(self):
creds = auth.KeystoneV3Credentials()
creds.project_domain_name = 'project_domain'
creds.domain_name = 'domain'
self.assertEqual('project_domain', creds.user_domain_name)
creds = auth.KeystoneV3Credentials()
creds.domain_name = 'domain'
creds.project_domain_name = 'project_domain'
self.assertEqual('project_domain', creds.project_domain_name)
def testProjectTenantNoCollision(self):
creds = auth.KeystoneV3Credentials(tenant_id='tenant')
self.assertEqual('tenant', creds.project_id)
creds = auth.KeystoneV3Credentials(project_id='project')
self.assertEqual('project', creds.tenant_id)
creds = auth.KeystoneV3Credentials(tenant_name='tenant')
self.assertEqual('tenant', creds.project_name)
creds = auth.KeystoneV3Credentials(project_name='project')
self.assertEqual('project', creds.tenant_name)
def testProjectTenantCollision(self):
attrs = {'tenant_id': 'tenant', 'project_id': 'project'}
self.assertRaises(
exceptions.InvalidCredentials, auth.KeystoneV3Credentials, **attrs)
attrs = {'tenant_name': 'tenant', 'project_name': 'project'}
self.assertRaises(
exceptions.InvalidCredentials, auth.KeystoneV3Credentials, **attrs)
class TestReplaceVersion(base.TestCase):
def test_version_no_trailing_path(self):
self.assertEqual(
'http://localhost:35357/v2.0',
auth.replace_version('http://localhost:35357/v3', 'v2.0'))
def test_version_no_trailing_path_solidus(self):
self.assertEqual(
'http://localhost:35357/v2.0/',
auth.replace_version('http://localhost:35357/v3/', 'v2.0'))
def test_version_trailing_path(self):
self.assertEqual(
'http://localhost:35357/v2.0/uuid',
auth.replace_version('http://localhost:35357/v3/uuid', 'v2.0'))
def test_version_trailing_path_solidus(self):
self.assertEqual(
'http://localhost:35357/v2.0/uuid/',
auth.replace_version('http://localhost:35357/v3/uuid/', 'v2.0'))
def test_no_version_base(self):
self.assertEqual(
'http://localhost:35357/v2.0',
auth.replace_version('http://localhost:35357', 'v2.0'))
def test_no_version_base_solidus(self):
self.assertEqual(
'http://localhost:35357/v2.0',
auth.replace_version('http://localhost:35357/', 'v2.0'))
def test_no_version_path(self):
self.assertEqual(
'http://localhost/identity/v2.0',
auth.replace_version('http://localhost/identity', 'v2.0'))
def test_no_version_path_solidus(self):
self.assertEqual(
'http://localhost/identity/v2.0',
auth.replace_version('http://localhost/identity/', 'v2.0'))
def test_path_version(self):
self.assertEqual(
'http://localhost/identity/v2.0',
auth.replace_version('http://localhost/identity/v3', 'v2.0'))
def test_path_version_solidus(self):
self.assertEqual(
'http://localhost/identity/v2.0/',
auth.replace_version('http://localhost/identity/v3/', 'v2.0'))
def test_path_version_trailing_path(self):
self.assertEqual(
'http://localhost/identity/v2.0/uuid',
auth.replace_version('http://localhost/identity/v3/uuid', 'v2.0'))
def test_path_version_trailing_path_solidus(self):
self.assertEqual(
'http://localhost/identity/v2.0/uuid/',
auth.replace_version('http://localhost/identity/v3/uuid/', 'v2.0'))
class TestKeystoneV3AuthProvider_DomainScope(BaseAuthTestsSetUp):
_endpoints = fake_identity.IDENTITY_V3_RESPONSE['token']['catalog']
_auth_provider_class = auth.KeystoneV3AuthProvider
credentials = fake_credentials.FakeKeystoneV3Credentials()
def setUp(self):
super(TestKeystoneV3AuthProvider_DomainScope, self).setUp()
self.patchobject(v3_client.V3TokenClient, 'raw_request',
fake_identity._fake_v3_response_domain_scope)
def test_get_auth_with_domain_scope(self):
self.auth_provider.scope = 'domain'
_, auth_data = self.auth_provider.get_auth()
self.assertIn('domain', auth_data)
self.assertNotIn('project', auth_data)
class TestGetCredentials(base.TestCase):
def test_invalid_identity_version(self):
with testtools.ExpectedException(exceptions.InvalidIdentityVersion,
'.* v1 .*'):
auth.get_credentials('http://localhost/identity/v3',
identity_version='v1')
|