Debian Codegraph @

keystoneclient/tests/unit/v2_0/test_roles.py

raw symbols SCIP
1 # Licensed under the Apache License, Version 2.0 (the "License"); you may
2 # not use this file except in compliance with the License. You may obtain
3 # a copy of the License at
4 #
5 # http://www.apache.org/licenses/LICENSE-2.0
6 #
7 # Unless required by applicable law or agreed to in writing, software
8 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10 # License for the specific language governing permissions and limitations
11 # under the License.
12
13 import uuid
14
15 from keystoneclient.tests.unit.v2_0 import utils
16 from keystoneclient.v2_0 import roles
17
18
19 class RoleTests(utils.ClientTestCase):
20 def setUp(self):
21 super(RoleTests, self).setUp()
22
23 self.ADMIN_ROLE_ID = uuid.uuid4().hex
24 self.MEMBER_ROLE_ID = uuid.uuid4().hex
25
26 self.TEST_ROLES = {
27 "roles": {
28 "values": [
29 {
30 "name": "admin",
31 "id": self.ADMIN_ROLE_ID,
32 },
33 {
34 "name": "member",
35 "id": self.MEMBER_ROLE_ID,
36 }
37 ],
38 },
39 }
40
41 def test_create(self):
42 req_body = {
43 "role": {
44 "name": "sysadmin",
45 }
46 }
47 role_id = uuid.uuid4().hex
48 resp_body = {
49 "role": {
50 "name": "sysadmin",
51 "id": role_id,
52 }
53 }
54 self.stub_url('POST', ['OS-KSADM', 'roles'], json=resp_body)
55
56 role = self.client.roles.create(req_body['role']['name'])
57 self.assertRequestBodyIs(json=req_body)
58 self.assertIsInstance(role, roles.Role)
59 self.assertEqual(role.id, role_id)
60 self.assertEqual(role.name, req_body['role']['name'])
61
62 def test_delete(self):
63 self.stub_url('DELETE',
64 ['OS-KSADM', 'roles', self.ADMIN_ROLE_ID],
65 status_code=204)
66 self.client.roles.delete(self.ADMIN_ROLE_ID)
67
68 def test_get(self):
69 self.stub_url('GET', ['OS-KSADM', 'roles', self.ADMIN_ROLE_ID],
70 json={'role': self.TEST_ROLES['roles']['values'][0]})
71
72 role = self.client.roles.get(self.ADMIN_ROLE_ID)
73 self.assertIsInstance(role, roles.Role)
74 self.assertEqual(role.id, self.ADMIN_ROLE_ID)
75 self.assertEqual(role.name, 'admin')
76
77 def test_list(self):
78 self.stub_url('GET', ['OS-KSADM', 'roles'],
79 json=self.TEST_ROLES)
80
81 role_list = self.client.roles.list()
82 [self.assertIsInstance(r, roles.Role) for r in role_list]
83
84 def test_roles_for_user(self):
85 self.stub_url('GET', ['users', 'foo', 'roles'],
86 json=self.TEST_ROLES)
87
88 role_list = self.client.roles.roles_for_user('foo')
89 [self.assertIsInstance(r, roles.Role) for r in role_list]
90
91 def test_roles_for_user_tenant(self):
92 self.stub_url('GET', ['tenants', 'barrr', 'users', 'foo',
93 'roles'], json=self.TEST_ROLES)
94
95 role_list = self.client.roles.roles_for_user('foo', 'barrr')
96 [self.assertIsInstance(r, roles.Role) for r in role_list]
97
98 def test_add_user_role(self):
99 self.stub_url('PUT', ['users', 'foo', 'roles', 'OS-KSADM',
100 'barrr'], status_code=204)
101
102 self.client.roles.add_user_role('foo', 'barrr')
103
104 def test_add_user_role_tenant(self):
105 id_ = uuid.uuid4().hex
106 self.stub_url('PUT', ['tenants', id_, 'users', 'foo', 'roles',
107 'OS-KSADM', 'barrr'], status_code=204)
108
109 self.client.roles.add_user_role('foo', 'barrr', id_)
110
111 def test_remove_user_role(self):
112 self.stub_url('DELETE', ['users', 'foo', 'roles', 'OS-KSADM',
113 'barrr'], status_code=204)
114 self.client.roles.remove_user_role('foo', 'barrr')
115
116 def test_remove_user_role_tenant(self):
117 id_ = uuid.uuid4().hex
118 self.stub_url('DELETE', ['tenants', id_, 'users', 'foo',
119 'roles', 'OS-KSADM', 'barrr'],
120 status_code=204)
121 self.client.roles.remove_user_role('foo', 'barrr', id_)