blob: 3eb6c080a420cc258d0d35c0ba2fd037ba4129ec [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
huangtianhua35f11872013-10-11 11:34:43 +080019from 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):
huangtianhua35f11872013-10-11 11:34:43 +080029 role_name = data_utils.rand_name(name='role-')
David Kranzb7afa922014-12-30 10:56:26 -050030 role = cls.client.create_role(role_name)
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
Masayuki Igawadf154682014-03-19 18:32:00 +090048 @test.attr(type='gate')
Chris Hoge7579c1a2015-02-26 14:12:15 -080049 @test.idempotent_id('75d9593f-50b7-4fcf-bd64-e3fb4a278e23')
chris fattarsi8ed39ac2012-04-30 14:11:27 -070050 def test_list_roles(self):
Anita Kunoa3702fe2014-08-04 11:20:25 +100051 """Return a list of all roles."""
David Kranzb7afa922014-12-30 10:56:26 -050052 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
Masayuki Igawadf154682014-03-19 18:32:00 +090057 @test.attr(type='gate')
Chris Hoge7579c1a2015-02-26 14:12:15 -080058 @test.idempotent_id('c62d909d-6c21-48c0-ae40-0a0760e6db5e')
chris fattarsi8ed39ac2012-04-30 14:11:27 -070059 def test_role_create_delete(self):
Anita Kunoa3702fe2014-08-04 11:20:25 +100060 """Role should be created, verified, and deleted."""
huangtianhua35f11872013-10-11 11:34:43 +080061 role_name = data_utils.rand_name(name='role-test-')
David Kranzb7afa922014-12-30 10:56:26 -050062 body = self.client.create_role(role_name)
chris fattarsi8ed39ac2012-04-30 14:11:27 -070063 self.assertEqual(role_name, body['name'])
64
David Kranzb7afa922014-12-30 10:56:26 -050065 body = self.client.list_roles()
chris fattarsi8ed39ac2012-04-30 14:11:27 -070066 found = [role for role in body if role['name'] == role_name]
67 self.assertTrue(any(found))
68
David Kranzb7afa922014-12-30 10:56:26 -050069 body = self.client.delete_role(found[0]['id'])
chris fattarsi8ed39ac2012-04-30 14:11:27 -070070
David Kranzb7afa922014-12-30 10:56:26 -050071 body = self.client.list_roles()
chris fattarsi8ed39ac2012-04-30 14:11:27 -070072 found = [role for role in body if role['name'] == role_name]
73 self.assertFalse(any(found))
74
Masayuki Igawadf154682014-03-19 18:32:00 +090075 @test.attr(type='gate')
Chris Hoge7579c1a2015-02-26 14:12:15 -080076 @test.idempotent_id('db6870bd-a6ed-43be-a9b1-2f10a5c9994f')
Gong Zhangcb6b8862014-02-20 15:14:05 +080077 def test_get_role_by_id(self):
Anita Kunoa3702fe2014-08-04 11:20:25 +100078 """Get a role by its id."""
Gong Zhangcb6b8862014-02-20 15:14:05 +080079 self.data.setup_test_role()
80 role_id = self.data.role['id']
81 role_name = self.data.role['name']
David Kranzb7afa922014-12-30 10:56:26 -050082 body = self.client.get_role(role_id)
Gong Zhangcb6b8862014-02-20 15:14:05 +080083 self.assertEqual(role_id, body['id'])
84 self.assertEqual(role_name, body['name'])
85
86 @test.attr(type='gate')
Chris Hoge7579c1a2015-02-26 14:12:15 -080087 @test.idempotent_id('0146f675-ffbd-4208-b3a4-60eb628dbc5e')
Rohit Karajgi69e80a02012-05-15 03:54:04 -070088 def test_assign_user_role(self):
Anita Kunoa3702fe2014-08-04 11:20:25 +100089 """Assign a role to a user on a tenant."""
Rohit Karajgi69e80a02012-05-15 03:54:04 -070090 (user, tenant, role) = self._get_role_params()
rajalakshmi-ganesan8ba945e2012-08-01 15:43:19 +053091 self.client.assign_user_role(tenant['id'], user['id'], role['id'])
David Kranzb7afa922014-12-30 10:56:26 -050092 roles = self.client.list_user_roles(tenant['id'], user['id'])
Adam Youngddb82892013-02-15 13:10:35 -050093 self.assert_role_in_role_list(role, roles)
Rohit Karajgi69e80a02012-05-15 03:54:04 -070094
Masayuki Igawadf154682014-03-19 18:32:00 +090095 @test.attr(type='gate')
Chris Hoge7579c1a2015-02-26 14:12:15 -080096 @test.idempotent_id('f0b9292c-d3ba-4082-aa6c-440489beef69')
Rohit Karajgi69e80a02012-05-15 03:54:04 -070097 def test_remove_user_role(self):
Anita Kunoa3702fe2014-08-04 11:20:25 +100098 """Remove a role assigned to a user on a tenant."""
Rohit Karajgi69e80a02012-05-15 03:54:04 -070099 (user, tenant, role) = self._get_role_params()
David Kranzb7afa922014-12-30 10:56:26 -0500100 user_role = self.client.assign_user_role(tenant['id'],
101 user['id'], role['id'])
David Kranze9d2f422014-07-02 13:57:41 -0400102 self.client.remove_user_role(tenant['id'], user['id'],
103 user_role['id'])
Rohit Karajgi69e80a02012-05-15 03:54:04 -0700104
Masayuki Igawadf154682014-03-19 18:32:00 +0900105 @test.attr(type='gate')
Chris Hoge7579c1a2015-02-26 14:12:15 -0800106 @test.idempotent_id('262e1e3e-ed71-4edd-a0e5-d64e83d66d05')
Rohit Karajgi69e80a02012-05-15 03:54:04 -0700107 def test_list_user_roles(self):
Anita Kunoa3702fe2014-08-04 11:20:25 +1000108 """List roles assigned to a user on tenant."""
Rohit Karajgi69e80a02012-05-15 03:54:04 -0700109 (user, tenant, role) = self._get_role_params()
rajalakshmi-ganesan8ba945e2012-08-01 15:43:19 +0530110 self.client.assign_user_role(tenant['id'], user['id'], role['id'])
David Kranzb7afa922014-12-30 10:56:26 -0500111 roles = self.client.list_user_roles(tenant['id'], user['id'])
Adam Youngddb82892013-02-15 13:10:35 -0500112 self.assert_role_in_role_list(role, roles)