blob: fabc0ed6f2b7218ad822cbab528f305003b63274 [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):
Attila Fazekas0d0c6162013-02-24 09:14:23 +010024 _interface = 'json'
chris fattarsi8ed39ac2012-04-30 14:11:27 -070025
Attila Fazekas0d0c6162013-02-24 09:14:23 +010026 @classmethod
Andrea Frittoli7688e742014-09-15 12:38:22 +010027 def resource_setup(cls):
28 super(RolesTestJSON, cls).resource_setup()
llg821243b20502014-02-22 10:32:49 +080029 for _ in moves.xrange(5):
huangtianhua35f11872013-10-11 11:34:43 +080030 role_name = data_utils.rand_name(name='role-')
David Kranze9d2f422014-07-02 13:57:41 -040031 _, 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
Masayuki Igawadf154682014-03-19 18:32:00 +090049 @test.attr(type='gate')
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 Kranze9d2f422014-07-02 13:57:41 -040052 _, 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 fattarsi8ed39ac2012-04-30 14:11:27 -070058 def test_role_create_delete(self):
Anita Kunoa3702fe2014-08-04 11:20:25 +100059 """Role should be created, verified, and deleted."""
huangtianhua35f11872013-10-11 11:34:43 +080060 role_name = data_utils.rand_name(name='role-test-')
David Kranze9d2f422014-07-02 13:57:41 -040061 _, body = self.client.create_role(role_name)
chris fattarsi8ed39ac2012-04-30 14:11:27 -070062 self.assertEqual(role_name, body['name'])
63
David Kranze9d2f422014-07-02 13:57:41 -040064 _, body = self.client.list_roles()
chris fattarsi8ed39ac2012-04-30 14:11:27 -070065 found = [role for role in body if role['name'] == role_name]
66 self.assertTrue(any(found))
67
David Kranze9d2f422014-07-02 13:57:41 -040068 _, body = self.client.delete_role(found[0]['id'])
chris fattarsi8ed39ac2012-04-30 14:11:27 -070069
David Kranze9d2f422014-07-02 13:57:41 -040070 _, body = self.client.list_roles()
chris fattarsi8ed39ac2012-04-30 14:11:27 -070071 found = [role for role in body if role['name'] == role_name]
72 self.assertFalse(any(found))
73
Masayuki Igawadf154682014-03-19 18:32:00 +090074 @test.attr(type='gate')
Gong Zhangcb6b8862014-02-20 15:14:05 +080075 def test_get_role_by_id(self):
Anita Kunoa3702fe2014-08-04 11:20:25 +100076 """Get a role by its id."""
Gong Zhangcb6b8862014-02-20 15:14:05 +080077 self.data.setup_test_role()
78 role_id = self.data.role['id']
79 role_name = self.data.role['name']
David Kranze9d2f422014-07-02 13:57:41 -040080 _, body = self.client.get_role(role_id)
Gong Zhangcb6b8862014-02-20 15:14:05 +080081 self.assertEqual(role_id, body['id'])
82 self.assertEqual(role_name, body['name'])
83
84 @test.attr(type='gate')
Rohit Karajgi69e80a02012-05-15 03:54:04 -070085 def test_assign_user_role(self):
Anita Kunoa3702fe2014-08-04 11:20:25 +100086 """Assign a role 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 self.client.assign_user_role(tenant['id'], user['id'], role['id'])
David Kranze9d2f422014-07-02 13:57:41 -040089 _, roles = self.client.list_user_roles(tenant['id'], user['id'])
Adam Youngddb82892013-02-15 13:10:35 -050090 self.assert_role_in_role_list(role, roles)
Rohit Karajgi69e80a02012-05-15 03:54:04 -070091
Masayuki Igawadf154682014-03-19 18:32:00 +090092 @test.attr(type='gate')
Rohit Karajgi69e80a02012-05-15 03:54:04 -070093 def test_remove_user_role(self):
Anita Kunoa3702fe2014-08-04 11:20:25 +100094 """Remove a role assigned to a user on a tenant."""
Rohit Karajgi69e80a02012-05-15 03:54:04 -070095 (user, tenant, role) = self._get_role_params()
David Kranze9d2f422014-07-02 13:57:41 -040096 _, user_role = self.client.assign_user_role(tenant['id'],
97 user['id'], role['id'])
98 self.client.remove_user_role(tenant['id'], user['id'],
99 user_role['id'])
Rohit Karajgi69e80a02012-05-15 03:54:04 -0700100
Masayuki Igawadf154682014-03-19 18:32:00 +0900101 @test.attr(type='gate')
Rohit Karajgi69e80a02012-05-15 03:54:04 -0700102 def test_list_user_roles(self):
Anita Kunoa3702fe2014-08-04 11:20:25 +1000103 """List roles assigned to a user on tenant."""
Rohit Karajgi69e80a02012-05-15 03:54:04 -0700104 (user, tenant, role) = self._get_role_params()
rajalakshmi-ganesan8ba945e2012-08-01 15:43:19 +0530105 self.client.assign_user_role(tenant['id'], user['id'], role['id'])
David Kranze9d2f422014-07-02 13:57:41 -0400106 _, roles = self.client.list_user_roles(tenant['id'], user['id'])
Adam Youngddb82892013-02-15 13:10:35 -0500107 self.assert_role_in_role_list(role, roles)