blob: d8865244404fc4eda32446d3768e99c4ef224a6b [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
Masayuki Igawaba7bcf62014-02-17 14:56:41 +090016from testtools import matchers
Sean Dague1937d092013-05-17 16:36:38 -040017
18from tempest.api.identity import base
Fei Long Wangd39431f2015-05-14 11:30:48 +120019from tempest.common.utils import data_utils
Masayuki Igawaba7bcf62014-02-17 14:56:41 +090020from tempest import test
Rohit Karajgi6b1e1542012-05-14 05:55:54 -070021
22
Matthew Treinishdb2c5972014-01-31 22:18:59 +000023class UsersTestJSON(base.BaseIdentityV2AdminTest):
Rohit Karajgi6b1e1542012-05-14 05:55:54 -070024
Adalberto Medeiros081464b2013-08-16 09:41:29 -040025 @classmethod
Andrea Frittoli7688e742014-09-15 12:38:22 +010026 def resource_setup(cls):
27 super(UsersTestJSON, cls).resource_setup()
Ken'ichi Ohmichi96508472015-03-23 01:43:42 +000028 cls.alt_user = data_utils.rand_name('test_user')
Zack Feldsteind8c5f7a2015-12-14 10:44:07 -060029 cls.alt_password = data_utils.rand_password()
Adalberto Medeiros081464b2013-08-16 09:41:29 -040030 cls.alt_email = cls.alt_user + '@testmail.tm'
Rohit Karajgi6b1e1542012-05-14 05:55:54 -070031
Masayuki Igawaba7bcf62014-02-17 14:56:41 +090032 @test.attr(type='smoke')
Chris Hoge7579c1a2015-02-26 14:12:15 -080033 @test.idempotent_id('2d55a71e-da1d-4b43-9c03-d269fd93d905')
Rohit Karajgi6b1e1542012-05-14 05:55:54 -070034 def test_create_user(self):
Sean Dague46c4a2b2013-01-03 17:54:17 -050035 # Create a user
Rohit Karajgi6b1e1542012-05-14 05:55:54 -070036 self.data.setup_test_tenant()
David Kranzb7afa922014-12-30 10:56:26 -050037 user = self.client.create_user(self.alt_user, self.alt_password,
38 self.data.tenant['id'],
Anusha Ramineni0cfb4612015-08-24 08:49:10 +053039 self.alt_email)['user']
Rohit Karajgi6b1e1542012-05-14 05:55:54 -070040 self.data.users.append(user)
Rohit Karajgi6b1e1542012-05-14 05:55:54 -070041 self.assertEqual(self.alt_user, user['name'])
42
Chris Hoge7579c1a2015-02-26 14:12:15 -080043 @test.idempotent_id('89d9fdb8-15c2-4304-a429-48715d0af33d')
huangtianhuafc8db4f2013-10-08 12:05:58 +080044 def test_create_user_with_enabled(self):
45 # Create a user with enabled : False
Rohit Karajgi6b1e1542012-05-14 05:55:54 -070046 self.data.setup_test_tenant()
Ken'ichi Ohmichi96508472015-03-23 01:43:42 +000047 name = data_utils.rand_name('test_user')
David Kranzb7afa922014-12-30 10:56:26 -050048 user = self.client.create_user(name, self.alt_password,
49 self.data.tenant['id'],
Anusha Ramineni0cfb4612015-08-24 08:49:10 +053050 self.alt_email, enabled=False)['user']
huangtianhuafc8db4f2013-10-08 12:05:58 +080051 self.data.users.append(user)
huangtianhuafc8db4f2013-10-08 12:05:58 +080052 self.assertEqual(name, user['name'])
Ken'ichi Ohmichi73cb70b2015-04-17 02:31:12 +000053 self.assertEqual(False, user['enabled'])
huangtianhuafc8db4f2013-10-08 12:05:58 +080054 self.assertEqual(self.alt_email, user['email'])
Rohit Karajgi6b1e1542012-05-14 05:55:54 -070055
Chris Hoge7579c1a2015-02-26 14:12:15 -080056 @test.idempotent_id('39d05857-e8a5-4ed4-ba83-0b52d3ab97ee')
Chang Bo Guob36b2f12013-09-13 04:52:00 -070057 def test_update_user(self):
58 # Test case to check if updating of user attributes is successful.
Ken'ichi Ohmichi96508472015-03-23 01:43:42 +000059 test_user = data_utils.rand_name('test_user')
Chang Bo Guob36b2f12013-09-13 04:52:00 -070060 self.data.setup_test_tenant()
David Kranzb7afa922014-12-30 10:56:26 -050061 user = self.client.create_user(test_user, self.alt_password,
62 self.data.tenant['id'],
Anusha Ramineni0cfb4612015-08-24 08:49:10 +053063 self.alt_email)['user']
Chang Bo Guob36b2f12013-09-13 04:52:00 -070064 # Delete the User at the end of this method
65 self.addCleanup(self.client.delete_user, user['id'])
66 # Updating user details with new values
Ken'ichi Ohmichi96508472015-03-23 01:43:42 +000067 u_name2 = data_utils.rand_name('user2')
Chang Bo Guob36b2f12013-09-13 04:52:00 -070068 u_email2 = u_name2 + '@testmail.tm'
David Kranzb7afa922014-12-30 10:56:26 -050069 update_user = self.client.update_user(user['id'], name=u_name2,
70 email=u_email2,
Anusha Ramineni0cfb4612015-08-24 08:49:10 +053071 enabled=False)['user']
Chang Bo Guob36b2f12013-09-13 04:52:00 -070072 self.assertEqual(u_name2, update_user['name'])
73 self.assertEqual(u_email2, update_user['email'])
Ken'ichi Ohmichi73cb70b2015-04-17 02:31:12 +000074 self.assertEqual(False, update_user['enabled'])
Chang Bo Guob36b2f12013-09-13 04:52:00 -070075 # GET by id after updating
Ken'ichi Ohmichi402b8752015-11-09 10:47:16 +000076 updated_user = self.client.show_user(user['id'])['user']
Chang Bo Guob36b2f12013-09-13 04:52:00 -070077 # Assert response body of GET after updating
78 self.assertEqual(u_name2, updated_user['name'])
79 self.assertEqual(u_email2, updated_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
Chris Hoge7579c1a2015-02-26 14:12:15 -080082 @test.idempotent_id('29ed26f4-a74e-4425-9a85-fdb49fa269d2')
Rohit Karajgi6b1e1542012-05-14 05:55:54 -070083 def test_delete_user(self):
Sean Dague46c4a2b2013-01-03 17:54:17 -050084 # Delete a user
Ken'ichi Ohmichi96508472015-03-23 01:43:42 +000085 test_user = data_utils.rand_name('test_user')
Rohit Karajgi6b1e1542012-05-14 05:55:54 -070086 self.data.setup_test_tenant()
David Kranzb7afa922014-12-30 10:56:26 -050087 user = self.client.create_user(test_user, self.alt_password,
88 self.data.tenant['id'],
Anusha Ramineni0cfb4612015-08-24 08:49:10 +053089 self.alt_email)['user']
David Kranze9d2f422014-07-02 13:57:41 -040090 self.client.delete_user(user['id'])
Rohit Karajgi6b1e1542012-05-14 05:55:54 -070091
Chris Hoge7579c1a2015-02-26 14:12:15 -080092 @test.idempotent_id('aca696c3-d645-4f45-b728-63646045beb1')
Rohit Karajgi6b1e1542012-05-14 05:55:54 -070093 def test_user_authentication(self):
Sean Dague46c4a2b2013-01-03 17:54:17 -050094 # Valid user's token is authenticated
Rohit Karajgi6b1e1542012-05-14 05:55:54 -070095 self.data.setup_test_user()
96 # Get a token
97 self.token_client.auth(self.data.test_user, self.data.test_password,
Zhongyue Luo79d8d362012-09-25 13:49:27 +080098 self.data.test_tenant)
Rohit Karajgi6b1e1542012-05-14 05:55:54 -070099 # Re-auth
David Kranzfb3efa72014-08-28 16:58:25 -0400100 self.token_client.auth(self.data.test_user,
101 self.data.test_password,
102 self.data.test_tenant)
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700103
Chris Hoge7579c1a2015-02-26 14:12:15 -0800104 @test.idempotent_id('5d1fa498-4c2d-4732-a8fe-2b054598cfdd')
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700105 def test_authentication_request_without_token(self):
Sean Dague46c4a2b2013-01-03 17:54:17 -0500106 # Request for token authentication with a valid token in header
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700107 self.data.setup_test_user()
108 self.token_client.auth(self.data.test_user, self.data.test_password,
109 self.data.test_tenant)
110 # Get the token of the current client
Andrea Frittoli8bbdb162014-01-06 11:06:13 +0000111 token = self.client.auth_provider.get_token()
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700112 # Delete the token from database
113 self.client.delete_token(token)
114 # Re-auth
David Kranzfb3efa72014-08-28 16:58:25 -0400115 self.token_client.auth(self.data.test_user,
116 self.data.test_password,
117 self.data.test_tenant)
Andrea Frittoli8bbdb162014-01-06 11:06:13 +0000118 self.client.auth_provider.clear_auth()
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700119
Chris Hoge7579c1a2015-02-26 14:12:15 -0800120 @test.idempotent_id('a149c02e-e5e0-4b89-809e-7e8faf33ccda')
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700121 def test_get_users(self):
Sean Dague46c4a2b2013-01-03 17:54:17 -0500122 # Get a list of users and find the test user
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700123 self.data.setup_test_user()
Ken'ichi Ohmichi402b8752015-11-09 10:47:16 +0000124 users = self.client.list_users()['users']
ivan-zhu1feeb382013-01-24 10:14:39 +0800125 self.assertThat([u['name'] for u in users],
Masayuki Igawaba7bcf62014-02-17 14:56:41 +0900126 matchers.Contains(self.data.test_user),
ivan-zhu1feeb382013-01-24 10:14:39 +0800127 "Could not find %s" % self.data.test_user)
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700128
Chris Hoge7579c1a2015-02-26 14:12:15 -0800129 @test.idempotent_id('6e317209-383a-4bed-9f10-075b7c82c79a')
rajalakshmi-ganesanefc8bd72012-05-30 17:52:11 +0530130 def test_list_users_for_tenant(self):
Sean Dague46c4a2b2013-01-03 17:54:17 -0500131 # Return a list of all users for a tenant
rajalakshmi-ganesanefc8bd72012-05-30 17:52:11 +0530132 self.data.setup_test_tenant()
133 user_ids = list()
134 fetched_user_ids = list()
Zack Feldsteind8c5f7a2015-12-14 10:44:07 -0600135 password1 = data_utils.rand_password()
Ken'ichi Ohmichi96508472015-03-23 01:43:42 +0000136 alt_tenant_user1 = data_utils.rand_name('tenant_user1')
Zack Feldsteind8c5f7a2015-12-14 10:44:07 -0600137 user1 = self.client.create_user(alt_tenant_user1, password1,
David Kranzb7afa922014-12-30 10:56:26 -0500138 self.data.tenant['id'],
Anusha Ramineni0cfb4612015-08-24 08:49:10 +0530139 'user1@123')['user']
rajalakshmi-ganesanefc8bd72012-05-30 17:52:11 +0530140 user_ids.append(user1['id'])
141 self.data.users.append(user1)
Zack Feldsteind8c5f7a2015-12-14 10:44:07 -0600142 password2 = data_utils.rand_password()
Ken'ichi Ohmichi96508472015-03-23 01:43:42 +0000143 alt_tenant_user2 = data_utils.rand_name('tenant_user2')
Zack Feldsteind8c5f7a2015-12-14 10:44:07 -0600144 user2 = self.client.create_user(alt_tenant_user2, password2,
David Kranzb7afa922014-12-30 10:56:26 -0500145 self.data.tenant['id'],
Anusha Ramineni0cfb4612015-08-24 08:49:10 +0530146 'user2@123')['user']
rajalakshmi-ganesanefc8bd72012-05-30 17:52:11 +0530147 user_ids.append(user2['id'])
148 self.data.users.append(user2)
Attila Fazekasf7f34f92013-08-01 17:01:44 +0200149 # List of users for the respective tenant ID
Daniel Melladob04da902015-11-20 17:43:12 +0100150 body = (self.tenants_client.list_tenant_users(self.data.tenant['id'])
Anusha Ramineni0cfb4612015-08-24 08:49:10 +0530151 ['users'])
rajalakshmi-ganesanefc8bd72012-05-30 17:52:11 +0530152 for i in body:
153 fetched_user_ids.append(i['id'])
Attila Fazekasf7f34f92013-08-01 17:01:44 +0200154 # verifying the user Id in the list
rajalakshmi-ganesanefc8bd72012-05-30 17:52:11 +0530155 missing_users =\
156 [user for user in user_ids if user not in fetched_user_ids]
157 self.assertEqual(0, len(missing_users),
Zhongyue Luoe0884a32012-09-25 17:24:17 +0800158 "Failed to find user %s in fetched list" %
159 ', '.join(m_user for m_user in missing_users))
rajalakshmi-ganesanefc8bd72012-05-30 17:52:11 +0530160
Chris Hoge7579c1a2015-02-26 14:12:15 -0800161 @test.idempotent_id('a8b54974-40e1-41c0-b812-50fc90827971')
rajalakshmi-ganesanefc8bd72012-05-30 17:52:11 +0530162 def test_list_users_with_roles_for_tenant(self):
Sean Dague46c4a2b2013-01-03 17:54:17 -0500163 # Return list of users on tenant when roles are assigned to users
rajalakshmi-ganesanefc8bd72012-05-30 17:52:11 +0530164 self.data.setup_test_user()
165 self.data.setup_test_role()
166 user = self.get_user_by_name(self.data.test_user)
167 tenant = self.get_tenant_by_name(self.data.test_tenant)
168 role = self.get_role_by_name(self.data.test_role)
Attila Fazekasf7f34f92013-08-01 17:01:44 +0200169 # Assigning roles to two users
rajalakshmi-ganesanefc8bd72012-05-30 17:52:11 +0530170 user_ids = list()
171 fetched_user_ids = list()
172 user_ids.append(user['id'])
Daniel Mellado6b16b922015-12-07 12:43:08 +0000173 role = self.roles_client.assign_user_role(tenant['id'], user['id'],
174 role['id'])['role']
Adalberto Medeiros081464b2013-08-16 09:41:29 -0400175
Ken'ichi Ohmichi96508472015-03-23 01:43:42 +0000176 alt_user2 = data_utils.rand_name('second_user')
Zack Feldsteind8c5f7a2015-12-14 10:44:07 -0600177 alt_password2 = data_utils.rand_password()
178 second_user = self.client.create_user(alt_user2, alt_password2,
David Kranzb7afa922014-12-30 10:56:26 -0500179 self.data.tenant['id'],
Anusha Ramineni0cfb4612015-08-24 08:49:10 +0530180 'user2@123')['user']
rajalakshmi-ganesanefc8bd72012-05-30 17:52:11 +0530181 user_ids.append(second_user['id'])
182 self.data.users.append(second_user)
Daniel Mellado6b16b922015-12-07 12:43:08 +0000183 role = self.roles_client.assign_user_role(tenant['id'],
184 second_user['id'],
185 role['id'])['role']
Attila Fazekasf7f34f92013-08-01 17:01:44 +0200186 # List of users with roles for the respective tenant ID
Daniel Melladob04da902015-11-20 17:43:12 +0100187 body = (self.tenants_client.list_tenant_users(self.data.tenant['id'])
Anusha Ramineni0cfb4612015-08-24 08:49:10 +0530188 ['users'])
rajalakshmi-ganesanefc8bd72012-05-30 17:52:11 +0530189 for i in body:
190 fetched_user_ids.append(i['id'])
Attila Fazekasf7f34f92013-08-01 17:01:44 +0200191 # verifying the user Id in the list
Monty Taylorb2ca5ca2013-04-28 18:00:21 -0700192 missing_users = [missing_user for missing_user in user_ids
193 if missing_user not in fetched_user_ids]
rajalakshmi-ganesanefc8bd72012-05-30 17:52:11 +0530194 self.assertEqual(0, len(missing_users),
Zhongyue Luoe0884a32012-09-25 17:24:17 +0800195 "Failed to find user %s in fetched list" %
196 ', '.join(m_user for m_user in missing_users))
rajalakshmi-ganesanefc8bd72012-05-30 17:52:11 +0530197
Chris Hoge7579c1a2015-02-26 14:12:15 -0800198 @test.idempotent_id('1aeb25ac-6ec5-4d8b-97cb-7ac3567a989f')
Abhijeet.Jainff5c3542014-05-06 16:07:30 +0530199 def test_update_user_password(self):
200 # Test case to check if updating of user password is successful.
201 self.data.setup_test_user()
202 # Updating the user with new password
Zack Feldsteind8c5f7a2015-12-14 10:44:07 -0600203 new_pass = data_utils.rand_password()
David Kranzb7afa922014-12-30 10:56:26 -0500204 update_user = self.client.update_user_password(
piyush110786980c7be2015-12-15 14:14:48 +0530205 self.data.user['id'], password=new_pass)['user']
Abhijeet.Jainff5c3542014-05-06 16:07:30 +0530206 self.assertEqual(update_user['id'], self.data.user['id'])
207
208 # Validate the updated password
209 # Get a token
David Kranzb7afa922014-12-30 10:56:26 -0500210 body = self.token_client.auth(self.data.test_user, new_pass,
211 self.data.test_tenant)
Abhijeet.Jainff5c3542014-05-06 16:07:30 +0530212 self.assertTrue('id' in body['token'])