blob: 8205d15e89b8bdb9f691bc4a78dada3dcc6c9058 [file] [log] [blame]
Jay Pipesf38eaac2012-06-21 13:37:35 -04001# vim: tabstop=4 shiftwidth=4 softtabstop=4
2
ZhiQiang Fan39f97222013-09-20 04:49:44 +08003# Copyright 2012 OpenStack Foundation
Jay Pipesf38eaac2012-06-21 13:37:35 -04004# 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
huangtianhua35f11872013-10-11 11:34:43 +080019from tempest.common.utils import data_utils
Giampaolo Lauriaea294952013-05-15 08:52:04 -040020from tempest.test import attr
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):
huangtianhua35f11872013-10-11 11:34:43 +080030 role_name = data_utils.rand_name(name='role-')
31 resp, role = cls.client.create_role(role_name)
Rohit Karajgi69e80a02012-05-15 03:54:04 -070032 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')
chris fattarsi8ed39ac2012-04-30 14:11:27 -070058 def test_role_create_delete(self):
Sean Dague46c4a2b2013-01-03 17:54:17 -050059 # Role should be created, verified, and deleted
huangtianhua35f11872013-10-11 11:34:43 +080060 role_name = data_utils.rand_name(name='role-test-')
chris fattarsi8ed39ac2012-04-30 14:11:27 -070061 resp, body = self.client.create_role(role_name)
Attila Fazekase191cb12013-07-29 06:41:52 +020062 self.assertIn('status', resp)
chris fattarsi8ed39ac2012-04-30 14:11:27 -070063 self.assertTrue(resp['status'].startswith('2'))
64 self.assertEqual(role_name, body['name'])
65
66 resp, body = self.client.list_roles()
67 found = [role for role in body if role['name'] == role_name]
68 self.assertTrue(any(found))
69
70 resp, body = self.client.delete_role(found[0]['id'])
Attila Fazekase191cb12013-07-29 06:41:52 +020071 self.assertIn('status', resp)
chris fattarsi8ed39ac2012-04-30 14:11:27 -070072 self.assertTrue(resp['status'].startswith('2'))
73
74 resp, body = self.client.list_roles()
75 found = [role for role in body if role['name'] == role_name]
76 self.assertFalse(any(found))
77
Giampaolo Lauriaea294952013-05-15 08:52:04 -040078 @attr(type='gate')
Rohit Karajgi69e80a02012-05-15 03:54:04 -070079 def test_assign_user_role(self):
Sean Dague46c4a2b2013-01-03 17:54:17 -050080 # Assign a role to a user on a tenant
Rohit Karajgi69e80a02012-05-15 03:54:04 -070081 (user, tenant, role) = self._get_role_params()
rajalakshmi-ganesan8ba945e2012-08-01 15:43:19 +053082 self.client.assign_user_role(tenant['id'], user['id'], role['id'])
83 resp, roles = self.client.list_user_roles(tenant['id'], user['id'])
Adam Youngddb82892013-02-15 13:10:35 -050084 self.assert_role_in_role_list(role, roles)
Rohit Karajgi69e80a02012-05-15 03:54:04 -070085
Giampaolo Lauriaea294952013-05-15 08:52:04 -040086 @attr(type='gate')
Rohit Karajgi69e80a02012-05-15 03:54:04 -070087 def test_remove_user_role(self):
Sean Dague46c4a2b2013-01-03 17:54:17 -050088 # Remove a role assigned to a user on a tenant
Rohit Karajgi69e80a02012-05-15 03:54:04 -070089 (user, tenant, role) = self._get_role_params()
rajalakshmi-ganesan8ba945e2012-08-01 15:43:19 +053090 resp, user_role = self.client.assign_user_role(tenant['id'],
91 user['id'], role['id'])
92 resp, body = self.client.remove_user_role(tenant['id'], user['id'],
93 user_role['id'])
Chang Bo Guofc77e932013-09-16 17:38:26 -070094 self.assertEqual(resp['status'], '204')
Rohit Karajgi69e80a02012-05-15 03:54:04 -070095
Giampaolo Lauriaea294952013-05-15 08:52:04 -040096 @attr(type='gate')
Rohit Karajgi69e80a02012-05-15 03:54:04 -070097 def test_list_user_roles(self):
Sean Dague46c4a2b2013-01-03 17:54:17 -050098 # List roles assigned to a user on tenant
Rohit Karajgi69e80a02012-05-15 03:54:04 -070099 (user, tenant, role) = self._get_role_params()
rajalakshmi-ganesan8ba945e2012-08-01 15:43:19 +0530100 self.client.assign_user_role(tenant['id'], user['id'], role['id'])
101 resp, roles = self.client.list_user_roles(tenant['id'], user['id'])
Adam Youngddb82892013-02-15 13:10:35 -0500102 self.assert_role_in_role_list(role, roles)
Rohit Karajgi69e80a02012-05-15 03:54:04 -0700103
Vincent Hou6b8a7b72012-08-25 01:24:33 +0800104
Attila Fazekas0d0c6162013-02-24 09:14:23 +0100105class RolesTestXML(RolesTestJSON):
106 _interface = 'xml'