blob: aa6496922f5d67eb647a5056a0d87357ec97246f [file] [log] [blame]
ZhiQiang Fan39f97222013-09-20 04:49:44 +08001# Copyright 2012 OpenStack Foundation
Jay Pipesf38eaac2012-06-21 13:37:35 -04002# All Rights Reserved.
3#
4# Licensed under the Apache License, Version 2.0 (the "License"); you may
5# not use this file except in compliance with the License. You may obtain
6# a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13# License for the specific language governing permissions and limitations
14# under the License.
15
Sean Dague1937d092013-05-17 16:36:38 -040016from tempest.api.identity import base
huangtianhua35f11872013-10-11 11:34:43 +080017from tempest.common.utils import data_utils
Giampaolo Lauriaea294952013-05-15 08:52:04 -040018from tempest.test import attr
chris fattarsi8ed39ac2012-04-30 14:11:27 -070019
20
Matthew Treinishdb2c5972014-01-31 22:18:59 +000021class RolesTestJSON(base.BaseIdentityV2AdminTest):
Attila Fazekas0d0c6162013-02-24 09:14:23 +010022 _interface = 'json'
chris fattarsi8ed39ac2012-04-30 14:11:27 -070023
Attila Fazekas0d0c6162013-02-24 09:14:23 +010024 @classmethod
chris fattarsi8ed39ac2012-04-30 14:11:27 -070025 def setUpClass(cls):
Attila Fazekas0d0c6162013-02-24 09:14:23 +010026 super(RolesTestJSON, cls).setUpClass()
chris fattarsi8ed39ac2012-04-30 14:11:27 -070027 for _ in xrange(5):
huangtianhua35f11872013-10-11 11:34:43 +080028 role_name = data_utils.rand_name(name='role-')
29 resp, role = cls.client.create_role(role_name)
Rohit Karajgi69e80a02012-05-15 03:54:04 -070030 cls.data.roles.append(role)
chris fattarsi8ed39ac2012-04-30 14:11:27 -070031
Rohit Karajgi69e80a02012-05-15 03:54:04 -070032 def _get_role_params(self):
33 self.data.setup_test_user()
34 self.data.setup_test_role()
35 user = self.get_user_by_name(self.data.test_user)
36 tenant = self.get_tenant_by_name(self.data.test_tenant)
37 role = self.get_role_by_name(self.data.test_role)
38 return (user, tenant, role)
chris fattarsi8ed39ac2012-04-30 14:11:27 -070039
Attila Fazekas0d0c6162013-02-24 09:14:23 +010040 def assert_role_in_role_list(self, role, roles):
41 found = False
42 for user_role in roles:
43 if user_role['id'] == role['id']:
44 found = True
45 self.assertTrue(found, "assigned role was not in list")
46
Giampaolo Lauriaea294952013-05-15 08:52:04 -040047 @attr(type='gate')
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
Giampaolo Lauriaea294952013-05-15 08:52:04 -040055 @attr(type='gate')
chris fattarsi8ed39ac2012-04-30 14:11:27 -070056 def test_role_create_delete(self):
Sean Dague46c4a2b2013-01-03 17:54:17 -050057 # Role should be created, verified, and deleted
huangtianhua35f11872013-10-11 11:34:43 +080058 role_name = data_utils.rand_name(name='role-test-')
chris fattarsi8ed39ac2012-04-30 14:11:27 -070059 resp, body = self.client.create_role(role_name)
Attila Fazekase191cb12013-07-29 06:41:52 +020060 self.assertIn('status', resp)
chris fattarsi8ed39ac2012-04-30 14:11:27 -070061 self.assertTrue(resp['status'].startswith('2'))
62 self.assertEqual(role_name, body['name'])
63
64 resp, body = self.client.list_roles()
65 found = [role for role in body if role['name'] == role_name]
66 self.assertTrue(any(found))
67
68 resp, body = self.client.delete_role(found[0]['id'])
Attila Fazekase191cb12013-07-29 06:41:52 +020069 self.assertIn('status', resp)
chris fattarsi8ed39ac2012-04-30 14:11:27 -070070 self.assertTrue(resp['status'].startswith('2'))
71
72 resp, body = self.client.list_roles()
73 found = [role for role in body if role['name'] == role_name]
74 self.assertFalse(any(found))
75
Giampaolo Lauriaea294952013-05-15 08:52:04 -040076 @attr(type='gate')
Rohit Karajgi69e80a02012-05-15 03:54:04 -070077 def test_assign_user_role(self):
Sean Dague46c4a2b2013-01-03 17:54:17 -050078 # Assign a role to a user on a tenant
Rohit Karajgi69e80a02012-05-15 03:54:04 -070079 (user, tenant, role) = self._get_role_params()
rajalakshmi-ganesan8ba945e2012-08-01 15:43:19 +053080 self.client.assign_user_role(tenant['id'], user['id'], role['id'])
81 resp, roles = self.client.list_user_roles(tenant['id'], user['id'])
Adam Youngddb82892013-02-15 13:10:35 -050082 self.assert_role_in_role_list(role, roles)
Rohit Karajgi69e80a02012-05-15 03:54:04 -070083
Giampaolo Lauriaea294952013-05-15 08:52:04 -040084 @attr(type='gate')
Rohit Karajgi69e80a02012-05-15 03:54:04 -070085 def test_remove_user_role(self):
Sean Dague46c4a2b2013-01-03 17:54:17 -050086 # Remove a role assigned to a user on a tenant
Rohit Karajgi69e80a02012-05-15 03:54:04 -070087 (user, tenant, role) = self._get_role_params()
rajalakshmi-ganesan8ba945e2012-08-01 15:43:19 +053088 resp, user_role = self.client.assign_user_role(tenant['id'],
89 user['id'], role['id'])
90 resp, body = self.client.remove_user_role(tenant['id'], user['id'],
91 user_role['id'])
Chang Bo Guofc77e932013-09-16 17:38:26 -070092 self.assertEqual(resp['status'], '204')
Rohit Karajgi69e80a02012-05-15 03:54:04 -070093
Giampaolo Lauriaea294952013-05-15 08:52:04 -040094 @attr(type='gate')
Rohit Karajgi69e80a02012-05-15 03:54:04 -070095 def test_list_user_roles(self):
Sean Dague46c4a2b2013-01-03 17:54:17 -050096 # List roles assigned to a user on tenant
Rohit Karajgi69e80a02012-05-15 03:54:04 -070097 (user, tenant, role) = self._get_role_params()
rajalakshmi-ganesan8ba945e2012-08-01 15:43:19 +053098 self.client.assign_user_role(tenant['id'], user['id'], role['id'])
99 resp, roles = self.client.list_user_roles(tenant['id'], user['id'])
Adam Youngddb82892013-02-15 13:10:35 -0500100 self.assert_role_in_role_list(role, roles)
Rohit Karajgi69e80a02012-05-15 03:54:04 -0700101
Vincent Hou6b8a7b72012-08-25 01:24:33 +0800102
Attila Fazekas0d0c6162013-02-24 09:14:23 +0100103class RolesTestXML(RolesTestJSON):
104 _interface = 'xml'