/
test_v3_assignment.py
4043 lines (3380 loc) · 172 KB
/
test_v3_assignment.py
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
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# 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 datetime
import random
import uuid
import freezegun
import http.client
from testtools import matchers
from keystone.common import provider_api
import keystone.conf
from keystone import exception
from keystone.resource.backends import base as resource_base
from keystone.tests import unit
from keystone.tests.unit import test_v3
CONF = keystone.conf.CONF
PROVIDERS = provider_api.ProviderAPIs
class SystemRoleAssignmentMixin(object):
def _create_new_role(self):
"""Create a role available for use anywhere and return the ID."""
ref = unit.new_role_ref()
response = self.post('/roles', body={'role': ref})
# We only really need the role ID, so omit the rest of the response and
# return the ID of the role we just created.
return response.json_body['role']['id']
def _create_group(self):
body = {
'group': {
'domain_id': self.domain_id,
'name': uuid.uuid4().hex
}
}
response = self.post('/groups/', body=body)
return response.json_body['group']
def _create_user(self):
body = {
'user': {
'domain_id': self.domain_id,
'name': uuid.uuid4().hex
}
}
response = self.post('/users/', body=body)
return response.json_body['user']
class AssignmentTestCase(test_v3.RestfulTestCase,
test_v3.AssignmentTestMixin,
SystemRoleAssignmentMixin):
"""Test roles and role assignments."""
def setUp(self):
super(AssignmentTestCase, self).setUp()
self.group = unit.new_group_ref(domain_id=self.domain_id)
self.group = PROVIDERS.identity_api.create_group(self.group)
self.group_id = self.group['id']
# Role CRUD tests
def test_create_role(self):
"""Call ``POST /roles``."""
ref = unit.new_role_ref()
r = self.post(
'/roles',
body={'role': ref})
return self.assertValidRoleResponse(r, ref)
def test_create_role_bad_request(self):
"""Call ``POST /roles``."""
self.post('/roles', body={'role': {}},
expected_status=http.client.BAD_REQUEST)
def test_list_head_roles(self):
"""Call ``GET & HEAD /roles``."""
resource_url = '/roles'
r = self.get(resource_url)
self.assertValidRoleListResponse(r, ref=self.role,
resource_url=resource_url)
self.head(resource_url, expected_status=http.client.OK)
def test_get_head_role(self):
"""Call ``GET & HEAD /roles/{role_id}``."""
resource_url = '/roles/%(role_id)s' % {
'role_id': self.role_id}
r = self.get(resource_url)
self.assertValidRoleResponse(r, self.role)
self.head(resource_url, expected_status=http.client.OK)
def test_update_role(self):
"""Call ``PATCH /roles/{role_id}``."""
ref = unit.new_role_ref()
del ref['id']
r = self.patch('/roles/%(role_id)s' % {
'role_id': self.role_id},
body={'role': ref})
self.assertValidRoleResponse(r, ref)
def test_delete_role(self):
"""Call ``DELETE /roles/{role_id}``."""
self.delete('/roles/%(role_id)s' % {
'role_id': self.role_id})
# Role Grants tests
def test_crud_user_project_role_grants(self):
role = unit.new_role_ref()
PROVIDERS.role_api.create_role(role['id'], role)
collection_url = (
'/projects/%(project_id)s/users/%(user_id)s/roles' % {
'project_id': self.project['id'],
'user_id': self.user['id']})
member_url = '%(collection_url)s/%(role_id)s' % {
'collection_url': collection_url,
'role_id': role['id']}
# There is a role assignment for self.user on self.project
r = self.get(collection_url)
self.assertValidRoleListResponse(r, ref=self.role,
expected_length=1)
self.put(member_url)
self.head(member_url)
self.get(member_url, expected_status=http.client.NO_CONTENT)
r = self.get(collection_url)
self.assertValidRoleListResponse(r, ref=role,
resource_url=collection_url,
expected_length=2)
self.head(collection_url, expected_status=http.client.OK)
self.delete(member_url)
r = self.get(collection_url)
self.assertValidRoleListResponse(r, ref=self.role, expected_length=1)
self.assertIn(collection_url, r.result['links']['self'])
self.head(collection_url, expected_status=http.client.OK)
def test_crud_user_project_role_grants_no_user(self):
"""Grant role on a project to a user that doesn't exist.
When grant a role on a project to a user that doesn't exist, the server
returns Not Found for the user.
"""
user_id = uuid.uuid4().hex
collection_url = (
'/projects/%(project_id)s/users/%(user_id)s/roles' % {
'project_id': self.project['id'], 'user_id': user_id})
member_url = '%(collection_url)s/%(role_id)s' % {
'collection_url': collection_url,
'role_id': self.role_id}
self.put(member_url, expected_status=http.client.NOT_FOUND)
self.head(member_url, expected_status=http.client.NOT_FOUND)
self.get(member_url, expected_status=http.client.NOT_FOUND)
def test_crud_user_domain_role_grants(self):
time = datetime.datetime.utcnow()
with freezegun.freeze_time(time) as frozen_datetime:
collection_url = (
'/domains/%(domain_id)s/users/%(user_id)s/roles' % {
'domain_id': self.domain_id,
'user_id': self.user['id']})
member_url = '%(collection_url)s/%(role_id)s' % {
'collection_url': collection_url,
'role_id': self.role_id}
self.put(member_url)
self.head(member_url)
self.get(member_url, expected_status=http.client.NO_CONTENT)
r = self.get(collection_url)
self.assertValidRoleListResponse(r, ref=self.role,
resource_url=collection_url)
self.head(collection_url, expected_status=http.client.OK)
self.delete(member_url)
# NOTE(lbragstad): Make sure we wait a second before we ask for the
# roles. This ensures the token we use isn't considered revoked
# because it was issued within the same second as a revocation
# event.
frozen_datetime.tick(delta=datetime.timedelta(seconds=1))
r = self.get(collection_url)
self.assertValidRoleListResponse(r, expected_length=0,
resource_url=collection_url)
self.head(collection_url, expected_status=http.client.OK)
def test_crud_user_domain_role_grants_no_user(self):
"""Grant role on a domain to a user that doesn't exist.
When grant a role on a domain to a user that doesn't exist, the server
returns 404 Not Found for the user.
"""
user_id = uuid.uuid4().hex
collection_url = (
'/domains/%(domain_id)s/users/%(user_id)s/roles' % {
'domain_id': self.domain_id, 'user_id': user_id})
member_url = '%(collection_url)s/%(role_id)s' % {
'collection_url': collection_url,
'role_id': self.role_id}
self.put(member_url, expected_status=http.client.NOT_FOUND)
self.head(member_url, expected_status=http.client.NOT_FOUND)
self.get(member_url, expected_status=http.client.NOT_FOUND)
def test_crud_group_project_role_grants(self):
time = datetime.datetime.utcnow()
with freezegun.freeze_time(time) as frozen_datetime:
collection_url = (
'/projects/%(project_id)s/groups/%(group_id)s/roles' % {
'project_id': self.project_id,
'group_id': self.group_id})
member_url = '%(collection_url)s/%(role_id)s' % {
'collection_url': collection_url,
'role_id': self.role_id}
self.put(member_url)
self.head(member_url)
self.get(member_url, expected_status=http.client.NO_CONTENT)
r = self.get(collection_url)
self.assertValidRoleListResponse(r, ref=self.role,
resource_url=collection_url)
self.head(collection_url, expected_status=http.client.OK)
self.delete(member_url)
# NOTE(lbragstad): Make sure we wait a second before we ask for the
# roles. This ensures the token we use isn't considered revoked
# because it was issued within the same second as a revocation
# event.
frozen_datetime.tick(delta=datetime.timedelta(seconds=1))
r = self.get(collection_url)
self.assertValidRoleListResponse(r, expected_length=0,
resource_url=collection_url)
self.head(collection_url, expected_status=http.client.OK)
def test_crud_group_project_role_grants_no_group(self):
"""Grant role on a project to a group that doesn't exist.
When grant a role on a project to a group that doesn't exist, the
server returns 404 Not Found for the group.
"""
group_id = uuid.uuid4().hex
collection_url = (
'/projects/%(project_id)s/groups/%(group_id)s/roles' % {
'project_id': self.project_id,
'group_id': group_id})
member_url = '%(collection_url)s/%(role_id)s' % {
'collection_url': collection_url,
'role_id': self.role_id}
self.put(member_url, expected_status=http.client.NOT_FOUND)
self.head(member_url, expected_status=http.client.NOT_FOUND)
self.get(member_url, expected_status=http.client.NOT_FOUND)
def test_crud_group_domain_role_grants(self):
time = datetime.datetime.utcnow()
with freezegun.freeze_time(time) as frozen_datetime:
collection_url = (
'/domains/%(domain_id)s/groups/%(group_id)s/roles' % {
'domain_id': self.domain_id,
'group_id': self.group_id})
member_url = '%(collection_url)s/%(role_id)s' % {
'collection_url': collection_url,
'role_id': self.role_id}
self.put(member_url)
self.head(member_url)
self.get(member_url, expected_status=http.client.NO_CONTENT)
r = self.get(collection_url)
self.assertValidRoleListResponse(r, ref=self.role,
resource_url=collection_url)
self.head(collection_url, expected_status=http.client.OK)
self.delete(member_url)
# NOTE(lbragstad): Make sure we wait a second before we ask for the
# roles. This ensures the token we use isn't considered revoked
# because it was issued within the same second as a revocation
# event.
frozen_datetime.tick(delta=datetime.timedelta(seconds=1))
r = self.get(collection_url)
self.assertValidRoleListResponse(r, expected_length=0,
resource_url=collection_url)
self.head(collection_url, expected_status=http.client.OK)
def test_crud_group_domain_role_grants_no_group(self):
"""Grant role on a domain to a group that doesn't exist.
When grant a role on a domain to a group that doesn't exist, the server
returns 404 Not Found for the group.
"""
group_id = uuid.uuid4().hex
collection_url = (
'/domains/%(domain_id)s/groups/%(group_id)s/roles' % {
'domain_id': self.domain_id,
'group_id': group_id})
member_url = '%(collection_url)s/%(role_id)s' % {
'collection_url': collection_url,
'role_id': self.role_id}
self.put(member_url, expected_status=http.client.NOT_FOUND)
self.head(member_url, expected_status=http.client.NOT_FOUND)
self.get(member_url, expected_status=http.client.NOT_FOUND)
def _create_new_user_and_assign_role_on_project(self):
"""Create a new user and assign user a role on a project."""
# Create a new user
new_user = unit.new_user_ref(domain_id=self.domain_id)
user_ref = PROVIDERS.identity_api.create_user(new_user)
# Assign the user a role on the project
collection_url = (
'/projects/%(project_id)s/users/%(user_id)s/roles' % {
'project_id': self.project_id,
'user_id': user_ref['id']})
member_url = ('%(collection_url)s/%(role_id)s' % {
'collection_url': collection_url,
'role_id': self.role_id})
self.put(member_url)
# Check the user has the role assigned
self.head(member_url)
self.get(member_url, expected_status=http.client.NO_CONTENT)
return member_url, user_ref
def test_delete_user_before_removing_role_assignment_succeeds(self):
"""Call ``DELETE`` on the user before the role assignment."""
member_url, user = self._create_new_user_and_assign_role_on_project()
# Delete the user from identity backend
PROVIDERS.identity_api.driver.delete_user(user['id'])
# Clean up the role assignment
self.delete(member_url)
# Make sure the role is gone
self.head(member_url, expected_status=http.client.NOT_FOUND)
def test_delete_group_before_removing_role_assignment_succeeds(self):
# Disable the cache so that we perform a fresh check of the identity
# backend when attempting to remove the role assignment.
self.config_fixture.config(group='cache', enabled=False)
# Create a new group
group = unit.new_group_ref(domain_id=self.domain_id)
group_ref = PROVIDERS.identity_api.create_group(group)
# Assign the user a role on the project
collection_url = (
'/projects/%(project_id)s/groups/%(group_id)s/roles' % {
'project_id': self.project_id,
'group_id': group_ref['id']})
member_url = ('%(collection_url)s/%(role_id)s' % {
'collection_url': collection_url,
'role_id': self.role_id})
self.put(member_url)
# Check the user has the role assigned
self.head(member_url)
self.get(member_url, expected_status=http.client.NO_CONTENT)
# Simulate removing the group via LDAP by directly removing it from the
# identity backend.
PROVIDERS.identity_api.driver.delete_group(group_ref['id'])
# Ensure we can clean up the role assignment even though the group
# doesn't exist
self.delete(member_url)
def test_delete_user_before_removing_system_assignments_succeeds(self):
system_role = self._create_new_role()
user = self._create_user()
path = (
'/system/users/%(user_id)s/roles/%(role_id)s' %
{'user_id': user['id'], 'role_id': system_role}
)
self.put(path)
response = self.get('/role_assignments')
number_of_assignments = len(response.json_body['role_assignments'])
path = '/users/%(user_id)s' % {'user_id': user['id']}
self.delete(path)
# The user with the system role assignment is a new user and only has
# one role on the system. We should expect one less role assignment in
# the list.
response = self.get('/role_assignments')
self.assertValidRoleAssignmentListResponse(
response, expected_length=number_of_assignments - 1
)
def test_delete_user_and_check_role_assignment_fails(self):
"""Call ``DELETE`` on the user and check the role assignment."""
member_url, user = self._create_new_user_and_assign_role_on_project()
# Delete the user from identity backend
PROVIDERS.identity_api.delete_user(user['id'])
# We should get a 404 Not Found when looking for the user in the
# identity backend because we're not performing a delete operation on
# the role.
self.head(member_url, expected_status=http.client.NOT_FOUND)
def test_token_revoked_once_group_role_grant_revoked(self):
"""Test token invalid when direct & indirect role on user is revoked.
When a role granted to a group is revoked for a given scope,
and user direct role is revoked, then tokens created
by user will be invalid.
"""
time = datetime.datetime.utcnow()
with freezegun.freeze_time(time) as frozen_datetime:
# creates grant from group on project.
PROVIDERS.assignment_api.create_grant(
role_id=self.role['id'], project_id=self.project['id'],
group_id=self.group['id']
)
# adds user to the group.
PROVIDERS.identity_api.add_user_to_group(
user_id=self.user['id'], group_id=self.group['id']
)
# creates a token for the user
auth_body = self.build_authentication_request(
user_id=self.user['id'],
password=self.user['password'],
project_id=self.project['id'])
token_resp = self.post('/auth/tokens', body=auth_body)
token = token_resp.headers.get('x-subject-token')
# validates the returned token; it should be valid.
self.head('/auth/tokens',
headers={'x-subject-token': token},
expected_status=http.client.OK)
frozen_datetime.tick(delta=datetime.timedelta(seconds=1))
# revokes the grant from group on project.
PROVIDERS.assignment_api.delete_grant(
role_id=self.role['id'], project_id=self.project['id'],
group_id=self.group['id'])
# revokes the direct role form user on project
PROVIDERS.assignment_api.delete_grant(
role_id=self.role['id'], project_id=self.project['id'],
user_id=self.user['id']
)
frozen_datetime.tick(delta=datetime.timedelta(seconds=1))
# validates the same token again; it should not longer be valid.
self.head('/auth/tokens', token=token,
expected_status=http.client.UNAUTHORIZED)
def test_delete_group_before_removing_system_assignments_succeeds(self):
system_role = self._create_new_role()
group = self._create_group()
path = (
'/system/groups/%(group_id)s/roles/%(role_id)s' %
{'group_id': group['id'], 'role_id': system_role}
)
self.put(path)
response = self.get('/role_assignments')
number_of_assignments = len(response.json_body['role_assignments'])
path = '/groups/%(group_id)s' % {'group_id': group['id']}
self.delete(path)
# The group with the system role assignment is a new group and only has
# one role on the system. We should expect one less role assignment in
# the list.
response = self.get('/role_assignments')
self.assertValidRoleAssignmentListResponse(
response, expected_length=number_of_assignments - 1
)
@unit.skip_if_cache_disabled('assignment')
def test_delete_grant_from_user_and_project_invalidate_cache(self):
# create a new project
new_project = unit.new_project_ref(domain_id=self.domain_id)
PROVIDERS.resource_api.create_project(new_project['id'], new_project)
collection_url = (
'/projects/%(project_id)s/users/%(user_id)s/roles' % {
'project_id': new_project['id'],
'user_id': self.user['id']})
member_url = '%(collection_url)s/%(role_id)s' % {
'collection_url': collection_url,
'role_id': self.role_id}
# create the user a grant on the new project
self.put(member_url)
# check the grant that was just created
self.head(member_url)
self.get(member_url, expected_status=http.client.NO_CONTENT)
resp = self.get(collection_url)
self.assertValidRoleListResponse(resp, ref=self.role,
resource_url=collection_url)
# delete the grant
self.delete(member_url)
# get the collection and ensure there are no roles on the project
resp = self.get(collection_url)
self.assertListEqual(resp.json_body['roles'], [])
@unit.skip_if_cache_disabled('assignment')
def test_delete_grant_from_user_and_domain_invalidates_cache(self):
# create a new domain
new_domain = unit.new_domain_ref()
PROVIDERS.resource_api.create_domain(new_domain['id'], new_domain)
collection_url = (
'/domains/%(domain_id)s/users/%(user_id)s/roles' % {
'domain_id': new_domain['id'],
'user_id': self.user['id']})
member_url = '%(collection_url)s/%(role_id)s' % {
'collection_url': collection_url,
'role_id': self.role_id}
# create the user a grant on the new domain
self.put(member_url)
# check the grant that was just created
self.head(member_url)
self.get(member_url, expected_status=http.client.NO_CONTENT)
resp = self.get(collection_url)
self.assertValidRoleListResponse(resp, ref=self.role,
resource_url=collection_url)
# delete the grant
self.delete(member_url)
# get the collection and ensure there are no roles on the domain
resp = self.get(collection_url)
self.assertListEqual(resp.json_body['roles'], [])
@unit.skip_if_cache_disabled('assignment')
def test_delete_grant_from_group_and_project_invalidates_cache(self):
# create a new project
new_project = unit.new_project_ref(domain_id=self.domain_id)
PROVIDERS.resource_api.create_project(new_project['id'], new_project)
collection_url = (
'/projects/%(project_id)s/groups/%(group_id)s/roles' % {
'project_id': new_project['id'],
'group_id': self.group['id']})
member_url = '%(collection_url)s/%(role_id)s' % {
'collection_url': collection_url,
'role_id': self.role_id}
# create the group a grant on the new project
self.put(member_url)
# check the grant that was just created
self.head(member_url)
self.get(member_url, expected_status=http.client.NO_CONTENT)
resp = self.get(collection_url)
self.assertValidRoleListResponse(resp, ref=self.role,
resource_url=collection_url)
# delete the grant
self.delete(member_url)
# get the collection and ensure there are no roles on the project
resp = self.get(collection_url)
self.assertListEqual(resp.json_body['roles'], [])
@unit.skip_if_cache_disabled('assignment')
def test_delete_grant_from_group_and_domain_invalidates_cache(self):
# create a new domain
new_domain = unit.new_domain_ref()
PROVIDERS.resource_api.create_domain(new_domain['id'], new_domain)
collection_url = (
'/domains/%(domain_id)s/groups/%(group_id)s/roles' % {
'domain_id': new_domain['id'],
'group_id': self.group['id']})
member_url = '%(collection_url)s/%(role_id)s' % {
'collection_url': collection_url,
'role_id': self.role_id}
# create the group a grant on the new domain
self.put(member_url)
# check the grant that was just created
self.head(member_url)
self.get(member_url, expected_status=http.client.NO_CONTENT)
resp = self.get(collection_url)
self.assertValidRoleListResponse(resp, ref=self.role,
resource_url=collection_url)
# delete the grant
self.delete(member_url)
# get the collection and ensure there are no roles on the domain
resp = self.get(collection_url)
self.assertListEqual(resp.json_body['roles'], [])
# Role Assignments tests
def test_get_head_role_assignments(self):
"""Call ``GET & HEAD /role_assignments``.
The sample data set up already has a user, group and project
that is part of self.domain. We use these plus a new user
we create as our data set, making sure we ignore any
role assignments that are already in existence.
Since we don't yet support a first class entity for role
assignments, we are only testing the LIST API. To create
and delete the role assignments we use the old grant APIs.
Test Plan:
- Create extra user for tests
- Get a list of all existing role assignments
- Add a new assignment for each of the four combinations, i.e.
group+domain, user+domain, group+project, user+project, using
the same role each time
- Get a new list of all role assignments, checking these four new
ones have been added
- Then delete the four we added
- Get a new list of all role assignments, checking the four have
been removed
"""
time = datetime.datetime.utcnow()
with freezegun.freeze_time(time) as frozen_datetime:
# Since the default fixtures already assign some roles to the
# user it creates, we also need a new user that will not have any
# existing assignments
user1 = unit.new_user_ref(domain_id=self.domain['id'])
user1 = PROVIDERS.identity_api.create_user(user1)
role = unit.new_role_ref()
PROVIDERS.role_api.create_role(role['id'], role)
collection_url = '/role_assignments'
r = self.get(collection_url)
self.assertValidRoleAssignmentListResponse(
r, resource_url=collection_url)
self.head(collection_url, expected_status=http.client.OK)
existing_assignments = len(r.result.get('role_assignments'))
# Now add one of each of the four types of assignment, making sure
# that we get them all back.
gd_entity = self.build_role_assignment_entity(
domain_id=self.domain_id,
group_id=self.group_id,
role_id=role['id'])
self.put(gd_entity['links']['assignment'])
r = self.get(collection_url)
self.assertValidRoleAssignmentListResponse(
r,
expected_length=existing_assignments + 1,
resource_url=collection_url)
self.assertRoleAssignmentInListResponse(r, gd_entity)
self.head(collection_url, expected_status=http.client.OK)
ud_entity = self.build_role_assignment_entity(
domain_id=self.domain_id,
user_id=user1['id'],
role_id=role['id'])
self.put(ud_entity['links']['assignment'])
r = self.get(collection_url)
self.assertValidRoleAssignmentListResponse(
r,
expected_length=existing_assignments + 2,
resource_url=collection_url)
self.assertRoleAssignmentInListResponse(r, ud_entity)
self.head(collection_url, expected_status=http.client.OK)
gp_entity = self.build_role_assignment_entity(
project_id=self.project_id, group_id=self.group_id,
role_id=role['id'])
self.put(gp_entity['links']['assignment'])
r = self.get(collection_url)
self.assertValidRoleAssignmentListResponse(
r,
expected_length=existing_assignments + 3,
resource_url=collection_url)
self.assertRoleAssignmentInListResponse(r, gp_entity)
self.head(collection_url, expected_status=http.client.OK)
up_entity = self.build_role_assignment_entity(
project_id=self.project_id, user_id=user1['id'],
role_id=role['id'])
self.put(up_entity['links']['assignment'])
r = self.get(collection_url)
self.assertValidRoleAssignmentListResponse(
r,
expected_length=existing_assignments + 4,
resource_url=collection_url)
self.assertRoleAssignmentInListResponse(r, up_entity)
self.head(collection_url, expected_status=http.client.OK)
# Now delete the four we added and make sure they are removed
# from the collection.
self.delete(gd_entity['links']['assignment'])
self.delete(ud_entity['links']['assignment'])
self.delete(gp_entity['links']['assignment'])
self.delete(up_entity['links']['assignment'])
frozen_datetime.tick(delta=datetime.timedelta(seconds=1))
r = self.get(collection_url)
self.assertValidRoleAssignmentListResponse(
r,
expected_length=existing_assignments,
resource_url=collection_url)
self.assertRoleAssignmentNotInListResponse(r, gd_entity)
self.assertRoleAssignmentNotInListResponse(r, ud_entity)
self.assertRoleAssignmentNotInListResponse(r, gp_entity)
self.assertRoleAssignmentNotInListResponse(r, up_entity)
self.head(collection_url, expected_status=http.client.OK)
def test_get_effective_role_assignments(self):
"""Call ``GET /role_assignments?effective``.
Test Plan:
- Create two extra user for tests
- Add these users to a group
- Add a role assignment for the group on a domain
- Get a list of all role assignments, checking one has been added
- Then get a list of all effective role assignments - the group
assignment should have turned into assignments on the domain
for each of the group members.
"""
user1 = unit.create_user(PROVIDERS.identity_api,
domain_id=self.domain['id'])
user2 = unit.create_user(PROVIDERS.identity_api,
domain_id=self.domain['id'])
PROVIDERS.identity_api.add_user_to_group(user1['id'], self.group['id'])
PROVIDERS.identity_api.add_user_to_group(user2['id'], self.group['id'])
collection_url = '/role_assignments'
r = self.get(collection_url)
self.assertValidRoleAssignmentListResponse(r,
resource_url=collection_url)
existing_assignments = len(r.result.get('role_assignments'))
gd_entity = self.build_role_assignment_entity(domain_id=self.domain_id,
group_id=self.group_id,
role_id=self.role_id)
self.put(gd_entity['links']['assignment'])
r = self.get(collection_url)
self.assertValidRoleAssignmentListResponse(
r,
expected_length=existing_assignments + 1,
resource_url=collection_url)
self.assertRoleAssignmentInListResponse(r, gd_entity)
# Now re-read the collection asking for effective roles - this
# should mean the group assignment is translated into the two
# member user assignments
collection_url = '/role_assignments?effective'
r = self.get(collection_url)
self.assertValidRoleAssignmentListResponse(
r,
expected_length=existing_assignments + 2,
resource_url=collection_url)
ud_entity = self.build_role_assignment_entity(
link=gd_entity['links']['assignment'], domain_id=self.domain_id,
user_id=user1['id'], role_id=self.role_id)
self.assertRoleAssignmentInListResponse(r, ud_entity)
ud_entity = self.build_role_assignment_entity(
link=gd_entity['links']['assignment'], domain_id=self.domain_id,
user_id=user2['id'], role_id=self.role_id)
self.assertRoleAssignmentInListResponse(r, ud_entity)
def test_check_effective_values_for_role_assignments(self):
"""Call ``GET & HEAD /role_assignments?effective=value``.
Check the various ways of specifying the 'effective'
query parameter. If the 'effective' query parameter
is included then this should always be treated as meaning 'True'
unless it is specified as:
{url}?effective=0
This is by design to match the agreed way of handling
policy checking on query/filter parameters.
Test Plan:
- Create two extra user for tests
- Add these users to a group
- Add a role assignment for the group on a domain
- Get a list of all role assignments, checking one has been added
- Then issue various request with different ways of defining
the 'effective' query parameter. As we have tested the
correctness of the data coming back when we get effective roles
in other tests, here we just use the count of entities to
know if we are getting effective roles or not
"""
user1 = unit.create_user(PROVIDERS.identity_api,
domain_id=self.domain['id'])
user2 = unit.create_user(PROVIDERS.identity_api,
domain_id=self.domain['id'])
PROVIDERS.identity_api.add_user_to_group(user1['id'], self.group['id'])
PROVIDERS.identity_api.add_user_to_group(user2['id'], self.group['id'])
collection_url = '/role_assignments'
r = self.get(collection_url, expected_status=http.client.OK)
self.head(collection_url, expected_status=http.client.OK)
self.assertValidRoleAssignmentListResponse(r,
resource_url=collection_url)
existing_assignments = len(r.result.get('role_assignments'))
gd_entity = self.build_role_assignment_entity(domain_id=self.domain_id,
group_id=self.group_id,
role_id=self.role_id)
self.put(gd_entity['links']['assignment'])
r = self.get(collection_url, expected_status=http.client.OK)
self.head(collection_url, expected_status=http.client.OK)
self.assertValidRoleAssignmentListResponse(
r,
expected_length=existing_assignments + 1,
resource_url=collection_url)
self.assertRoleAssignmentInListResponse(r, gd_entity)
# Now re-read the collection asking for effective roles,
# using the most common way of defining "effective'. This
# should mean the group assignment is translated into the two
# member user assignments
collection_url = '/role_assignments?effective'
r = self.get(collection_url, expected_status=http.client.OK)
self.head(collection_url, expected_status=http.client.OK)
self.assertValidRoleAssignmentListResponse(
r,
expected_length=existing_assignments + 2,
resource_url=collection_url)
# Now set 'effective' to false explicitly - should get
# back the regular roles
collection_url = '/role_assignments?effective=0'
r = self.get(collection_url, expected_status=http.client.OK)
self.head(collection_url, expected_status=http.client.OK)
self.assertValidRoleAssignmentListResponse(
r,
expected_length=existing_assignments + 1,
resource_url=collection_url)
# Now try setting 'effective' to 'False' explicitly- this is
# NOT supported as a way of setting a query or filter
# parameter to false by design. Hence we should get back
# effective roles.
collection_url = '/role_assignments?effective=False'
r = self.get(collection_url, expected_status=http.client.OK)
self.head(collection_url, expected_status=http.client.OK)
self.assertValidRoleAssignmentListResponse(
r,
expected_length=existing_assignments + 2,
resource_url=collection_url)
# Now set 'effective' to True explicitly
collection_url = '/role_assignments?effective=True'
r = self.get(collection_url, expected_status=http.client.OK)
self.head(collection_url, expected_status=http.client.OK)
self.assertValidRoleAssignmentListResponse(
r,
expected_length=existing_assignments + 2,
resource_url=collection_url)
def test_filtered_role_assignments(self):
"""Call ``GET /role_assignments?filters``.
Test Plan:
- Create extra users, group, role and project for tests
- Make the following assignments:
Give group1, role1 on project1 and domain
Give user1, role2 on project1 and domain
Make User1 a member of Group1
- Test a series of single filter list calls, checking that
the correct results are obtained
- Test a multi-filtered list call
- Test listing all effective roles for a given user
- Test the equivalent of the list of roles in a project scoped
token (all effective roles for a user on a project)
"""
# Since the default fixtures already assign some roles to the
# user it creates, we also need a new user that will not have any
# existing assignments
user1 = unit.create_user(PROVIDERS.identity_api,
domain_id=self.domain['id'])
user2 = unit.create_user(PROVIDERS.identity_api,
domain_id=self.domain['id'])
group1 = unit.new_group_ref(domain_id=self.domain['id'])
group1 = PROVIDERS.identity_api.create_group(group1)
PROVIDERS.identity_api.add_user_to_group(user1['id'], group1['id'])
PROVIDERS.identity_api.add_user_to_group(user2['id'], group1['id'])
project1 = unit.new_project_ref(domain_id=self.domain['id'])
PROVIDERS.resource_api.create_project(project1['id'], project1)
self.role1 = unit.new_role_ref()
PROVIDERS.role_api.create_role(self.role1['id'], self.role1)
self.role2 = unit.new_role_ref()
PROVIDERS.role_api.create_role(self.role2['id'], self.role2)
# Now add one of each of the six types of assignment
gd_entity = self.build_role_assignment_entity(
domain_id=self.domain_id, group_id=group1['id'],
role_id=self.role1['id'])
self.put(gd_entity['links']['assignment'])
ud_entity = self.build_role_assignment_entity(domain_id=self.domain_id,
user_id=user1['id'],
role_id=self.role2['id'])
self.put(ud_entity['links']['assignment'])
gp_entity = self.build_role_assignment_entity(
project_id=project1['id'],
group_id=group1['id'],
role_id=self.role1['id'])
self.put(gp_entity['links']['assignment'])
up_entity = self.build_role_assignment_entity(
project_id=project1['id'],
user_id=user1['id'],
role_id=self.role2['id'])
self.put(up_entity['links']['assignment'])
gs_entity = self.build_role_assignment_entity(
system='all',
group_id=group1['id'],
role_id=self.role1['id'])
self.put(gs_entity['links']['assignment'])
us_entity = self.build_role_assignment_entity(
system='all',
user_id=user1['id'],
role_id=self.role2['id'])
self.put(us_entity['links']['assignment'])
us2_entity = self.build_role_assignment_entity(
system='all',
user_id=user2['id'],
role_id=self.role2['id'])
self.put(us2_entity['links']['assignment'])
# Now list by various filters to make sure we get back the right ones
collection_url = ('/role_assignments?scope.project.id=%s' %
project1['id'])
r = self.get(collection_url, expected_status=http.client.OK)
self.head(collection_url, expected_status=http.client.OK)
self.assertValidRoleAssignmentListResponse(r,
expected_length=2,
resource_url=collection_url)
self.assertRoleAssignmentInListResponse(r, up_entity)
self.assertRoleAssignmentInListResponse(r, gp_entity)
collection_url = ('/role_assignments?scope.domain.id=%s' %
self.domain['id'])
r = self.get(collection_url, expected_status=http.client.OK)
self.head(collection_url, expected_status=http.client.OK)
self.assertValidRoleAssignmentListResponse(r,
expected_length=2,
resource_url=collection_url)
self.assertRoleAssignmentInListResponse(r, ud_entity)
self.assertRoleAssignmentInListResponse(r, gd_entity)
collection_url = '/role_assignments?user.id=%s' % user1['id']
r = self.get(collection_url, expected_status=http.client.OK)
self.head(collection_url, expected_status=http.client.OK)
self.assertValidRoleAssignmentListResponse(r,
expected_length=3,
resource_url=collection_url)
self.assertRoleAssignmentInListResponse(r, up_entity)
self.assertRoleAssignmentInListResponse(r, ud_entity)
collection_url = '/role_assignments?group.id=%s' % group1['id']
r = self.get(collection_url, expected_status=http.client.OK)
self.head(collection_url, expected_status=http.client.OK)
self.assertValidRoleAssignmentListResponse(r,
expected_length=3,
resource_url=collection_url)
self.assertRoleAssignmentInListResponse(r, gd_entity)
self.assertRoleAssignmentInListResponse(r, gp_entity)