huangtianhua | 1b855bc | 2013-10-10 11:12:44 +0800 | [diff] [blame] | 1 | # Copyright 2013 Huawei Technologies Co.,LTD. |
| 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 | |
huangtianhua | 1b855bc | 2013-10-10 11:12:44 +0800 | [diff] [blame] | 16 | from tempest.api.identity import base |
Pradeep Kumar | 1c79628 | 2017-04-27 16:48:36 +0530 | [diff] [blame] | 17 | from tempest import config |
Ken'ichi Ohmichi | 7bd2575 | 2017-03-10 10:45:39 -0800 | [diff] [blame] | 18 | from tempest.lib.common.utils import data_utils |
Ken'ichi Ohmichi | eeabdd2 | 2017-01-27 17:46:00 -0800 | [diff] [blame] | 19 | from tempest.lib import decorators |
Pradeep Kumar | 1c79628 | 2017-04-27 16:48:36 +0530 | [diff] [blame] | 20 | from tempest.lib import exceptions as lib_exc |
| 21 | |
| 22 | CONF = config.CONF |
huangtianhua | 1b855bc | 2013-10-10 11:12:44 +0800 | [diff] [blame] | 23 | |
| 24 | |
Matthew Treinish | db2c597 | 2014-01-31 22:18:59 +0000 | [diff] [blame] | 25 | class TokensTestJSON(base.BaseIdentityV2AdminTest): |
huangtianhua | 1b855bc | 2013-10-10 11:12:44 +0800 | [diff] [blame] | 26 | |
Ken'ichi Ohmichi | eeabdd2 | 2017-01-27 17:46:00 -0800 | [diff] [blame] | 27 | @decorators.idempotent_id('453ad4d5-e486-4b2f-be72-cffc8149e586') |
Pradeep Kumar | 1c79628 | 2017-04-27 16:48:36 +0530 | [diff] [blame] | 28 | def test_create_check_get_delete_token(self): |
huangtianhua | 1b855bc | 2013-10-10 11:12:44 +0800 | [diff] [blame] | 29 | # get a token by username and password |
Ken'ichi Ohmichi | 9650847 | 2015-03-23 01:43:42 +0000 | [diff] [blame] | 30 | user_name = data_utils.rand_name(name='user') |
Zack Feldstein | d8c5f7a | 2015-12-14 10:44:07 -0600 | [diff] [blame] | 31 | user_password = data_utils.rand_password() |
huangtianhua | 1b855bc | 2013-10-10 11:12:44 +0800 | [diff] [blame] | 32 | # first:create a tenant |
zhufl | 963d2c3 | 2017-04-20 15:44:58 +0800 | [diff] [blame] | 33 | tenant = self.setup_test_tenant() |
huangtianhua | 1b855bc | 2013-10-10 11:12:44 +0800 | [diff] [blame] | 34 | # second:create a user |
zhufl | 75d51a9 | 2017-04-11 16:02:39 +0800 | [diff] [blame] | 35 | user = self.create_test_user(name=user_name, |
| 36 | password=user_password, |
| 37 | tenantId=tenant['id'], |
| 38 | email='') |
huangtianhua | 1b855bc | 2013-10-10 11:12:44 +0800 | [diff] [blame] | 39 | # then get a token for the user |
David Kranz | b7afa92 | 2014-12-30 10:56:26 -0500 | [diff] [blame] | 40 | body = self.token_client.auth(user_name, |
| 41 | user_password, |
| 42 | tenant['name']) |
Andrea Frittoli | 8bbdb16 | 2014-01-06 11:06:13 +0000 | [diff] [blame] | 43 | self.assertEqual(body['token']['tenant']['name'], |
huangtianhua | 1b855bc | 2013-10-10 11:12:44 +0800 | [diff] [blame] | 44 | tenant['name']) |
Zhi Kun Liu | 30caeae | 2014-02-26 15:30:24 +0800 | [diff] [blame] | 45 | # Perform GET Token |
Andrea Frittoli | 8bbdb16 | 2014-01-06 11:06:13 +0000 | [diff] [blame] | 46 | token_id = body['token']['id'] |
Pradeep Kumar | 1c79628 | 2017-04-27 16:48:36 +0530 | [diff] [blame] | 47 | self.client.check_token_existence(token_id) |
Ken'ichi Ohmichi | 402b875 | 2015-11-09 10:47:16 +0000 | [diff] [blame] | 48 | token_details = self.client.show_token(token_id)['access'] |
Zhi Kun Liu | 30caeae | 2014-02-26 15:30:24 +0800 | [diff] [blame] | 49 | self.assertEqual(token_id, token_details['token']['id']) |
| 50 | self.assertEqual(user['id'], token_details['user']['id']) |
| 51 | self.assertEqual(user_name, token_details['user']['name']) |
| 52 | self.assertEqual(tenant['name'], |
| 53 | token_details['token']['tenant']['name']) |
| 54 | # then delete the token |
David Kranz | e9d2f42 | 2014-07-02 13:57:41 -0400 | [diff] [blame] | 55 | self.client.delete_token(token_id) |
Pradeep Kumar | 1c79628 | 2017-04-27 16:48:36 +0530 | [diff] [blame] | 56 | self.assertRaises(lib_exc.NotFound, |
| 57 | self.client.check_token_existence, |
| 58 | token_id) |
huangtianhua | 1b855bc | 2013-10-10 11:12:44 +0800 | [diff] [blame] | 59 | |
Ken'ichi Ohmichi | eeabdd2 | 2017-01-27 17:46:00 -0800 | [diff] [blame] | 60 | @decorators.idempotent_id('25ba82ee-8a32-4ceb-8f50-8b8c71e8765e') |
Brant Knudson | a4cfe0c | 2014-03-15 09:36:45 -0500 | [diff] [blame] | 61 | def test_rescope_token(self): |
Ken'ichi Ohmichi | 9e3dac0 | 2015-11-19 07:01:07 +0000 | [diff] [blame] | 62 | """An unscoped token can be requested |
| 63 | |
| 64 | That token can be used to request a scoped token. |
Brant Knudson | a4cfe0c | 2014-03-15 09:36:45 -0500 | [diff] [blame] | 65 | """ |
| 66 | |
| 67 | # Create a user. |
Ken'ichi Ohmichi | 9650847 | 2015-03-23 01:43:42 +0000 | [diff] [blame] | 68 | user_name = data_utils.rand_name(name='user') |
Zack Feldstein | d8c5f7a | 2015-12-14 10:44:07 -0600 | [diff] [blame] | 69 | user_password = data_utils.rand_password() |
Brant Knudson | a4cfe0c | 2014-03-15 09:36:45 -0500 | [diff] [blame] | 70 | tenant_id = None # No default tenant so will get unscoped token. |
zhufl | 75d51a9 | 2017-04-11 16:02:39 +0800 | [diff] [blame] | 71 | user = self.create_test_user(name=user_name, |
| 72 | password=user_password, |
| 73 | tenantId=tenant_id, |
| 74 | email='') |
Brant Knudson | a4cfe0c | 2014-03-15 09:36:45 -0500 | [diff] [blame] | 75 | |
Brant Knudson | 840011b | 2014-03-16 11:14:14 -0500 | [diff] [blame] | 76 | # Create a couple tenants. |
Ken'ichi Ohmichi | 9650847 | 2015-03-23 01:43:42 +0000 | [diff] [blame] | 77 | tenant1_name = data_utils.rand_name(name='tenant') |
zhufl | 963d2c3 | 2017-04-20 15:44:58 +0800 | [diff] [blame] | 78 | tenant1 = self.setup_test_tenant(name=tenant1_name) |
Brant Knudson | 840011b | 2014-03-16 11:14:14 -0500 | [diff] [blame] | 79 | |
Ken'ichi Ohmichi | 9650847 | 2015-03-23 01:43:42 +0000 | [diff] [blame] | 80 | tenant2_name = data_utils.rand_name(name='tenant') |
zhufl | 963d2c3 | 2017-04-20 15:44:58 +0800 | [diff] [blame] | 81 | tenant2 = self.setup_test_tenant(name=tenant2_name) |
Brant Knudson | a4cfe0c | 2014-03-15 09:36:45 -0500 | [diff] [blame] | 82 | |
| 83 | # Create a role |
zhufl | 66b616a | 2017-04-11 15:00:32 +0800 | [diff] [blame] | 84 | role = self.setup_test_role() |
Brant Knudson | a4cfe0c | 2014-03-15 09:36:45 -0500 | [diff] [blame] | 85 | |
Brant Knudson | 840011b | 2014-03-16 11:14:14 -0500 | [diff] [blame] | 86 | # Grant the user the role on the tenants. |
ghanshyam | 50894fc | 2016-06-17 13:20:25 +0900 | [diff] [blame] | 87 | self.roles_client.create_user_role_on_project(tenant1['id'], |
| 88 | user['id'], |
| 89 | role['id']) |
Brant Knudson | 840011b | 2014-03-16 11:14:14 -0500 | [diff] [blame] | 90 | |
ghanshyam | 50894fc | 2016-06-17 13:20:25 +0900 | [diff] [blame] | 91 | self.roles_client.create_user_role_on_project(tenant2['id'], |
| 92 | user['id'], |
| 93 | role['id']) |
Brant Knudson | a4cfe0c | 2014-03-15 09:36:45 -0500 | [diff] [blame] | 94 | |
| 95 | # Get an unscoped token. |
David Kranz | b7afa92 | 2014-12-30 10:56:26 -0500 | [diff] [blame] | 96 | body = self.token_client.auth(user_name, user_password) |
Brant Knudson | a4cfe0c | 2014-03-15 09:36:45 -0500 | [diff] [blame] | 97 | |
| 98 | token_id = body['token']['id'] |
| 99 | |
Brant Knudson | 840011b | 2014-03-16 11:14:14 -0500 | [diff] [blame] | 100 | # Use the unscoped token to get a token scoped to tenant1 |
David Kranz | b7afa92 | 2014-12-30 10:56:26 -0500 | [diff] [blame] | 101 | body = self.token_client.auth_token(token_id, |
| 102 | tenant=tenant1_name) |
Brant Knudson | a4cfe0c | 2014-03-15 09:36:45 -0500 | [diff] [blame] | 103 | |
Brant Knudson | 840011b | 2014-03-16 11:14:14 -0500 | [diff] [blame] | 104 | scoped_token_id = body['token']['id'] |
| 105 | |
| 106 | # Revoke the scoped token |
David Kranz | e9d2f42 | 2014-07-02 13:57:41 -0400 | [diff] [blame] | 107 | self.client.delete_token(scoped_token_id) |
Brant Knudson | 840011b | 2014-03-16 11:14:14 -0500 | [diff] [blame] | 108 | |
| 109 | # Use the unscoped token to get a token scoped to tenant2 |
David Kranz | b7afa92 | 2014-12-30 10:56:26 -0500 | [diff] [blame] | 110 | body = self.token_client.auth_token(token_id, |
| 111 | tenant=tenant2_name) |
Pradeep Kumar | 1c79628 | 2017-04-27 16:48:36 +0530 | [diff] [blame] | 112 | |
| 113 | @decorators.idempotent_id('ca3ea6f7-ed08-4a61-adbd-96906456ad31') |
| 114 | def test_list_endpoints_for_token(self): |
Andrea Frittoli (andreaf) | 60cb4b2 | 2018-01-18 10:11:35 +0000 | [diff] [blame] | 115 | tempest_services = ['keystone', 'nova', 'neutron', 'swift', 'cinder', |
| 116 | 'neutron'] |
Pradeep Kumar | 1c79628 | 2017-04-27 16:48:36 +0530 | [diff] [blame] | 117 | # get a token for the user |
| 118 | creds = self.os_primary.credentials |
| 119 | username = creds.username |
| 120 | password = creds.password |
| 121 | tenant_name = creds.tenant_name |
| 122 | token = self.token_client.auth(username, |
| 123 | password, |
| 124 | tenant_name)['token'] |
| 125 | endpoints = self.client.list_endpoints_for_token( |
| 126 | token['id'])['endpoints'] |
| 127 | self.assertIsInstance(endpoints, list) |
| 128 | # Store list of service names |
| 129 | service_names = [e['name'] for e in endpoints] |
Andrea Frittoli (andreaf) | 60cb4b2 | 2018-01-18 10:11:35 +0000 | [diff] [blame] | 130 | # Get the list of available services. Keystone is always available. |
Pradeep Kumar | 1c79628 | 2017-04-27 16:48:36 +0530 | [diff] [blame] | 131 | available_services = [s[0] for s in list( |
Andrea Frittoli (andreaf) | 60cb4b2 | 2018-01-18 10:11:35 +0000 | [diff] [blame] | 132 | CONF.service_available.items()) if s[1] is True] + ['keystone'] |
Pradeep Kumar | 1c79628 | 2017-04-27 16:48:36 +0530 | [diff] [blame] | 133 | # Verify that all available services are present. |
Andrea Frittoli (andreaf) | 60cb4b2 | 2018-01-18 10:11:35 +0000 | [diff] [blame] | 134 | for service in tempest_services: |
| 135 | if service in available_services: |
| 136 | self.assertIn(service, service_names) |