blob: b4c93899e1f5faa4d1696950be8d1bf885ec3d55 [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
Ken'ichi Ohmichi7bd25752017-03-10 10:45:39 -080017from tempest.lib.common.utils import data_utils
Ken'ichi Ohmichieeabdd22017-01-27 17:46:00 -080018from tempest.lib import decorators
huangtianhua1b855bc2013-10-10 11:12:44 +080019
20
Matthew Treinishdb2c5972014-01-31 22:18:59 +000021class TokensTestJSON(base.BaseIdentityV2AdminTest):
huangtianhua1b855bc2013-10-10 11:12:44 +080022
Ken'ichi Ohmichieeabdd22017-01-27 17:46:00 -080023 @decorators.idempotent_id('453ad4d5-e486-4b2f-be72-cffc8149e586')
Zhi Kun Liu30caeae2014-02-26 15:30:24 +080024 def test_create_get_delete_token(self):
huangtianhua1b855bc2013-10-10 11:12:44 +080025 # get a token by username and password
Ken'ichi Ohmichi96508472015-03-23 01:43:42 +000026 user_name = data_utils.rand_name(name='user')
Zack Feldsteind8c5f7a2015-12-14 10:44:07 -060027 user_password = data_utils.rand_password()
huangtianhua1b855bc2013-10-10 11:12:44 +080028 # first:create a tenant
zhufl963d2c32017-04-20 15:44:58 +080029 tenant = self.setup_test_tenant()
huangtianhua1b855bc2013-10-10 11:12:44 +080030 # second:create a user
zhufl75d51a92017-04-11 16:02:39 +080031 user = self.create_test_user(name=user_name,
32 password=user_password,
33 tenantId=tenant['id'],
34 email='')
huangtianhua1b855bc2013-10-10 11:12:44 +080035 # then get a token for the user
David Kranzb7afa922014-12-30 10:56:26 -050036 body = self.token_client.auth(user_name,
37 user_password,
38 tenant['name'])
Andrea Frittoli8bbdb162014-01-06 11:06:13 +000039 self.assertEqual(body['token']['tenant']['name'],
huangtianhua1b855bc2013-10-10 11:12:44 +080040 tenant['name'])
Zhi Kun Liu30caeae2014-02-26 15:30:24 +080041 # Perform GET Token
Andrea Frittoli8bbdb162014-01-06 11:06:13 +000042 token_id = body['token']['id']
Ken'ichi Ohmichi402b8752015-11-09 10:47:16 +000043 token_details = self.client.show_token(token_id)['access']
Zhi Kun Liu30caeae2014-02-26 15:30:24 +080044 self.assertEqual(token_id, token_details['token']['id'])
45 self.assertEqual(user['id'], token_details['user']['id'])
46 self.assertEqual(user_name, token_details['user']['name'])
47 self.assertEqual(tenant['name'],
48 token_details['token']['tenant']['name'])
49 # then delete the token
David Kranze9d2f422014-07-02 13:57:41 -040050 self.client.delete_token(token_id)
huangtianhua1b855bc2013-10-10 11:12:44 +080051
Ken'ichi Ohmichieeabdd22017-01-27 17:46:00 -080052 @decorators.idempotent_id('25ba82ee-8a32-4ceb-8f50-8b8c71e8765e')
Brant Knudsona4cfe0c2014-03-15 09:36:45 -050053 def test_rescope_token(self):
Ken'ichi Ohmichi9e3dac02015-11-19 07:01:07 +000054 """An unscoped token can be requested
55
56 That token can be used to request a scoped token.
Brant Knudsona4cfe0c2014-03-15 09:36:45 -050057 """
58
59 # Create a user.
Ken'ichi Ohmichi96508472015-03-23 01:43:42 +000060 user_name = data_utils.rand_name(name='user')
Zack Feldsteind8c5f7a2015-12-14 10:44:07 -060061 user_password = data_utils.rand_password()
Brant Knudsona4cfe0c2014-03-15 09:36:45 -050062 tenant_id = None # No default tenant so will get unscoped token.
zhufl75d51a92017-04-11 16:02:39 +080063 user = self.create_test_user(name=user_name,
64 password=user_password,
65 tenantId=tenant_id,
66 email='')
Brant Knudsona4cfe0c2014-03-15 09:36:45 -050067
Brant Knudson840011b2014-03-16 11:14:14 -050068 # Create a couple tenants.
Ken'ichi Ohmichi96508472015-03-23 01:43:42 +000069 tenant1_name = data_utils.rand_name(name='tenant')
zhufl963d2c32017-04-20 15:44:58 +080070 tenant1 = self.setup_test_tenant(name=tenant1_name)
Brant Knudson840011b2014-03-16 11:14:14 -050071
Ken'ichi Ohmichi96508472015-03-23 01:43:42 +000072 tenant2_name = data_utils.rand_name(name='tenant')
zhufl963d2c32017-04-20 15:44:58 +080073 tenant2 = self.setup_test_tenant(name=tenant2_name)
Brant Knudsona4cfe0c2014-03-15 09:36:45 -050074
75 # Create a role
zhufl66b616a2017-04-11 15:00:32 +080076 role = self.setup_test_role()
Brant Knudsona4cfe0c2014-03-15 09:36:45 -050077
Brant Knudson840011b2014-03-16 11:14:14 -050078 # Grant the user the role on the tenants.
ghanshyam50894fc2016-06-17 13:20:25 +090079 self.roles_client.create_user_role_on_project(tenant1['id'],
80 user['id'],
81 role['id'])
Brant Knudson840011b2014-03-16 11:14:14 -050082
ghanshyam50894fc2016-06-17 13:20:25 +090083 self.roles_client.create_user_role_on_project(tenant2['id'],
84 user['id'],
85 role['id'])
Brant Knudsona4cfe0c2014-03-15 09:36:45 -050086
87 # Get an unscoped token.
David Kranzb7afa922014-12-30 10:56:26 -050088 body = self.token_client.auth(user_name, user_password)
Brant Knudsona4cfe0c2014-03-15 09:36:45 -050089
90 token_id = body['token']['id']
91
Brant Knudson840011b2014-03-16 11:14:14 -050092 # Use the unscoped token to get a token scoped to tenant1
David Kranzb7afa922014-12-30 10:56:26 -050093 body = self.token_client.auth_token(token_id,
94 tenant=tenant1_name)
Brant Knudsona4cfe0c2014-03-15 09:36:45 -050095
Brant Knudson840011b2014-03-16 11:14:14 -050096 scoped_token_id = body['token']['id']
97
98 # Revoke the scoped token
David Kranze9d2f422014-07-02 13:57:41 -040099 self.client.delete_token(scoped_token_id)
Brant Knudson840011b2014-03-16 11:14:14 -0500100
101 # Use the unscoped token to get a token scoped to tenant2
David Kranzb7afa922014-12-30 10:56:26 -0500102 body = self.token_client.auth_token(token_id,
103 tenant=tenant2_name)