blob: 2f7e9414f6f2f16c2a31b79f48e962afeaacab53 [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
Fei Long Wangd39431f2015-05-14 11:30:48 +120017from tempest.common.utils import data_utils
Matthew Treinish5c660ab2014-05-18 21:14:36 -040018from tempest import test
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
Chris Hoge7579c1a2015-02-26 14:12:15 -080023 @test.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
Ken'ichi Ohmichi96508472015-03-23 01:43:42 +000029 tenant_name = data_utils.rand_name(name='tenant')
ghanshyam7668fad2016-06-15 18:17:39 +090030 tenant = self.tenants_client.create_tenant(name=tenant_name)['tenant']
Castulo J. Martineze3adee42016-07-14 10:40:08 -070031 # Delete the tenant at the end of the test
32 self.addCleanup(self.tenants_client.delete_tenant, tenant['id'])
huangtianhua1b855bc2013-10-10 11:12:44 +080033 # second:create a user
ghanshyame1c6c1c2016-06-15 14:50:41 +090034 user = self.users_client.create_user(name=user_name,
35 password=user_password,
36 tenantId=tenant['id'],
37 email='')['user']
Castulo J. Martineze3adee42016-07-14 10:40:08 -070038 # Delete the user at the end of the test
39 self.addCleanup(self.users_client.delete_user, user['id'])
huangtianhua1b855bc2013-10-10 11:12:44 +080040 # then get a token for the user
David Kranzb7afa922014-12-30 10:56:26 -050041 body = self.token_client.auth(user_name,
42 user_password,
43 tenant['name'])
Andrea Frittoli8bbdb162014-01-06 11:06:13 +000044 self.assertEqual(body['token']['tenant']['name'],
huangtianhua1b855bc2013-10-10 11:12:44 +080045 tenant['name'])
Zhi Kun Liu30caeae2014-02-26 15:30:24 +080046 # Perform GET Token
Andrea Frittoli8bbdb162014-01-06 11:06:13 +000047 token_id = body['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)
huangtianhua1b855bc2013-10-10 11:12:44 +080056
Chris Hoge7579c1a2015-02-26 14:12:15 -080057 @test.idempotent_id('25ba82ee-8a32-4ceb-8f50-8b8c71e8765e')
Brant Knudsona4cfe0c2014-03-15 09:36:45 -050058 def test_rescope_token(self):
Ken'ichi Ohmichi9e3dac02015-11-19 07:01:07 +000059 """An unscoped token can be requested
60
61 That token can be used to request a scoped token.
Brant Knudsona4cfe0c2014-03-15 09:36:45 -050062 """
63
64 # Create a user.
Ken'ichi Ohmichi96508472015-03-23 01:43:42 +000065 user_name = data_utils.rand_name(name='user')
Zack Feldsteind8c5f7a2015-12-14 10:44:07 -060066 user_password = data_utils.rand_password()
Brant Knudsona4cfe0c2014-03-15 09:36:45 -050067 tenant_id = None # No default tenant so will get unscoped token.
68 email = ''
ghanshyame1c6c1c2016-06-15 14:50:41 +090069 user = self.users_client.create_user(name=user_name,
70 password=user_password,
71 tenantId=tenant_id,
72 email=email)['user']
Castulo J. Martineze3adee42016-07-14 10:40:08 -070073 # Delete the user at the end of the test
74 self.addCleanup(self.users_client.delete_user, user['id'])
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')
ghanshyam7668fad2016-06-15 18:17:39 +090078 tenant1 = self.tenants_client.create_tenant(
79 name=tenant1_name)['tenant']
Castulo J. Martineze3adee42016-07-14 10:40:08 -070080 # Delete the tenant at the end of the test
81 self.addCleanup(self.tenants_client.delete_tenant, tenant1['id'])
Brant Knudson840011b2014-03-16 11:14:14 -050082
Ken'ichi Ohmichi96508472015-03-23 01:43:42 +000083 tenant2_name = data_utils.rand_name(name='tenant')
ghanshyam7668fad2016-06-15 18:17:39 +090084 tenant2 = self.tenants_client.create_tenant(
85 name=tenant2_name)['tenant']
Castulo J. Martineze3adee42016-07-14 10:40:08 -070086 # Delete the tenant at the end of the test
87 self.addCleanup(self.tenants_client.delete_tenant, tenant2['id'])
Brant Knudsona4cfe0c2014-03-15 09:36:45 -050088
89 # Create a role
Ken'ichi Ohmichi96508472015-03-23 01:43:42 +000090 role_name = data_utils.rand_name(name='role')
piyush110786afaaf262015-12-11 18:54:05 +053091 role = self.roles_client.create_role(name=role_name)['role']
Castulo J. Martineze3adee42016-07-14 10:40:08 -070092 # Delete the role at the end of the test
93 self.addCleanup(self.roles_client.delete_role, role['id'])
Brant Knudsona4cfe0c2014-03-15 09:36:45 -050094
Brant Knudson840011b2014-03-16 11:14:14 -050095 # Grant the user the role on the tenants.
ghanshyam50894fc2016-06-17 13:20:25 +090096 self.roles_client.create_user_role_on_project(tenant1['id'],
97 user['id'],
98 role['id'])
Brant Knudson840011b2014-03-16 11:14:14 -050099
ghanshyam50894fc2016-06-17 13:20:25 +0900100 self.roles_client.create_user_role_on_project(tenant2['id'],
101 user['id'],
102 role['id'])
Brant Knudsona4cfe0c2014-03-15 09:36:45 -0500103
104 # Get an unscoped token.
David Kranzb7afa922014-12-30 10:56:26 -0500105 body = self.token_client.auth(user_name, user_password)
Brant Knudsona4cfe0c2014-03-15 09:36:45 -0500106
107 token_id = body['token']['id']
108
Brant Knudson840011b2014-03-16 11:14:14 -0500109 # Use the unscoped token to get a token scoped to tenant1
David Kranzb7afa922014-12-30 10:56:26 -0500110 body = self.token_client.auth_token(token_id,
111 tenant=tenant1_name)
Brant Knudsona4cfe0c2014-03-15 09:36:45 -0500112
Brant Knudson840011b2014-03-16 11:14:14 -0500113 scoped_token_id = body['token']['id']
114
115 # Revoke the scoped token
David Kranze9d2f422014-07-02 13:57:41 -0400116 self.client.delete_token(scoped_token_id)
Brant Knudson840011b2014-03-16 11:14:14 -0500117
118 # Use the unscoped token to get a token scoped to tenant2
David Kranzb7afa922014-12-30 10:56:26 -0500119 body = self.token_client.auth_token(token_id,
120 tenant=tenant2_name)