|
15 | 15 |
|
16 | 16 | from unittest.mock import Mock
|
17 | 17 |
|
18 |
| -import synapse |
19 | 18 | import synapse.api.errors
|
| 19 | +import synapse.rest.admin |
20 | 20 | from synapse.api.constants import EventTypes
|
21 | 21 | from synapse.config.room_directory import RoomDirectoryConfig
|
22 | 22 | from synapse.rest.client import directory, login, room
|
@@ -432,6 +432,106 @@ def test_allowed(self):
|
432 | 432 | self.assertEquals(200, channel.code, channel.result)
|
433 | 433 |
|
434 | 434 |
|
| 435 | +class TestCreatePublishedRoomACL(unittest.HomeserverTestCase): |
| 436 | + data = {"room_alias_name": "unofficial_test"} |
| 437 | + |
| 438 | + servlets = [ |
| 439 | + synapse.rest.admin.register_servlets_for_client_rest_resource, |
| 440 | + login.register_servlets, |
| 441 | + directory.register_servlets, |
| 442 | + room.register_servlets, |
| 443 | + ] |
| 444 | + hijack_auth = False |
| 445 | + |
| 446 | + def prepare(self, reactor, clock, hs): |
| 447 | + self.allowed_user_id = self.register_user("allowed", "pass") |
| 448 | + self.allowed_access_token = self.login("allowed", "pass") |
| 449 | + |
| 450 | + self.denied_user_id = self.register_user("denied", "pass") |
| 451 | + self.denied_access_token = self.login("denied", "pass") |
| 452 | + |
| 453 | + # This time we add custom room list publication rules |
| 454 | + config = {} |
| 455 | + config["alias_creation_rules"] = [] |
| 456 | + config["room_list_publication_rules"] = [ |
| 457 | + {"user_id": "*", "alias": "*", "action": "deny"}, |
| 458 | + {"user_id": self.allowed_user_id, "alias": "*", "action": "allow"}, |
| 459 | + ] |
| 460 | + |
| 461 | + rd_config = RoomDirectoryConfig() |
| 462 | + rd_config.read_config(config) |
| 463 | + |
| 464 | + self.hs.config.roomdirectory.is_publishing_room_allowed = ( |
| 465 | + rd_config.is_publishing_room_allowed |
| 466 | + ) |
| 467 | + |
| 468 | + return hs |
| 469 | + |
| 470 | + def test_denied_without_publication_permission(self): |
| 471 | + """ |
| 472 | + Try to create a room, register an alias for it, and publish it, |
| 473 | + as a user without permission to publish rooms. |
| 474 | + (This is used as both a standalone test & as a helper function.) |
| 475 | + """ |
| 476 | + self.helper.create_room_as( |
| 477 | + self.denied_user_id, |
| 478 | + tok=self.denied_access_token, |
| 479 | + extra_content=self.data, |
| 480 | + is_public=True, |
| 481 | + expect_code=403, |
| 482 | + ) |
| 483 | + |
| 484 | + def test_allowed_when_creating_private_room(self): |
| 485 | + """ |
| 486 | + Try to create a room, register an alias for it, and NOT publish it, |
| 487 | + as a user without permission to publish rooms. |
| 488 | + (This is used as both a standalone test & as a helper function.) |
| 489 | + """ |
| 490 | + self.helper.create_room_as( |
| 491 | + self.denied_user_id, |
| 492 | + tok=self.denied_access_token, |
| 493 | + extra_content=self.data, |
| 494 | + is_public=False, |
| 495 | + expect_code=200, |
| 496 | + ) |
| 497 | + |
| 498 | + def test_allowed_with_publication_permission(self): |
| 499 | + """ |
| 500 | + Try to create a room, register an alias for it, and publish it, |
| 501 | + as a user WITH permission to publish rooms. |
| 502 | + (This is used as both a standalone test & as a helper function.) |
| 503 | + """ |
| 504 | + self.helper.create_room_as( |
| 505 | + self.allowed_user_id, |
| 506 | + tok=self.allowed_access_token, |
| 507 | + extra_content=self.data, |
| 508 | + is_public=False, |
| 509 | + expect_code=200, |
| 510 | + ) |
| 511 | + |
| 512 | + def test_can_create_as_private_room_after_rejection(self): |
| 513 | + """ |
| 514 | + After failing to publish a room with an alias as a user without publish permission, |
| 515 | + retry as the same user, but without publishing the room. |
| 516 | +
|
| 517 | + This should pass, but used to fail because the alias was registered by the first |
| 518 | + request, even though the room creation was denied. |
| 519 | + """ |
| 520 | + self.test_denied_without_publication_permission() |
| 521 | + self.test_allowed_when_creating_private_room() |
| 522 | + |
| 523 | + def test_can_create_with_permission_after_rejection(self): |
| 524 | + """ |
| 525 | + After failing to publish a room with an alias as a user without publish permission, |
| 526 | + retry as someone with permission, using the same alias. |
| 527 | +
|
| 528 | + This also used to fail because of the alias having been registered by the first |
| 529 | + request, leaving it unavailable for any other user's new rooms. |
| 530 | + """ |
| 531 | + self.test_denied_without_publication_permission() |
| 532 | + self.test_allowed_with_publication_permission() |
| 533 | + |
| 534 | + |
435 | 535 | class TestRoomListSearchDisabled(unittest.HomeserverTestCase):
|
436 | 536 | user_id = "@test:test"
|
437 | 537 |
|
|
0 commit comments