blob: 3f3d16e463dc56eb4cb0217d88bf6f7ada94dacc [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
llg821243b20502014-02-22 10:32:49 +080016from six import moves
17
Sean Dague1937d092013-05-17 16:36:38 -040018from tempest.api.identity import base
Fei Long Wangd39431f2015-05-14 11:30:48 +120019from tempest.common.utils import data_utils
Masayuki Igawadf154682014-03-19 18:32:00 +090020from tempest import test
chris fattarsi8ed39ac2012-04-30 14:11:27 -070021
22
Matthew Treinishdb2c5972014-01-31 22:18:59 +000023class RolesTestJSON(base.BaseIdentityV2AdminTest):
chris fattarsi8ed39ac2012-04-30 14:11:27 -070024
Attila Fazekas0d0c6162013-02-24 09:14:23 +010025 @classmethod
Andrea Frittoli7688e742014-09-15 12:38:22 +010026 def resource_setup(cls):
27 super(RolesTestJSON, cls).resource_setup()
llg821243b20502014-02-22 10:32:49 +080028 for _ in moves.xrange(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']
Rohit Karajgi69e80a02012-05-15 03:54:04 -070031 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 Hoge7579c1a2015-02-26 14:12:15 -080048 @test.idempotent_id('75d9593f-50b7-4fcf-bd64-e3fb4a278e23')
chris fattarsi8ed39ac2012-04-30 14:11:27 -070049 def test_list_roles(self):
Anita Kunoa3702fe2014-08-04 11:20:25 +100050 """Return a list of all roles."""
Daniel Mellado6b16b922015-12-07 12:43:08 +000051 body = self.roles_client.list_roles()['roles']
Rohit Karajgi69e80a02012-05-15 03:54:04 -070052 found = [role for role in body if role in self.data.roles]
chris fattarsi8ed39ac2012-04-30 14:11:27 -070053 self.assertTrue(any(found))
Rohit Karajgi69e80a02012-05-15 03:54:04 -070054 self.assertEqual(len(found), len(self.data.roles))
55
Chris Hoge7579c1a2015-02-26 14:12:15 -080056 @test.idempotent_id('c62d909d-6c21-48c0-ae40-0a0760e6db5e')
chris fattarsi8ed39ac2012-04-30 14:11:27 -070057 def test_role_create_delete(self):
Anita Kunoa3702fe2014-08-04 11:20:25 +100058 """Role should be created, verified, and deleted."""
Ken'ichi Ohmichi96508472015-03-23 01:43:42 +000059 role_name = data_utils.rand_name(name='role-test')
piyush110786afaaf262015-12-11 18:54:05 +053060 body = self.roles_client.create_role(name=role_name)['role']
chris fattarsi8ed39ac2012-04-30 14:11:27 -070061 self.assertEqual(role_name, body['name'])
62
Daniel Mellado6b16b922015-12-07 12:43:08 +000063 body = self.roles_client.list_roles()['roles']
chris fattarsi8ed39ac2012-04-30 14:11:27 -070064 found = [role for role in body if role['name'] == role_name]
65 self.assertTrue(any(found))
66
Daniel Mellado6b16b922015-12-07 12:43:08 +000067 body = self.roles_client.delete_role(found[0]['id'])
chris fattarsi8ed39ac2012-04-30 14:11:27 -070068
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]
71 self.assertFalse(any(found))
72
Chris Hoge7579c1a2015-02-26 14:12:15 -080073 @test.idempotent_id('db6870bd-a6ed-43be-a9b1-2f10a5c9994f')
Gong Zhangcb6b8862014-02-20 15:14:05 +080074 def test_get_role_by_id(self):
Anita Kunoa3702fe2014-08-04 11:20:25 +100075 """Get a role by its id."""
Gong Zhangcb6b8862014-02-20 15:14:05 +080076 self.data.setup_test_role()
77 role_id = self.data.role['id']
78 role_name = self.data.role['name']
Daniel Mellado6b16b922015-12-07 12:43:08 +000079 body = self.roles_client.show_role(role_id)['role']
Gong Zhangcb6b8862014-02-20 15:14:05 +080080 self.assertEqual(role_id, body['id'])
81 self.assertEqual(role_name, body['name'])
82
Chris Hoge7579c1a2015-02-26 14:12:15 -080083 @test.idempotent_id('0146f675-ffbd-4208-b3a4-60eb628dbc5e')
Rohit Karajgi69e80a02012-05-15 03:54:04 -070084 def test_assign_user_role(self):
Anita Kunoa3702fe2014-08-04 11:20:25 +100085 """Assign a role to a user on a tenant."""
Rohit Karajgi69e80a02012-05-15 03:54:04 -070086 (user, tenant, role) = self._get_role_params()
Daniel Mellado6b16b922015-12-07 12:43:08 +000087 self.roles_client.assign_user_role(tenant['id'], user['id'],
88 role['id'])
89 roles = self.roles_client.list_user_roles(tenant['id'],
90 user['id'])['roles']
Adam Youngddb82892013-02-15 13:10:35 -050091 self.assert_role_in_role_list(role, roles)
Rohit Karajgi69e80a02012-05-15 03:54:04 -070092
Chris Hoge7579c1a2015-02-26 14:12:15 -080093 @test.idempotent_id('f0b9292c-d3ba-4082-aa6c-440489beef69')
Rohit Karajgi69e80a02012-05-15 03:54:04 -070094 def test_remove_user_role(self):
Anita Kunoa3702fe2014-08-04 11:20:25 +100095 """Remove a role assigned to a user on a tenant."""
Rohit Karajgi69e80a02012-05-15 03:54:04 -070096 (user, tenant, role) = self._get_role_params()
Daniel Mellado6b16b922015-12-07 12:43:08 +000097 user_role = self.roles_client.assign_user_role(tenant['id'],
98 user['id'],
99 role['id'])['role']
100 self.roles_client.delete_user_role(tenant['id'], user['id'],
101 user_role['id'])
Rohit Karajgi69e80a02012-05-15 03:54:04 -0700102
Chris Hoge7579c1a2015-02-26 14:12:15 -0800103 @test.idempotent_id('262e1e3e-ed71-4edd-a0e5-d64e83d66d05')
Rohit Karajgi69e80a02012-05-15 03:54:04 -0700104 def test_list_user_roles(self):
Anita Kunoa3702fe2014-08-04 11:20:25 +1000105 """List roles assigned to a user on tenant."""
Rohit Karajgi69e80a02012-05-15 03:54:04 -0700106 (user, tenant, role) = self._get_role_params()
Daniel Mellado6b16b922015-12-07 12:43:08 +0000107 self.roles_client.assign_user_role(tenant['id'], user['id'],
108 role['id'])
109 roles = self.roles_client.list_user_roles(tenant['id'],
110 user['id'])['roles']
Adam Youngddb82892013-02-15 13:10:35 -0500111 self.assert_role_in_role_list(role, roles)