blob: 109ce3774de8df6253938a80db528a1f9ca62e14 [file] [log] [blame]
Rohit Karajgi6b1e1542012-05-14 05:55:54 -07001import unittest2 as unittest
2from nose.plugins.attrib import attr
3from base_admin_test import BaseAdminTest
4from tempest import exceptions
5from tempest.common.utils.data_utils import rand_name
6
7
8class UsersTest(BaseAdminTest):
9
10 alt_user = rand_name('test_user_')
11 alt_password = rand_name('pass_')
12 alt_email = alt_user + '@testmail.tm'
13 alt_tenant = rand_name('test_tenant_')
14 alt_description = rand_name('desc_')
15
16 @attr(type='smoke')
17 def test_create_user(self):
18 """Create a user"""
19 self.data.setup_test_tenant()
20 resp, user = self.client.create_user(self.alt_user, self.alt_password,
21 self.data.tenant['id'],
22 self.alt_email)
23 self.data.users.append(user)
24 self.assertEqual('200', resp['status'])
25 self.assertEqual(self.alt_user, user['name'])
26
27 @attr(type='negative')
28 def test_create_user_by_unauthorized_user(self):
29 """Non-admin should not be authorized to create a user"""
30 self.data.setup_test_tenant()
31 self.assertRaises(exceptions.Unauthorized,
32 self.non_admin_client.create_user, self.alt_user,
33 self.alt_password, self.data.tenant['id'],
34 self.alt_email)
35
36 @attr(type='negative')
37 @unittest.skip("Until Bug 987121 is fixed")
38 def test_create_user_with_empty_name(self):
39 """User with an empty name should not be created"""
40 self.data.setup_test_tenant()
41 self.assertRaises(exceptions.BadRequest, self.client.create_user, '',
42 self.alt_password, self.data.tenant['id'],
43 self.alt_email)
44
45 @attr(type='negative')
46 @unittest.skip("Until Bug 966249 is fixed")
47 def test_create_user_with_name_length_over_64(self):
48 """Length of user name filed should be restricted to 64 characters"""
49 self.data.setup_test_tenant()
50 self.assertRaises(exceptions.BadRequest, self.client.create_user,
51 'a' * 64, self.alt_password,
52 self.data.tenant['id'], self.alt_email)
53
54 @attr(type='negative')
55 def test_create_user_with_duplicate_name(self):
56 """Duplicate user should not be created"""
57 self.data.setup_test_user()
58 self.assertRaises(exceptions.Duplicate, self.client.create_user,
59 self.data.test_user, self.data.test_password,
60 self.data.tenant['id'], self.data.test_email)
61
62 @attr(type='negative')
63 @unittest.skip("Until Bug 999084 is fixed")
64 def test_create_user_with_empty_password(self):
65 """User with an empty password should not be created"""
66 self.data.setup_test_tenant()
67 self.assertRaises(exceptions.BadRequest, self.client.create_user,
68 self.alt_user, '', self.data.tenant['id'],
69 self.alt_email)
70
71 @attr(type='nagative')
72 @unittest.skip("Until Bug 999084 is fixed")
73 def test_create_user_with_long_password(self):
74 """User having password exceeding max length should not be created"""
75 self.data.setup_test_tenant()
76 self.assertRaises(exceptions.BadRequest, self.client.create_user,
77 self.alt_user, 'a' * 64, self.data.tenant['id'],
78 self.alt_email)
79
80 @attr(type='negative')
81 @unittest.skip("Until Bug 999084 is fixed")
82 def test_create_user_with_invalid_email_format(self):
83 """Email format should be validated while creating a user"""
84 self.data.setup_test_tenant()
85 self.assertRaises(exceptions.BadRequest, self.client.create_user,
86 self.alt_user, '', self.data.tenant['id'], '12345')
87
88 @attr(type='negative')
89 def test_create_user_for_non_existant_tenant(self):
90 """Attempt to create a user in a non-existent tenant should fail"""
91 self.assertRaises(exceptions.NotFound, self.client.create_user,
92 self.alt_user, self.alt_password, '49ffgg99999',
93 self.alt_email)
94
95 @attr(type='negative')
96 def test_create_user_request_without_a_token(self):
97 """Request to create a user without a valid token should fail"""
98 self.data.setup_test_tenant()
99 # Get the token of the current client
100 token = self.client.get_auth()
101 # Delete the token from database
102 self.client.delete_token(token)
103 self.assertRaises(exceptions.Unauthorized, self.client.create_user,
104 self.alt_user, self.alt_password,
105 self.data.tenant['id'], self.alt_email)
106
107 # Unset the token to allow further tests to generate a new token
108 self.client.clear_auth()
109
110 @attr(type='smoke')
111 def test_delete_user(self):
112 """Delete a user"""
113 self.data.setup_test_tenant()
114 resp, user = self.client.create_user('user_1234', self.alt_password,
115 self.data.tenant['id'],
116 self.alt_email)
117 resp, body = self.client.delete_user(user['id'])
118 self.assertEquals('204', resp['status'])
119
120 @attr(type='negative')
121 def test_delete_users_by_unauthorized_user(self):
122 """Non admin user should not be authorized to delete a user"""
123 self.data.setup_test_user()
124 self.assertRaises(exceptions.Unauthorized,
125 self.non_admin_client.delete_user,
126 self.data.user['id'])
127
128 @attr(type='negative')
129 def test_delete_non_existant_user(self):
130 """Attempt to delete a non-existent user should fail"""
131 self.assertRaises(exceptions.NotFound, self.client.delete_user,
132 'junk12345123')
133
134 @attr(type='smoke')
135 def test_user_authentication(self):
136 """Valid user's token is authenticated"""
137 self.data.setup_test_user()
138 # Get a token
139 self.token_client.auth(self.data.test_user, self.data.test_password,
140 self.data.test_tenant)
141 # Re-auth
142 resp, body = self.token_client.auth(self.data.test_user,
143 self.data.test_password,
144 self.data.test_tenant)
145 self.assertEqual('200', resp['status'])
146
147 @attr(type='negative')
148 def test_authentication_for_disabled_user(self):
149 """Disabled user's token should not get authenticated"""
150 self.data.setup_test_user()
151 self.disable_user(self.data.test_user)
152 self.assertRaises(exceptions.Unauthorized, self.token_client.auth,
153 self.data.test_user,
154 self.data.test_password,
155 self.data.test_tenant)
156
157 @attr(type='negative')
158 @unittest.skip('Until Bug 988920 is fixed')
159 def test_authentication_when_tenant_is_disabled(self):
160 """User's token for a disabled tenant should not be authenticated"""
161 self.data.setup_test_user()
162 self.disable_tenant(self.data.test_tenant)
163 self.assertRaises(exceptions.Unauthorized, self.token_client.auth,
164 self.data.test_user,
165 self.data.test_password,
166 self.data.test_tenant)
167
168 @attr(type='negative')
169 @unittest.skip('Until Bug 988920 is fixed')
170 def test_authentication_with_invalid_tenant(self):
171 """User's token for an invalid tenant should not be authenticated"""
172 self.data.setup_one_user()
173 self.assertRaises(exceptions.Unauthorized, self.token_client.auth,
174 self.data.test_user,
175 self.data.test_password,
176 'junktenant1234')
177
178 @attr(type='negative')
179 def test_authentication_with_invalid_username(self):
180 """Non-existent user's token should not get authenticated"""
181 self.assertRaises(exceptions.Unauthorized, self.token_client.auth,
182 'junkuser123', self.data.test_password,
183 self.data.test_tenant)
184
185 @attr(type='negative')
186 def test_authentication_with_invalid_password(self):
187 """User's token with invalid password should not be authenticated"""
188 self.data.setup_test_user()
189 self.assertRaises(exceptions.Unauthorized, self.token_client.auth,
190 self.data.test_user, 'junkpass1234',
191 self.data.test_tenant)
192
193 @attr(type='positive')
194 def test_authentication_request_without_token(self):
195 """Request for token authentication with a valid token in header"""
196 self.data.setup_test_user()
197 self.token_client.auth(self.data.test_user, self.data.test_password,
198 self.data.test_tenant)
199 # Get the token of the current client
200 token = self.client.get_auth()
201 # Delete the token from database
202 self.client.delete_token(token)
203 # Re-auth
204 resp, body = self.token_client.auth(self.data.test_user,
205 self.data.test_password,
206 self.data.test_tenant)
207 self.assertEqual('200', resp['status'])
208 self.client.clear_auth()
209
210 @attr(type='smoke')
211 def test_get_users(self):
212 """Get a list of users and find the test user"""
213 self.data.setup_test_user()
214 resp, users = self.client.get_users()
215 self.assertIn(self.data.test_user, [u['name'] for u in users],
216 "Could not find %s" % self.data.test_user)
217
218 @attr(type='negative')
219 def test_get_users_by_unauthorized_user(self):
220 """Non admin user should not be authorized to get user list"""
221 self.data.setup_test_user()
222 self.assertRaises(exceptions.Unauthorized,
223 self.non_admin_client.get_users)
224
225 def test_get_users_request_without_token(self):
226 """Request to get list of users without a valid token should fail"""
227 token = self.client.get_auth()
228 self.client.delete_token(token)
229 self.assertRaises(exceptions.Unauthorized, self.client.get_users)
230 self.client.clear_auth()