blob: 6f90b0442d1539bc6ccd5ac95ca8ae1ac16ca7a7 [file] [log] [blame]
Jay Pipesf38eaac2012-06-21 13:37:35 -04001# vim: tabstop=4 shiftwidth=4 softtabstop=4
2
3# Copyright 2012 OpenStack, LLC
4# All Rights Reserved.
5#
6# Licensed under the Apache License, Version 2.0 (the "License"); you may
7# not use this file except in compliance with the License. You may obtain
8# a copy of the License at
9#
10# http://www.apache.org/licenses/LICENSE-2.0
11#
12# Unless required by applicable law or agreed to in writing, software
13# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15# License for the specific language governing permissions and limitations
16# under the License.
17
Sean Dague1937d092013-05-17 16:36:38 -040018from testtools.matchers._basic import Contains
19
20from tempest.api.identity import base
Rohit Karajgi6b1e1542012-05-14 05:55:54 -070021from tempest.common.utils.data_utils import rand_name
Matthew Treinisha83a16e2012-12-07 13:44:02 -050022from tempest import exceptions
Chris Yeoh01cb2792013-02-09 22:25:37 +103023from tempest.test import attr
Rohit Karajgi6b1e1542012-05-14 05:55:54 -070024
25
Attila Fazekas0d0c6162013-02-24 09:14:23 +010026class UsersTestJSON(base.BaseIdentityAdminTest):
27 _interface = 'json'
Rohit Karajgi6b1e1542012-05-14 05:55:54 -070028
29 alt_user = rand_name('test_user_')
30 alt_password = rand_name('pass_')
31 alt_email = alt_user + '@testmail.tm'
32 alt_tenant = rand_name('test_tenant_')
33 alt_description = rand_name('desc_')
34
Giulio Fidenteba3985a2013-05-29 01:46:36 +020035 @attr(type='smoke')
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()
39 resp, user = self.client.create_user(self.alt_user, self.alt_password,
Zhongyue Luo79d8d362012-09-25 13:49:27 +080040 self.data.tenant['id'],
41 self.alt_email)
Rohit Karajgi6b1e1542012-05-14 05:55:54 -070042 self.data.users.append(user)
43 self.assertEqual('200', resp['status'])
44 self.assertEqual(self.alt_user, user['name'])
45
Alvaro Lopez Garcia3c3a4b82013-06-12 16:04:51 +020046 @attr(type=['negative', 'gate'])
Rohit Karajgi6b1e1542012-05-14 05:55:54 -070047 def test_create_user_by_unauthorized_user(self):
Sean Dague46c4a2b2013-01-03 17:54:17 -050048 # Non-admin should not be authorized to create a user
Rohit Karajgi6b1e1542012-05-14 05:55:54 -070049 self.data.setup_test_tenant()
50 self.assertRaises(exceptions.Unauthorized,
51 self.non_admin_client.create_user, self.alt_user,
52 self.alt_password, self.data.tenant['id'],
53 self.alt_email)
54
Alvaro Lopez Garcia3c3a4b82013-06-12 16:04:51 +020055 @attr(type=['negative', 'gate'])
Rohit Karajgi6b1e1542012-05-14 05:55:54 -070056 def test_create_user_with_empty_name(self):
Sean Dague46c4a2b2013-01-03 17:54:17 -050057 # User with an empty name should not be created
Rohit Karajgi6b1e1542012-05-14 05:55:54 -070058 self.data.setup_test_tenant()
59 self.assertRaises(exceptions.BadRequest, self.client.create_user, '',
60 self.alt_password, self.data.tenant['id'],
61 self.alt_email)
62
Alvaro Lopez Garcia250ba602013-06-12 14:31:57 +020063 @attr(type=['negative', 'gate'])
64 def test_create_user_with_name_length_over_255(self):
65 # Length of user name filed should be restricted to 255 characters
Rohit Karajgi6b1e1542012-05-14 05:55:54 -070066 self.data.setup_test_tenant()
67 self.assertRaises(exceptions.BadRequest, self.client.create_user,
Alvaro Lopez Garcia250ba602013-06-12 14:31:57 +020068 'a' * 256, self.alt_password,
Zhongyue Luo79d8d362012-09-25 13:49:27 +080069 self.data.tenant['id'], self.alt_email)
Rohit Karajgi6b1e1542012-05-14 05:55:54 -070070
Alvaro Lopez Garcia3c3a4b82013-06-12 16:04:51 +020071 @attr(type=['negative', 'gate'])
Rohit Karajgi6b1e1542012-05-14 05:55:54 -070072 def test_create_user_with_duplicate_name(self):
Sean Dague46c4a2b2013-01-03 17:54:17 -050073 # Duplicate user should not be created
Rohit Karajgi6b1e1542012-05-14 05:55:54 -070074 self.data.setup_test_user()
75 self.assertRaises(exceptions.Duplicate, self.client.create_user,
76 self.data.test_user, self.data.test_password,
77 self.data.tenant['id'], self.data.test_email)
78
Alvaro Lopez Garcia3c3a4b82013-06-12 16:04:51 +020079 @attr(type=['negative', 'gate'])
Rohit Karajgi6b1e1542012-05-14 05:55:54 -070080 def test_create_user_for_non_existant_tenant(self):
Sean Dague46c4a2b2013-01-03 17:54:17 -050081 # Attempt to create a user in a non-existent tenant should fail
Rohit Karajgi6b1e1542012-05-14 05:55:54 -070082 self.assertRaises(exceptions.NotFound, self.client.create_user,
Zhongyue Luo79d8d362012-09-25 13:49:27 +080083 self.alt_user, self.alt_password, '49ffgg99999',
84 self.alt_email)
Rohit Karajgi6b1e1542012-05-14 05:55:54 -070085
Alvaro Lopez Garcia3c3a4b82013-06-12 16:04:51 +020086 @attr(type=['negative', 'gate'])
Rohit Karajgi6b1e1542012-05-14 05:55:54 -070087 def test_create_user_request_without_a_token(self):
Sean Dague46c4a2b2013-01-03 17:54:17 -050088 # Request to create a user without a valid token should fail
Rohit Karajgi6b1e1542012-05-14 05:55:54 -070089 self.data.setup_test_tenant()
90 # Get the token of the current client
91 token = self.client.get_auth()
92 # Delete the token from database
93 self.client.delete_token(token)
94 self.assertRaises(exceptions.Unauthorized, self.client.create_user,
Zhongyue Luoe0884a32012-09-25 17:24:17 +080095 self.alt_user, self.alt_password,
96 self.data.tenant['id'], self.alt_email)
Rohit Karajgi6b1e1542012-05-14 05:55:54 -070097
98 # Unset the token to allow further tests to generate a new token
99 self.client.clear_auth()
100
Giulio Fidenteba3985a2013-05-29 01:46:36 +0200101 @attr(type='smoke')
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700102 def test_delete_user(self):
Sean Dague46c4a2b2013-01-03 17:54:17 -0500103 # Delete a user
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700104 self.data.setup_test_tenant()
105 resp, user = self.client.create_user('user_1234', self.alt_password,
Zhongyue Luo79d8d362012-09-25 13:49:27 +0800106 self.data.tenant['id'],
107 self.alt_email)
Chris Yeoh7ed62072013-02-22 11:08:14 +1030108 self.assertEquals('200', resp['status'])
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700109 resp, body = self.client.delete_user(user['id'])
110 self.assertEquals('204', resp['status'])
111
Alvaro Lopez Garcia3c3a4b82013-06-12 16:04:51 +0200112 @attr(type=['negative', 'gate'])
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700113 def test_delete_users_by_unauthorized_user(self):
Sean Dague46c4a2b2013-01-03 17:54:17 -0500114 # Non admin user should not be authorized to delete a user
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700115 self.data.setup_test_user()
116 self.assertRaises(exceptions.Unauthorized,
Zhongyue Luo79d8d362012-09-25 13:49:27 +0800117 self.non_admin_client.delete_user,
118 self.data.user['id'])
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700119
Alvaro Lopez Garcia3c3a4b82013-06-12 16:04:51 +0200120 @attr(type=['negative', 'gate'])
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700121 def test_delete_non_existant_user(self):
Sean Dague46c4a2b2013-01-03 17:54:17 -0500122 # Attempt to delete a non-existent user should fail
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700123 self.assertRaises(exceptions.NotFound, self.client.delete_user,
124 'junk12345123')
125
Giulio Fidenteba3985a2013-05-29 01:46:36 +0200126 @attr(type='smoke')
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700127 def test_user_authentication(self):
Sean Dague46c4a2b2013-01-03 17:54:17 -0500128 # Valid user's token is authenticated
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700129 self.data.setup_test_user()
130 # Get a token
131 self.token_client.auth(self.data.test_user, self.data.test_password,
Zhongyue Luo79d8d362012-09-25 13:49:27 +0800132 self.data.test_tenant)
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700133 # Re-auth
134 resp, body = self.token_client.auth(self.data.test_user,
135 self.data.test_password,
136 self.data.test_tenant)
137 self.assertEqual('200', resp['status'])
138
Alvaro Lopez Garcia3c3a4b82013-06-12 16:04:51 +0200139 @attr(type=['negative', 'gate'])
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700140 def test_authentication_for_disabled_user(self):
Sean Dague46c4a2b2013-01-03 17:54:17 -0500141 # Disabled user's token should not get authenticated
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700142 self.data.setup_test_user()
143 self.disable_user(self.data.test_user)
144 self.assertRaises(exceptions.Unauthorized, self.token_client.auth,
145 self.data.test_user,
146 self.data.test_password,
147 self.data.test_tenant)
148
Alvaro Lopez Garcia3c3a4b82013-06-12 16:04:51 +0200149 @attr(type=['negative', 'gate'])
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700150 def test_authentication_when_tenant_is_disabled(self):
Sean Dague46c4a2b2013-01-03 17:54:17 -0500151 # User's token for a disabled tenant should not be authenticated
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700152 self.data.setup_test_user()
153 self.disable_tenant(self.data.test_tenant)
154 self.assertRaises(exceptions.Unauthorized, self.token_client.auth,
Zhongyue Luo79d8d362012-09-25 13:49:27 +0800155 self.data.test_user,
156 self.data.test_password,
157 self.data.test_tenant)
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700158
Alvaro Lopez Garcia3c3a4b82013-06-12 16:04:51 +0200159 @attr(type=['negative', 'gate'])
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700160 def test_authentication_with_invalid_tenant(self):
Sean Dague46c4a2b2013-01-03 17:54:17 -0500161 # User's token for an invalid tenant should not be authenticated
Giampaolo Lauria2a9653e2013-01-15 14:11:45 -0500162 self.data.setup_test_user()
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700163 self.assertRaises(exceptions.Unauthorized, self.token_client.auth,
Zhongyue Luo79d8d362012-09-25 13:49:27 +0800164 self.data.test_user,
165 self.data.test_password,
166 'junktenant1234')
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700167
Alvaro Lopez Garcia3c3a4b82013-06-12 16:04:51 +0200168 @attr(type=['negative', 'gate'])
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700169 def test_authentication_with_invalid_username(self):
Sean Dague46c4a2b2013-01-03 17:54:17 -0500170 # Non-existent user's token should not get authenticated
ivan-zhufa2adf92013-01-13 00:18:25 +0800171 self.data.setup_test_user()
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700172 self.assertRaises(exceptions.Unauthorized, self.token_client.auth,
Zhongyue Luo79d8d362012-09-25 13:49:27 +0800173 'junkuser123', self.data.test_password,
174 self.data.test_tenant)
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700175
Alvaro Lopez Garcia3c3a4b82013-06-12 16:04:51 +0200176 @attr(type=['negative', 'gate'])
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700177 def test_authentication_with_invalid_password(self):
Sean Dague46c4a2b2013-01-03 17:54:17 -0500178 # User's token with invalid password should not be authenticated
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700179 self.data.setup_test_user()
180 self.assertRaises(exceptions.Unauthorized, self.token_client.auth,
181 self.data.test_user, 'junkpass1234',
182 self.data.test_tenant)
183
Giampaolo Lauriaea294952013-05-15 08:52:04 -0400184 @attr(type='gate')
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700185 def test_authentication_request_without_token(self):
Sean Dague46c4a2b2013-01-03 17:54:17 -0500186 # Request for token authentication with a valid token in header
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700187 self.data.setup_test_user()
188 self.token_client.auth(self.data.test_user, self.data.test_password,
189 self.data.test_tenant)
190 # Get the token of the current client
191 token = self.client.get_auth()
192 # Delete the token from database
193 self.client.delete_token(token)
194 # Re-auth
195 resp, body = self.token_client.auth(self.data.test_user,
196 self.data.test_password,
197 self.data.test_tenant)
198 self.assertEqual('200', resp['status'])
199 self.client.clear_auth()
200
Giulio Fidenteba3985a2013-05-29 01:46:36 +0200201 @attr(type='smoke')
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700202 def test_get_users(self):
Sean Dague46c4a2b2013-01-03 17:54:17 -0500203 # Get a list of users and find the test user
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700204 self.data.setup_test_user()
205 resp, users = self.client.get_users()
ivan-zhu1feeb382013-01-24 10:14:39 +0800206 self.assertThat([u['name'] for u in users],
207 Contains(self.data.test_user),
208 "Could not find %s" % self.data.test_user)
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700209
Alvaro Lopez Garcia3c3a4b82013-06-12 16:04:51 +0200210 @attr(type=['negative', 'gate'])
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700211 def test_get_users_by_unauthorized_user(self):
Sean Dague46c4a2b2013-01-03 17:54:17 -0500212 # Non admin user should not be authorized to get user list
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700213 self.data.setup_test_user()
214 self.assertRaises(exceptions.Unauthorized,
Zhongyue Luo79d8d362012-09-25 13:49:27 +0800215 self.non_admin_client.get_users)
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700216
Alvaro Lopez Garcia3c3a4b82013-06-12 16:04:51 +0200217 @attr(type=['negative', 'gate'])
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700218 def test_get_users_request_without_token(self):
Sean Dague46c4a2b2013-01-03 17:54:17 -0500219 # Request to get list of users without a valid token should fail
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700220 token = self.client.get_auth()
221 self.client.delete_token(token)
222 self.assertRaises(exceptions.Unauthorized, self.client.get_users)
223 self.client.clear_auth()
rajalakshmi-ganesanefc8bd72012-05-30 17:52:11 +0530224
Giampaolo Lauriaea294952013-05-15 08:52:04 -0400225 @attr(type='gate')
rajalakshmi-ganesanefc8bd72012-05-30 17:52:11 +0530226 def test_list_users_for_tenant(self):
Sean Dague46c4a2b2013-01-03 17:54:17 -0500227 # Return a list of all users for a tenant
rajalakshmi-ganesanefc8bd72012-05-30 17:52:11 +0530228 self.data.setup_test_tenant()
229 user_ids = list()
230 fetched_user_ids = list()
231 resp, user1 = self.client.create_user('tenant_user1', 'password1',
Zhongyue Luo79d8d362012-09-25 13:49:27 +0800232 self.data.tenant['id'],
233 'user1@123')
Chris Yeoh7ed62072013-02-22 11:08:14 +1030234 self.assertEquals('200', resp['status'])
rajalakshmi-ganesanefc8bd72012-05-30 17:52:11 +0530235 user_ids.append(user1['id'])
236 self.data.users.append(user1)
237 resp, user2 = self.client.create_user('tenant_user2', 'password2',
Zhongyue Luo79d8d362012-09-25 13:49:27 +0800238 self.data.tenant['id'],
239 'user2@123')
Chris Yeoh7ed62072013-02-22 11:08:14 +1030240 self.assertEquals('200', resp['status'])
rajalakshmi-ganesanefc8bd72012-05-30 17:52:11 +0530241 user_ids.append(user2['id'])
242 self.data.users.append(user2)
Attila Fazekasf7f34f92013-08-01 17:01:44 +0200243 # List of users for the respective tenant ID
rajalakshmi-ganesanefc8bd72012-05-30 17:52:11 +0530244 resp, body = self.client.list_users_for_tenant(self.data.tenant['id'])
Attila Fazekase191cb12013-07-29 06:41:52 +0200245 self.assertIn(resp['status'], ('200', '203'))
rajalakshmi-ganesanefc8bd72012-05-30 17:52:11 +0530246 for i in body:
247 fetched_user_ids.append(i['id'])
Attila Fazekasf7f34f92013-08-01 17:01:44 +0200248 # verifying the user Id in the list
rajalakshmi-ganesanefc8bd72012-05-30 17:52:11 +0530249 missing_users =\
250 [user for user in user_ids if user not in fetched_user_ids]
251 self.assertEqual(0, len(missing_users),
Zhongyue Luoe0884a32012-09-25 17:24:17 +0800252 "Failed to find user %s in fetched list" %
253 ', '.join(m_user for m_user in missing_users))
rajalakshmi-ganesanefc8bd72012-05-30 17:52:11 +0530254
Giampaolo Lauriaea294952013-05-15 08:52:04 -0400255 @attr(type='gate')
rajalakshmi-ganesanefc8bd72012-05-30 17:52:11 +0530256 def test_list_users_with_roles_for_tenant(self):
Sean Dague46c4a2b2013-01-03 17:54:17 -0500257 # Return list of users on tenant when roles are assigned to users
rajalakshmi-ganesanefc8bd72012-05-30 17:52:11 +0530258 self.data.setup_test_user()
259 self.data.setup_test_role()
260 user = self.get_user_by_name(self.data.test_user)
261 tenant = self.get_tenant_by_name(self.data.test_tenant)
262 role = self.get_role_by_name(self.data.test_role)
Attila Fazekasf7f34f92013-08-01 17:01:44 +0200263 # Assigning roles to two users
rajalakshmi-ganesanefc8bd72012-05-30 17:52:11 +0530264 user_ids = list()
265 fetched_user_ids = list()
266 user_ids.append(user['id'])
Chris Yeoh7ed62072013-02-22 11:08:14 +1030267 resp, role = self.client.assign_user_role(tenant['id'], user['id'],
268 role['id'])
269 self.assertEquals('200', resp['status'])
rajalakshmi-ganesanefc8bd72012-05-30 17:52:11 +0530270 resp, second_user = self.client.create_user('second_user', 'password1',
Zhongyue Luo79d8d362012-09-25 13:49:27 +0800271 self.data.tenant['id'],
272 'user1@123')
Chris Yeoh7ed62072013-02-22 11:08:14 +1030273 self.assertEquals('200', resp['status'])
rajalakshmi-ganesanefc8bd72012-05-30 17:52:11 +0530274 user_ids.append(second_user['id'])
275 self.data.users.append(second_user)
Chris Yeoh7ed62072013-02-22 11:08:14 +1030276 resp, role = self.client.assign_user_role(tenant['id'],
277 second_user['id'],
278 role['id'])
279 self.assertEquals('200', resp['status'])
Attila Fazekasf7f34f92013-08-01 17:01:44 +0200280 # List of users with roles for the respective tenant ID
rajalakshmi-ganesanefc8bd72012-05-30 17:52:11 +0530281 resp, body = self.client.list_users_for_tenant(self.data.tenant['id'])
Chris Yeoh7ed62072013-02-22 11:08:14 +1030282 self.assertEquals('200', resp['status'])
rajalakshmi-ganesanefc8bd72012-05-30 17:52:11 +0530283 for i in body:
284 fetched_user_ids.append(i['id'])
Attila Fazekasf7f34f92013-08-01 17:01:44 +0200285 # verifying the user Id in the list
Monty Taylorb2ca5ca2013-04-28 18:00:21 -0700286 missing_users = [missing_user for missing_user in user_ids
287 if missing_user not in fetched_user_ids]
rajalakshmi-ganesanefc8bd72012-05-30 17:52:11 +0530288 self.assertEqual(0, len(missing_users),
Zhongyue Luoe0884a32012-09-25 17:24:17 +0800289 "Failed to find user %s in fetched list" %
290 ', '.join(m_user for m_user in missing_users))
rajalakshmi-ganesanefc8bd72012-05-30 17:52:11 +0530291
Alvaro Lopez Garcia3c3a4b82013-06-12 16:04:51 +0200292 @attr(type=['negative', 'gate'])
rajalakshmi-ganesanefc8bd72012-05-30 17:52:11 +0530293 def test_list_users_with_invalid_tenant(self):
Sean Dague46c4a2b2013-01-03 17:54:17 -0500294 # Should not be able to return a list of all
295 # users for a nonexistant tenant
Attila Fazekasf7f34f92013-08-01 17:01:44 +0200296 # Assign invalid tenant ids
rajalakshmi-ganesanefc8bd72012-05-30 17:52:11 +0530297 invalid_id = list()
298 invalid_id.append(rand_name('999'))
299 invalid_id.append('alpha')
300 invalid_id.append(rand_name("dddd@#%%^$"))
301 invalid_id.append('!@#()$%^&*?<>{}[]')
Attila Fazekasf7f34f92013-08-01 17:01:44 +0200302 # List the users with invalid tenant id
rajalakshmi-ganesanefc8bd72012-05-30 17:52:11 +0530303 for invalid in invalid_id:
donald-ngoc17be892013-03-28 15:48:37 -0700304 self.assertRaises(exceptions.NotFound,
305 self.client.list_users_for_tenant, invalid)
Vincent Hou6b8a7b72012-08-25 01:24:33 +0800306
307
Attila Fazekas0d0c6162013-02-24 09:14:23 +0100308class UsersTestXML(UsersTestJSON):
309 _interface = 'xml'