blob: 7ad932b56e8b862e534229eec5dd57adacc63430 [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
Rohit Karajgi6b1e1542012-05-14 05:55:54 -070018from nose.plugins.attrib import attr
Jay Pipesf38eaac2012-06-21 13:37:35 -040019import unittest2 as unittest
20
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
Vincent Hou6b8a7b72012-08-25 01:24:33 +080023from tempest.tests.identity import base
Rohit Karajgi6b1e1542012-05-14 05:55:54 -070024
25
Vincent Hou6b8a7b72012-08-25 01:24:33 +080026class UsersTestBase(object):
Rohit Karajgi6b1e1542012-05-14 05:55:54 -070027
28 alt_user = rand_name('test_user_')
29 alt_password = rand_name('pass_')
30 alt_email = alt_user + '@testmail.tm'
31 alt_tenant = rand_name('test_tenant_')
32 alt_description = rand_name('desc_')
33
34 @attr(type='smoke')
35 def test_create_user(self):
Sean Dague46c4a2b2013-01-03 17:54:17 -050036 # Create a user
Rohit Karajgi6b1e1542012-05-14 05:55:54 -070037 self.data.setup_test_tenant()
38 resp, user = self.client.create_user(self.alt_user, self.alt_password,
Zhongyue Luo79d8d362012-09-25 13:49:27 +080039 self.data.tenant['id'],
40 self.alt_email)
Rohit Karajgi6b1e1542012-05-14 05:55:54 -070041 self.data.users.append(user)
42 self.assertEqual('200', resp['status'])
43 self.assertEqual(self.alt_user, user['name'])
44
45 @attr(type='negative')
46 def test_create_user_by_unauthorized_user(self):
Sean Dague46c4a2b2013-01-03 17:54:17 -050047 # Non-admin should not be authorized to create a user
Rohit Karajgi6b1e1542012-05-14 05:55:54 -070048 self.data.setup_test_tenant()
49 self.assertRaises(exceptions.Unauthorized,
50 self.non_admin_client.create_user, self.alt_user,
51 self.alt_password, self.data.tenant['id'],
52 self.alt_email)
53
54 @attr(type='negative')
Rohit Karajgi6b1e1542012-05-14 05:55:54 -070055 def test_create_user_with_empty_name(self):
Sean Dague46c4a2b2013-01-03 17:54:17 -050056 # User with an empty name should not be created
Rohit Karajgi6b1e1542012-05-14 05:55:54 -070057 self.data.setup_test_tenant()
58 self.assertRaises(exceptions.BadRequest, self.client.create_user, '',
59 self.alt_password, self.data.tenant['id'],
60 self.alt_email)
61
62 @attr(type='negative')
Rohit Karajgi6b1e1542012-05-14 05:55:54 -070063 def test_create_user_with_name_length_over_64(self):
Sean Dague46c4a2b2013-01-03 17:54:17 -050064 # Length of user name filed should be restricted to 64 characters
Rohit Karajgi6b1e1542012-05-14 05:55:54 -070065 self.data.setup_test_tenant()
66 self.assertRaises(exceptions.BadRequest, self.client.create_user,
Zhongyue Luo79d8d362012-09-25 13:49:27 +080067 'a' * 65, self.alt_password,
68 self.data.tenant['id'], self.alt_email)
Rohit Karajgi6b1e1542012-05-14 05:55:54 -070069
70 @attr(type='negative')
71 def test_create_user_with_duplicate_name(self):
Sean Dague46c4a2b2013-01-03 17:54:17 -050072 # Duplicate user should not be created
Rohit Karajgi6b1e1542012-05-14 05:55:54 -070073 self.data.setup_test_user()
74 self.assertRaises(exceptions.Duplicate, self.client.create_user,
75 self.data.test_user, self.data.test_password,
76 self.data.tenant['id'], self.data.test_email)
77
78 @attr(type='negative')
79 @unittest.skip("Until Bug 999084 is fixed")
80 def test_create_user_with_empty_password(self):
Sean Dague46c4a2b2013-01-03 17:54:17 -050081 # User with an empty password should not be created
Rohit Karajgi6b1e1542012-05-14 05:55:54 -070082 self.data.setup_test_tenant()
83 self.assertRaises(exceptions.BadRequest, self.client.create_user,
84 self.alt_user, '', self.data.tenant['id'],
85 self.alt_email)
86
87 @attr(type='nagative')
88 @unittest.skip("Until Bug 999084 is fixed")
89 def test_create_user_with_long_password(self):
Sean Dague46c4a2b2013-01-03 17:54:17 -050090 # User having password exceeding max length should not be created
Rohit Karajgi6b1e1542012-05-14 05:55:54 -070091 self.data.setup_test_tenant()
92 self.assertRaises(exceptions.BadRequest, self.client.create_user,
David Kranz28e35c52012-07-10 10:14:38 -040093 self.alt_user, 'a' * 65, self.data.tenant['id'],
Rohit Karajgi6b1e1542012-05-14 05:55:54 -070094 self.alt_email)
95
96 @attr(type='negative')
97 @unittest.skip("Until Bug 999084 is fixed")
98 def test_create_user_with_invalid_email_format(self):
Sean Dague46c4a2b2013-01-03 17:54:17 -050099 # Email format should be validated while creating a user
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700100 self.data.setup_test_tenant()
101 self.assertRaises(exceptions.BadRequest, self.client.create_user,
Zhongyue Luo79d8d362012-09-25 13:49:27 +0800102 self.alt_user, '', self.data.tenant['id'], '12345')
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700103
104 @attr(type='negative')
105 def test_create_user_for_non_existant_tenant(self):
Sean Dague46c4a2b2013-01-03 17:54:17 -0500106 # Attempt to create a user in a non-existent tenant should fail
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700107 self.assertRaises(exceptions.NotFound, self.client.create_user,
Zhongyue Luo79d8d362012-09-25 13:49:27 +0800108 self.alt_user, self.alt_password, '49ffgg99999',
109 self.alt_email)
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700110
111 @attr(type='negative')
112 def test_create_user_request_without_a_token(self):
Sean Dague46c4a2b2013-01-03 17:54:17 -0500113 # Request to create a user without a valid token should fail
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700114 self.data.setup_test_tenant()
115 # Get the token of the current client
116 token = self.client.get_auth()
117 # Delete the token from database
118 self.client.delete_token(token)
119 self.assertRaises(exceptions.Unauthorized, self.client.create_user,
Zhongyue Luoe0884a32012-09-25 17:24:17 +0800120 self.alt_user, self.alt_password,
121 self.data.tenant['id'], self.alt_email)
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700122
123 # Unset the token to allow further tests to generate a new token
124 self.client.clear_auth()
125
126 @attr(type='smoke')
127 def test_delete_user(self):
Sean Dague46c4a2b2013-01-03 17:54:17 -0500128 # Delete a user
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700129 self.data.setup_test_tenant()
130 resp, user = self.client.create_user('user_1234', self.alt_password,
Zhongyue Luo79d8d362012-09-25 13:49:27 +0800131 self.data.tenant['id'],
132 self.alt_email)
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700133 resp, body = self.client.delete_user(user['id'])
134 self.assertEquals('204', resp['status'])
135
136 @attr(type='negative')
137 def test_delete_users_by_unauthorized_user(self):
Sean Dague46c4a2b2013-01-03 17:54:17 -0500138 # Non admin user should not be authorized to delete a user
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700139 self.data.setup_test_user()
140 self.assertRaises(exceptions.Unauthorized,
Zhongyue Luo79d8d362012-09-25 13:49:27 +0800141 self.non_admin_client.delete_user,
142 self.data.user['id'])
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700143
144 @attr(type='negative')
145 def test_delete_non_existant_user(self):
Sean Dague46c4a2b2013-01-03 17:54:17 -0500146 # Attempt to delete a non-existent user should fail
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700147 self.assertRaises(exceptions.NotFound, self.client.delete_user,
148 'junk12345123')
149
150 @attr(type='smoke')
151 def test_user_authentication(self):
Sean Dague46c4a2b2013-01-03 17:54:17 -0500152 # Valid user's token is authenticated
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700153 self.data.setup_test_user()
154 # Get a token
155 self.token_client.auth(self.data.test_user, self.data.test_password,
Zhongyue Luo79d8d362012-09-25 13:49:27 +0800156 self.data.test_tenant)
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700157 # Re-auth
158 resp, body = self.token_client.auth(self.data.test_user,
159 self.data.test_password,
160 self.data.test_tenant)
161 self.assertEqual('200', resp['status'])
162
163 @attr(type='negative')
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700164 def test_authentication_for_disabled_user(self):
Sean Dague46c4a2b2013-01-03 17:54:17 -0500165 # Disabled user's token should not get authenticated
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700166 self.data.setup_test_user()
167 self.disable_user(self.data.test_user)
168 self.assertRaises(exceptions.Unauthorized, self.token_client.auth,
169 self.data.test_user,
170 self.data.test_password,
171 self.data.test_tenant)
172
173 @attr(type='negative')
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700174 def test_authentication_when_tenant_is_disabled(self):
Sean Dague46c4a2b2013-01-03 17:54:17 -0500175 # User's token for a disabled tenant should not be authenticated
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700176 self.data.setup_test_user()
177 self.disable_tenant(self.data.test_tenant)
178 self.assertRaises(exceptions.Unauthorized, self.token_client.auth,
Zhongyue Luo79d8d362012-09-25 13:49:27 +0800179 self.data.test_user,
180 self.data.test_password,
181 self.data.test_tenant)
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700182
183 @attr(type='negative')
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700184 def test_authentication_with_invalid_tenant(self):
Sean Dague46c4a2b2013-01-03 17:54:17 -0500185 # User's token for an invalid tenant should not be authenticated
Giampaolo Lauria2a9653e2013-01-15 14:11:45 -0500186 self.data.setup_test_user()
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700187 self.assertRaises(exceptions.Unauthorized, self.token_client.auth,
Zhongyue Luo79d8d362012-09-25 13:49:27 +0800188 self.data.test_user,
189 self.data.test_password,
190 'junktenant1234')
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700191
192 @attr(type='negative')
193 def test_authentication_with_invalid_username(self):
Sean Dague46c4a2b2013-01-03 17:54:17 -0500194 # Non-existent user's token should not get authenticated
ivan-zhufa2adf92013-01-13 00:18:25 +0800195 self.data.setup_test_user()
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700196 self.assertRaises(exceptions.Unauthorized, self.token_client.auth,
Zhongyue Luo79d8d362012-09-25 13:49:27 +0800197 'junkuser123', self.data.test_password,
198 self.data.test_tenant)
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700199
200 @attr(type='negative')
201 def test_authentication_with_invalid_password(self):
Sean Dague46c4a2b2013-01-03 17:54:17 -0500202 # User's token with invalid password should not be authenticated
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700203 self.data.setup_test_user()
204 self.assertRaises(exceptions.Unauthorized, self.token_client.auth,
205 self.data.test_user, 'junkpass1234',
206 self.data.test_tenant)
207
208 @attr(type='positive')
209 def test_authentication_request_without_token(self):
Sean Dague46c4a2b2013-01-03 17:54:17 -0500210 # Request for token authentication with a valid token in header
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700211 self.data.setup_test_user()
212 self.token_client.auth(self.data.test_user, self.data.test_password,
213 self.data.test_tenant)
214 # Get the token of the current client
215 token = self.client.get_auth()
216 # Delete the token from database
217 self.client.delete_token(token)
218 # Re-auth
219 resp, body = self.token_client.auth(self.data.test_user,
220 self.data.test_password,
221 self.data.test_tenant)
222 self.assertEqual('200', resp['status'])
223 self.client.clear_auth()
224
225 @attr(type='smoke')
226 def test_get_users(self):
Sean Dague46c4a2b2013-01-03 17:54:17 -0500227 # Get a list of users and find the test user
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700228 self.data.setup_test_user()
229 resp, users = self.client.get_users()
230 self.assertIn(self.data.test_user, [u['name'] for u in users],
Zhongyue Luoe0884a32012-09-25 17:24:17 +0800231 "Could not find %s" % self.data.test_user)
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700232
233 @attr(type='negative')
234 def test_get_users_by_unauthorized_user(self):
Sean Dague46c4a2b2013-01-03 17:54:17 -0500235 # Non admin user should not be authorized to get user list
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700236 self.data.setup_test_user()
237 self.assertRaises(exceptions.Unauthorized,
Zhongyue Luo79d8d362012-09-25 13:49:27 +0800238 self.non_admin_client.get_users)
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700239
rajalakshmi-ganesanefc8bd72012-05-30 17:52:11 +0530240 @attr(type='negative')
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700241 def test_get_users_request_without_token(self):
Sean Dague46c4a2b2013-01-03 17:54:17 -0500242 # Request to get list of users without a valid token should fail
Rohit Karajgi6b1e1542012-05-14 05:55:54 -0700243 token = self.client.get_auth()
244 self.client.delete_token(token)
245 self.assertRaises(exceptions.Unauthorized, self.client.get_users)
246 self.client.clear_auth()
rajalakshmi-ganesanefc8bd72012-05-30 17:52:11 +0530247
248 @attr(type='positive')
249 def test_list_users_for_tenant(self):
Sean Dague46c4a2b2013-01-03 17:54:17 -0500250 # Return a list of all users for a tenant
rajalakshmi-ganesanefc8bd72012-05-30 17:52:11 +0530251 self.data.setup_test_tenant()
252 user_ids = list()
253 fetched_user_ids = list()
254 resp, user1 = self.client.create_user('tenant_user1', 'password1',
Zhongyue Luo79d8d362012-09-25 13:49:27 +0800255 self.data.tenant['id'],
256 'user1@123')
rajalakshmi-ganesanefc8bd72012-05-30 17:52:11 +0530257 user_ids.append(user1['id'])
258 self.data.users.append(user1)
259 resp, user2 = self.client.create_user('tenant_user2', 'password2',
Zhongyue Luo79d8d362012-09-25 13:49:27 +0800260 self.data.tenant['id'],
261 'user2@123')
rajalakshmi-ganesanefc8bd72012-05-30 17:52:11 +0530262 user_ids.append(user2['id'])
263 self.data.users.append(user2)
264 #List of users for the respective tenant ID
265 resp, body = self.client.list_users_for_tenant(self.data.tenant['id'])
266 self.assertTrue(resp['status'].startswith('2'))
267 for i in body:
268 fetched_user_ids.append(i['id'])
269 #verifying the user Id in the list
270 missing_users =\
271 [user for user in user_ids if user not in fetched_user_ids]
272 self.assertEqual(0, len(missing_users),
Zhongyue Luoe0884a32012-09-25 17:24:17 +0800273 "Failed to find user %s in fetched list" %
274 ', '.join(m_user for m_user in missing_users))
rajalakshmi-ganesanefc8bd72012-05-30 17:52:11 +0530275
276 @attr(type='positive')
277 def test_list_users_with_roles_for_tenant(self):
Sean Dague46c4a2b2013-01-03 17:54:17 -0500278 # Return list of users on tenant when roles are assigned to users
rajalakshmi-ganesanefc8bd72012-05-30 17:52:11 +0530279 self.data.setup_test_user()
280 self.data.setup_test_role()
281 user = self.get_user_by_name(self.data.test_user)
282 tenant = self.get_tenant_by_name(self.data.test_tenant)
283 role = self.get_role_by_name(self.data.test_role)
284 #Assigning roles to two users
285 user_ids = list()
286 fetched_user_ids = list()
287 user_ids.append(user['id'])
288 self.client.assign_user_role(tenant['id'], user['id'], role['id'])
289 resp, second_user = self.client.create_user('second_user', 'password1',
Zhongyue Luo79d8d362012-09-25 13:49:27 +0800290 self.data.tenant['id'],
291 'user1@123')
rajalakshmi-ganesanefc8bd72012-05-30 17:52:11 +0530292 user_ids.append(second_user['id'])
293 self.data.users.append(second_user)
294 self.client.assign_user_role(tenant['id'], second_user['id'],
295 role['id'])
296 #List of users with roles for the respective tenant ID
297 resp, body = self.client.list_users_for_tenant(self.data.tenant['id'])
298 self.assertTrue(resp['status'].startswith('2'))
299 for i in body:
300 fetched_user_ids.append(i['id'])
301 #verifying the user Id in the list
302 missing_users =\
303 [user for user in user_ids if user not in fetched_user_ids]
304 self.assertEqual(0, len(missing_users),
Zhongyue Luoe0884a32012-09-25 17:24:17 +0800305 "Failed to find user %s in fetched list" %
306 ', '.join(m_user for m_user in missing_users))
rajalakshmi-ganesanefc8bd72012-05-30 17:52:11 +0530307
308 @attr(type='negative')
309 def test_list_users_with_invalid_tenant(self):
Sean Dague46c4a2b2013-01-03 17:54:17 -0500310 # Should not be able to return a list of all
311 # users for a nonexistant tenant
rajalakshmi-ganesanefc8bd72012-05-30 17:52:11 +0530312 #Assign invalid tenant ids
313 invalid_id = list()
314 invalid_id.append(rand_name('999'))
315 invalid_id.append('alpha')
316 invalid_id.append(rand_name("dddd@#%%^$"))
317 invalid_id.append('!@#()$%^&*?<>{}[]')
318 #List the users with invalid tenant id
319 fail = list()
320 for invalid in invalid_id:
321 try:
322 resp, body = self.client.list_users_for_tenant(invalid)
323 except exceptions.NotFound:
324 pass
325 else:
326 fail.append(invalid)
327 if len(fail) != 0:
328 self.fail('Should raise Not Found when list users with invalid'
Zhongyue Luoe0884a32012-09-25 17:24:17 +0800329 'tenant ids %s' % fail)
Vincent Hou6b8a7b72012-08-25 01:24:33 +0800330
331
332class UsersTestJSON(base.BaseIdentityAdminTestJSON,
333 UsersTestBase):
334 @classmethod
335 def setUpClass(cls):
336 super(UsersTestJSON, cls).setUpClass()
337
338
Zhongyue Luoe0884a32012-09-25 17:24:17 +0800339class UsersTestXML(base.BaseIdentityAdminTestXML, UsersTestBase):
Vincent Hou6b8a7b72012-08-25 01:24:33 +0800340 @classmethod
341 def setUpClass(cls):
342 super(UsersTestXML, cls).setUpClass()