blob: 53c5b0d671dd027d6fe969566f5efd63d0451f59 [file] [log] [blame]
Jay Pipesf38eaac2012-06-21 13:37:35 -04001# vim: tabstop=4 shiftwidth=4 softtabstop=4
2
3# Copyright 2012 OpenStack, LLC
4# All Rights Reserved.
5#
6# Licensed under the Apache License, Version 2.0 (the "License"); you may
7# not use this file except in compliance with the License. You may obtain
8# a copy of the License at
9#
10# http://www.apache.org/licenses/LICENSE-2.0
11#
12# Unless required by applicable law or agreed to in writing, software
13# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15# License for the specific language governing permissions and limitations
16# under the License.
17
chris fattarsi8ed39ac2012-04-30 14:11:27 -070018from tempest.common.utils.data_utils import rand_name
Matthew Treinisha83a16e2012-12-07 13:44:02 -050019from tempest import exceptions
Vincent Hou6b8a7b72012-08-25 01:24:33 +080020from tempest.tests.identity import base
chris fattarsi8ed39ac2012-04-30 14:11:27 -070021
22
Attila Fazekas0d0c6162013-02-24 09:14:23 +010023class RolesTestJSON(base.BaseIdentityAdminTest):
24 _interface = 'json'
chris fattarsi8ed39ac2012-04-30 14:11:27 -070025
Attila Fazekas0d0c6162013-02-24 09:14:23 +010026 @classmethod
chris fattarsi8ed39ac2012-04-30 14:11:27 -070027 def setUpClass(cls):
Attila Fazekas0d0c6162013-02-24 09:14:23 +010028 super(RolesTestJSON, cls).setUpClass()
chris fattarsi8ed39ac2012-04-30 14:11:27 -070029 for _ in xrange(5):
Rohit Karajgi69e80a02012-05-15 03:54:04 -070030 resp, role = cls.client.create_role(rand_name('role-'))
31 cls.data.roles.append(role)
chris fattarsi8ed39ac2012-04-30 14:11:27 -070032
Rohit Karajgi69e80a02012-05-15 03:54:04 -070033 def _get_role_params(self):
34 self.data.setup_test_user()
35 self.data.setup_test_role()
36 user = self.get_user_by_name(self.data.test_user)
37 tenant = self.get_tenant_by_name(self.data.test_tenant)
38 role = self.get_role_by_name(self.data.test_role)
39 return (user, tenant, role)
chris fattarsi8ed39ac2012-04-30 14:11:27 -070040
Attila Fazekas0d0c6162013-02-24 09:14:23 +010041 def assert_role_in_role_list(self, role, roles):
42 found = False
43 for user_role in roles:
44 if user_role['id'] == role['id']:
45 found = True
46 self.assertTrue(found, "assigned role was not in list")
47
chris fattarsi8ed39ac2012-04-30 14:11:27 -070048 def test_list_roles(self):
Sean Dague46c4a2b2013-01-03 17:54:17 -050049 # Return a list of all roles
chris fattarsi8ed39ac2012-04-30 14:11:27 -070050 resp, body = self.client.list_roles()
Rohit Karajgi69e80a02012-05-15 03:54:04 -070051 found = [role for role in body if role in self.data.roles]
chris fattarsi8ed39ac2012-04-30 14:11:27 -070052 self.assertTrue(any(found))
Rohit Karajgi69e80a02012-05-15 03:54:04 -070053 self.assertEqual(len(found), len(self.data.roles))
54
55 def test_list_roles_by_unauthorized_user(self):
Sean Dague46c4a2b2013-01-03 17:54:17 -050056 # Non admin user should not be able to list roles
Rohit Karajgi69e80a02012-05-15 03:54:04 -070057 self.assertRaises(exceptions.Unauthorized,
Zhongyue Luo79d8d362012-09-25 13:49:27 +080058 self.non_admin_client.list_roles)
Rohit Karajgi69e80a02012-05-15 03:54:04 -070059
60 def test_list_roles_request_without_token(self):
Sean Dague46c4a2b2013-01-03 17:54:17 -050061 # Request to list roles without a valid token should fail
Rohit Karajgi69e80a02012-05-15 03:54:04 -070062 token = self.client.get_auth()
63 self.client.delete_token(token)
64 self.assertRaises(exceptions.Unauthorized, self.client.list_roles)
65 self.client.clear_auth()
chris fattarsi8ed39ac2012-04-30 14:11:27 -070066
67 def test_role_create_delete(self):
Sean Dague46c4a2b2013-01-03 17:54:17 -050068 # Role should be created, verified, and deleted
chris fattarsi8ed39ac2012-04-30 14:11:27 -070069 role_name = rand_name('role-test-')
70 resp, body = self.client.create_role(role_name)
71 self.assertTrue('status' in resp)
72 self.assertTrue(resp['status'].startswith('2'))
73 self.assertEqual(role_name, body['name'])
74
75 resp, body = self.client.list_roles()
76 found = [role for role in body if role['name'] == role_name]
77 self.assertTrue(any(found))
78
79 resp, body = self.client.delete_role(found[0]['id'])
80 self.assertTrue('status' in resp)
81 self.assertTrue(resp['status'].startswith('2'))
82
83 resp, body = self.client.list_roles()
84 found = [role for role in body if role['name'] == role_name]
85 self.assertFalse(any(found))
86
chris fattarsi8ed39ac2012-04-30 14:11:27 -070087 def test_role_create_blank_name(self):
Sean Dague46c4a2b2013-01-03 17:54:17 -050088 # Should not be able to create a role with a blank name
David Kranz28e35c52012-07-10 10:14:38 -040089 self.assertRaises(exceptions.BadRequest, self.client.create_role, '')
chris fattarsi8ed39ac2012-04-30 14:11:27 -070090
91 def test_role_create_duplicate(self):
Sean Dague46c4a2b2013-01-03 17:54:17 -050092 # Role names should be unique
chris fattarsi8ed39ac2012-04-30 14:11:27 -070093 role_name = rand_name('role-dup-')
94 resp, body = self.client.create_role(role_name)
95 role1_id = body.get('id')
96 self.assertTrue('status' in resp)
97 self.assertTrue(resp['status'].startswith('2'))
98
99 try:
100 resp, body = self.client.create_role(role_name)
101 # this should raise an exception
102 self.fail('Should not be able to create a duplicate role name.'
103 ' %s' % role_name)
104 except exceptions.Duplicate:
105 pass
106 self.client.delete_role(role1_id)
Rohit Karajgi69e80a02012-05-15 03:54:04 -0700107
Rohit Karajgi69e80a02012-05-15 03:54:04 -0700108 def test_assign_user_role(self):
Sean Dague46c4a2b2013-01-03 17:54:17 -0500109 # Assign a role to a user on a tenant
Rohit Karajgi69e80a02012-05-15 03:54:04 -0700110 (user, tenant, role) = self._get_role_params()
rajalakshmi-ganesan8ba945e2012-08-01 15:43:19 +0530111 self.client.assign_user_role(tenant['id'], user['id'], role['id'])
112 resp, roles = self.client.list_user_roles(tenant['id'], user['id'])
Adam Youngddb82892013-02-15 13:10:35 -0500113 self.assert_role_in_role_list(role, roles)
Rohit Karajgi69e80a02012-05-15 03:54:04 -0700114
115 def test_assign_user_role_by_unauthorized_user(self):
Sean Dague46c4a2b2013-01-03 17:54:17 -0500116 # Non admin user should not be authorized to assign a role to user
Rohit Karajgi69e80a02012-05-15 03:54:04 -0700117 (user, tenant, role) = self._get_role_params()
118 self.assertRaises(exceptions.Unauthorized,
119 self.non_admin_client.assign_user_role,
rajalakshmi-ganesan8ba945e2012-08-01 15:43:19 +0530120 tenant['id'], user['id'], role['id'])
Rohit Karajgi69e80a02012-05-15 03:54:04 -0700121
122 def test_assign_user_role_request_without_token(self):
Sean Dague46c4a2b2013-01-03 17:54:17 -0500123 # Request to assign a role to a user without a valid token
Rohit Karajgi69e80a02012-05-15 03:54:04 -0700124 (user, tenant, role) = self._get_role_params()
125 token = self.client.get_auth()
126 self.client.delete_token(token)
127 self.assertRaises(exceptions.Unauthorized,
rajalakshmi-ganesan8ba945e2012-08-01 15:43:19 +0530128 self.client.assign_user_role, tenant['id'],
129 user['id'], role['id'])
Rohit Karajgi69e80a02012-05-15 03:54:04 -0700130 self.client.clear_auth()
131
Rohit Karajgi69e80a02012-05-15 03:54:04 -0700132 def test_assign_user_role_for_non_existent_user(self):
Sean Dague46c4a2b2013-01-03 17:54:17 -0500133 # Attempt to assign a role to a non existent user should fail
Rohit Karajgi69e80a02012-05-15 03:54:04 -0700134 (user, tenant, role) = self._get_role_params()
135 self.assertRaises(exceptions.NotFound, self.client.assign_user_role,
Zhongyue Luo79d8d362012-09-25 13:49:27 +0800136 tenant['id'], 'junk-user-id-999', role['id'])
Rohit Karajgi69e80a02012-05-15 03:54:04 -0700137
Rohit Karajgi69e80a02012-05-15 03:54:04 -0700138 def test_assign_user_role_for_non_existent_role(self):
Sean Dague46c4a2b2013-01-03 17:54:17 -0500139 # Attempt to assign a non existent role to user should fail
Rohit Karajgi69e80a02012-05-15 03:54:04 -0700140 (user, tenant, role) = self._get_role_params()
141 self.assertRaises(exceptions.NotFound, self.client.assign_user_role,
Zhongyue Luo79d8d362012-09-25 13:49:27 +0800142 tenant['id'], user['id'], 'junk-role-id-12345')
Rohit Karajgi69e80a02012-05-15 03:54:04 -0700143
Rohit Karajgi69e80a02012-05-15 03:54:04 -0700144 def test_assign_user_role_for_non_existent_tenant(self):
Sean Dague46c4a2b2013-01-03 17:54:17 -0500145 # Attempt to assign a role on a non existent tenant should fail
Rohit Karajgi69e80a02012-05-15 03:54:04 -0700146 (user, tenant, role) = self._get_role_params()
147 self.assertRaises(exceptions.NotFound, self.client.assign_user_role,
Zhongyue Luo79d8d362012-09-25 13:49:27 +0800148 'junk-tenant-1234', user['id'], role['id'])
Rohit Karajgi69e80a02012-05-15 03:54:04 -0700149
Rohit Karajgi69e80a02012-05-15 03:54:04 -0700150 def test_assign_duplicate_user_role(self):
Sean Dague46c4a2b2013-01-03 17:54:17 -0500151 # Duplicate user role should not get assigned
Rohit Karajgi69e80a02012-05-15 03:54:04 -0700152 (user, tenant, role) = self._get_role_params()
rajalakshmi-ganesan8ba945e2012-08-01 15:43:19 +0530153 self.client.assign_user_role(tenant['id'], user['id'], role['id'])
Rohit Karajgi69e80a02012-05-15 03:54:04 -0700154 self.assertRaises(exceptions.Duplicate, self.client.assign_user_role,
rajalakshmi-ganesan8ba945e2012-08-01 15:43:19 +0530155 tenant['id'], user['id'], role['id'])
Rohit Karajgi69e80a02012-05-15 03:54:04 -0700156
Rohit Karajgi69e80a02012-05-15 03:54:04 -0700157 def test_remove_user_role(self):
Sean Dague46c4a2b2013-01-03 17:54:17 -0500158 # Remove a role assigned to a user on a tenant
Rohit Karajgi69e80a02012-05-15 03:54:04 -0700159 (user, tenant, role) = self._get_role_params()
rajalakshmi-ganesan8ba945e2012-08-01 15:43:19 +0530160 resp, user_role = self.client.assign_user_role(tenant['id'],
161 user['id'], role['id'])
162 resp, body = self.client.remove_user_role(tenant['id'], user['id'],
163 user_role['id'])
Rohit Karajgi69e80a02012-05-15 03:54:04 -0700164 self.assertEquals(resp['status'], '204')
165
166 def test_remove_user_role_by_unauthorized_user(self):
Sean Dague46c4a2b2013-01-03 17:54:17 -0500167 # Non admin user should not be authorized to remove a user's role
Rohit Karajgi69e80a02012-05-15 03:54:04 -0700168 (user, tenant, role) = self._get_role_params()
rajalakshmi-ganesan8ba945e2012-08-01 15:43:19 +0530169 resp, user_role = self.client.assign_user_role(tenant['id'],
170 user['id'],
171 role['id'])
Rohit Karajgi69e80a02012-05-15 03:54:04 -0700172 self.assertRaises(exceptions.Unauthorized,
Zhongyue Luo79d8d362012-09-25 13:49:27 +0800173 self.non_admin_client.remove_user_role,
174 tenant['id'], user['id'], role['id'])
Rohit Karajgi69e80a02012-05-15 03:54:04 -0700175
176 def test_remove_user_role_request_without_token(self):
Sean Dague46c4a2b2013-01-03 17:54:17 -0500177 # Request to remove a user's role without a valid token
Rohit Karajgi69e80a02012-05-15 03:54:04 -0700178 (user, tenant, role) = self._get_role_params()
rajalakshmi-ganesan8ba945e2012-08-01 15:43:19 +0530179 resp, user_role = self.client.assign_user_role(tenant['id'],
180 user['id'],
181 role['id'])
Rohit Karajgi69e80a02012-05-15 03:54:04 -0700182 token = self.client.get_auth()
183 self.client.delete_token(token)
184 self.assertRaises(exceptions.Unauthorized,
Zhongyue Luo79d8d362012-09-25 13:49:27 +0800185 self.client.remove_user_role, tenant['id'],
186 user['id'], role['id'])
Rohit Karajgi69e80a02012-05-15 03:54:04 -0700187 self.client.clear_auth()
188
Rohit Karajgi69e80a02012-05-15 03:54:04 -0700189 def test_remove_user_role_non_existant_user(self):
Sean Dague46c4a2b2013-01-03 17:54:17 -0500190 # Attempt to remove a role from a non existent user should fail
Rohit Karajgi69e80a02012-05-15 03:54:04 -0700191 (user, tenant, role) = self._get_role_params()
rajalakshmi-ganesan8ba945e2012-08-01 15:43:19 +0530192 resp, user_role = self.client.assign_user_role(tenant['id'],
193 user['id'],
194 role['id'])
Rohit Karajgi69e80a02012-05-15 03:54:04 -0700195 self.assertRaises(exceptions.NotFound, self.client.remove_user_role,
Zhongyue Luo79d8d362012-09-25 13:49:27 +0800196 tenant['id'], 'junk-user-id-123', role['id'])
Rohit Karajgi69e80a02012-05-15 03:54:04 -0700197
Rohit Karajgi69e80a02012-05-15 03:54:04 -0700198 def test_remove_user_role_non_existant_role(self):
Sean Dague46c4a2b2013-01-03 17:54:17 -0500199 # Attempt to delete a non existent role from a user should fail
Rohit Karajgi69e80a02012-05-15 03:54:04 -0700200 (user, tenant, role) = self._get_role_params()
rajalakshmi-ganesan8ba945e2012-08-01 15:43:19 +0530201 resp, user_role = self.client.assign_user_role(tenant['id'],
202 user['id'],
203 role['id'])
Rohit Karajgi69e80a02012-05-15 03:54:04 -0700204 self.assertRaises(exceptions.NotFound, self.client.remove_user_role,
rajalakshmi-ganesan8ba945e2012-08-01 15:43:19 +0530205 tenant['id'], user['id'], 'junk-user-role-123')
206
207 def test_remove_user_role_non_existant_tenant(self):
Sean Dague46c4a2b2013-01-03 17:54:17 -0500208 # Attempt to remove a role from a non existent tenant should fail
rajalakshmi-ganesan8ba945e2012-08-01 15:43:19 +0530209 (user, tenant, role) = self._get_role_params()
210 resp, user_role = self.client.assign_user_role(tenant['id'],
211 user['id'],
212 role['id'])
213 self.assertRaises(exceptions.NotFound, self.client.remove_user_role,
214 'junk-tenant-id-123', user['id'], role['id'])
Rohit Karajgi69e80a02012-05-15 03:54:04 -0700215
216 def test_list_user_roles(self):
Sean Dague46c4a2b2013-01-03 17:54:17 -0500217 # List roles assigned to a user on tenant
Rohit Karajgi69e80a02012-05-15 03:54:04 -0700218 (user, tenant, role) = self._get_role_params()
rajalakshmi-ganesan8ba945e2012-08-01 15:43:19 +0530219 self.client.assign_user_role(tenant['id'], user['id'], role['id'])
220 resp, roles = self.client.list_user_roles(tenant['id'], user['id'])
Adam Youngddb82892013-02-15 13:10:35 -0500221 self.assert_role_in_role_list(role, roles)
Rohit Karajgi69e80a02012-05-15 03:54:04 -0700222
223 def test_list_user_roles_by_unauthorized_user(self):
Sean Dague46c4a2b2013-01-03 17:54:17 -0500224 # Non admin user should not be authorized to list a user's roles
Rohit Karajgi69e80a02012-05-15 03:54:04 -0700225 (user, tenant, role) = self._get_role_params()
rajalakshmi-ganesan8ba945e2012-08-01 15:43:19 +0530226 self.client.assign_user_role(tenant['id'], user['id'], role['id'])
Rohit Karajgi69e80a02012-05-15 03:54:04 -0700227 self.assertRaises(exceptions.Unauthorized,
rajalakshmi-ganesan8ba945e2012-08-01 15:43:19 +0530228 self.non_admin_client.list_user_roles, tenant['id'],
229 user['id'])
Rohit Karajgi69e80a02012-05-15 03:54:04 -0700230
231 def test_list_user_roles_request_without_token(self):
Sean Dague46c4a2b2013-01-03 17:54:17 -0500232 # Request to list user's roles without a valid token should fail
Rohit Karajgi69e80a02012-05-15 03:54:04 -0700233 (user, tenant, role) = self._get_role_params()
234 token = self.client.get_auth()
235 self.client.delete_token(token)
Jaroslav Henner501cacd2012-08-16 10:32:38 +0200236 try:
237 self.assertRaises(exceptions.Unauthorized,
238 self.client.list_user_roles, tenant['id'],
239 user['id'])
240 finally:
241 self.client.clear_auth()
Rohit Karajgi69e80a02012-05-15 03:54:04 -0700242
Rohit Karajgi69e80a02012-05-15 03:54:04 -0700243 def test_list_user_roles_for_non_existent_user(self):
Sean Dague46c4a2b2013-01-03 17:54:17 -0500244 # Attempt to list roles of a non existent user should fail
rajalakshmi-ganesan8ba945e2012-08-01 15:43:19 +0530245 (user, tenant, role) = self._get_role_params()
Rohit Karajgi69e80a02012-05-15 03:54:04 -0700246 self.assertRaises(exceptions.NotFound, self.client.list_user_roles,
Zhongyue Luo79d8d362012-09-25 13:49:27 +0800247 tenant['id'], 'junk-role-aabbcc11')
Vincent Hou6b8a7b72012-08-25 01:24:33 +0800248
249
Attila Fazekas0d0c6162013-02-24 09:14:23 +0100250class RolesTestXML(RolesTestJSON):
251 _interface = 'xml'