blob: 0f783b3f33173ed3babf2e8253ea402d837602c0 [file] [log] [blame]
ZhiQiang Fan39f97222013-09-20 04:49:44 +08001# Copyright 2012 OpenStack Foundation
Jay Pipesf38eaac2012-06-21 13:37:35 -04002# 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
Yaroslav Lobankovcbcb6112016-03-08 12:30:01 -060016import time
17
Masayuki Igawaba7bcf62014-02-17 14:56:41 +090018from testtools import matchers
Sean Dague1937d092013-05-17 16:36:38 -040019
20from tempest.api.identity import base
Fei Long Wangd39431f2015-05-14 11:30:48 +120021from tempest.common.utils import data_utils
Masayuki Igawaba7bcf62014-02-17 14:56:41 +090022from tempest import test
Rohit Karajgi6b1e1542012-05-14 05:55:54 -070023
24
Matthew Treinishdb2c5972014-01-31 22:18:59 +000025class UsersTestJSON(base.BaseIdentityV2AdminTest):
Rohit Karajgi6b1e1542012-05-14 05:55:54 -070026
Adalberto Medeiros081464b2013-08-16 09:41:29 -040027 @classmethod
Andrea Frittoli7688e742014-09-15 12:38:22 +010028 def resource_setup(cls):
29 super(UsersTestJSON, cls).resource_setup()
Ken'ichi Ohmichi96508472015-03-23 01:43:42 +000030 cls.alt_user = data_utils.rand_name('test_user')
Zack Feldsteind8c5f7a2015-12-14 10:44:07 -060031 cls.alt_password = data_utils.rand_password()
Adalberto Medeiros081464b2013-08-16 09:41:29 -040032 cls.alt_email = cls.alt_user + '@testmail.tm'
Rohit Karajgi6b1e1542012-05-14 05:55:54 -070033
Masayuki Igawaba7bcf62014-02-17 14:56:41 +090034 @test.attr(type='smoke')
Chris Hoge7579c1a2015-02-26 14:12:15 -080035 @test.idempotent_id('2d55a71e-da1d-4b43-9c03-d269fd93d905')
Rohit Karajgi6b1e1542012-05-14 05:55:54 -070036 def test_create_user(self):
Sean Dague46c4a2b2013-01-03 17:54:17 -050037 # Create a user
Rohit Karajgi6b1e1542012-05-14 05:55:54 -070038 self.data.setup_test_tenant()
ghanshyame1c6c1c2016-06-15 14:50:41 +090039 user = self.users_client.create_user(name=self.alt_user,
40 password=self.alt_password,
41 tenantId=self.data.tenant['id'],
42 email=self.alt_email)['user']
Rohit Karajgi6b1e1542012-05-14 05:55:54 -070043 self.data.users.append(user)
Rohit Karajgi6b1e1542012-05-14 05:55:54 -070044 self.assertEqual(self.alt_user, user['name'])
45
Chris Hoge7579c1a2015-02-26 14:12:15 -080046 @test.idempotent_id('89d9fdb8-15c2-4304-a429-48715d0af33d')
huangtianhuafc8db4f2013-10-08 12:05:58 +080047 def test_create_user_with_enabled(self):
48 # Create a user with enabled : False
Rohit Karajgi6b1e1542012-05-14 05:55:54 -070049 self.data.setup_test_tenant()
Ken'ichi Ohmichi96508472015-03-23 01:43:42 +000050 name = data_utils.rand_name('test_user')
ghanshyame1c6c1c2016-06-15 14:50:41 +090051 user = self.users_client.create_user(name=name,
52 password=self.alt_password,
53 tenantId=self.data.tenant['id'],
54 email=self.alt_email,
Daniel Mellado82c83a52015-12-09 15:16:49 +000055 enabled=False)['user']
huangtianhuafc8db4f2013-10-08 12:05:58 +080056 self.data.users.append(user)
huangtianhuafc8db4f2013-10-08 12:05:58 +080057 self.assertEqual(name, user['name'])
Ken'ichi Ohmichi73cb70b2015-04-17 02:31:12 +000058 self.assertEqual(False, user['enabled'])
huangtianhuafc8db4f2013-10-08 12:05:58 +080059 self.assertEqual(self.alt_email, user['email'])
Rohit Karajgi6b1e1542012-05-14 05:55:54 -070060
Chris Hoge7579c1a2015-02-26 14:12:15 -080061 @test.idempotent_id('39d05857-e8a5-4ed4-ba83-0b52d3ab97ee')
Chang Bo Guob36b2f12013-09-13 04:52:00 -070062 def test_update_user(self):
63 # Test case to check if updating of user attributes is successful.
Ken'ichi Ohmichi96508472015-03-23 01:43:42 +000064 test_user = data_utils.rand_name('test_user')
Chang Bo Guob36b2f12013-09-13 04:52:00 -070065 self.data.setup_test_tenant()
ghanshyame1c6c1c2016-06-15 14:50:41 +090066 user = self.users_client.create_user(name=test_user,
67 password=self.alt_password,
68 tenantId=self.data.tenant['id'],
69 email=self.alt_email)['user']
Chang Bo Guob36b2f12013-09-13 04:52:00 -070070 # Delete the User at the end of this method
Daniel Mellado82c83a52015-12-09 15:16:49 +000071 self.addCleanup(self.users_client.delete_user, user['id'])
Chang Bo Guob36b2f12013-09-13 04:52:00 -070072 # Updating user details with new values
Ken'ichi Ohmichi96508472015-03-23 01:43:42 +000073 u_name2 = data_utils.rand_name('user2')
Chang Bo Guob36b2f12013-09-13 04:52:00 -070074 u_email2 = u_name2 + '@testmail.tm'
Daniel Mellado82c83a52015-12-09 15:16:49 +000075 update_user = self.users_client.update_user(user['id'], name=u_name2,
76 email=u_email2,
77 enabled=False)['user']
Chang Bo Guob36b2f12013-09-13 04:52:00 -070078 self.assertEqual(u_name2, update_user['name'])
79 self.assertEqual(u_email2, update_user['email'])
Ken'ichi Ohmichi73cb70b2015-04-17 02:31:12 +000080 self.assertEqual(False, update_user['enabled'])
Chang Bo Guob36b2f12013-09-13 04:52:00 -070081 # GET by id after updating
Daniel Mellado82c83a52015-12-09 15:16:49 +000082 updated_user = self.users_client.show_user(user['id'])['user']
Chang Bo Guob36b2f12013-09-13 04:52:00 -070083 # Assert response body of GET after updating
84 self.assertEqual(u_name2, updated_user['name'])
85 self.assertEqual(u_email2, updated_user['email'])
Ken'ichi Ohmichi73cb70b2015-04-17 02:31:12 +000086 self.assertEqual(False, update_user['enabled'])
Chang Bo Guob36b2f12013-09-13 04:52:00 -070087
Chris Hoge7579c1a2015-02-26 14:12:15 -080088 @test.idempotent_id('29ed26f4-a74e-4425-9a85-fdb49fa269d2')
Rohit Karajgi6b1e1542012-05-14 05:55:54 -070089 def test_delete_user(self):
Sean Dague46c4a2b2013-01-03 17:54:17 -050090 # Delete a user
Ken'ichi Ohmichi96508472015-03-23 01:43:42 +000091 test_user = data_utils.rand_name('test_user')
Rohit Karajgi6b1e1542012-05-14 05:55:54 -070092 self.data.setup_test_tenant()
ghanshyame1c6c1c2016-06-15 14:50:41 +090093 user = self.users_client.create_user(name=test_user,
94 password=self.alt_password,
95 tenantId=self.data.tenant['id'],
96 email=self.alt_email)['user']
Daniel Mellado82c83a52015-12-09 15:16:49 +000097 self.users_client.delete_user(user['id'])
Rohit Karajgi6b1e1542012-05-14 05:55:54 -070098
Chris Hoge7579c1a2015-02-26 14:12:15 -080099 @test.idempotent_id('aca696c3-d645-4f45-b728-63646045beb1')
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700100 def test_user_authentication(self):
Sean Dague46c4a2b2013-01-03 17:54:17 -0500101 # Valid user's token is authenticated
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700102 self.data.setup_test_user()
103 # Get a token
Yaroslav Lobankov95aa3f72016-01-28 15:39:49 -0600104 self.token_client.auth(self.data.user['name'],
105 self.data.user_password,
106 self.data.tenant['name'])
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700107 # Re-auth
Yaroslav Lobankov95aa3f72016-01-28 15:39:49 -0600108 self.token_client.auth(self.data.user['name'],
109 self.data.user_password,
110 self.data.tenant['name'])
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700111
Chris Hoge7579c1a2015-02-26 14:12:15 -0800112 @test.idempotent_id('5d1fa498-4c2d-4732-a8fe-2b054598cfdd')
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700113 def test_authentication_request_without_token(self):
Sean Dague46c4a2b2013-01-03 17:54:17 -0500114 # Request for token authentication with a valid token in header
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700115 self.data.setup_test_user()
Yaroslav Lobankov95aa3f72016-01-28 15:39:49 -0600116 self.token_client.auth(self.data.user['name'],
117 self.data.user_password,
118 self.data.tenant['name'])
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700119 # Get the token of the current client
Andrea Frittoli8bbdb162014-01-06 11:06:13 +0000120 token = self.client.auth_provider.get_token()
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700121 # Delete the token from database
122 self.client.delete_token(token)
123 # Re-auth
Yaroslav Lobankov95aa3f72016-01-28 15:39:49 -0600124 self.token_client.auth(self.data.user['name'],
125 self.data.user_password,
126 self.data.tenant['name'])
Andrea Frittoli8bbdb162014-01-06 11:06:13 +0000127 self.client.auth_provider.clear_auth()
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700128
Chris Hoge7579c1a2015-02-26 14:12:15 -0800129 @test.idempotent_id('a149c02e-e5e0-4b89-809e-7e8faf33ccda')
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700130 def test_get_users(self):
Sean Dague46c4a2b2013-01-03 17:54:17 -0500131 # Get a list of users and find the test user
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700132 self.data.setup_test_user()
Daniel Mellado82c83a52015-12-09 15:16:49 +0000133 users = self.users_client.list_users()['users']
ivan-zhu1feeb382013-01-24 10:14:39 +0800134 self.assertThat([u['name'] for u in users],
Yaroslav Lobankov95aa3f72016-01-28 15:39:49 -0600135 matchers.Contains(self.data.user['name']),
136 "Could not find %s" % self.data.user['name'])
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700137
Chris Hoge7579c1a2015-02-26 14:12:15 -0800138 @test.idempotent_id('6e317209-383a-4bed-9f10-075b7c82c79a')
rajalakshmi-ganesanefc8bd72012-05-30 17:52:11 +0530139 def test_list_users_for_tenant(self):
Sean Dague46c4a2b2013-01-03 17:54:17 -0500140 # Return a list of all users for a tenant
rajalakshmi-ganesanefc8bd72012-05-30 17:52:11 +0530141 self.data.setup_test_tenant()
142 user_ids = list()
143 fetched_user_ids = list()
Zack Feldsteind8c5f7a2015-12-14 10:44:07 -0600144 password1 = data_utils.rand_password()
Ken'ichi Ohmichi96508472015-03-23 01:43:42 +0000145 alt_tenant_user1 = data_utils.rand_name('tenant_user1')
ghanshyame1c6c1c2016-06-15 14:50:41 +0900146 user1 = self.users_client.create_user(name=alt_tenant_user1,
147 password=password1,
148 tenantId=self.data.tenant['id'],
149 email='user1@123')['user']
rajalakshmi-ganesanefc8bd72012-05-30 17:52:11 +0530150 user_ids.append(user1['id'])
151 self.data.users.append(user1)
Zack Feldsteind8c5f7a2015-12-14 10:44:07 -0600152 password2 = data_utils.rand_password()
Ken'ichi Ohmichi96508472015-03-23 01:43:42 +0000153 alt_tenant_user2 = data_utils.rand_name('tenant_user2')
ghanshyame1c6c1c2016-06-15 14:50:41 +0900154 user2 = self.users_client.create_user(name=alt_tenant_user2,
155 password=password2,
156 tenantId=self.data.tenant['id'],
157 email='user2@123')['user']
rajalakshmi-ganesanefc8bd72012-05-30 17:52:11 +0530158 user_ids.append(user2['id'])
159 self.data.users.append(user2)
Attila Fazekasf7f34f92013-08-01 17:01:44 +0200160 # List of users for the respective tenant ID
Daniel Melladob04da902015-11-20 17:43:12 +0100161 body = (self.tenants_client.list_tenant_users(self.data.tenant['id'])
Anusha Ramineni0cfb4612015-08-24 08:49:10 +0530162 ['users'])
rajalakshmi-ganesanefc8bd72012-05-30 17:52:11 +0530163 for i in body:
164 fetched_user_ids.append(i['id'])
Attila Fazekasf7f34f92013-08-01 17:01:44 +0200165 # verifying the user Id in the list
rajalakshmi-ganesanefc8bd72012-05-30 17:52:11 +0530166 missing_users =\
167 [user for user in user_ids if user not in fetched_user_ids]
168 self.assertEqual(0, len(missing_users),
Zhongyue Luoe0884a32012-09-25 17:24:17 +0800169 "Failed to find user %s in fetched list" %
170 ', '.join(m_user for m_user in missing_users))
rajalakshmi-ganesanefc8bd72012-05-30 17:52:11 +0530171
Chris Hoge7579c1a2015-02-26 14:12:15 -0800172 @test.idempotent_id('a8b54974-40e1-41c0-b812-50fc90827971')
rajalakshmi-ganesanefc8bd72012-05-30 17:52:11 +0530173 def test_list_users_with_roles_for_tenant(self):
Sean Dague46c4a2b2013-01-03 17:54:17 -0500174 # Return list of users on tenant when roles are assigned to users
rajalakshmi-ganesanefc8bd72012-05-30 17:52:11 +0530175 self.data.setup_test_user()
176 self.data.setup_test_role()
Yaroslav Lobankov95aa3f72016-01-28 15:39:49 -0600177 user = self.get_user_by_name(self.data.user['name'])
178 tenant = self.get_tenant_by_name(self.data.tenant['name'])
179 role = self.get_role_by_name(self.data.role['name'])
Attila Fazekasf7f34f92013-08-01 17:01:44 +0200180 # Assigning roles to two users
rajalakshmi-ganesanefc8bd72012-05-30 17:52:11 +0530181 user_ids = list()
182 fetched_user_ids = list()
183 user_ids.append(user['id'])
Daniel Mellado6b16b922015-12-07 12:43:08 +0000184 role = self.roles_client.assign_user_role(tenant['id'], user['id'],
185 role['id'])['role']
Adalberto Medeiros081464b2013-08-16 09:41:29 -0400186
Ken'ichi Ohmichi96508472015-03-23 01:43:42 +0000187 alt_user2 = data_utils.rand_name('second_user')
Zack Feldsteind8c5f7a2015-12-14 10:44:07 -0600188 alt_password2 = data_utils.rand_password()
ghanshyame1c6c1c2016-06-15 14:50:41 +0900189 second_user = self.users_client.create_user(
190 name=alt_user2,
191 password=alt_password2,
192 tenantId=self.data.tenant['id'],
193 email='user2@123')['user']
rajalakshmi-ganesanefc8bd72012-05-30 17:52:11 +0530194 user_ids.append(second_user['id'])
195 self.data.users.append(second_user)
Daniel Mellado6b16b922015-12-07 12:43:08 +0000196 role = self.roles_client.assign_user_role(tenant['id'],
197 second_user['id'],
198 role['id'])['role']
Attila Fazekasf7f34f92013-08-01 17:01:44 +0200199 # List of users with roles for the respective tenant ID
Daniel Melladob04da902015-11-20 17:43:12 +0100200 body = (self.tenants_client.list_tenant_users(self.data.tenant['id'])
Anusha Ramineni0cfb4612015-08-24 08:49:10 +0530201 ['users'])
rajalakshmi-ganesanefc8bd72012-05-30 17:52:11 +0530202 for i in body:
203 fetched_user_ids.append(i['id'])
Attila Fazekasf7f34f92013-08-01 17:01:44 +0200204 # verifying the user Id in the list
Monty Taylorb2ca5ca2013-04-28 18:00:21 -0700205 missing_users = [missing_user for missing_user in user_ids
206 if missing_user not in fetched_user_ids]
rajalakshmi-ganesanefc8bd72012-05-30 17:52:11 +0530207 self.assertEqual(0, len(missing_users),
Zhongyue Luoe0884a32012-09-25 17:24:17 +0800208 "Failed to find user %s in fetched list" %
209 ', '.join(m_user for m_user in missing_users))
rajalakshmi-ganesanefc8bd72012-05-30 17:52:11 +0530210
Chris Hoge7579c1a2015-02-26 14:12:15 -0800211 @test.idempotent_id('1aeb25ac-6ec5-4d8b-97cb-7ac3567a989f')
Abhijeet.Jainff5c3542014-05-06 16:07:30 +0530212 def test_update_user_password(self):
213 # Test case to check if updating of user password is successful.
214 self.data.setup_test_user()
215 # Updating the user with new password
Zack Feldsteind8c5f7a2015-12-14 10:44:07 -0600216 new_pass = data_utils.rand_password()
Daniel Mellado82c83a52015-12-09 15:16:49 +0000217 update_user = self.users_client.update_user_password(
piyush110786980c7be2015-12-15 14:14:48 +0530218 self.data.user['id'], password=new_pass)['user']
Abhijeet.Jainff5c3542014-05-06 16:07:30 +0530219 self.assertEqual(update_user['id'], self.data.user['id'])
Yaroslav Lobankovcbcb6112016-03-08 12:30:01 -0600220 # NOTE(morganfainberg): Fernet tokens are not subsecond aware and
221 # Keystone should only be precise to the second. Sleep to ensure
222 # we are passing the second boundary.
223 time.sleep(1)
224 # Validate the updated password through getting a token.
Yaroslav Lobankov95aa3f72016-01-28 15:39:49 -0600225 body = self.token_client.auth(self.data.user['name'], new_pass,
226 self.data.tenant['name'])
Abhijeet.Jainff5c3542014-05-06 16:07:30 +0530227 self.assertTrue('id' in body['token'])