blob: 7358ce9af40870ba06d8c79f27e97c05b7e6bc75 [file] [log] [blame]
ZhiQiang Fan39f97222013-09-20 04:49:44 +08001# Copyright 2012 OpenStack Foundation
nayna-patelb35f7232013-06-28 07:08:44 +00002# 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
Masayuki Igawabfa07602015-01-20 18:47:17 +090017from tempest_lib import exceptions as lib_exc
18
nayna-patelb35f7232013-06-28 07:08:44 +000019from tempest.api.identity import base
Matthew Treinish5c660ab2014-05-18 21:14:36 -040020from tempest import test
nayna-patelb35f7232013-06-28 07:08:44 +000021
22
Masayuki Igawabe64ed32014-02-19 14:32:03 +090023class TokensV3TestJSON(base.BaseIdentityV3AdminTest):
nayna-patelb35f7232013-06-28 07:08:44 +000024
Matthew Treinish5c660ab2014-05-18 21:14:36 -040025 @test.attr(type='smoke')
Chris Hoge7579c1a2015-02-26 14:12:15 -080026 @test.idempotent_id('0f9f5a5f-d5cd-4a86-8a5b-c5ded151f212')
nayna-patelb35f7232013-06-28 07:08:44 +000027 def test_tokens(self):
28 # Valid user's token is authenticated
29 # Create a User
Masayuki Igawa259c1132013-10-31 17:48:44 +090030 u_name = data_utils.rand_name('user-')
nayna-patelb35f7232013-06-28 07:08:44 +000031 u_desc = '%s-description' % u_name
32 u_email = '%s@testmail.tm' % u_name
Masayuki Igawa259c1132013-10-31 17:48:44 +090033 u_password = data_utils.rand_name('pass-')
David Kranzd8ccb792014-12-29 11:32:05 -050034 user = self.client.create_user(
nayna-patelb35f7232013-06-28 07:08:44 +000035 u_name, description=u_desc, password=u_password,
36 email=u_email)
Matthew Treinishdb2c5972014-01-31 22:18:59 +000037 self.addCleanup(self.client.delete_user, user['id'])
nayna-patelb35f7232013-06-28 07:08:44 +000038 # Perform Authentication
Jamie Lennox97504612015-02-26 16:47:06 +110039 resp = self.token.auth(user_id=user['id'],
40 password=u_password).response
nayna-patelb35f7232013-06-28 07:08:44 +000041 subject_token = resp['x-subject-token']
42 # Perform GET Token
David Kranzd8ccb792014-12-29 11:32:05 -050043 token_details = self.client.get_token(subject_token)
nayna-patelb35f7232013-06-28 07:08:44 +000044 self.assertEqual(resp['x-subject-token'], subject_token)
45 self.assertEqual(token_details['user']['id'], user['id'])
46 self.assertEqual(token_details['user']['name'], u_name)
47 # Perform Delete Token
David Kranze9d2f422014-07-02 13:57:41 -040048 self.client.delete_token(subject_token)
Masayuki Igawabfa07602015-01-20 18:47:17 +090049 self.assertRaises(lib_exc.NotFound, self.client.get_token,
nayna-patelb35f7232013-06-28 07:08:44 +000050 subject_token)
51
Matthew Treinish5c660ab2014-05-18 21:14:36 -040052 @test.attr(type='gate')
Chris Hoge7579c1a2015-02-26 14:12:15 -080053 @test.idempotent_id('565fa210-1da1-4563-999b-f7b5b67cf112')
Brant Knudsonc5553292014-03-15 11:06:05 -050054 def test_rescope_token(self):
Brant Knudson5ee44a42014-03-16 10:55:21 -050055 """Rescope a token.
56
57 An unscoped token can be requested, that token can be used to request a
58 scoped token. The scoped token can be revoked, and the original token
59 used to get a token in a different project.
60
Brant Knudsonc5553292014-03-15 11:06:05 -050061 """
62
63 # Create a user.
64 user_name = data_utils.rand_name(name='user-')
65 user_password = data_utils.rand_name(name='pass-')
David Kranzd8ccb792014-12-29 11:32:05 -050066 user = self.client.create_user(user_name, password=user_password)
Brant Knudsonc5553292014-03-15 11:06:05 -050067 self.addCleanup(self.client.delete_user, user['id'])
68
Brant Knudson5ee44a42014-03-16 10:55:21 -050069 # Create a couple projects
70 project1_name = data_utils.rand_name(name='project-')
David Kranzd8ccb792014-12-29 11:32:05 -050071 project1 = self.client.create_project(project1_name)
Brant Knudson5ee44a42014-03-16 10:55:21 -050072 self.addCleanup(self.client.delete_project, project1['id'])
73
74 project2_name = data_utils.rand_name(name='project-')
David Kranzd8ccb792014-12-29 11:32:05 -050075 project2 = self.client.create_project(project2_name)
Brant Knudson5ee44a42014-03-16 10:55:21 -050076 self.addCleanup(self.client.delete_project, project2['id'])
Brant Knudsonc5553292014-03-15 11:06:05 -050077
78 # Create a role
79 role_name = data_utils.rand_name(name='role-')
David Kranzd8ccb792014-12-29 11:32:05 -050080 role = self.client.create_role(role_name)
Brant Knudsonc5553292014-03-15 11:06:05 -050081 self.addCleanup(self.client.delete_role, role['id'])
82
Brant Knudson5ee44a42014-03-16 10:55:21 -050083 # Grant the user the role on both projects.
David Kranze9d2f422014-07-02 13:57:41 -040084 self.client.assign_user_role(project1['id'], user['id'],
85 role['id'])
Brant Knudson5ee44a42014-03-16 10:55:21 -050086
David Kranze9d2f422014-07-02 13:57:41 -040087 self.client.assign_user_role(project2['id'], user['id'],
88 role['id'])
Brant Knudsonc5553292014-03-15 11:06:05 -050089
90 # Get an unscoped token.
Jamie Lennox97504612015-02-26 16:47:06 +110091 token_auth = self.token.auth(user_id=user['id'],
David Kranzd8ccb792014-12-29 11:32:05 -050092 password=user_password)
Brant Knudsonc5553292014-03-15 11:06:05 -050093
David Kranzd8ccb792014-12-29 11:32:05 -050094 token_id = token_auth.response['x-subject-token']
Brant Knudsonc5553292014-03-15 11:06:05 -050095 orig_expires_at = token_auth['token']['expires_at']
96 orig_issued_at = token_auth['token']['issued_at']
97 orig_user = token_auth['token']['user']
98
99 self.assertIsInstance(token_auth['token']['expires_at'], unicode)
100 self.assertIsInstance(token_auth['token']['issued_at'], unicode)
101 self.assertEqual(['password'], token_auth['token']['methods'])
102 self.assertEqual(user['id'], token_auth['token']['user']['id'])
103 self.assertEqual(user['name'], token_auth['token']['user']['name'])
104 self.assertEqual('default',
105 token_auth['token']['user']['domain']['id'])
106 self.assertEqual('Default',
107 token_auth['token']['user']['domain']['name'])
108 self.assertNotIn('catalog', token_auth['token'])
109 self.assertNotIn('project', token_auth['token'])
110 self.assertNotIn('roles', token_auth['token'])
111
112 # Use the unscoped token to get a scoped token.
David Kranzd8ccb792014-12-29 11:32:05 -0500113 token_auth = self.token.auth(token=token_id,
Jamie Lennox97504612015-02-26 16:47:06 +1100114 project_name=project1_name,
115 project_domain_name='Default')
David Kranzd8ccb792014-12-29 11:32:05 -0500116 token1_id = token_auth.response['x-subject-token']
Brant Knudsonc5553292014-03-15 11:06:05 -0500117
118 self.assertEqual(orig_expires_at, token_auth['token']['expires_at'],
119 'Expiration time should match original token')
120 self.assertIsInstance(token_auth['token']['issued_at'], unicode)
121 self.assertNotEqual(orig_issued_at, token_auth['token']['issued_at'])
122 self.assertEqual(set(['password', 'token']),
123 set(token_auth['token']['methods']))
124 self.assertEqual(orig_user, token_auth['token']['user'],
125 'User should match original token')
126 self.assertIsInstance(token_auth['token']['catalog'], list)
Brant Knudson5ee44a42014-03-16 10:55:21 -0500127 self.assertEqual(project1['id'],
Brant Knudsonc5553292014-03-15 11:06:05 -0500128 token_auth['token']['project']['id'])
Brant Knudson5ee44a42014-03-16 10:55:21 -0500129 self.assertEqual(project1['name'],
Brant Knudsonc5553292014-03-15 11:06:05 -0500130 token_auth['token']['project']['name'])
131 self.assertEqual('default',
132 token_auth['token']['project']['domain']['id'])
133 self.assertEqual('Default',
134 token_auth['token']['project']['domain']['name'])
135 self.assertEqual(1, len(token_auth['token']['roles']))
136 self.assertEqual(role['id'], token_auth['token']['roles'][0]['id'])
137 self.assertEqual(role['name'], token_auth['token']['roles'][0]['name'])
138
Brant Knudson5ee44a42014-03-16 10:55:21 -0500139 # Revoke the unscoped token.
David Kranze9d2f422014-07-02 13:57:41 -0400140 self.client.delete_token(token1_id)
Brant Knudson5ee44a42014-03-16 10:55:21 -0500141
142 # Now get another scoped token using the unscoped token.
David Kranzd8ccb792014-12-29 11:32:05 -0500143 token_auth = self.token.auth(token=token_id,
Jamie Lennox97504612015-02-26 16:47:06 +1100144 project_name=project2_name,
145 project_domain_name='Default')
Brant Knudson5ee44a42014-03-16 10:55:21 -0500146
147 self.assertEqual(project2['id'],
148 token_auth['token']['project']['id'])
149 self.assertEqual(project2['name'],
150 token_auth['token']['project']['name'])