blob: 307e21d0d86d246eae1ca16103dd54152f5df142 [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
Masayuki Igawadf154682014-03-19 18:32:00 +090027 @test.safe_setup
chris fattarsi8ed39ac2012-04-30 14:11:27 -070028 def setUpClass(cls):
Attila Fazekas0d0c6162013-02-24 09:14:23 +010029 super(RolesTestJSON, cls).setUpClass()
llg821243b20502014-02-22 10:32:49 +080030 for _ in moves.xrange(5):
huangtianhua35f11872013-10-11 11:34:43 +080031 role_name = data_utils.rand_name(name='role-')
32 resp, role = cls.client.create_role(role_name)
Rohit Karajgi69e80a02012-05-15 03:54:04 -070033 cls.data.roles.append(role)
chris fattarsi8ed39ac2012-04-30 14:11:27 -070034
Rohit Karajgi69e80a02012-05-15 03:54:04 -070035 def _get_role_params(self):
36 self.data.setup_test_user()
37 self.data.setup_test_role()
38 user = self.get_user_by_name(self.data.test_user)
39 tenant = self.get_tenant_by_name(self.data.test_tenant)
40 role = self.get_role_by_name(self.data.test_role)
41 return (user, tenant, role)
chris fattarsi8ed39ac2012-04-30 14:11:27 -070042
Attila Fazekas0d0c6162013-02-24 09:14:23 +010043 def assert_role_in_role_list(self, role, roles):
44 found = False
45 for user_role in roles:
46 if user_role['id'] == role['id']:
47 found = True
48 self.assertTrue(found, "assigned role was not in list")
49
Masayuki Igawadf154682014-03-19 18:32:00 +090050 @test.attr(type='gate')
chris fattarsi8ed39ac2012-04-30 14:11:27 -070051 def test_list_roles(self):
Sean Dague46c4a2b2013-01-03 17:54:17 -050052 # Return a list of all roles
chris fattarsi8ed39ac2012-04-30 14:11:27 -070053 resp, body = self.client.list_roles()
Rohit Karajgi69e80a02012-05-15 03:54:04 -070054 found = [role for role in body if role in self.data.roles]
chris fattarsi8ed39ac2012-04-30 14:11:27 -070055 self.assertTrue(any(found))
Rohit Karajgi69e80a02012-05-15 03:54:04 -070056 self.assertEqual(len(found), len(self.data.roles))
57
Masayuki Igawadf154682014-03-19 18:32:00 +090058 @test.attr(type='gate')
chris fattarsi8ed39ac2012-04-30 14:11:27 -070059 def test_role_create_delete(self):
Sean Dague46c4a2b2013-01-03 17:54:17 -050060 # Role should be created, verified, and deleted
huangtianhua35f11872013-10-11 11:34:43 +080061 role_name = data_utils.rand_name(name='role-test-')
chris fattarsi8ed39ac2012-04-30 14:11:27 -070062 resp, body = self.client.create_role(role_name)
Attila Fazekase191cb12013-07-29 06:41:52 +020063 self.assertIn('status', resp)
chris fattarsi8ed39ac2012-04-30 14:11:27 -070064 self.assertTrue(resp['status'].startswith('2'))
65 self.assertEqual(role_name, body['name'])
66
67 resp, body = self.client.list_roles()
68 found = [role for role in body if role['name'] == role_name]
69 self.assertTrue(any(found))
70
71 resp, body = self.client.delete_role(found[0]['id'])
Attila Fazekase191cb12013-07-29 06:41:52 +020072 self.assertIn('status', resp)
chris fattarsi8ed39ac2012-04-30 14:11:27 -070073 self.assertTrue(resp['status'].startswith('2'))
74
75 resp, body = self.client.list_roles()
76 found = [role for role in body if role['name'] == role_name]
77 self.assertFalse(any(found))
78
Masayuki Igawadf154682014-03-19 18:32:00 +090079 @test.attr(type='gate')
Gong Zhangcb6b8862014-02-20 15:14:05 +080080 def test_get_role_by_id(self):
81 # Get a role by its id
82 self.data.setup_test_role()
83 role_id = self.data.role['id']
84 role_name = self.data.role['name']
85 resp, body = self.client.get_role(role_id)
86 self.assertIn('status', resp)
87 self.assertTrue('200', resp['status'])
88 self.assertEqual(role_id, body['id'])
89 self.assertEqual(role_name, body['name'])
90
91 @test.attr(type='gate')
Rohit Karajgi69e80a02012-05-15 03:54:04 -070092 def test_assign_user_role(self):
Sean Dague46c4a2b2013-01-03 17:54:17 -050093 # Assign a role to a user on a tenant
Rohit Karajgi69e80a02012-05-15 03:54:04 -070094 (user, tenant, role) = self._get_role_params()
rajalakshmi-ganesan8ba945e2012-08-01 15:43:19 +053095 self.client.assign_user_role(tenant['id'], user['id'], role['id'])
96 resp, roles = self.client.list_user_roles(tenant['id'], user['id'])
Adam Youngddb82892013-02-15 13:10:35 -050097 self.assert_role_in_role_list(role, roles)
Rohit Karajgi69e80a02012-05-15 03:54:04 -070098
Masayuki Igawadf154682014-03-19 18:32:00 +090099 @test.attr(type='gate')
Rohit Karajgi69e80a02012-05-15 03:54:04 -0700100 def test_remove_user_role(self):
Sean Dague46c4a2b2013-01-03 17:54:17 -0500101 # Remove a role assigned to a user on a tenant
Rohit Karajgi69e80a02012-05-15 03:54:04 -0700102 (user, tenant, role) = self._get_role_params()
rajalakshmi-ganesan8ba945e2012-08-01 15:43:19 +0530103 resp, user_role = self.client.assign_user_role(tenant['id'],
104 user['id'], role['id'])
105 resp, body = self.client.remove_user_role(tenant['id'], user['id'],
106 user_role['id'])
Chang Bo Guofc77e932013-09-16 17:38:26 -0700107 self.assertEqual(resp['status'], '204')
Rohit Karajgi69e80a02012-05-15 03:54:04 -0700108
Masayuki Igawadf154682014-03-19 18:32:00 +0900109 @test.attr(type='gate')
Rohit Karajgi69e80a02012-05-15 03:54:04 -0700110 def test_list_user_roles(self):
Sean Dague46c4a2b2013-01-03 17:54:17 -0500111 # List roles assigned to a user on tenant
Rohit Karajgi69e80a02012-05-15 03:54:04 -0700112 (user, tenant, role) = self._get_role_params()
rajalakshmi-ganesan8ba945e2012-08-01 15:43:19 +0530113 self.client.assign_user_role(tenant['id'], user['id'], role['id'])
114 resp, roles = self.client.list_user_roles(tenant['id'], user['id'])
Adam Youngddb82892013-02-15 13:10:35 -0500115 self.assert_role_in_role_list(role, roles)
Rohit Karajgi69e80a02012-05-15 03:54:04 -0700116
Vincent Hou6b8a7b72012-08-25 01:24:33 +0800117
Attila Fazekas0d0c6162013-02-24 09:14:23 +0100118class RolesTestXML(RolesTestJSON):
119 _interface = 'xml'