| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
import uuid |
| 14 |
|
| 15 |
from keystoneauth1.exceptions import http |
| 16 |
from keystoneclient import exceptions |
| 17 |
from keystoneclient.tests.functional import base |
| 18 |
from keystoneclient.tests.functional.v3 import client_fixtures as fixtures |
| 19 |
|
| 20 |
|
| 21 |
class ProjectsTestMixin(object): |
| 22 |
|
| 23 |
def check_project(self, project, project_ref=None): |
| 24 |
self.assertIsNotNone(project.id) |
| 25 |
self.assertIn('self', project.links) |
| 26 |
self.assertIn('/projects/' + project.id, project.links['self']) |
| 27 |
|
| 28 |
if project_ref: |
| 29 |
self.assertEqual(project_ref['name'], project.name) |
| 30 |
self.assertEqual(project_ref['domain'], project.domain_id) |
| 31 |
self.assertEqual(project_ref['enabled'], project.enabled) |
| 32 |
|
| 33 |
|
| 34 |
if hasattr(project_ref, 'description'): |
| 35 |
self.assertEqual(project_ref['description'], |
| 36 |
project.description) |
| 37 |
if hasattr(project_ref, 'parent'): |
| 38 |
self.assertEqual(project_ref['parent'], project.parent) |
| 39 |
|
| 40 |
else: |
| 41 |
|
| 42 |
self.assertIsNotNone(project.name) |
| 43 |
self.assertIsNotNone(project.domain_id) |
| 44 |
self.assertIsNotNone(project.enabled) |
| 45 |
|
| 46 |
|
| 47 |
class ProjectsTestCase(base.V3ClientTestCase, ProjectsTestMixin): |
| 48 |
|
| 49 |
def setUp(self): |
| 50 |
super(ProjectsTestCase, self).setUp() |
| 51 |
self.test_domain = fixtures.Domain(self.client) |
| 52 |
self.useFixture(self.test_domain) |
| 53 |
|
| 54 |
self.test_project = fixtures.Project(self.client, self.test_domain.id) |
| 55 |
self.useFixture(self.test_project) |
| 56 |
self.special_tag = '~`!@#$%^&*()-_+=<>.? \'"' |
| 57 |
|
| 58 |
def test_create_subproject(self): |
| 59 |
project_ref = { |
| 60 |
'name': fixtures.RESOURCE_NAME_PREFIX + uuid.uuid4().hex, |
| 61 |
'domain': self.test_domain.id, |
| 62 |
'enabled': True, |
| 63 |
'description': uuid.uuid4().hex, |
| 64 |
'parent': self.test_project.id} |
| 65 |
|
| 66 |
project = self.client.projects.create(**project_ref) |
| 67 |
self.addCleanup(self.client.projects.delete, project) |
| 68 |
self.check_project(project, project_ref) |
| 69 |
|
| 70 |
def test_create_project(self): |
| 71 |
project_ref = { |
| 72 |
'name': fixtures.RESOURCE_NAME_PREFIX + uuid.uuid4().hex, |
| 73 |
'domain': self.test_domain.id, |
| 74 |
'enabled': True, |
| 75 |
'description': uuid.uuid4().hex} |
| 76 |
|
| 77 |
project = self.client.projects.create(**project_ref) |
| 78 |
self.addCleanup(self.client.projects.delete, project) |
| 79 |
self.check_project(project, project_ref) |
| 80 |
|
| 81 |
def test_get_project(self): |
| 82 |
project_ret = self.client.projects.get(self.test_project.id) |
| 83 |
self.check_project(project_ret, self.test_project.ref) |
| 84 |
|
| 85 |
def test_get_project_invalid_params(self): |
| 86 |
self.assertRaises(exceptions.ValidationError, |
| 87 |
self.client.projects.get, |
| 88 |
self.test_project.id, |
| 89 |
subtree_as_list=True, subtree_as_ids=True) |
| 90 |
self.assertRaises(exceptions.ValidationError, |
| 91 |
self.client.projects.get, |
| 92 |
self.test_project.id, |
| 93 |
parents_as_list=True, parents_as_ids=True) |
| 94 |
|
| 95 |
def test_get_hierarchy_as_list(self): |
| 96 |
project = fixtures.Project(self.client, self.test_domain.id, |
| 97 |
parent=self.test_project.id) |
| 98 |
self.useFixture(project) |
| 99 |
|
| 100 |
child_project = fixtures.Project(self.client, self.test_domain.id, |
| 101 |
parent=project.id) |
| 102 |
self.useFixture(child_project) |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
role = fixtures.Role(self.client) |
| 108 |
self.useFixture(role) |
| 109 |
self.client.roles.grant(role.id, user=self.user_id, |
| 110 |
project=self.test_project.id) |
| 111 |
self.client.roles.grant(role.id, user=self.user_id, |
| 112 |
project=project.id) |
| 113 |
self.client.roles.grant(role.id, user=self.user_id, |
| 114 |
project=child_project.id) |
| 115 |
|
| 116 |
project_ret = self.client.projects.get(project.id, |
| 117 |
subtree_as_list=True, |
| 118 |
parents_as_list=True) |
| 119 |
|
| 120 |
self.check_project(project_ret, project.ref) |
| 121 |
self.assertCountEqual( |
| 122 |
[{'project': self.test_project.entity.to_dict()}], |
| 123 |
project_ret.parents) |
| 124 |
self.assertCountEqual( |
| 125 |
[{'project': child_project.entity.to_dict()}], |
| 126 |
project_ret.subtree) |
| 127 |
|
| 128 |
def test_get_hierarchy_as_ids(self): |
| 129 |
project = fixtures.Project(self.client, self.test_domain.id, |
| 130 |
parent=self.test_project.id) |
| 131 |
self.useFixture(project) |
| 132 |
|
| 133 |
child_project = fixtures.Project(self.client, self.test_domain.id, |
| 134 |
parent=project.id) |
| 135 |
self.useFixture(child_project) |
| 136 |
|
| 137 |
project_ret = self.client.projects.get(project.id, |
| 138 |
subtree_as_ids=True, |
| 139 |
parents_as_ids=True) |
| 140 |
|
| 141 |
self.assertCountEqual([self.test_project.id], project_ret.parents) |
| 142 |
self.assertCountEqual([child_project.id], project_ret.subtree) |
| 143 |
|
| 144 |
def test_list_projects(self): |
| 145 |
project_one = fixtures.Project(self.client, self.test_domain.id) |
| 146 |
self.useFixture(project_one) |
| 147 |
|
| 148 |
project_two = fixtures.Project(self.client, self.test_domain.id) |
| 149 |
self.useFixture(project_two) |
| 150 |
|
| 151 |
projects = self.client.projects.list() |
| 152 |
|
| 153 |
|
| 154 |
for project in projects: |
| 155 |
self.check_project(project) |
| 156 |
|
| 157 |
self.assertIn(project_one.entity, projects) |
| 158 |
self.assertIn(project_two.entity, projects) |
| 159 |
|
| 160 |
def test_list_subprojects(self): |
| 161 |
parent_project = fixtures.Project(self.client, self.test_domain.id) |
| 162 |
self.useFixture(parent_project) |
| 163 |
|
| 164 |
child_project_one = fixtures.Project(self.client, self.test_domain.id, |
| 165 |
parent=parent_project.id) |
| 166 |
self.useFixture(child_project_one) |
| 167 |
|
| 168 |
child_project_two = fixtures.Project(self.client, self.test_domain.id, |
| 169 |
parent=parent_project.id) |
| 170 |
self.useFixture(child_project_two) |
| 171 |
|
| 172 |
projects = self.client.projects.list(parent=parent_project.id) |
| 173 |
|
| 174 |
|
| 175 |
for project in projects: |
| 176 |
self.check_project(project) |
| 177 |
|
| 178 |
self.assertIn(child_project_one.entity, projects) |
| 179 |
self.assertIn(child_project_two.entity, projects) |
| 180 |
|
| 181 |
|
| 182 |
self.assertNotIn(parent_project.entity, projects) |
| 183 |
|
| 184 |
def test_update_project(self): |
| 185 |
project = fixtures.Project(self.client, self.test_domain.id) |
| 186 |
self.useFixture(project) |
| 187 |
|
| 188 |
new_name = fixtures.RESOURCE_NAME_PREFIX + uuid.uuid4().hex |
| 189 |
new_description = uuid.uuid4().hex |
| 190 |
project_ret = self.client.projects.update(project.id, |
| 191 |
name=new_name, |
| 192 |
enabled=False, |
| 193 |
description=new_description) |
| 194 |
|
| 195 |
project.ref.update({'name': new_name, 'enabled': False, |
| 196 |
'description': new_description}) |
| 197 |
self.check_project(project_ret, project.ref) |
| 198 |
|
| 199 |
def test_update_project_domain_not_allowed(self): |
| 200 |
domain = fixtures.Domain(self.client) |
| 201 |
self.useFixture(domain) |
| 202 |
|
| 203 |
self.assertRaises(http.BadRequest, |
| 204 |
self.client.projects.update, |
| 205 |
self.test_project.id, domain=domain.id) |
| 206 |
|
| 207 |
def test_delete_project(self): |
| 208 |
project = self.client.projects.create(name=uuid.uuid4().hex, |
| 209 |
domain=self.test_domain.id, |
| 210 |
enabled=True) |
| 211 |
|
| 212 |
self.client.projects.delete(project.id) |
| 213 |
self.assertRaises(http.NotFound, |
| 214 |
self.client.projects.get, |
| 215 |
project.id) |
| 216 |
|
| 217 |
def test_list_projects_with_tag_filters(self): |
| 218 |
project_one = fixtures.Project( |
| 219 |
self.client, self.test_domain.id, |
| 220 |
tags=['tag1']) |
| 221 |
project_two = fixtures.Project( |
| 222 |
self.client, self.test_domain.id, |
| 223 |
tags=['tag1', 'tag2']) |
| 224 |
project_three = fixtures.Project( |
| 225 |
self.client, self.test_domain.id, |
| 226 |
tags=['tag2', 'tag3']) |
| 227 |
|
| 228 |
self.useFixture(project_one) |
| 229 |
self.useFixture(project_two) |
| 230 |
self.useFixture(project_three) |
| 231 |
|
| 232 |
projects = self.client.projects.list(tags='tag1') |
| 233 |
project_ids = [] |
| 234 |
for project in projects: |
| 235 |
project_ids.append(project.id) |
| 236 |
self.assertIn(project_one.id, project_ids) |
| 237 |
|
| 238 |
projects = self.client.projects.list(tags_any='tag1') |
| 239 |
project_ids = [] |
| 240 |
for project in projects: |
| 241 |
project_ids.append(project.id) |
| 242 |
self.assertIn(project_one.id, project_ids) |
| 243 |
self.assertIn(project_two.id, project_ids) |
| 244 |
|
| 245 |
projects = self.client.projects.list(not_tags='tag1') |
| 246 |
project_ids = [] |
| 247 |
for project in projects: |
| 248 |
project_ids.append(project.id) |
| 249 |
self.assertNotIn(project_one.id, project_ids) |
| 250 |
|
| 251 |
projects = self.client.projects.list(not_tags_any='tag1,tag2') |
| 252 |
project_ids = [] |
| 253 |
for project in projects: |
| 254 |
project_ids.append(project.id) |
| 255 |
self.assertNotIn(project_one.id, project_ids) |
| 256 |
self.assertNotIn(project_two.id, project_ids) |
| 257 |
self.assertNotIn(project_three.id, project_ids) |
| 258 |
|
| 259 |
projects = self.client.projects.list(tags='tag1,tag2') |
| 260 |
project_ids = [] |
| 261 |
for project in projects: |
| 262 |
project_ids.append(project.id) |
| 263 |
self.assertNotIn(project_one.id, project_ids) |
| 264 |
self.assertIn(project_two.id, project_ids) |
| 265 |
self.assertNotIn(project_three.id, project_ids) |
| 266 |
|
| 267 |
def test_add_tag(self): |
| 268 |
project = fixtures.Project(self.client, self.test_domain.id) |
| 269 |
self.useFixture(project) |
| 270 |
|
| 271 |
tags = self.client.projects.get(project.id).tags |
| 272 |
self.assertEqual([], tags) |
| 273 |
|
| 274 |
project.add_tag('tag1') |
| 275 |
tags = self.client.projects.get(project.id).tags |
| 276 |
self.assertEqual(['tag1'], tags) |
| 277 |
|
| 278 |
|
| 279 |
self.assertRaises(http.BadRequest, |
| 280 |
project.add_tag, |
| 281 |
'tag1') |
| 282 |
|
| 283 |
def test_update_tags(self): |
| 284 |
project = fixtures.Project(self.client, self.test_domain.id) |
| 285 |
self.useFixture(project) |
| 286 |
|
| 287 |
tags = self.client.projects.get(project.id).tags |
| 288 |
self.assertEqual([], tags) |
| 289 |
|
| 290 |
project.update_tags(['tag1', 'tag2', self.special_tag]) |
| 291 |
tags = self.client.projects.get(project.id).tags |
| 292 |
self.assertIn('tag1', tags) |
| 293 |
self.assertIn('tag2', tags) |
| 294 |
self.assertIn(self.special_tag, tags) |
| 295 |
self.assertEqual(3, len(tags)) |
| 296 |
|
| 297 |
project.update_tags([]) |
| 298 |
tags = self.client.projects.get(project.id).tags |
| 299 |
self.assertEqual([], tags) |
| 300 |
|
| 301 |
|
| 302 |
self.assertRaises(http.BadRequest, |
| 303 |
project.update_tags, |
| 304 |
['tag1', 'tag1']) |
| 305 |
|
| 306 |
def test_delete_tag(self): |
| 307 |
project = fixtures.Project( |
| 308 |
self.client, self.test_domain.id, |
| 309 |
tags=['tag1', self.special_tag]) |
| 310 |
self.useFixture(project) |
| 311 |
|
| 312 |
project.delete_tag('tag1') |
| 313 |
tags = self.client.projects.get(project.id).tags |
| 314 |
self.assertEqual([self.special_tag], tags) |
| 315 |
|
| 316 |
project.delete_tag(self.special_tag) |
| 317 |
tags = self.client.projects.get(project.id).tags |
| 318 |
self.assertEqual([], tags) |
| 319 |
|
| 320 |
def test_delete_all_tags(self): |
| 321 |
project_one = fixtures.Project( |
| 322 |
self.client, self.test_domain.id, |
| 323 |
tags=['tag1']) |
| 324 |
|
| 325 |
project_two = fixtures.Project( |
| 326 |
self.client, self.test_domain.id, |
| 327 |
tags=['tag1', 'tag2', self.special_tag]) |
| 328 |
|
| 329 |
project_three = fixtures.Project( |
| 330 |
self.client, self.test_domain.id, |
| 331 |
tags=[]) |
| 332 |
|
| 333 |
self.useFixture(project_one) |
| 334 |
self.useFixture(project_two) |
| 335 |
self.useFixture(project_three) |
| 336 |
|
| 337 |
result_one = project_one.delete_all_tags() |
| 338 |
tags_one = self.client.projects.get(project_one.id).tags |
| 339 |
tags_two = self.client.projects.get(project_two.id).tags |
| 340 |
self.assertEqual([], result_one) |
| 341 |
self.assertEqual([], tags_one) |
| 342 |
self.assertIn('tag1', tags_two) |
| 343 |
|
| 344 |
result_two = project_two.delete_all_tags() |
| 345 |
tags_two = self.client.projects.get(project_two.id).tags |
| 346 |
self.assertEqual([], result_two) |
| 347 |
self.assertEqual([], tags_two) |
| 348 |
|
| 349 |
result_three = project_three.delete_all_tags() |
| 350 |
tags_three = self.client.projects.get(project_three.id).tags |
| 351 |
self.assertEqual([], result_three) |
| 352 |
self.assertEqual([], tags_three) |
| 353 |
|
| 354 |
def test_list_tags(self): |
| 355 |
tags_one = ['tag1'] |
| 356 |
project_one = fixtures.Project( |
| 357 |
self.client, self.test_domain.id, |
| 358 |
tags=tags_one) |
| 359 |
|
| 360 |
tags_two = ['tag1', 'tag2'] |
| 361 |
project_two = fixtures.Project( |
| 362 |
self.client, self.test_domain.id, |
| 363 |
tags=tags_two) |
| 364 |
|
| 365 |
tags_three = [] |
| 366 |
project_three = fixtures.Project( |
| 367 |
self.client, self.test_domain.id, |
| 368 |
tags=tags_three) |
| 369 |
|
| 370 |
self.useFixture(project_one) |
| 371 |
self.useFixture(project_two) |
| 372 |
self.useFixture(project_three) |
| 373 |
|
| 374 |
result_one = project_one.list_tags() |
| 375 |
result_two = project_two.list_tags() |
| 376 |
result_three = project_three.list_tags() |
| 377 |
|
| 378 |
for tag in tags_one: |
| 379 |
self.assertIn(tag, result_one) |
| 380 |
self.assertEqual(1, len(result_one)) |
| 381 |
|
| 382 |
for tag in tags_two: |
| 383 |
self.assertIn(tag, result_two) |
| 384 |
self.assertEqual(2, len(result_two)) |
| 385 |
|
| 386 |
for tag in tags_three: |
| 387 |
self.assertIn(tag, result_three) |
| 388 |
self.assertEqual(0, len(result_three)) |
| 389 |
|
| 390 |
def test_check_tag(self): |
| 391 |
project = fixtures.Project( |
| 392 |
self.client, self.test_domain.id, |
| 393 |
tags=['tag1']) |
| 394 |
self.useFixture(project) |
| 395 |
|
| 396 |
tags = self.client.projects.get(project.id).tags |
| 397 |
self.assertEqual(['tag1'], tags) |
| 398 |
self.assertTrue(project.check_tag('tag1')) |
| 399 |
self.assertFalse(project.check_tag('tag2')) |
| 400 |
self.assertFalse(project.check_tag(self.special_tag)) |
| 401 |
|
| 402 |
def test_add_invalid_tags(self): |
| 403 |
project_one = fixtures.Project( |
| 404 |
self.client, self.test_domain.id) |
| 405 |
|
| 406 |
self.useFixture(project_one) |
| 407 |
|
| 408 |
self.assertRaises(exceptions.BadRequest, |
| 409 |
project_one.add_tag, |
| 410 |
',') |
| 411 |
self.assertRaises(exceptions.BadRequest, |
| 412 |
project_one.add_tag, |
| 413 |
'/') |
| 414 |
self.assertRaises(exceptions.BadRequest, |
| 415 |
project_one.add_tag, |
| 416 |
'') |
| 417 |
|
| 418 |
def test_update_invalid_tags(self): |
| 419 |
tags_comma = ['tag1', ','] |
| 420 |
tags_slash = ['tag1', '/'] |
| 421 |
tags_blank = ['tag1', ''] |
| 422 |
project_one = fixtures.Project( |
| 423 |
self.client, self.test_domain.id) |
| 424 |
|
| 425 |
self.useFixture(project_one) |
| 426 |
|
| 427 |
self.assertRaises(exceptions.BadRequest, |
| 428 |
project_one.update_tags, |
| 429 |
tags_comma) |
| 430 |
self.assertRaises(exceptions.BadRequest, |
| 431 |
project_one.update_tags, |
| 432 |
tags_slash) |
| 433 |
self.assertRaises(exceptions.BadRequest, |
| 434 |
project_one.update_tags, |
| 435 |
tags_blank) |
| 436 |
|
| 437 |
def test_create_project_invalid_tags(self): |
| 438 |
project_ref = { |
| 439 |
'name': fixtures.RESOURCE_NAME_PREFIX + uuid.uuid4().hex, |
| 440 |
'domain': self.test_domain.id, |
| 441 |
'enabled': True, |
| 442 |
'description': uuid.uuid4().hex, |
| 443 |
'tags': ','} |
| 444 |
|
| 445 |
self.assertRaises(exceptions.BadRequest, |
| 446 |
self.client.projects.create, |
| 447 |
**project_ref) |
| 448 |
|
| 449 |
project_ref = { |
| 450 |
'name': fixtures.RESOURCE_NAME_PREFIX + uuid.uuid4().hex, |
| 451 |
'domain': self.test_domain.id, |
| 452 |
'enabled': True, |
| 453 |
'description': uuid.uuid4().hex, |
| 454 |
'tags': '/'} |
| 455 |
|
| 456 |
self.assertRaises(exceptions.BadRequest, |
| 457 |
self.client.projects.create, |
| 458 |
**project_ref) |
| 459 |
|
| 460 |
project_ref = { |
| 461 |
'name': fixtures.RESOURCE_NAME_PREFIX + uuid.uuid4().hex, |
| 462 |
'domain': self.test_domain.id, |
| 463 |
'enabled': True, |
| 464 |
'description': uuid.uuid4().hex, |
| 465 |
'tags': ''} |
| 466 |
|
| 467 |
self.assertRaises(exceptions.BadRequest, |
| 468 |
self.client.projects.create, |
| 469 |
**project_ref) |