blob: 6ce1a8be54fb190845e980726ddc807d5cd226e7 [file] [log] [blame]
huangtianhua1b855bc2013-10-10 11:12:44 +08001# 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
huangtianhua1b855bc2013-10-10 11:12:44 +080016from tempest.api.identity import base
Pradeep Kumar1c796282017-04-27 16:48:36 +053017from tempest import config
Ken'ichi Ohmichi7bd25752017-03-10 10:45:39 -080018from tempest.lib.common.utils import data_utils
Ken'ichi Ohmichieeabdd22017-01-27 17:46:00 -080019from tempest.lib import decorators
Pradeep Kumar1c796282017-04-27 16:48:36 +053020from tempest.lib import exceptions as lib_exc
21
22CONF = config.CONF
huangtianhua1b855bc2013-10-10 11:12:44 +080023
24
Matthew Treinishdb2c5972014-01-31 22:18:59 +000025class TokensTestJSON(base.BaseIdentityV2AdminTest):
huangtianhua1b855bc2013-10-10 11:12:44 +080026
Ken'ichi Ohmichieeabdd22017-01-27 17:46:00 -080027 @decorators.idempotent_id('453ad4d5-e486-4b2f-be72-cffc8149e586')
Pradeep Kumar1c796282017-04-27 16:48:36 +053028 def test_create_check_get_delete_token(self):
huangtianhua1b855bc2013-10-10 11:12:44 +080029 # get a token by username and password
Ken'ichi Ohmichi96508472015-03-23 01:43:42 +000030 user_name = data_utils.rand_name(name='user')
Zack Feldsteind8c5f7a2015-12-14 10:44:07 -060031 user_password = data_utils.rand_password()
huangtianhua1b855bc2013-10-10 11:12:44 +080032 # first:create a tenant
zhufl963d2c32017-04-20 15:44:58 +080033 tenant = self.setup_test_tenant()
huangtianhua1b855bc2013-10-10 11:12:44 +080034 # second:create a user
zhufl75d51a92017-04-11 16:02:39 +080035 user = self.create_test_user(name=user_name,
36 password=user_password,
37 tenantId=tenant['id'],
38 email='')
huangtianhua1b855bc2013-10-10 11:12:44 +080039 # then get a token for the user
David Kranzb7afa922014-12-30 10:56:26 -050040 body = self.token_client.auth(user_name,
41 user_password,
42 tenant['name'])
Andrea Frittoli8bbdb162014-01-06 11:06:13 +000043 self.assertEqual(body['token']['tenant']['name'],
huangtianhua1b855bc2013-10-10 11:12:44 +080044 tenant['name'])
Zhi Kun Liu30caeae2014-02-26 15:30:24 +080045 # Perform GET Token
Andrea Frittoli8bbdb162014-01-06 11:06:13 +000046 token_id = body['token']['id']
Pradeep Kumar1c796282017-04-27 16:48:36 +053047 self.client.check_token_existence(token_id)
Ken'ichi Ohmichi402b8752015-11-09 10:47:16 +000048 token_details = self.client.show_token(token_id)['access']
Zhi Kun Liu30caeae2014-02-26 15:30:24 +080049 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 Kranze9d2f422014-07-02 13:57:41 -040055 self.client.delete_token(token_id)
Pradeep Kumar1c796282017-04-27 16:48:36 +053056 self.assertRaises(lib_exc.NotFound,
57 self.client.check_token_existence,
58 token_id)
huangtianhua1b855bc2013-10-10 11:12:44 +080059
Ken'ichi Ohmichieeabdd22017-01-27 17:46:00 -080060 @decorators.idempotent_id('25ba82ee-8a32-4ceb-8f50-8b8c71e8765e')
Brant Knudsona4cfe0c2014-03-15 09:36:45 -050061 def test_rescope_token(self):
Ken'ichi Ohmichi9e3dac02015-11-19 07:01:07 +000062 """An unscoped token can be requested
63
64 That token can be used to request a scoped token.
Brant Knudsona4cfe0c2014-03-15 09:36:45 -050065 """
66
67 # Create a user.
Ken'ichi Ohmichi96508472015-03-23 01:43:42 +000068 user_name = data_utils.rand_name(name='user')
Zack Feldsteind8c5f7a2015-12-14 10:44:07 -060069 user_password = data_utils.rand_password()
Brant Knudsona4cfe0c2014-03-15 09:36:45 -050070 tenant_id = None # No default tenant so will get unscoped token.
zhufl75d51a92017-04-11 16:02:39 +080071 user = self.create_test_user(name=user_name,
72 password=user_password,
73 tenantId=tenant_id,
74 email='')
Brant Knudsona4cfe0c2014-03-15 09:36:45 -050075
Brant Knudson840011b2014-03-16 11:14:14 -050076 # Create a couple tenants.
Ken'ichi Ohmichi96508472015-03-23 01:43:42 +000077 tenant1_name = data_utils.rand_name(name='tenant')
zhufl963d2c32017-04-20 15:44:58 +080078 tenant1 = self.setup_test_tenant(name=tenant1_name)
Brant Knudson840011b2014-03-16 11:14:14 -050079
Ken'ichi Ohmichi96508472015-03-23 01:43:42 +000080 tenant2_name = data_utils.rand_name(name='tenant')
zhufl963d2c32017-04-20 15:44:58 +080081 tenant2 = self.setup_test_tenant(name=tenant2_name)
Brant Knudsona4cfe0c2014-03-15 09:36:45 -050082
83 # Create a role
zhufl66b616a2017-04-11 15:00:32 +080084 role = self.setup_test_role()
Brant Knudsona4cfe0c2014-03-15 09:36:45 -050085
Brant Knudson840011b2014-03-16 11:14:14 -050086 # Grant the user the role on the tenants.
ghanshyam50894fc2016-06-17 13:20:25 +090087 self.roles_client.create_user_role_on_project(tenant1['id'],
88 user['id'],
89 role['id'])
Brant Knudson840011b2014-03-16 11:14:14 -050090
ghanshyam50894fc2016-06-17 13:20:25 +090091 self.roles_client.create_user_role_on_project(tenant2['id'],
92 user['id'],
93 role['id'])
Brant Knudsona4cfe0c2014-03-15 09:36:45 -050094
95 # Get an unscoped token.
David Kranzb7afa922014-12-30 10:56:26 -050096 body = self.token_client.auth(user_name, user_password)
Brant Knudsona4cfe0c2014-03-15 09:36:45 -050097
98 token_id = body['token']['id']
99
Brant Knudson840011b2014-03-16 11:14:14 -0500100 # Use the unscoped token to get a token scoped to tenant1
David Kranzb7afa922014-12-30 10:56:26 -0500101 body = self.token_client.auth_token(token_id,
102 tenant=tenant1_name)
Brant Knudsona4cfe0c2014-03-15 09:36:45 -0500103
Brant Knudson840011b2014-03-16 11:14:14 -0500104 scoped_token_id = body['token']['id']
105
106 # Revoke the scoped token
David Kranze9d2f422014-07-02 13:57:41 -0400107 self.client.delete_token(scoped_token_id)
Brant Knudson840011b2014-03-16 11:14:14 -0500108
109 # Use the unscoped token to get a token scoped to tenant2
David Kranzb7afa922014-12-30 10:56:26 -0500110 body = self.token_client.auth_token(token_id,
111 tenant=tenant2_name)
Pradeep Kumar1c796282017-04-27 16:48:36 +0530112
113 @decorators.idempotent_id('ca3ea6f7-ed08-4a61-adbd-96906456ad31')
114 def test_list_endpoints_for_token(self):
Andrea Frittoli (andreaf)60cb4b22018-01-18 10:11:35 +0000115 tempest_services = ['keystone', 'nova', 'neutron', 'swift', 'cinder',
116 'neutron']
Pradeep Kumar1c796282017-04-27 16:48:36 +0530117 # 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)60cb4b22018-01-18 10:11:35 +0000130 # Get the list of available services. Keystone is always available.
Pradeep Kumar1c796282017-04-27 16:48:36 +0530131 available_services = [s[0] for s in list(
Andrea Frittoli (andreaf)60cb4b22018-01-18 10:11:35 +0000132 CONF.service_available.items()) if s[1] is True] + ['keystone']
Pradeep Kumar1c796282017-04-27 16:48:36 +0530133 # Verify that all available services are present.
Andrea Frittoli (andreaf)60cb4b22018-01-18 10:11:35 +0000134 for service in tempest_services:
135 if service in available_services:
136 self.assertIn(service, service_names)