test_space_summary.py 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. # Copyright 2021 The Matrix.org Foundation C.I.C.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. from typing import Any, Iterable, Optional, Tuple
  15. from unittest import mock
  16. from synapse.api.errors import AuthError
  17. from synapse.handlers.space_summary import _child_events_comparison_key
  18. from synapse.rest import admin
  19. from synapse.rest.client.v1 import login, room
  20. from synapse.server import HomeServer
  21. from synapse.types import JsonDict
  22. from tests import unittest
  23. def _create_event(room_id: str, order: Optional[Any] = None):
  24. result = mock.Mock()
  25. result.room_id = room_id
  26. result.content = {}
  27. if order is not None:
  28. result.content["order"] = order
  29. return result
  30. def _order(*events):
  31. return sorted(events, key=_child_events_comparison_key)
  32. class TestSpaceSummarySort(unittest.TestCase):
  33. def test_no_order_last(self):
  34. """An event with no ordering is placed behind those with an ordering."""
  35. ev1 = _create_event("!abc:test")
  36. ev2 = _create_event("!xyz:test", "xyz")
  37. self.assertEqual([ev2, ev1], _order(ev1, ev2))
  38. def test_order(self):
  39. """The ordering should be used."""
  40. ev1 = _create_event("!abc:test", "xyz")
  41. ev2 = _create_event("!xyz:test", "abc")
  42. self.assertEqual([ev2, ev1], _order(ev1, ev2))
  43. def test_order_room_id(self):
  44. """Room ID is a tie-breaker for ordering."""
  45. ev1 = _create_event("!abc:test", "abc")
  46. ev2 = _create_event("!xyz:test", "abc")
  47. self.assertEqual([ev1, ev2], _order(ev1, ev2))
  48. def test_invalid_ordering_type(self):
  49. """Invalid orderings are considered the same as missing."""
  50. ev1 = _create_event("!abc:test", 1)
  51. ev2 = _create_event("!xyz:test", "xyz")
  52. self.assertEqual([ev2, ev1], _order(ev1, ev2))
  53. ev1 = _create_event("!abc:test", {})
  54. self.assertEqual([ev2, ev1], _order(ev1, ev2))
  55. ev1 = _create_event("!abc:test", [])
  56. self.assertEqual([ev2, ev1], _order(ev1, ev2))
  57. ev1 = _create_event("!abc:test", True)
  58. self.assertEqual([ev2, ev1], _order(ev1, ev2))
  59. def test_invalid_ordering_value(self):
  60. """Invalid orderings are considered the same as missing."""
  61. ev1 = _create_event("!abc:test", "foo\n")
  62. ev2 = _create_event("!xyz:test", "xyz")
  63. self.assertEqual([ev2, ev1], _order(ev1, ev2))
  64. ev1 = _create_event("!abc:test", "a" * 51)
  65. self.assertEqual([ev2, ev1], _order(ev1, ev2))
  66. class SpaceSummaryTestCase(unittest.HomeserverTestCase):
  67. servlets = [
  68. admin.register_servlets_for_client_rest_resource,
  69. room.register_servlets,
  70. login.register_servlets,
  71. ]
  72. def prepare(self, reactor, clock, hs: HomeServer):
  73. self.hs = hs
  74. self.handler = self.hs.get_space_summary_handler()
  75. self.user = self.register_user("user", "pass")
  76. self.token = self.login("user", "pass")
  77. def _add_child(self, space_id: str, room_id: str, token: str) -> None:
  78. """Add a child room to a space."""
  79. self.helper.send_state(
  80. space_id,
  81. event_type="m.space.child",
  82. body={"via": [self.hs.hostname]},
  83. tok=token,
  84. state_key=room_id,
  85. )
  86. def _assert_rooms(self, result: JsonDict, rooms: Iterable[str]) -> None:
  87. """Assert that the expected room IDs are in the response."""
  88. self.assertCountEqual([room.get("room_id") for room in result["rooms"]], rooms)
  89. def _assert_events(
  90. self, result: JsonDict, events: Iterable[Tuple[str, str]]
  91. ) -> None:
  92. """Assert that the expected parent / child room IDs are in the response."""
  93. self.assertCountEqual(
  94. [
  95. (event.get("room_id"), event.get("state_key"))
  96. for event in result["events"]
  97. ],
  98. events,
  99. )
  100. def test_simple_space(self):
  101. """Test a simple space with a single room."""
  102. space = self.helper.create_room_as(self.user, tok=self.token)
  103. room = self.helper.create_room_as(self.user, tok=self.token)
  104. self._add_child(space, room, self.token)
  105. result = self.get_success(self.handler.get_space_summary(self.user, space))
  106. # The result should have the space and the room in it, along with a link
  107. # from space -> room.
  108. self._assert_rooms(result, [space, room])
  109. self._assert_events(result, [(space, room)])
  110. def test_visibility(self):
  111. """A user not in a space cannot inspect it."""
  112. space = self.helper.create_room_as(self.user, tok=self.token)
  113. room = self.helper.create_room_as(self.user, tok=self.token)
  114. self._add_child(space, room, self.token)
  115. user2 = self.register_user("user2", "pass")
  116. token2 = self.login("user2", "pass")
  117. # The user cannot see the space.
  118. self.get_failure(self.handler.get_space_summary(user2, space), AuthError)
  119. # Joining the room causes it to be visible.
  120. self.helper.join(space, user2, tok=token2)
  121. result = self.get_success(self.handler.get_space_summary(user2, space))
  122. # The result should only have the space, but includes the link to the room.
  123. self._assert_rooms(result, [space])
  124. self._assert_events(result, [(space, room)])
  125. def test_world_readable(self):
  126. """A world-readable room is visible to everyone."""
  127. space = self.helper.create_room_as(self.user, tok=self.token)
  128. room = self.helper.create_room_as(self.user, tok=self.token)
  129. self._add_child(space, room, self.token)
  130. self.helper.send_state(
  131. space,
  132. event_type="m.room.history_visibility",
  133. body={"history_visibility": "world_readable"},
  134. tok=self.token,
  135. )
  136. user2 = self.register_user("user2", "pass")
  137. # The space should be visible, as well as the link to the room.
  138. result = self.get_success(self.handler.get_space_summary(user2, space))
  139. self._assert_rooms(result, [space])
  140. self._assert_events(result, [(space, room)])