blob: 2c5fb74dcce1139667727b9b38bc71ea62bbd2ff [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
17from 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 _interface = 'json'
23
Matthew Treinish5c660ab2014-05-18 21:14:36 -040024 @test.attr(type='gate')
Zhi Kun Liu30caeae2014-02-26 15:30:24 +080025 def test_create_get_delete_token(self):
huangtianhua1b855bc2013-10-10 11:12:44 +080026 # get a token by username and password
27 user_name = data_utils.rand_name(name='user-')
28 user_password = data_utils.rand_name(name='pass-')
29 # first:create a tenant
30 tenant_name = data_utils.rand_name(name='tenant-')
David Kranze9d2f422014-07-02 13:57:41 -040031 _, tenant = self.client.create_tenant(tenant_name)
huangtianhua1b855bc2013-10-10 11:12:44 +080032 self.data.tenants.append(tenant)
33 # second:create a user
David Kranze9d2f422014-07-02 13:57:41 -040034 _, user = self.client.create_user(user_name, user_password,
35 tenant['id'], '')
huangtianhua1b855bc2013-10-10 11:12:44 +080036 self.data.users.append(user)
37 # then get a token for the user
David Kranzfb3efa72014-08-28 16:58:25 -040038 _, body = self.token_client.auth(user_name,
39 user_password,
40 tenant['name'])
Andrea Frittoli8bbdb162014-01-06 11:06:13 +000041 self.assertEqual(body['token']['tenant']['name'],
huangtianhua1b855bc2013-10-10 11:12:44 +080042 tenant['name'])
Zhi Kun Liu30caeae2014-02-26 15:30:24 +080043 # Perform GET Token
Andrea Frittoli8bbdb162014-01-06 11:06:13 +000044 token_id = body['token']['id']
David Kranze9d2f422014-07-02 13:57:41 -040045 _, token_details = self.client.get_token(token_id)
Zhi Kun Liu30caeae2014-02-26 15:30:24 +080046 self.assertEqual(token_id, token_details['token']['id'])
47 self.assertEqual(user['id'], token_details['user']['id'])
48 self.assertEqual(user_name, token_details['user']['name'])
49 self.assertEqual(tenant['name'],
50 token_details['token']['tenant']['name'])
51 # then delete the token
David Kranze9d2f422014-07-02 13:57:41 -040052 self.client.delete_token(token_id)
huangtianhua1b855bc2013-10-10 11:12:44 +080053
Matthew Treinish5c660ab2014-05-18 21:14:36 -040054 @test.attr(type='gate')
Brant Knudsona4cfe0c2014-03-15 09:36:45 -050055 def test_rescope_token(self):
56 """An unscoped token can be requested, that token can be used to
57 request a scoped token.
58 """
59
60 # Create a user.
61 user_name = data_utils.rand_name(name='user-')
62 user_password = data_utils.rand_name(name='pass-')
63 tenant_id = None # No default tenant so will get unscoped token.
64 email = ''
David Kranze9d2f422014-07-02 13:57:41 -040065 _, user = self.client.create_user(user_name, user_password,
66 tenant_id, email)
Brant Knudsona4cfe0c2014-03-15 09:36:45 -050067 self.data.users.append(user)
68
Brant Knudson840011b2014-03-16 11:14:14 -050069 # Create a couple tenants.
70 tenant1_name = data_utils.rand_name(name='tenant-')
David Kranze9d2f422014-07-02 13:57:41 -040071 _, tenant1 = self.client.create_tenant(tenant1_name)
Brant Knudson840011b2014-03-16 11:14:14 -050072 self.data.tenants.append(tenant1)
73
74 tenant2_name = data_utils.rand_name(name='tenant-')
David Kranze9d2f422014-07-02 13:57:41 -040075 _, tenant2 = self.client.create_tenant(tenant2_name)
Brant Knudson840011b2014-03-16 11:14:14 -050076 self.data.tenants.append(tenant2)
Brant Knudsona4cfe0c2014-03-15 09:36:45 -050077
78 # Create a role
79 role_name = data_utils.rand_name(name='role-')
David Kranze9d2f422014-07-02 13:57:41 -040080 _, role = self.client.create_role(role_name)
Brant Knudsona4cfe0c2014-03-15 09:36:45 -050081 self.data.roles.append(role)
82
Brant Knudson840011b2014-03-16 11:14:14 -050083 # Grant the user the role on the tenants.
David Kranze9d2f422014-07-02 13:57:41 -040084 self.client.assign_user_role(tenant1['id'], user['id'],
85 role['id'])
Brant Knudson840011b2014-03-16 11:14:14 -050086
David Kranze9d2f422014-07-02 13:57:41 -040087 self.client.assign_user_role(tenant2['id'], user['id'],
88 role['id'])
Brant Knudsona4cfe0c2014-03-15 09:36:45 -050089
90 # Get an unscoped token.
David Kranzfb3efa72014-08-28 16:58:25 -040091 _, body = self.token_client.auth(user_name, user_password)
Brant Knudsona4cfe0c2014-03-15 09:36:45 -050092
93 token_id = body['token']['id']
94
Brant Knudson840011b2014-03-16 11:14:14 -050095 # Use the unscoped token to get a token scoped to tenant1
David Kranzfb3efa72014-08-28 16:58:25 -040096 _, body = self.token_client.auth_token(token_id,
97 tenant=tenant1_name)
Brant Knudsona4cfe0c2014-03-15 09:36:45 -050098
Brant Knudson840011b2014-03-16 11:14:14 -050099 scoped_token_id = body['token']['id']
100
101 # Revoke the scoped token
David Kranze9d2f422014-07-02 13:57:41 -0400102 self.client.delete_token(scoped_token_id)
Brant Knudson840011b2014-03-16 11:14:14 -0500103
104 # Use the unscoped token to get a token scoped to tenant2
David Kranzfb3efa72014-08-28 16:58:25 -0400105 _, body = self.token_client.auth_token(token_id,
106 tenant=tenant2_name)
Brant Knudson840011b2014-03-16 11:14:14 -0500107
huangtianhua1b855bc2013-10-10 11:12:44 +0800108
109class TokensTestXML(TokensTestJSON):
110 _interface = 'xml'