blob: 860e5af7e5b0c173de7621cdd63a01990989b08d [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
Matthew Treinish01472ff2015-02-20 17:26:52 -050016from tempest_lib.common.utils import data_utils
17
huangtianhua1b855bc2013-10-10 11:12:44 +080018from tempest.api.identity import base
Matthew Treinish5c660ab2014-05-18 21:14:36 -040019from tempest import test
huangtianhua1b855bc2013-10-10 11:12:44 +080020
21
Matthew Treinishdb2c5972014-01-31 22:18:59 +000022class TokensTestJSON(base.BaseIdentityV2AdminTest):
huangtianhua1b855bc2013-10-10 11:12:44 +080023
Matthew Treinish5c660ab2014-05-18 21:14:36 -040024 @test.attr(type='gate')
Chris Hoge7579c1a2015-02-26 14:12:15 -080025 @test.idempotent_id('453ad4d5-e486-4b2f-be72-cffc8149e586')
Zhi Kun Liu30caeae2014-02-26 15:30:24 +080026 def test_create_get_delete_token(self):
huangtianhua1b855bc2013-10-10 11:12:44 +080027 # get a token by username and password
Ken'ichi Ohmichi96508472015-03-23 01:43:42 +000028 user_name = data_utils.rand_name(name='user')
29 user_password = data_utils.rand_name(name='pass')
huangtianhua1b855bc2013-10-10 11:12:44 +080030 # first:create a tenant
Ken'ichi Ohmichi96508472015-03-23 01:43:42 +000031 tenant_name = data_utils.rand_name(name='tenant')
David Kranzb7afa922014-12-30 10:56:26 -050032 tenant = self.client.create_tenant(tenant_name)
huangtianhua1b855bc2013-10-10 11:12:44 +080033 self.data.tenants.append(tenant)
34 # second:create a user
David Kranzb7afa922014-12-30 10:56:26 -050035 user = self.client.create_user(user_name, user_password,
36 tenant['id'], '')
huangtianhua1b855bc2013-10-10 11:12:44 +080037 self.data.users.append(user)
38 # then get a token for the user
David Kranzb7afa922014-12-30 10:56:26 -050039 body = self.token_client.auth(user_name,
40 user_password,
41 tenant['name'])
Andrea Frittoli8bbdb162014-01-06 11:06:13 +000042 self.assertEqual(body['token']['tenant']['name'],
huangtianhua1b855bc2013-10-10 11:12:44 +080043 tenant['name'])
Zhi Kun Liu30caeae2014-02-26 15:30:24 +080044 # Perform GET Token
Andrea Frittoli8bbdb162014-01-06 11:06:13 +000045 token_id = body['token']['id']
David Kranzb7afa922014-12-30 10:56:26 -050046 token_details = self.client.get_token(token_id)
Zhi Kun Liu30caeae2014-02-26 15:30:24 +080047 self.assertEqual(token_id, token_details['token']['id'])
48 self.assertEqual(user['id'], token_details['user']['id'])
49 self.assertEqual(user_name, token_details['user']['name'])
50 self.assertEqual(tenant['name'],
51 token_details['token']['tenant']['name'])
52 # then delete the token
David Kranze9d2f422014-07-02 13:57:41 -040053 self.client.delete_token(token_id)
huangtianhua1b855bc2013-10-10 11:12:44 +080054
Matthew Treinish5c660ab2014-05-18 21:14:36 -040055 @test.attr(type='gate')
Chris Hoge7579c1a2015-02-26 14:12:15 -080056 @test.idempotent_id('25ba82ee-8a32-4ceb-8f50-8b8c71e8765e')
Brant Knudsona4cfe0c2014-03-15 09:36:45 -050057 def test_rescope_token(self):
58 """An unscoped token can be requested, that token can be used to
59 request a scoped token.
60 """
61
62 # Create a user.
Ken'ichi Ohmichi96508472015-03-23 01:43:42 +000063 user_name = data_utils.rand_name(name='user')
64 user_password = data_utils.rand_name(name='pass')
Brant Knudsona4cfe0c2014-03-15 09:36:45 -050065 tenant_id = None # No default tenant so will get unscoped token.
66 email = ''
David Kranzb7afa922014-12-30 10:56:26 -050067 user = self.client.create_user(user_name, user_password,
68 tenant_id, email)
Brant Knudsona4cfe0c2014-03-15 09:36:45 -050069 self.data.users.append(user)
70
Brant Knudson840011b2014-03-16 11:14:14 -050071 # Create a couple tenants.
Ken'ichi Ohmichi96508472015-03-23 01:43:42 +000072 tenant1_name = data_utils.rand_name(name='tenant')
David Kranzb7afa922014-12-30 10:56:26 -050073 tenant1 = self.client.create_tenant(tenant1_name)
Brant Knudson840011b2014-03-16 11:14:14 -050074 self.data.tenants.append(tenant1)
75
Ken'ichi Ohmichi96508472015-03-23 01:43:42 +000076 tenant2_name = data_utils.rand_name(name='tenant')
David Kranzb7afa922014-12-30 10:56:26 -050077 tenant2 = self.client.create_tenant(tenant2_name)
Brant Knudson840011b2014-03-16 11:14:14 -050078 self.data.tenants.append(tenant2)
Brant Knudsona4cfe0c2014-03-15 09:36:45 -050079
80 # Create a role
Ken'ichi Ohmichi96508472015-03-23 01:43:42 +000081 role_name = data_utils.rand_name(name='role')
David Kranzb7afa922014-12-30 10:56:26 -050082 role = self.client.create_role(role_name)
Brant Knudsona4cfe0c2014-03-15 09:36:45 -050083 self.data.roles.append(role)
84
Brant Knudson840011b2014-03-16 11:14:14 -050085 # Grant the user the role on the tenants.
David Kranze9d2f422014-07-02 13:57:41 -040086 self.client.assign_user_role(tenant1['id'], user['id'],
87 role['id'])
Brant Knudson840011b2014-03-16 11:14:14 -050088
David Kranze9d2f422014-07-02 13:57:41 -040089 self.client.assign_user_role(tenant2['id'], user['id'],
90 role['id'])
Brant Knudsona4cfe0c2014-03-15 09:36:45 -050091
92 # Get an unscoped token.
David Kranzb7afa922014-12-30 10:56:26 -050093 body = self.token_client.auth(user_name, user_password)
Brant Knudsona4cfe0c2014-03-15 09:36:45 -050094
95 token_id = body['token']['id']
96
Brant Knudson840011b2014-03-16 11:14:14 -050097 # Use the unscoped token to get a token scoped to tenant1
David Kranzb7afa922014-12-30 10:56:26 -050098 body = self.token_client.auth_token(token_id,
99 tenant=tenant1_name)
Brant Knudsona4cfe0c2014-03-15 09:36:45 -0500100
Brant Knudson840011b2014-03-16 11:14:14 -0500101 scoped_token_id = body['token']['id']
102
103 # Revoke the scoped token
David Kranze9d2f422014-07-02 13:57:41 -0400104 self.client.delete_token(scoped_token_id)
Brant Knudson840011b2014-03-16 11:14:14 -0500105
106 # Use the unscoped token to get a token scoped to tenant2
David Kranzb7afa922014-12-30 10:56:26 -0500107 body = self.token_client.auth_token(token_id,
108 tenant=tenant2_name)