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 | |
llg8212 | 43b2050 | 2014-02-22 10:32:49 +0800 | [diff] [blame] | 16 | from six import moves |
| 17 | |
Sean Dague | 1937d09 | 2013-05-17 16:36:38 -0400 | [diff] [blame] | 18 | from tempest.api.identity import base |
huangtianhua | 35f1187 | 2013-10-11 11:34:43 +0800 | [diff] [blame] | 19 | from tempest.common.utils import data_utils |
Masayuki Igawa | df15468 | 2014-03-19 18:32:00 +0900 | [diff] [blame] | 20 | from tempest import test |
chris fattarsi | 8ed39ac | 2012-04-30 14:11:27 -0700 | [diff] [blame] | 21 | |
| 22 | |
Matthew Treinish | db2c597 | 2014-01-31 22:18:59 +0000 | [diff] [blame] | 23 | class RolesTestJSON(base.BaseIdentityV2AdminTest): |
Attila Fazekas | 0d0c616 | 2013-02-24 09:14:23 +0100 | [diff] [blame] | 24 | _interface = 'json' |
chris fattarsi | 8ed39ac | 2012-04-30 14:11:27 -0700 | [diff] [blame] | 25 | |
Attila Fazekas | 0d0c616 | 2013-02-24 09:14:23 +0100 | [diff] [blame] | 26 | @classmethod |
Masayuki Igawa | df15468 | 2014-03-19 18:32:00 +0900 | [diff] [blame] | 27 | @test.safe_setup |
chris fattarsi | 8ed39ac | 2012-04-30 14:11:27 -0700 | [diff] [blame] | 28 | def setUpClass(cls): |
Attila Fazekas | 0d0c616 | 2013-02-24 09:14:23 +0100 | [diff] [blame] | 29 | super(RolesTestJSON, cls).setUpClass() |
llg8212 | 43b2050 | 2014-02-22 10:32:49 +0800 | [diff] [blame] | 30 | for _ in moves.xrange(5): |
huangtianhua | 35f1187 | 2013-10-11 11:34:43 +0800 | [diff] [blame] | 31 | role_name = data_utils.rand_name(name='role-') |
| 32 | resp, role = cls.client.create_role(role_name) |
Rohit Karajgi | 69e80a0 | 2012-05-15 03:54:04 -0700 | [diff] [blame] | 33 | cls.data.roles.append(role) |
chris fattarsi | 8ed39ac | 2012-04-30 14:11:27 -0700 | [diff] [blame] | 34 | |
Rohit Karajgi | 69e80a0 | 2012-05-15 03:54:04 -0700 | [diff] [blame] | 35 | 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 fattarsi | 8ed39ac | 2012-04-30 14:11:27 -0700 | [diff] [blame] | 42 | |
Attila Fazekas | 0d0c616 | 2013-02-24 09:14:23 +0100 | [diff] [blame] | 43 | 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 Igawa | df15468 | 2014-03-19 18:32:00 +0900 | [diff] [blame] | 50 | @test.attr(type='gate') |
chris fattarsi | 8ed39ac | 2012-04-30 14:11:27 -0700 | [diff] [blame] | 51 | def test_list_roles(self): |
Sean Dague | 46c4a2b | 2013-01-03 17:54:17 -0500 | [diff] [blame] | 52 | # Return a list of all roles |
chris fattarsi | 8ed39ac | 2012-04-30 14:11:27 -0700 | [diff] [blame] | 53 | resp, body = self.client.list_roles() |
Rohit Karajgi | 69e80a0 | 2012-05-15 03:54:04 -0700 | [diff] [blame] | 54 | found = [role for role in body if role in self.data.roles] |
chris fattarsi | 8ed39ac | 2012-04-30 14:11:27 -0700 | [diff] [blame] | 55 | self.assertTrue(any(found)) |
Rohit Karajgi | 69e80a0 | 2012-05-15 03:54:04 -0700 | [diff] [blame] | 56 | self.assertEqual(len(found), len(self.data.roles)) |
| 57 | |
Masayuki Igawa | df15468 | 2014-03-19 18:32:00 +0900 | [diff] [blame] | 58 | @test.attr(type='gate') |
chris fattarsi | 8ed39ac | 2012-04-30 14:11:27 -0700 | [diff] [blame] | 59 | def test_role_create_delete(self): |
Sean Dague | 46c4a2b | 2013-01-03 17:54:17 -0500 | [diff] [blame] | 60 | # Role should be created, verified, and deleted |
huangtianhua | 35f1187 | 2013-10-11 11:34:43 +0800 | [diff] [blame] | 61 | role_name = data_utils.rand_name(name='role-test-') |
chris fattarsi | 8ed39ac | 2012-04-30 14:11:27 -0700 | [diff] [blame] | 62 | resp, body = self.client.create_role(role_name) |
Attila Fazekas | e191cb1 | 2013-07-29 06:41:52 +0200 | [diff] [blame] | 63 | self.assertIn('status', resp) |
chris fattarsi | 8ed39ac | 2012-04-30 14:11:27 -0700 | [diff] [blame] | 64 | 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 Fazekas | e191cb1 | 2013-07-29 06:41:52 +0200 | [diff] [blame] | 72 | self.assertIn('status', resp) |
chris fattarsi | 8ed39ac | 2012-04-30 14:11:27 -0700 | [diff] [blame] | 73 | 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 Igawa | df15468 | 2014-03-19 18:32:00 +0900 | [diff] [blame] | 79 | @test.attr(type='gate') |
Gong Zhang | cb6b886 | 2014-02-20 15:14:05 +0800 | [diff] [blame] | 80 | 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 Karajgi | 69e80a0 | 2012-05-15 03:54:04 -0700 | [diff] [blame] | 92 | def test_assign_user_role(self): |
Sean Dague | 46c4a2b | 2013-01-03 17:54:17 -0500 | [diff] [blame] | 93 | # Assign a role to a user on a tenant |
Rohit Karajgi | 69e80a0 | 2012-05-15 03:54:04 -0700 | [diff] [blame] | 94 | (user, tenant, role) = self._get_role_params() |
rajalakshmi-ganesan | 8ba945e | 2012-08-01 15:43:19 +0530 | [diff] [blame] | 95 | self.client.assign_user_role(tenant['id'], user['id'], role['id']) |
| 96 | resp, roles = self.client.list_user_roles(tenant['id'], user['id']) |
Adam Young | ddb8289 | 2013-02-15 13:10:35 -0500 | [diff] [blame] | 97 | self.assert_role_in_role_list(role, roles) |
Rohit Karajgi | 69e80a0 | 2012-05-15 03:54:04 -0700 | [diff] [blame] | 98 | |
Masayuki Igawa | df15468 | 2014-03-19 18:32:00 +0900 | [diff] [blame] | 99 | @test.attr(type='gate') |
Rohit Karajgi | 69e80a0 | 2012-05-15 03:54:04 -0700 | [diff] [blame] | 100 | def test_remove_user_role(self): |
Sean Dague | 46c4a2b | 2013-01-03 17:54:17 -0500 | [diff] [blame] | 101 | # Remove a role assigned to a user on a tenant |
Rohit Karajgi | 69e80a0 | 2012-05-15 03:54:04 -0700 | [diff] [blame] | 102 | (user, tenant, role) = self._get_role_params() |
rajalakshmi-ganesan | 8ba945e | 2012-08-01 15:43:19 +0530 | [diff] [blame] | 103 | 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 Guo | fc77e93 | 2013-09-16 17:38:26 -0700 | [diff] [blame] | 107 | self.assertEqual(resp['status'], '204') |
Rohit Karajgi | 69e80a0 | 2012-05-15 03:54:04 -0700 | [diff] [blame] | 108 | |
Masayuki Igawa | df15468 | 2014-03-19 18:32:00 +0900 | [diff] [blame] | 109 | @test.attr(type='gate') |
Rohit Karajgi | 69e80a0 | 2012-05-15 03:54:04 -0700 | [diff] [blame] | 110 | def test_list_user_roles(self): |
Sean Dague | 46c4a2b | 2013-01-03 17:54:17 -0500 | [diff] [blame] | 111 | # List roles assigned to a user on tenant |
Rohit Karajgi | 69e80a0 | 2012-05-15 03:54:04 -0700 | [diff] [blame] | 112 | (user, tenant, role) = self._get_role_params() |
rajalakshmi-ganesan | 8ba945e | 2012-08-01 15:43:19 +0530 | [diff] [blame] | 113 | self.client.assign_user_role(tenant['id'], user['id'], role['id']) |
| 114 | resp, roles = self.client.list_user_roles(tenant['id'], user['id']) |
Adam Young | ddb8289 | 2013-02-15 13:10:35 -0500 | [diff] [blame] | 115 | self.assert_role_in_role_list(role, roles) |
Rohit Karajgi | 69e80a0 | 2012-05-15 03:54:04 -0700 | [diff] [blame] | 116 | |
Vincent Hou | 6b8a7b7 | 2012-08-25 01:24:33 +0800 | [diff] [blame] | 117 | |
Attila Fazekas | 0d0c616 | 2013-02-24 09:14:23 +0100 | [diff] [blame] | 118 | class RolesTestXML(RolesTestJSON): |
| 119 | _interface = 'xml' |