ZhiQiang Fan | 39f9722 | 2013-09-20 04:49:44 +0800 | [diff] [blame] | 1 | # Copyright 2012 OpenStack Foundation |
Jay Pipes | f38eaac | 2012-06-21 13:37:35 -0400 | [diff] [blame] | 2 | # 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 Dague | 1937d09 | 2013-05-17 16:36:38 -0400 | [diff] [blame] | 16 | from tempest.api.identity import base |
huangtianhua | 35f1187 | 2013-10-11 11:34:43 +0800 | [diff] [blame] | 17 | from tempest.common.utils import data_utils |
Giampaolo Lauria | ea29495 | 2013-05-15 08:52:04 -0400 | [diff] [blame] | 18 | from tempest.test import attr |
chris fattarsi | 8ed39ac | 2012-04-30 14:11:27 -0700 | [diff] [blame] | 19 | |
| 20 | |
Matthew Treinish | db2c597 | 2014-01-31 22:18:59 +0000 | [diff] [blame] | 21 | class RolesTestJSON(base.BaseIdentityV2AdminTest): |
Attila Fazekas | 0d0c616 | 2013-02-24 09:14:23 +0100 | [diff] [blame] | 22 | _interface = 'json' |
chris fattarsi | 8ed39ac | 2012-04-30 14:11:27 -0700 | [diff] [blame] | 23 | |
Attila Fazekas | 0d0c616 | 2013-02-24 09:14:23 +0100 | [diff] [blame] | 24 | @classmethod |
chris fattarsi | 8ed39ac | 2012-04-30 14:11:27 -0700 | [diff] [blame] | 25 | def setUpClass(cls): |
Attila Fazekas | 0d0c616 | 2013-02-24 09:14:23 +0100 | [diff] [blame] | 26 | super(RolesTestJSON, cls).setUpClass() |
chris fattarsi | 8ed39ac | 2012-04-30 14:11:27 -0700 | [diff] [blame] | 27 | for _ in xrange(5): |
huangtianhua | 35f1187 | 2013-10-11 11:34:43 +0800 | [diff] [blame] | 28 | role_name = data_utils.rand_name(name='role-') |
| 29 | resp, role = cls.client.create_role(role_name) |
Rohit Karajgi | 69e80a0 | 2012-05-15 03:54:04 -0700 | [diff] [blame] | 30 | cls.data.roles.append(role) |
chris fattarsi | 8ed39ac | 2012-04-30 14:11:27 -0700 | [diff] [blame] | 31 | |
Rohit Karajgi | 69e80a0 | 2012-05-15 03:54:04 -0700 | [diff] [blame] | 32 | def _get_role_params(self): |
| 33 | self.data.setup_test_user() |
| 34 | self.data.setup_test_role() |
| 35 | user = self.get_user_by_name(self.data.test_user) |
| 36 | tenant = self.get_tenant_by_name(self.data.test_tenant) |
| 37 | role = self.get_role_by_name(self.data.test_role) |
| 38 | return (user, tenant, role) |
chris fattarsi | 8ed39ac | 2012-04-30 14:11:27 -0700 | [diff] [blame] | 39 | |
Attila Fazekas | 0d0c616 | 2013-02-24 09:14:23 +0100 | [diff] [blame] | 40 | def assert_role_in_role_list(self, role, roles): |
| 41 | found = False |
| 42 | for user_role in roles: |
| 43 | if user_role['id'] == role['id']: |
| 44 | found = True |
| 45 | self.assertTrue(found, "assigned role was not in list") |
| 46 | |
Giampaolo Lauria | ea29495 | 2013-05-15 08:52:04 -0400 | [diff] [blame] | 47 | @attr(type='gate') |
chris fattarsi | 8ed39ac | 2012-04-30 14:11:27 -0700 | [diff] [blame] | 48 | def test_list_roles(self): |
Sean Dague | 46c4a2b | 2013-01-03 17:54:17 -0500 | [diff] [blame] | 49 | # Return a list of all roles |
chris fattarsi | 8ed39ac | 2012-04-30 14:11:27 -0700 | [diff] [blame] | 50 | resp, body = self.client.list_roles() |
Rohit Karajgi | 69e80a0 | 2012-05-15 03:54:04 -0700 | [diff] [blame] | 51 | found = [role for role in body if role in self.data.roles] |
chris fattarsi | 8ed39ac | 2012-04-30 14:11:27 -0700 | [diff] [blame] | 52 | self.assertTrue(any(found)) |
Rohit Karajgi | 69e80a0 | 2012-05-15 03:54:04 -0700 | [diff] [blame] | 53 | self.assertEqual(len(found), len(self.data.roles)) |
| 54 | |
Giampaolo Lauria | ea29495 | 2013-05-15 08:52:04 -0400 | [diff] [blame] | 55 | @attr(type='gate') |
chris fattarsi | 8ed39ac | 2012-04-30 14:11:27 -0700 | [diff] [blame] | 56 | def test_role_create_delete(self): |
Sean Dague | 46c4a2b | 2013-01-03 17:54:17 -0500 | [diff] [blame] | 57 | # Role should be created, verified, and deleted |
huangtianhua | 35f1187 | 2013-10-11 11:34:43 +0800 | [diff] [blame] | 58 | role_name = data_utils.rand_name(name='role-test-') |
chris fattarsi | 8ed39ac | 2012-04-30 14:11:27 -0700 | [diff] [blame] | 59 | resp, body = self.client.create_role(role_name) |
Attila Fazekas | e191cb1 | 2013-07-29 06:41:52 +0200 | [diff] [blame] | 60 | self.assertIn('status', resp) |
chris fattarsi | 8ed39ac | 2012-04-30 14:11:27 -0700 | [diff] [blame] | 61 | self.assertTrue(resp['status'].startswith('2')) |
| 62 | self.assertEqual(role_name, body['name']) |
| 63 | |
| 64 | resp, body = self.client.list_roles() |
| 65 | found = [role for role in body if role['name'] == role_name] |
| 66 | self.assertTrue(any(found)) |
| 67 | |
| 68 | resp, body = self.client.delete_role(found[0]['id']) |
Attila Fazekas | e191cb1 | 2013-07-29 06:41:52 +0200 | [diff] [blame] | 69 | self.assertIn('status', resp) |
chris fattarsi | 8ed39ac | 2012-04-30 14:11:27 -0700 | [diff] [blame] | 70 | self.assertTrue(resp['status'].startswith('2')) |
| 71 | |
| 72 | resp, body = self.client.list_roles() |
| 73 | found = [role for role in body if role['name'] == role_name] |
| 74 | self.assertFalse(any(found)) |
| 75 | |
Giampaolo Lauria | ea29495 | 2013-05-15 08:52:04 -0400 | [diff] [blame] | 76 | @attr(type='gate') |
Rohit Karajgi | 69e80a0 | 2012-05-15 03:54:04 -0700 | [diff] [blame] | 77 | def test_assign_user_role(self): |
Sean Dague | 46c4a2b | 2013-01-03 17:54:17 -0500 | [diff] [blame] | 78 | # Assign a role to a user on a tenant |
Rohit Karajgi | 69e80a0 | 2012-05-15 03:54:04 -0700 | [diff] [blame] | 79 | (user, tenant, role) = self._get_role_params() |
rajalakshmi-ganesan | 8ba945e | 2012-08-01 15:43:19 +0530 | [diff] [blame] | 80 | self.client.assign_user_role(tenant['id'], user['id'], role['id']) |
| 81 | resp, roles = self.client.list_user_roles(tenant['id'], user['id']) |
Adam Young | ddb8289 | 2013-02-15 13:10:35 -0500 | [diff] [blame] | 82 | self.assert_role_in_role_list(role, roles) |
Rohit Karajgi | 69e80a0 | 2012-05-15 03:54:04 -0700 | [diff] [blame] | 83 | |
Giampaolo Lauria | ea29495 | 2013-05-15 08:52:04 -0400 | [diff] [blame] | 84 | @attr(type='gate') |
Rohit Karajgi | 69e80a0 | 2012-05-15 03:54:04 -0700 | [diff] [blame] | 85 | def test_remove_user_role(self): |
Sean Dague | 46c4a2b | 2013-01-03 17:54:17 -0500 | [diff] [blame] | 86 | # Remove a role assigned to a user on a tenant |
Rohit Karajgi | 69e80a0 | 2012-05-15 03:54:04 -0700 | [diff] [blame] | 87 | (user, tenant, role) = self._get_role_params() |
rajalakshmi-ganesan | 8ba945e | 2012-08-01 15:43:19 +0530 | [diff] [blame] | 88 | resp, user_role = self.client.assign_user_role(tenant['id'], |
| 89 | user['id'], role['id']) |
| 90 | resp, body = self.client.remove_user_role(tenant['id'], user['id'], |
| 91 | user_role['id']) |
Chang Bo Guo | fc77e93 | 2013-09-16 17:38:26 -0700 | [diff] [blame] | 92 | self.assertEqual(resp['status'], '204') |
Rohit Karajgi | 69e80a0 | 2012-05-15 03:54:04 -0700 | [diff] [blame] | 93 | |
Giampaolo Lauria | ea29495 | 2013-05-15 08:52:04 -0400 | [diff] [blame] | 94 | @attr(type='gate') |
Rohit Karajgi | 69e80a0 | 2012-05-15 03:54:04 -0700 | [diff] [blame] | 95 | def test_list_user_roles(self): |
Sean Dague | 46c4a2b | 2013-01-03 17:54:17 -0500 | [diff] [blame] | 96 | # List roles assigned to a user on tenant |
Rohit Karajgi | 69e80a0 | 2012-05-15 03:54:04 -0700 | [diff] [blame] | 97 | (user, tenant, role) = self._get_role_params() |
rajalakshmi-ganesan | 8ba945e | 2012-08-01 15:43:19 +0530 | [diff] [blame] | 98 | self.client.assign_user_role(tenant['id'], user['id'], role['id']) |
| 99 | resp, roles = self.client.list_user_roles(tenant['id'], user['id']) |
Adam Young | ddb8289 | 2013-02-15 13:10:35 -0500 | [diff] [blame] | 100 | self.assert_role_in_role_list(role, roles) |
Rohit Karajgi | 69e80a0 | 2012-05-15 03:54:04 -0700 | [diff] [blame] | 101 | |
Vincent Hou | 6b8a7b7 | 2012-08-25 01:24:33 +0800 | [diff] [blame] | 102 | |
Attila Fazekas | 0d0c616 | 2013-02-24 09:14:23 +0100 | [diff] [blame] | 103 | class RolesTestXML(RolesTestJSON): |
| 104 | _interface = 'xml' |