blob: 124bb5f66570d1ff9a3d590c525f2eb992c64612 [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
Ken'ichi Ohmichi7bd25752017-03-10 10:45:39 -080017from tempest.lib.common.utils import data_utils
Castulo J. Martineze3adee42016-07-14 10:40:08 -070018from tempest.lib.common.utils import test_utils
Ken'ichi Ohmichieeabdd22017-01-27 17:46:00 -080019from tempest.lib import decorators
chris fattarsi8ed39ac2012-04-30 14:11:27 -070020
21
Matthew Treinishdb2c5972014-01-31 22:18:59 +000022class RolesTestJSON(base.BaseIdentityV2AdminTest):
chris fattarsi8ed39ac2012-04-30 14:11:27 -070023
Attila Fazekas0d0c6162013-02-24 09:14:23 +010024 @classmethod
Andrea Frittoli7688e742014-09-15 12:38:22 +010025 def resource_setup(cls):
26 super(RolesTestJSON, cls).resource_setup()
Castulo J. Martineze3adee42016-07-14 10:40:08 -070027 cls.roles = list()
Sirushti Murugesan12dc9732016-07-13 22:49:17 +053028 for _ in range(5):
Ken'ichi Ohmichi96508472015-03-23 01:43:42 +000029 role_name = data_utils.rand_name(name='role')
piyush110786afaaf262015-12-11 18:54:05 +053030 role = cls.roles_client.create_role(name=role_name)['role']
Castulo J. Martineze3adee42016-07-14 10:40:08 -070031 cls.roles.append(role)
32
33 @classmethod
34 def resource_cleanup(cls):
35 super(RolesTestJSON, cls).resource_cleanup()
36 for role in cls.roles:
37 cls.roles_client.delete_role(role['id'])
chris fattarsi8ed39ac2012-04-30 14:11:27 -070038
Rohit Karajgi69e80a02012-05-15 03:54:04 -070039 def _get_role_params(self):
Castulo J. Martineze3adee42016-07-14 10:40:08 -070040 user = self.setup_test_user()
41 tenant = self.tenants_client.show_tenant(user['tenantId'])['tenant']
42 role = self.setup_test_role()
Rohit Karajgi69e80a02012-05-15 03:54:04 -070043 return (user, tenant, role)
chris fattarsi8ed39ac2012-04-30 14:11:27 -070044
Attila Fazekas0d0c6162013-02-24 09:14:23 +010045 def assert_role_in_role_list(self, role, roles):
46 found = False
47 for user_role in roles:
48 if user_role['id'] == role['id']:
49 found = True
50 self.assertTrue(found, "assigned role was not in list")
51
Ken'ichi Ohmichieeabdd22017-01-27 17:46:00 -080052 @decorators.idempotent_id('75d9593f-50b7-4fcf-bd64-e3fb4a278e23')
chris fattarsi8ed39ac2012-04-30 14:11:27 -070053 def test_list_roles(self):
Anita Kunoa3702fe2014-08-04 11:20:25 +100054 """Return a list of all roles."""
Daniel Mellado6b16b922015-12-07 12:43:08 +000055 body = self.roles_client.list_roles()['roles']
Castulo J. Martineze3adee42016-07-14 10:40:08 -070056 found = [role for role in body if role in self.roles]
zhufl63f059e2017-06-12 13:56:18 +080057 self.assertNotEmpty(found)
Castulo J. Martineze3adee42016-07-14 10:40:08 -070058 self.assertEqual(len(found), len(self.roles))
Rohit Karajgi69e80a02012-05-15 03:54:04 -070059
Ken'ichi Ohmichieeabdd22017-01-27 17:46:00 -080060 @decorators.idempotent_id('c62d909d-6c21-48c0-ae40-0a0760e6db5e')
chris fattarsi8ed39ac2012-04-30 14:11:27 -070061 def test_role_create_delete(self):
Anita Kunoa3702fe2014-08-04 11:20:25 +100062 """Role should be created, verified, and deleted."""
Ken'ichi Ohmichi96508472015-03-23 01:43:42 +000063 role_name = data_utils.rand_name(name='role-test')
piyush110786afaaf262015-12-11 18:54:05 +053064 body = self.roles_client.create_role(name=role_name)['role']
Castulo J. Martineze3adee42016-07-14 10:40:08 -070065 self.addCleanup(test_utils.call_and_ignore_notfound_exc,
66 self.roles_client.delete_role, body['id'])
chris fattarsi8ed39ac2012-04-30 14:11:27 -070067 self.assertEqual(role_name, body['name'])
68
Daniel Mellado6b16b922015-12-07 12:43:08 +000069 body = self.roles_client.list_roles()['roles']
chris fattarsi8ed39ac2012-04-30 14:11:27 -070070 found = [role for role in body if role['name'] == role_name]
zhufl63f059e2017-06-12 13:56:18 +080071 self.assertNotEmpty(found)
chris fattarsi8ed39ac2012-04-30 14:11:27 -070072
Daniel Mellado6b16b922015-12-07 12:43:08 +000073 body = self.roles_client.delete_role(found[0]['id'])
chris fattarsi8ed39ac2012-04-30 14:11:27 -070074
Daniel Mellado6b16b922015-12-07 12:43:08 +000075 body = self.roles_client.list_roles()['roles']
chris fattarsi8ed39ac2012-04-30 14:11:27 -070076 found = [role for role in body if role['name'] == role_name]
zhufl63f059e2017-06-12 13:56:18 +080077 self.assertEmpty(found)
chris fattarsi8ed39ac2012-04-30 14:11:27 -070078
Ken'ichi Ohmichieeabdd22017-01-27 17:46:00 -080079 @decorators.idempotent_id('db6870bd-a6ed-43be-a9b1-2f10a5c9994f')
Gong Zhangcb6b8862014-02-20 15:14:05 +080080 def test_get_role_by_id(self):
Anita Kunoa3702fe2014-08-04 11:20:25 +100081 """Get a role by its id."""
Castulo J. Martineze3adee42016-07-14 10:40:08 -070082 role = self.setup_test_role()
83 role_id = role['id']
84 role_name = role['name']
Daniel Mellado6b16b922015-12-07 12:43:08 +000085 body = self.roles_client.show_role(role_id)['role']
Gong Zhangcb6b8862014-02-20 15:14:05 +080086 self.assertEqual(role_id, body['id'])
87 self.assertEqual(role_name, body['name'])
88
Ken'ichi Ohmichieeabdd22017-01-27 17:46:00 -080089 @decorators.idempotent_id('0146f675-ffbd-4208-b3a4-60eb628dbc5e')
Rohit Karajgi69e80a02012-05-15 03:54:04 -070090 def test_assign_user_role(self):
Anita Kunoa3702fe2014-08-04 11:20:25 +100091 """Assign a role to a user on a tenant."""
Rohit Karajgi69e80a02012-05-15 03:54:04 -070092 (user, tenant, role) = self._get_role_params()
ghanshyam50894fc2016-06-17 13:20:25 +090093 self.roles_client.create_user_role_on_project(tenant['id'],
94 user['id'],
95 role['id'])
96 roles = self.roles_client.list_user_roles_on_project(
97 tenant['id'], user['id'])['roles']
Adam Youngddb82892013-02-15 13:10:35 -050098 self.assert_role_in_role_list(role, roles)
Rohit Karajgi69e80a02012-05-15 03:54:04 -070099
Ken'ichi Ohmichieeabdd22017-01-27 17:46:00 -0800100 @decorators.idempotent_id('f0b9292c-d3ba-4082-aa6c-440489beef69')
Rohit Karajgi69e80a02012-05-15 03:54:04 -0700101 def test_remove_user_role(self):
Anita Kunoa3702fe2014-08-04 11:20:25 +1000102 """Remove a role assigned to a user on a tenant."""
Rohit Karajgi69e80a02012-05-15 03:54:04 -0700103 (user, tenant, role) = self._get_role_params()
ghanshyam50894fc2016-06-17 13:20:25 +0900104 user_role = self.roles_client.create_user_role_on_project(
105 tenant['id'], user['id'], role['id'])['role']
106 self.roles_client.delete_role_from_user_on_project(tenant['id'],
107 user['id'],
108 user_role['id'])
Rohit Karajgi69e80a02012-05-15 03:54:04 -0700109
Ken'ichi Ohmichieeabdd22017-01-27 17:46:00 -0800110 @decorators.idempotent_id('262e1e3e-ed71-4edd-a0e5-d64e83d66d05')
Rohit Karajgi69e80a02012-05-15 03:54:04 -0700111 def test_list_user_roles(self):
Anita Kunoa3702fe2014-08-04 11:20:25 +1000112 """List roles assigned to a user on tenant."""
Rohit Karajgi69e80a02012-05-15 03:54:04 -0700113 (user, tenant, role) = self._get_role_params()
ghanshyam50894fc2016-06-17 13:20:25 +0900114 self.roles_client.create_user_role_on_project(tenant['id'],
115 user['id'],
116 role['id'])
117 roles = self.roles_client.list_user_roles_on_project(
118 tenant['id'], user['id'])['roles']
Adam Youngddb82892013-02-15 13:10:35 -0500119 self.assert_role_in_role_list(role, roles)