blob: 08b86caf5240372daca7be9b976abe35c628d0b0 [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
Sean Dague1937d092013-05-17 16:36:38 -040018from tempest.api.identity import base
chris fattarsi8ed39ac2012-04-30 14:11:27 -070019from tempest.common.utils.data_utils import rand_name
Matthew Treinisha83a16e2012-12-07 13:44:02 -050020from tempest import exceptions
Giampaolo Lauriaea294952013-05-15 08:52:04 -040021from tempest.test import attr
chris fattarsi8ed39ac2012-04-30 14:11:27 -070022
23
Attila Fazekas0d0c6162013-02-24 09:14:23 +010024class RolesTestJSON(base.BaseIdentityAdminTest):
25 _interface = 'json'
chris fattarsi8ed39ac2012-04-30 14:11:27 -070026
Attila Fazekas0d0c6162013-02-24 09:14:23 +010027 @classmethod
chris fattarsi8ed39ac2012-04-30 14:11:27 -070028 def setUpClass(cls):
Attila Fazekas0d0c6162013-02-24 09:14:23 +010029 super(RolesTestJSON, cls).setUpClass()
chris fattarsi8ed39ac2012-04-30 14:11:27 -070030 for _ in xrange(5):
Rohit Karajgi69e80a02012-05-15 03:54:04 -070031 resp, role = cls.client.create_role(rand_name('role-'))
32 cls.data.roles.append(role)
chris fattarsi8ed39ac2012-04-30 14:11:27 -070033
Rohit Karajgi69e80a02012-05-15 03:54:04 -070034 def _get_role_params(self):
35 self.data.setup_test_user()
36 self.data.setup_test_role()
37 user = self.get_user_by_name(self.data.test_user)
38 tenant = self.get_tenant_by_name(self.data.test_tenant)
39 role = self.get_role_by_name(self.data.test_role)
40 return (user, tenant, role)
chris fattarsi8ed39ac2012-04-30 14:11:27 -070041
Attila Fazekas0d0c6162013-02-24 09:14:23 +010042 def assert_role_in_role_list(self, role, roles):
43 found = False
44 for user_role in roles:
45 if user_role['id'] == role['id']:
46 found = True
47 self.assertTrue(found, "assigned role was not in list")
48
Giampaolo Lauriaea294952013-05-15 08:52:04 -040049 @attr(type='gate')
chris fattarsi8ed39ac2012-04-30 14:11:27 -070050 def test_list_roles(self):
Sean Dague46c4a2b2013-01-03 17:54:17 -050051 # Return a list of all roles
chris fattarsi8ed39ac2012-04-30 14:11:27 -070052 resp, body = self.client.list_roles()
Rohit Karajgi69e80a02012-05-15 03:54:04 -070053 found = [role for role in body if role in self.data.roles]
chris fattarsi8ed39ac2012-04-30 14:11:27 -070054 self.assertTrue(any(found))
Rohit Karajgi69e80a02012-05-15 03:54:04 -070055 self.assertEqual(len(found), len(self.data.roles))
56
Giampaolo Lauriaea294952013-05-15 08:52:04 -040057 @attr(type='gate')
Rohit Karajgi69e80a02012-05-15 03:54:04 -070058 def test_list_roles_by_unauthorized_user(self):
Sean Dague46c4a2b2013-01-03 17:54:17 -050059 # Non admin user should not be able to list roles
Rohit Karajgi69e80a02012-05-15 03:54:04 -070060 self.assertRaises(exceptions.Unauthorized,
Zhongyue Luo79d8d362012-09-25 13:49:27 +080061 self.non_admin_client.list_roles)
Rohit Karajgi69e80a02012-05-15 03:54:04 -070062
Giampaolo Lauriaea294952013-05-15 08:52:04 -040063 @attr(type='gate')
Rohit Karajgi69e80a02012-05-15 03:54:04 -070064 def test_list_roles_request_without_token(self):
Sean Dague46c4a2b2013-01-03 17:54:17 -050065 # Request to list roles without a valid token should fail
Rohit Karajgi69e80a02012-05-15 03:54:04 -070066 token = self.client.get_auth()
67 self.client.delete_token(token)
68 self.assertRaises(exceptions.Unauthorized, self.client.list_roles)
69 self.client.clear_auth()
chris fattarsi8ed39ac2012-04-30 14:11:27 -070070
Giampaolo Lauriaea294952013-05-15 08:52:04 -040071 @attr(type='gate')
chris fattarsi8ed39ac2012-04-30 14:11:27 -070072 def test_role_create_delete(self):
Sean Dague46c4a2b2013-01-03 17:54:17 -050073 # Role should be created, verified, and deleted
chris fattarsi8ed39ac2012-04-30 14:11:27 -070074 role_name = rand_name('role-test-')
75 resp, body = self.client.create_role(role_name)
76 self.assertTrue('status' in resp)
77 self.assertTrue(resp['status'].startswith('2'))
78 self.assertEqual(role_name, body['name'])
79
80 resp, body = self.client.list_roles()
81 found = [role for role in body if role['name'] == role_name]
82 self.assertTrue(any(found))
83
84 resp, body = self.client.delete_role(found[0]['id'])
85 self.assertTrue('status' in resp)
86 self.assertTrue(resp['status'].startswith('2'))
87
88 resp, body = self.client.list_roles()
89 found = [role for role in body if role['name'] == role_name]
90 self.assertFalse(any(found))
91
Giampaolo Lauriaea294952013-05-15 08:52:04 -040092 @attr(type='gate')
chris fattarsi8ed39ac2012-04-30 14:11:27 -070093 def test_role_create_blank_name(self):
Sean Dague46c4a2b2013-01-03 17:54:17 -050094 # Should not be able to create a role with a blank name
David Kranz28e35c52012-07-10 10:14:38 -040095 self.assertRaises(exceptions.BadRequest, self.client.create_role, '')
chris fattarsi8ed39ac2012-04-30 14:11:27 -070096
Giampaolo Lauriaea294952013-05-15 08:52:04 -040097 @attr(type='gate')
chris fattarsi8ed39ac2012-04-30 14:11:27 -070098 def test_role_create_duplicate(self):
Sean Dague46c4a2b2013-01-03 17:54:17 -050099 # Role names should be unique
chris fattarsi8ed39ac2012-04-30 14:11:27 -0700100 role_name = rand_name('role-dup-')
101 resp, body = self.client.create_role(role_name)
102 role1_id = body.get('id')
103 self.assertTrue('status' in resp)
104 self.assertTrue(resp['status'].startswith('2'))
Chris Yeohe04628e2013-02-25 17:12:21 +1030105 self.addCleanup(self.client.delete_role, role1_id)
106 self.assertRaises(exceptions.Duplicate, self.client.create_role,
107 role_name)
Rohit Karajgi69e80a02012-05-15 03:54:04 -0700108
Giampaolo Lauriaea294952013-05-15 08:52:04 -0400109 @attr(type='gate')
Rohit Karajgi69e80a02012-05-15 03:54:04 -0700110 def test_assign_user_role(self):
Sean Dague46c4a2b2013-01-03 17:54:17 -0500111 # Assign a role to a user on a tenant
Rohit Karajgi69e80a02012-05-15 03:54:04 -0700112 (user, tenant, role) = self._get_role_params()
rajalakshmi-ganesan8ba945e2012-08-01 15:43:19 +0530113 self.client.assign_user_role(tenant['id'], user['id'], role['id'])
114 resp, roles = self.client.list_user_roles(tenant['id'], user['id'])
Adam Youngddb82892013-02-15 13:10:35 -0500115 self.assert_role_in_role_list(role, roles)
Rohit Karajgi69e80a02012-05-15 03:54:04 -0700116
Giampaolo Lauriaea294952013-05-15 08:52:04 -0400117 @attr(type='gate')
Rohit Karajgi69e80a02012-05-15 03:54:04 -0700118 def test_assign_user_role_by_unauthorized_user(self):
Sean Dague46c4a2b2013-01-03 17:54:17 -0500119 # Non admin user should not be authorized to assign a role to user
Rohit Karajgi69e80a02012-05-15 03:54:04 -0700120 (user, tenant, role) = self._get_role_params()
121 self.assertRaises(exceptions.Unauthorized,
122 self.non_admin_client.assign_user_role,
rajalakshmi-ganesan8ba945e2012-08-01 15:43:19 +0530123 tenant['id'], user['id'], role['id'])
Rohit Karajgi69e80a02012-05-15 03:54:04 -0700124
Giampaolo Lauriaea294952013-05-15 08:52:04 -0400125 @attr(type='gate')
Rohit Karajgi69e80a02012-05-15 03:54:04 -0700126 def test_assign_user_role_request_without_token(self):
Sean Dague46c4a2b2013-01-03 17:54:17 -0500127 # Request to assign a role to a user without a valid token
Rohit Karajgi69e80a02012-05-15 03:54:04 -0700128 (user, tenant, role) = self._get_role_params()
129 token = self.client.get_auth()
130 self.client.delete_token(token)
131 self.assertRaises(exceptions.Unauthorized,
rajalakshmi-ganesan8ba945e2012-08-01 15:43:19 +0530132 self.client.assign_user_role, tenant['id'],
133 user['id'], role['id'])
Rohit Karajgi69e80a02012-05-15 03:54:04 -0700134 self.client.clear_auth()
135
Giampaolo Lauriaea294952013-05-15 08:52:04 -0400136 @attr(type='gate')
Rohit Karajgi69e80a02012-05-15 03:54:04 -0700137 def test_assign_user_role_for_non_existent_user(self):
Sean Dague46c4a2b2013-01-03 17:54:17 -0500138 # Attempt to assign a role to a non existent user should fail
Rohit Karajgi69e80a02012-05-15 03:54:04 -0700139 (user, tenant, role) = self._get_role_params()
140 self.assertRaises(exceptions.NotFound, self.client.assign_user_role,
Zhongyue Luo79d8d362012-09-25 13:49:27 +0800141 tenant['id'], 'junk-user-id-999', role['id'])
Rohit Karajgi69e80a02012-05-15 03:54:04 -0700142
Giampaolo Lauriaea294952013-05-15 08:52:04 -0400143 @attr(type='gate')
Rohit Karajgi69e80a02012-05-15 03:54:04 -0700144 def test_assign_user_role_for_non_existent_role(self):
Sean Dague46c4a2b2013-01-03 17:54:17 -0500145 # Attempt to assign a non existent role to user 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 tenant['id'], user['id'], 'junk-role-id-12345')
Rohit Karajgi69e80a02012-05-15 03:54:04 -0700149
Giampaolo Lauriaea294952013-05-15 08:52:04 -0400150 @attr(type='gate')
Rohit Karajgi69e80a02012-05-15 03:54:04 -0700151 def test_assign_user_role_for_non_existent_tenant(self):
Sean Dague46c4a2b2013-01-03 17:54:17 -0500152 # Attempt to assign a role on a non existent tenant should fail
Rohit Karajgi69e80a02012-05-15 03:54:04 -0700153 (user, tenant, role) = self._get_role_params()
154 self.assertRaises(exceptions.NotFound, self.client.assign_user_role,
Zhongyue Luo79d8d362012-09-25 13:49:27 +0800155 'junk-tenant-1234', user['id'], role['id'])
Rohit Karajgi69e80a02012-05-15 03:54:04 -0700156
Giampaolo Lauriaea294952013-05-15 08:52:04 -0400157 @attr(type='gate')
Rohit Karajgi69e80a02012-05-15 03:54:04 -0700158 def test_assign_duplicate_user_role(self):
Sean Dague46c4a2b2013-01-03 17:54:17 -0500159 # Duplicate user role should not get assigned
Rohit Karajgi69e80a02012-05-15 03:54:04 -0700160 (user, tenant, role) = self._get_role_params()
rajalakshmi-ganesan8ba945e2012-08-01 15:43:19 +0530161 self.client.assign_user_role(tenant['id'], user['id'], role['id'])
Rohit Karajgi69e80a02012-05-15 03:54:04 -0700162 self.assertRaises(exceptions.Duplicate, self.client.assign_user_role,
rajalakshmi-ganesan8ba945e2012-08-01 15:43:19 +0530163 tenant['id'], user['id'], role['id'])
Rohit Karajgi69e80a02012-05-15 03:54:04 -0700164
Giampaolo Lauriaea294952013-05-15 08:52:04 -0400165 @attr(type='gate')
Rohit Karajgi69e80a02012-05-15 03:54:04 -0700166 def test_remove_user_role(self):
Sean Dague46c4a2b2013-01-03 17:54:17 -0500167 # Remove a role assigned to a user on a tenant
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'], role['id'])
171 resp, body = self.client.remove_user_role(tenant['id'], user['id'],
172 user_role['id'])
Rohit Karajgi69e80a02012-05-15 03:54:04 -0700173 self.assertEquals(resp['status'], '204')
174
Giampaolo Lauriaea294952013-05-15 08:52:04 -0400175 @attr(type='gate')
Rohit Karajgi69e80a02012-05-15 03:54:04 -0700176 def test_remove_user_role_by_unauthorized_user(self):
Sean Dague46c4a2b2013-01-03 17:54:17 -0500177 # Non admin user should not be authorized to remove a user's role
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 self.assertRaises(exceptions.Unauthorized,
Zhongyue Luo79d8d362012-09-25 13:49:27 +0800183 self.non_admin_client.remove_user_role,
184 tenant['id'], user['id'], role['id'])
Rohit Karajgi69e80a02012-05-15 03:54:04 -0700185
Giampaolo Lauriaea294952013-05-15 08:52:04 -0400186 @attr(type='gate')
Rohit Karajgi69e80a02012-05-15 03:54:04 -0700187 def test_remove_user_role_request_without_token(self):
Sean Dague46c4a2b2013-01-03 17:54:17 -0500188 # Request to remove a user's role without a valid token
Rohit Karajgi69e80a02012-05-15 03:54:04 -0700189 (user, tenant, role) = self._get_role_params()
rajalakshmi-ganesan8ba945e2012-08-01 15:43:19 +0530190 resp, user_role = self.client.assign_user_role(tenant['id'],
191 user['id'],
192 role['id'])
Rohit Karajgi69e80a02012-05-15 03:54:04 -0700193 token = self.client.get_auth()
194 self.client.delete_token(token)
195 self.assertRaises(exceptions.Unauthorized,
Zhongyue Luo79d8d362012-09-25 13:49:27 +0800196 self.client.remove_user_role, tenant['id'],
197 user['id'], role['id'])
Rohit Karajgi69e80a02012-05-15 03:54:04 -0700198 self.client.clear_auth()
199
Giampaolo Lauriaea294952013-05-15 08:52:04 -0400200 @attr(type='gate')
Rohit Karajgi69e80a02012-05-15 03:54:04 -0700201 def test_remove_user_role_non_existant_user(self):
Sean Dague46c4a2b2013-01-03 17:54:17 -0500202 # Attempt to remove a role from a non existent user should fail
Rohit Karajgi69e80a02012-05-15 03:54:04 -0700203 (user, tenant, role) = self._get_role_params()
rajalakshmi-ganesan8ba945e2012-08-01 15:43:19 +0530204 resp, user_role = self.client.assign_user_role(tenant['id'],
205 user['id'],
206 role['id'])
Rohit Karajgi69e80a02012-05-15 03:54:04 -0700207 self.assertRaises(exceptions.NotFound, self.client.remove_user_role,
Zhongyue Luo79d8d362012-09-25 13:49:27 +0800208 tenant['id'], 'junk-user-id-123', role['id'])
Rohit Karajgi69e80a02012-05-15 03:54:04 -0700209
Giampaolo Lauriaea294952013-05-15 08:52:04 -0400210 @attr(type='gate')
Rohit Karajgi69e80a02012-05-15 03:54:04 -0700211 def test_remove_user_role_non_existant_role(self):
Sean Dague46c4a2b2013-01-03 17:54:17 -0500212 # Attempt to delete a non existent role from a user should fail
Rohit Karajgi69e80a02012-05-15 03:54:04 -0700213 (user, tenant, role) = self._get_role_params()
rajalakshmi-ganesan8ba945e2012-08-01 15:43:19 +0530214 resp, user_role = self.client.assign_user_role(tenant['id'],
215 user['id'],
216 role['id'])
Rohit Karajgi69e80a02012-05-15 03:54:04 -0700217 self.assertRaises(exceptions.NotFound, self.client.remove_user_role,
rajalakshmi-ganesan8ba945e2012-08-01 15:43:19 +0530218 tenant['id'], user['id'], 'junk-user-role-123')
219
Giampaolo Lauriaea294952013-05-15 08:52:04 -0400220 @attr(type='gate')
rajalakshmi-ganesan8ba945e2012-08-01 15:43:19 +0530221 def test_remove_user_role_non_existant_tenant(self):
Sean Dague46c4a2b2013-01-03 17:54:17 -0500222 # Attempt to remove a role from a non existent tenant should fail
rajalakshmi-ganesan8ba945e2012-08-01 15:43:19 +0530223 (user, tenant, role) = self._get_role_params()
224 resp, user_role = self.client.assign_user_role(tenant['id'],
225 user['id'],
226 role['id'])
227 self.assertRaises(exceptions.NotFound, self.client.remove_user_role,
228 'junk-tenant-id-123', user['id'], role['id'])
Rohit Karajgi69e80a02012-05-15 03:54:04 -0700229
Giampaolo Lauriaea294952013-05-15 08:52:04 -0400230 @attr(type='gate')
Rohit Karajgi69e80a02012-05-15 03:54:04 -0700231 def test_list_user_roles(self):
Sean Dague46c4a2b2013-01-03 17:54:17 -0500232 # List roles assigned to a user on tenant
Rohit Karajgi69e80a02012-05-15 03:54:04 -0700233 (user, tenant, role) = self._get_role_params()
rajalakshmi-ganesan8ba945e2012-08-01 15:43:19 +0530234 self.client.assign_user_role(tenant['id'], user['id'], role['id'])
235 resp, roles = self.client.list_user_roles(tenant['id'], user['id'])
Adam Youngddb82892013-02-15 13:10:35 -0500236 self.assert_role_in_role_list(role, roles)
Rohit Karajgi69e80a02012-05-15 03:54:04 -0700237
Giampaolo Lauriaea294952013-05-15 08:52:04 -0400238 @attr(type='gate')
Rohit Karajgi69e80a02012-05-15 03:54:04 -0700239 def test_list_user_roles_by_unauthorized_user(self):
Sean Dague46c4a2b2013-01-03 17:54:17 -0500240 # Non admin user should not be authorized to list a user's roles
Rohit Karajgi69e80a02012-05-15 03:54:04 -0700241 (user, tenant, role) = self._get_role_params()
rajalakshmi-ganesan8ba945e2012-08-01 15:43:19 +0530242 self.client.assign_user_role(tenant['id'], user['id'], role['id'])
Rohit Karajgi69e80a02012-05-15 03:54:04 -0700243 self.assertRaises(exceptions.Unauthorized,
rajalakshmi-ganesan8ba945e2012-08-01 15:43:19 +0530244 self.non_admin_client.list_user_roles, tenant['id'],
245 user['id'])
Rohit Karajgi69e80a02012-05-15 03:54:04 -0700246
Giampaolo Lauriaea294952013-05-15 08:52:04 -0400247 @attr(type='gate')
Rohit Karajgi69e80a02012-05-15 03:54:04 -0700248 def test_list_user_roles_request_without_token(self):
Sean Dague46c4a2b2013-01-03 17:54:17 -0500249 # Request to list user's roles without a valid token should fail
Rohit Karajgi69e80a02012-05-15 03:54:04 -0700250 (user, tenant, role) = self._get_role_params()
251 token = self.client.get_auth()
252 self.client.delete_token(token)
Jaroslav Henner501cacd2012-08-16 10:32:38 +0200253 try:
254 self.assertRaises(exceptions.Unauthorized,
255 self.client.list_user_roles, tenant['id'],
256 user['id'])
257 finally:
258 self.client.clear_auth()
Rohit Karajgi69e80a02012-05-15 03:54:04 -0700259
Giampaolo Lauriaea294952013-05-15 08:52:04 -0400260 @attr(type='gate')
Rohit Karajgi69e80a02012-05-15 03:54:04 -0700261 def test_list_user_roles_for_non_existent_user(self):
Sean Dague46c4a2b2013-01-03 17:54:17 -0500262 # Attempt to list roles of a non existent user should fail
rajalakshmi-ganesan8ba945e2012-08-01 15:43:19 +0530263 (user, tenant, role) = self._get_role_params()
Rohit Karajgi69e80a02012-05-15 03:54:04 -0700264 self.assertRaises(exceptions.NotFound, self.client.list_user_roles,
Zhongyue Luo79d8d362012-09-25 13:49:27 +0800265 tenant['id'], 'junk-role-aabbcc11')
Vincent Hou6b8a7b72012-08-25 01:24:33 +0800266
267
Attila Fazekas0d0c6162013-02-24 09:14:23 +0100268class RolesTestXML(RolesTestJSON):
269 _interface = 'xml'