| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
from unittest import mock |
| 16 |
import uuid |
| 17 |
|
| 18 |
from keystoneclient import exceptions |
| 19 |
from keystoneclient.tests.unit.v3 import utils |
| 20 |
from keystoneclient.v3 import users |
| 21 |
|
| 22 |
|
| 23 |
class UserTests(utils.ClientTestCase, utils.CrudTests): |
| 24 |
def setUp(self): |
| 25 |
super(UserTests, self).setUp() |
| 26 |
self.key = 'user' |
| 27 |
self.collection_key = 'users' |
| 28 |
self.model = users.User |
| 29 |
self.manager = self.client.users |
| 30 |
|
| 31 |
def new_ref(self, **kwargs): |
| 32 |
kwargs = super(UserTests, self).new_ref(**kwargs) |
| 33 |
kwargs.setdefault('description', uuid.uuid4().hex) |
| 34 |
kwargs.setdefault('domain_id', uuid.uuid4().hex) |
| 35 |
kwargs.setdefault('enabled', True) |
| 36 |
kwargs.setdefault('name', uuid.uuid4().hex) |
| 37 |
kwargs.setdefault('default_project_id', uuid.uuid4().hex) |
| 38 |
return kwargs |
| 39 |
|
| 40 |
def test_add_user_to_group(self): |
| 41 |
group_id = uuid.uuid4().hex |
| 42 |
ref = self.new_ref() |
| 43 |
self.stub_url('PUT', |
| 44 |
['groups', group_id, self.collection_key, ref['id']], |
| 45 |
status_code=204) |
| 46 |
|
| 47 |
self.manager.add_to_group(user=ref['id'], group=group_id) |
| 48 |
self.assertRaises(exceptions.ValidationError, |
| 49 |
self.manager.remove_from_group, |
| 50 |
user=ref['id'], |
| 51 |
group=None) |
| 52 |
|
| 53 |
def test_list_users_in_group(self): |
| 54 |
group_id = uuid.uuid4().hex |
| 55 |
ref_list = [self.new_ref(), self.new_ref()] |
| 56 |
|
| 57 |
self.stub_entity('GET', |
| 58 |
['groups', group_id, self.collection_key], |
| 59 |
entity=ref_list) |
| 60 |
|
| 61 |
returned_list = self.manager.list(group=group_id) |
| 62 |
self.assertEqual(len(ref_list), len(returned_list)) |
| 63 |
[self.assertIsInstance(r, self.model) for r in returned_list] |
| 64 |
|
| 65 |
def test_check_user_in_group(self): |
| 66 |
group_id = uuid.uuid4().hex |
| 67 |
ref = self.new_ref() |
| 68 |
|
| 69 |
self.stub_url('HEAD', |
| 70 |
['groups', group_id, self.collection_key, ref['id']], |
| 71 |
status_code=204) |
| 72 |
|
| 73 |
self.manager.check_in_group(user=ref['id'], group=group_id) |
| 74 |
|
| 75 |
self.assertRaises(exceptions.ValidationError, |
| 76 |
self.manager.check_in_group, |
| 77 |
user=ref['id'], |
| 78 |
group=None) |
| 79 |
|
| 80 |
def test_remove_user_from_group(self): |
| 81 |
group_id = uuid.uuid4().hex |
| 82 |
ref = self.new_ref() |
| 83 |
|
| 84 |
self.stub_url('DELETE', |
| 85 |
['groups', group_id, self.collection_key, ref['id']], |
| 86 |
status_code=204) |
| 87 |
|
| 88 |
self.manager.remove_from_group(user=ref['id'], group=group_id) |
| 89 |
self.assertRaises(exceptions.ValidationError, |
| 90 |
self.manager.remove_from_group, |
| 91 |
user=ref['id'], |
| 92 |
group=None) |
| 93 |
|
| 94 |
def test_create_doesnt_log_password(self): |
| 95 |
password = uuid.uuid4().hex |
| 96 |
ref = self.new_ref() |
| 97 |
|
| 98 |
self.stub_entity('POST', [self.collection_key], |
| 99 |
status_code=201, entity=ref) |
| 100 |
|
| 101 |
req_ref = ref.copy() |
| 102 |
req_ref.pop('id') |
| 103 |
param_ref = req_ref.copy() |
| 104 |
|
| 105 |
param_ref['password'] = password |
| 106 |
params = utils.parameterize(param_ref) |
| 107 |
|
| 108 |
self.manager.create(**params) |
| 109 |
|
| 110 |
self.assertNotIn(password, self.logger.output) |
| 111 |
|
| 112 |
def test_create_with_project(self): |
| 113 |
|
| 114 |
|
| 115 |
self.deprecations.expect_deprecations() |
| 116 |
ref = self.new_ref() |
| 117 |
|
| 118 |
self.stub_entity('POST', [self.collection_key], |
| 119 |
status_code=201, entity=ref) |
| 120 |
|
| 121 |
req_ref = ref.copy() |
| 122 |
req_ref.pop('id') |
| 123 |
param_ref = req_ref.copy() |
| 124 |
|
| 125 |
param_ref['project_id'] = param_ref.pop('default_project_id') |
| 126 |
params = utils.parameterize(param_ref) |
| 127 |
|
| 128 |
returned = self.manager.create(**params) |
| 129 |
self.assertIsInstance(returned, self.model) |
| 130 |
for attr in ref: |
| 131 |
self.assertEqual( |
| 132 |
getattr(returned, attr), |
| 133 |
ref[attr], |
| 134 |
'Expected different %s' % attr) |
| 135 |
self.assertEntityRequestBodyIs(req_ref) |
| 136 |
|
| 137 |
def test_create_with_project_and_default_project(self): |
| 138 |
|
| 139 |
|
| 140 |
self.deprecations.expect_deprecations() |
| 141 |
|
| 142 |
ref = self.new_ref() |
| 143 |
|
| 144 |
self.stub_entity('POST', |
| 145 |
[self.collection_key], |
| 146 |
status_code=201, entity=ref) |
| 147 |
|
| 148 |
req_ref = ref.copy() |
| 149 |
req_ref.pop('id') |
| 150 |
param_ref = req_ref.copy() |
| 151 |
|
| 152 |
|
| 153 |
param_ref['project_id'] = 'project' |
| 154 |
params = utils.parameterize(param_ref) |
| 155 |
|
| 156 |
returned = self.manager.create(**params) |
| 157 |
self.assertIsInstance(returned, self.model) |
| 158 |
for attr in ref: |
| 159 |
self.assertEqual( |
| 160 |
getattr(returned, attr), |
| 161 |
ref[attr], |
| 162 |
'Expected different %s' % attr) |
| 163 |
self.assertEntityRequestBodyIs(req_ref) |
| 164 |
|
| 165 |
def test_update_doesnt_log_password(self): |
| 166 |
password = uuid.uuid4().hex |
| 167 |
ref = self.new_ref() |
| 168 |
|
| 169 |
req_ref = ref.copy() |
| 170 |
req_ref.pop('id') |
| 171 |
param_ref = req_ref.copy() |
| 172 |
|
| 173 |
self.stub_entity('PATCH', |
| 174 |
[self.collection_key, ref['id']], |
| 175 |
status_code=200, entity=ref) |
| 176 |
|
| 177 |
param_ref['password'] = password |
| 178 |
params = utils.parameterize(param_ref) |
| 179 |
|
| 180 |
self.manager.update(ref['id'], **params) |
| 181 |
|
| 182 |
self.assertNotIn(password, self.logger.output) |
| 183 |
|
| 184 |
def test_update_with_project(self): |
| 185 |
|
| 186 |
|
| 187 |
self.deprecations.expect_deprecations() |
| 188 |
|
| 189 |
ref = self.new_ref() |
| 190 |
req_ref = ref.copy() |
| 191 |
req_ref.pop('id') |
| 192 |
param_ref = req_ref.copy() |
| 193 |
|
| 194 |
self.stub_entity('PATCH', |
| 195 |
[self.collection_key, ref['id']], |
| 196 |
status_code=200, entity=ref) |
| 197 |
|
| 198 |
|
| 199 |
param_ref['project_id'] = param_ref.pop('default_project_id') |
| 200 |
params = utils.parameterize(param_ref) |
| 201 |
|
| 202 |
returned = self.manager.update(ref['id'], **params) |
| 203 |
self.assertIsInstance(returned, self.model) |
| 204 |
for attr in ref: |
| 205 |
self.assertEqual( |
| 206 |
getattr(returned, attr), |
| 207 |
ref[attr], |
| 208 |
'Expected different %s' % attr) |
| 209 |
self.assertEntityRequestBodyIs(req_ref) |
| 210 |
|
| 211 |
def test_update_with_project_and_default_project(self, ref=None): |
| 212 |
self.deprecations.expect_deprecations() |
| 213 |
|
| 214 |
ref = self.new_ref() |
| 215 |
req_ref = ref.copy() |
| 216 |
req_ref.pop('id') |
| 217 |
param_ref = req_ref.copy() |
| 218 |
|
| 219 |
self.stub_entity('PATCH', |
| 220 |
[self.collection_key, ref['id']], |
| 221 |
status_code=200, entity=ref) |
| 222 |
|
| 223 |
|
| 224 |
param_ref['project_id'] = 'project' |
| 225 |
params = utils.parameterize(param_ref) |
| 226 |
|
| 227 |
returned = self.manager.update(ref['id'], **params) |
| 228 |
self.assertIsInstance(returned, self.model) |
| 229 |
for attr in ref: |
| 230 |
self.assertEqual( |
| 231 |
getattr(returned, attr), |
| 232 |
ref[attr], |
| 233 |
'Expected different %s' % attr) |
| 234 |
self.assertEntityRequestBodyIs(req_ref) |
| 235 |
|
| 236 |
def test_update_password(self): |
| 237 |
old_password = uuid.uuid4().hex |
| 238 |
new_password = uuid.uuid4().hex |
| 239 |
|
| 240 |
self.stub_url('POST', |
| 241 |
[self.collection_key, self.TEST_USER_ID, 'password']) |
| 242 |
self.client.user_id = self.TEST_USER_ID |
| 243 |
self.manager.update_password(old_password, new_password) |
| 244 |
|
| 245 |
exp_req_body = { |
| 246 |
'user': { |
| 247 |
'password': new_password, 'original_password': old_password |
| 248 |
} |
| 249 |
} |
| 250 |
|
| 251 |
self.assertEqual( |
| 252 |
'%s/users/%s/password' % (self.TEST_URL, self.TEST_USER_ID), |
| 253 |
self.requests_mock.last_request.url) |
| 254 |
self.assertRequestBodyIs(json=exp_req_body) |
| 255 |
self.assertNotIn(old_password, self.logger.output) |
| 256 |
self.assertNotIn(new_password, self.logger.output) |
| 257 |
|
| 258 |
def test_update_password_with_no_hardcoded_endpoint_filter(self): |
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
old_password = uuid.uuid4().hex |
| 263 |
new_password = uuid.uuid4().hex |
| 264 |
expected_params = {'user': {'password': new_password, |
| 265 |
'original_password': old_password}} |
| 266 |
user_password_update_path = '/users/%s/password' % self.TEST_USER_ID |
| 267 |
|
| 268 |
self.client.user_id = self.TEST_USER_ID |
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
with mock.patch('keystoneclient.base.Manager._update') as m: |
| 275 |
self.manager.update_password(old_password, new_password) |
| 276 |
m.assert_called_with(user_password_update_path, expected_params, |
| 277 |
method='POST', log=False) |
| 278 |
|
| 279 |
def test_update_password_with_bad_inputs(self): |
| 280 |
old_password = uuid.uuid4().hex |
| 281 |
new_password = uuid.uuid4().hex |
| 282 |
|
| 283 |
|
| 284 |
self.assertRaises(exceptions.ValidationError, |
| 285 |
self.manager.update_password, |
| 286 |
old_password, None) |
| 287 |
self.assertRaises(exceptions.ValidationError, |
| 288 |
self.manager.update_password, |
| 289 |
old_password, '') |
| 290 |
|
| 291 |
|
| 292 |
self.assertRaises(exceptions.ValidationError, |
| 293 |
self.manager.update_password, |
| 294 |
None, new_password) |
| 295 |
self.assertRaises(exceptions.ValidationError, |
| 296 |
self.manager.update_password, |
| 297 |
'', new_password) |
| 298 |
|
| 299 |
|
| 300 |
self.assertRaises(exceptions.ValidationError, |
| 301 |
self.manager.update_password, |
| 302 |
None, None) |
| 303 |
self.assertRaises(exceptions.ValidationError, |
| 304 |
self.manager.update_password, |
| 305 |
'', '') |
| 306 |
password = uuid.uuid4().hex |
| 307 |
self.assertRaises(exceptions.ValidationError, |
| 308 |
self.manager.update_password, |
| 309 |
password, password) |