-
Notifications
You must be signed in to change notification settings - Fork 3
/
test_role.py
390 lines (315 loc) · 15.1 KB
/
test_role.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
# Copyright 2020 SUSE LLC
#
# 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 abc
from tempest.api.identity import base
from tempest.lib.common.utils import data_utils
from tempest.lib import exceptions
from keystone_tempest_plugin.tests.rbac.v3 import base as rbac_base
class IdentityV3RbacRoleTest(rbac_base.IdentityV3RbacBaseTests,
metaclass=abc.ABCMeta):
@classmethod
def setup_clients(cls):
super(IdentityV3RbacRoleTest, cls).setup_clients()
cls.persona = getattr(cls, 'os_%s' % cls.credentials[0])
cls.client = cls.persona.roles_v3_client
cls.admin_client = cls.os_system_admin
cls.admin_roles_client = cls.admin_client.roles_v3_client
@classmethod
def resource_setup(cls):
super(IdentityV3RbacRoleTest, cls).resource_setup()
cls.own_domain = cls.persona.credentials.domain_id
cls.domain_id = cls.admin_client.domains_client.create_domain(
name=data_utils.rand_name('domain'))['domain']['id']
cls.addClassResourceCleanup(
cls.admin_client.domains_client.delete_domain,
cls.domain_id)
cls.addClassResourceCleanup(
cls.admin_client.domains_client.update_domain,
cls.domain_id,
enabled=False)
def role(self, domain_id=None):
role = {}
name = data_utils.rand_name('role')
role['name'] = name
if domain_id:
role['domain_id'] = domain_id
return role
@abc.abstractmethod
def test_identity_create_role(self):
"""Test identity:create_role policy.
This test must check:
* whether the persona can create a role
"""
pass
@abc.abstractmethod
def test_identity_get_role(self):
"""Test identity:get_role policy.
This test must check:
* whether the persona can get a role
* whether the persona can get a role that does not exist
"""
pass
@abc.abstractmethod
def test_identity_list_roles(self):
"""Test identity:list_roles policy.
This test must check:
* whether the persona can list roles
"""
pass
@abc.abstractmethod
def test_identity_update_role(self):
"""Test identity:update_role policy.
This test must check:
* whether the persona can update a role
* whether the persona can update a role that does not exist
"""
pass
@abc.abstractmethod
def test_identity_delete_role(self):
"""Test identity:delete_role policy.
This test must check
* whether the persona can delete a role
* whether the persona can delete a role that does not exist
"""
pass
@abc.abstractmethod
def test_identity_create_domain_role(self):
"""Test identity:create_domain_role policy.
This test must check:
* whether the persona can create a domain role in their own domain
* whether the persona can create a domain role in another domain
"""
pass
@abc.abstractmethod
def test_identity_get_domain_role(self):
"""Test identity:get_domain_role policy.
This test must check:
* whether the persona can get a domain role in their own domain
* whether the persona can get a domain role in another domain
* whether the persona can get a domain role that does not exist
"""
pass
@abc.abstractmethod
def test_identity_list_domain_roles(self):
"""Test identity:list_domain_roles policy.
This test must check:
* whether the persona can list domain roles for their own domain
* whether the persona can list domain roles for another domain
"""
pass
@abc.abstractmethod
def test_identity_update_domain_role(self):
"""Test identity:update_domain_role policy.
This test must check:
* whether the persona can update a domain role for their own domain
* whether the persona can update a domain role for another domain
* whether the persona can update a domain role that does not exist
"""
pass
@abc.abstractmethod
def test_identity_delete_domain_role(self):
"""Test identity:delete_domain_role policy.
This test must check
* whether the persona can delete a domain role for their own domain
* whether the persona can delete a domain role for another domain
* whether the persona can delete a domain role that does not exist
"""
pass
class SystemAdminTests(IdentityV3RbacRoleTest, base.BaseIdentityTest):
credentials = ['system_admin']
def test_identity_create_role(self):
# user can create role
resp = self.do_request('create_role',
expected_status=201,
**self.role())
self.addCleanup(self.admin_roles_client.delete_role,
resp['role']['id'])
def test_identity_get_role(self):
# user can get role
role = self.admin_roles_client.create_role(**self.role())['role']
self.addCleanup(self.admin_roles_client.delete_role, role['id'])
self.do_request('show_role', role_id=role['id'])
# user gets a 404 for nonexistent role
self.do_request('show_role', expected_status=exceptions.NotFound,
role_id=data_utils.rand_uuid_hex())
def test_identity_list_roles(self):
# user can list roles
role = self.admin_roles_client.create_role(**self.role())['role']
self.addCleanup(self.admin_roles_client.delete_role, role['id'])
self.do_request('list_roles')
def test_identity_update_role(self):
# user can update role
role = self.admin_roles_client.create_role(**self.role())['role']
self.addCleanup(self.admin_roles_client.delete_role, role['id'])
self.do_request('update_role',
role_id=role['id'],
description=data_utils.arbitrary_string())
# user gets a 404 for nonexistent role
self.do_request('update_role', expected_status=exceptions.NotFound,
role_id=data_utils.rand_uuid_hex(),
description=data_utils.arbitrary_string())
def test_identity_delete_role(self):
# user can delete role
role = self.admin_roles_client.create_role(**self.role())['role']
self.do_request('delete_role', expected_status=204, role_id=role['id'])
# user gets a 404 for nonexistent role
self.do_request('delete_role', expected_status=exceptions.NotFound,
role_id=data_utils.rand_uuid_hex())
def test_identity_create_domain_role(self):
# user can create domain role
resp = self.do_request('create_role',
expected_status=201,
**self.role(domain_id=self.domain_id))
self.addCleanup(self.admin_roles_client.delete_role,
resp['role']['id'])
def test_identity_get_domain_role(self):
# user can get domain role
role = self.admin_roles_client.create_role(
**self.role(domain_id=self.domain_id))['role']
self.addCleanup(self.admin_roles_client.delete_role, role['id'])
self.do_request('show_role', role_id=role['id'])
# user gets a 404 for nonexistent domain role
self.do_request('show_role', expected_status=exceptions.NotFound,
role_id=data_utils.rand_uuid_hex())
def test_identity_list_domain_roles(self):
# user can list domain roles
role = self.admin_roles_client.create_role(
**self.role(domain_id=self.domain_id))['role']
self.addCleanup(self.admin_roles_client.delete_role, role['id'])
self.do_request('list_roles', domain_id=self.domain_id)
def test_identity_update_domain_role(self):
# user can update domain role
role = self.admin_roles_client.create_role(
**self.role(domain_id=self.domain_id))['role']
self.addCleanup(self.admin_roles_client.delete_role, role['id'])
self.do_request('update_role',
role_id=role['id'],
description=data_utils.arbitrary_string())
# user gets a 404 for nonexistent domain role
self.do_request('update_role', expected_status=exceptions.NotFound,
role_id=data_utils.rand_uuid_hex(),
description=data_utils.arbitrary_string())
def test_identity_delete_domain_role(self):
# user can delete role in other domain
role = self.admin_roles_client.create_role(
**self.role(domain_id=self.domain_id))['role']
self.do_request('delete_role', expected_status=204, role_id=role['id'])
# user gets a 404 for nonexistent role
self.do_request('delete_role', expected_status=exceptions.NotFound,
role_id=data_utils.rand_uuid_hex())
class SystemMemberTests(SystemAdminTests):
credentials = ['system_member', 'system_admin']
def test_identity_create_role(self):
# user cannot create role
self.do_request('create_role',
expected_status=exceptions.Forbidden,
**self.role())
def test_identity_update_role(self):
# user cannot update role
role = self.admin_roles_client.create_role(
**self.role())['role']
self.addCleanup(self.admin_roles_client.delete_role, role['id'])
self.do_request('update_role', expected_status=exceptions.Forbidden,
role_id=role['id'],
description=data_utils.arbitrary_string())
# user gets a 404 for nonexistent role
self.do_request('update_role', expected_status=exceptions.NotFound,
role_id=data_utils.rand_uuid_hex(),
description=data_utils.arbitrary_string())
def test_identity_delete_role(self):
# user can delete role
role = self.admin_roles_client.create_role(
**self.role())['role']
self.do_request('delete_role', expected_status=exceptions.Forbidden,
role_id=role['id'])
# user gets a 404 for nonexistent domain role
self.do_request('delete_role', expected_status=exceptions.NotFound,
role_id=data_utils.rand_uuid_hex())
def test_identity_create_domain_role(self):
# user cannot create domain role
self.do_request('create_role',
expected_status=exceptions.Forbidden,
**self.role(domain_id=self.domain_id))
def test_identity_update_domain_role(self):
# user cannot update domain role
role = self.admin_roles_client.create_role(
**self.role(domain_id=self.domain_id))['role']
self.addCleanup(self.admin_roles_client.delete_role, role['id'])
self.do_request('update_role', expected_status=exceptions.Forbidden,
role_id=role['id'],
description=data_utils.arbitrary_string())
# user gets a 404 for nonexistent domain role
self.do_request('update_role', expected_status=exceptions.NotFound,
role_id=data_utils.rand_uuid_hex(),
description=data_utils.arbitrary_string())
def test_identity_delete_domain_role(self):
# user can delete domain role
role = self.admin_roles_client.create_role(
**self.role(domain_id=self.domain_id))['role']
self.do_request('delete_role', expected_status=exceptions.Forbidden,
role_id=role['id'])
# user gets a 404 for nonexistent domain role
self.do_request('delete_role', expected_status=exceptions.NotFound,
role_id=data_utils.rand_uuid_hex())
class SystemReaderTests(SystemMemberTests):
credentials = ['system_reader', 'system_admin']
class DomainAdminTests(SystemReaderTests):
credentials = ['domain_admin', 'system_admin']
def test_identity_get_role(self):
# user cannot get role
role = self.admin_roles_client.create_role(
**self.role())['role']
self.addCleanup(self.admin_roles_client.delete_role, role['id'])
self.do_request('show_role', expected_status=exceptions.Forbidden,
role_id=role['id'])
# user gets a 404 for nonexistent role
self.do_request('show_role', expected_status=exceptions.NotFound,
role_id=data_utils.rand_uuid_hex())
def test_identity_list_roles(self):
# user cannot list roles
role = self.admin_roles_client.create_role(**self.role())['role']
self.addCleanup(self.admin_roles_client.delete_role, role['id'])
self.do_request('list_roles', expected_status=exceptions.Forbidden)
def test_identity_get_domain_role(self):
# user cannot get domain role in own domain
role = self.admin_roles_client.create_role(**self.role())['role']
self.addCleanup(self.admin_roles_client.delete_role, role['id'])
self.do_request('show_role', expected_status=exceptions.Forbidden,
role_id=role['id'])
# user gets a 404 for nonexistent domain role
self.do_request('show_role', expected_status=exceptions.NotFound,
role_id=data_utils.rand_uuid_hex())
def test_identity_list_domain_roles(self):
# user cannot list domain roles in own domain
role = self.admin_roles_client.create_role(
**self.role(domain_id=self.own_domain))['role']
self.addCleanup(self.admin_roles_client.delete_role, role['id'])
self.do_request('list_roles', expected_status=exceptions.Forbidden,
domain_id=self.persona.credentials.domain_id)
# user cannot get domain role in other domain
role = self.admin_roles_client.create_role(
**self.role(domain_id=self.domain_id))['role']
self.addCleanup(self.admin_roles_client.delete_role, role['id'])
self.do_request('list_roles', expected_status=exceptions.Forbidden,
domain_id=self.domain_id)
class DomainMemberTests(DomainAdminTests):
credentials = ['domain_member', 'system_admin']
class DomainReaderTests(DomainMemberTests):
credentials = ['domain_reader', 'system_admin']
class ProjectAdminTests(SystemAdminTests):
credentials = ['project_admin', 'system_admin']
class ProjectMemberTests(DomainReaderTests):
credentials = ['project_member', 'system_admin']
class ProjectReaderTests(ProjectMemberTests):
credentials = ['project_reader', 'system_admin']