blob: 78a2aada5cffe14ffb05ba6fbe1dbd5c0a5a4607 [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):
zhufla7635d72020-04-29 14:36:41 +080026 """Test keystone tokens via v2 API"""
huangtianhua1b855bc2013-10-10 11:12:44 +080027
Ken'ichi Ohmichieeabdd22017-01-27 17:46:00 -080028 @decorators.idempotent_id('453ad4d5-e486-4b2f-be72-cffc8149e586')
Pradeep Kumar1c796282017-04-27 16:48:36 +053029 def test_create_check_get_delete_token(self):
zhufla7635d72020-04-29 14:36:41 +080030 """Test getting create/check/get/delete token for user via v2 API"""
huangtianhua1b855bc2013-10-10 11:12:44 +080031 # get a token by username and password
Martin Kopec213d0a42023-11-30 10:28:14 +010032 user_name = data_utils.rand_name(
33 name='user', prefix=CONF.resource_name_prefix)
Zack Feldsteind8c5f7a2015-12-14 10:44:07 -060034 user_password = data_utils.rand_password()
huangtianhua1b855bc2013-10-10 11:12:44 +080035 # first:create a tenant
zhufl963d2c32017-04-20 15:44:58 +080036 tenant = self.setup_test_tenant()
huangtianhua1b855bc2013-10-10 11:12:44 +080037 # second:create a user
zhufl75d51a92017-04-11 16:02:39 +080038 user = self.create_test_user(name=user_name,
39 password=user_password,
40 tenantId=tenant['id'],
41 email='')
huangtianhua1b855bc2013-10-10 11:12:44 +080042 # then get a token for the user
David Kranzb7afa922014-12-30 10:56:26 -050043 body = self.token_client.auth(user_name,
44 user_password,
45 tenant['name'])
Andrea Frittoli8bbdb162014-01-06 11:06:13 +000046 self.assertEqual(body['token']['tenant']['name'],
huangtianhua1b855bc2013-10-10 11:12:44 +080047 tenant['name'])
Zhi Kun Liu30caeae2014-02-26 15:30:24 +080048 # Perform GET Token
Andrea Frittoli8bbdb162014-01-06 11:06:13 +000049 token_id = body['token']['id']
Pradeep Kumar1c796282017-04-27 16:48:36 +053050 self.client.check_token_existence(token_id)
Ken'ichi Ohmichi402b8752015-11-09 10:47:16 +000051 token_details = self.client.show_token(token_id)['access']
Zhi Kun Liu30caeae2014-02-26 15:30:24 +080052 self.assertEqual(token_id, token_details['token']['id'])
53 self.assertEqual(user['id'], token_details['user']['id'])
54 self.assertEqual(user_name, token_details['user']['name'])
55 self.assertEqual(tenant['name'],
56 token_details['token']['tenant']['name'])
57 # then delete the token
David Kranze9d2f422014-07-02 13:57:41 -040058 self.client.delete_token(token_id)
Pradeep Kumar1c796282017-04-27 16:48:36 +053059 self.assertRaises(lib_exc.NotFound,
60 self.client.check_token_existence,
61 token_id)
huangtianhua1b855bc2013-10-10 11:12:44 +080062
Ken'ichi Ohmichieeabdd22017-01-27 17:46:00 -080063 @decorators.idempotent_id('25ba82ee-8a32-4ceb-8f50-8b8c71e8765e')
Brant Knudsona4cfe0c2014-03-15 09:36:45 -050064 def test_rescope_token(self):
zhufla7635d72020-04-29 14:36:41 +080065 """Test an unscoped token can be requested via v2 API
Ken'ichi Ohmichi9e3dac02015-11-19 07:01:07 +000066
67 That token can be used to request a scoped token.
Brant Knudsona4cfe0c2014-03-15 09:36:45 -050068 """
69
70 # Create a user.
Martin Kopec213d0a42023-11-30 10:28:14 +010071 user_name = data_utils.rand_name(
72 name='user', prefix=CONF.resource_name_prefix)
Zack Feldsteind8c5f7a2015-12-14 10:44:07 -060073 user_password = data_utils.rand_password()
Brant Knudsona4cfe0c2014-03-15 09:36:45 -050074 tenant_id = None # No default tenant so will get unscoped token.
zhufl75d51a92017-04-11 16:02:39 +080075 user = self.create_test_user(name=user_name,
76 password=user_password,
77 tenantId=tenant_id,
78 email='')
Brant Knudsona4cfe0c2014-03-15 09:36:45 -050079
Brant Knudson840011b2014-03-16 11:14:14 -050080 # Create a couple tenants.
Martin Kopec213d0a42023-11-30 10:28:14 +010081 tenant1_name = data_utils.rand_name(
82 name='tenant', prefix=CONF.resource_name_prefix)
zhufl963d2c32017-04-20 15:44:58 +080083 tenant1 = self.setup_test_tenant(name=tenant1_name)
Brant Knudson840011b2014-03-16 11:14:14 -050084
Martin Kopec213d0a42023-11-30 10:28:14 +010085 tenant2_name = data_utils.rand_name(
86 name='tenant', prefix=CONF.resource_name_prefix)
zhufl963d2c32017-04-20 15:44:58 +080087 tenant2 = self.setup_test_tenant(name=tenant2_name)
Brant Knudsona4cfe0c2014-03-15 09:36:45 -050088
89 # Create a role
zhufl66b616a2017-04-11 15:00:32 +080090 role = self.setup_test_role()
Brant Knudsona4cfe0c2014-03-15 09:36:45 -050091
Brant Knudson840011b2014-03-16 11:14:14 -050092 # Grant the user the role on the tenants.
ghanshyam50894fc2016-06-17 13:20:25 +090093 self.roles_client.create_user_role_on_project(tenant1['id'],
94 user['id'],
95 role['id'])
Brant Knudson840011b2014-03-16 11:14:14 -050096
ghanshyam50894fc2016-06-17 13:20:25 +090097 self.roles_client.create_user_role_on_project(tenant2['id'],
98 user['id'],
99 role['id'])
Brant Knudsona4cfe0c2014-03-15 09:36:45 -0500100
101 # Get an unscoped token.
David Kranzb7afa922014-12-30 10:56:26 -0500102 body = self.token_client.auth(user_name, user_password)
Brant Knudsona4cfe0c2014-03-15 09:36:45 -0500103
104 token_id = body['token']['id']
105
Brant Knudson840011b2014-03-16 11:14:14 -0500106 # Use the unscoped token to get a token scoped to tenant1
David Kranzb7afa922014-12-30 10:56:26 -0500107 body = self.token_client.auth_token(token_id,
108 tenant=tenant1_name)
Brant Knudsona4cfe0c2014-03-15 09:36:45 -0500109
Brant Knudson840011b2014-03-16 11:14:14 -0500110 scoped_token_id = body['token']['id']
111
112 # Revoke the scoped token
David Kranze9d2f422014-07-02 13:57:41 -0400113 self.client.delete_token(scoped_token_id)
Brant Knudson840011b2014-03-16 11:14:14 -0500114
115 # Use the unscoped token to get a token scoped to tenant2
David Kranzb7afa922014-12-30 10:56:26 -0500116 body = self.token_client.auth_token(token_id,
117 tenant=tenant2_name)
Pradeep Kumar1c796282017-04-27 16:48:36 +0530118
119 @decorators.idempotent_id('ca3ea6f7-ed08-4a61-adbd-96906456ad31')
120 def test_list_endpoints_for_token(self):
zhufla7635d72020-04-29 14:36:41 +0800121 """Test listing endpoints for token via v2 API"""
Andrea Frittoli (andreaf)60cb4b22018-01-18 10:11:35 +0000122 tempest_services = ['keystone', 'nova', 'neutron', 'swift', 'cinder',
123 'neutron']
Pradeep Kumar1c796282017-04-27 16:48:36 +0530124 # get a token for the user
125 creds = self.os_primary.credentials
126 username = creds.username
127 password = creds.password
128 tenant_name = creds.tenant_name
129 token = self.token_client.auth(username,
130 password,
131 tenant_name)['token']
132 endpoints = self.client.list_endpoints_for_token(
133 token['id'])['endpoints']
134 self.assertIsInstance(endpoints, list)
135 # Store list of service names
136 service_names = [e['name'] for e in endpoints]
Andrea Frittoli (andreaf)60cb4b22018-01-18 10:11:35 +0000137 # Get the list of available services. Keystone is always available.
Pradeep Kumar1c796282017-04-27 16:48:36 +0530138 available_services = [s[0] for s in list(
Andrea Frittoli (andreaf)60cb4b22018-01-18 10:11:35 +0000139 CONF.service_available.items()) if s[1] is True] + ['keystone']
Pradeep Kumar1c796282017-04-27 16:48:36 +0530140 # Verify that all available services are present.
Andrea Frittoli (andreaf)60cb4b22018-01-18 10:11:35 +0000141 for service in tempest_services:
142 if service in available_services:
143 self.assertIn(service, service_names)