1
0

test_visibility.py 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. # Copyright 2018 New Vector Ltd
  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. import logging
  15. from typing import Optional
  16. from unittest.mock import patch
  17. from synapse.api.room_versions import RoomVersions
  18. from synapse.events import EventBase, make_event_from_dict
  19. from synapse.types import JsonDict, create_requester
  20. from synapse.visibility import filter_events_for_client, filter_events_for_server
  21. from tests import unittest
  22. from tests.utils import create_room
  23. logger = logging.getLogger(__name__)
  24. TEST_ROOM_ID = "!TEST:ROOM"
  25. class FilterEventsForServerTestCase(unittest.HomeserverTestCase):
  26. def setUp(self) -> None:
  27. super(FilterEventsForServerTestCase, self).setUp()
  28. self.event_creation_handler = self.hs.get_event_creation_handler()
  29. self.event_builder_factory = self.hs.get_event_builder_factory()
  30. self.storage = self.hs.get_storage()
  31. self.get_success(create_room(self.hs, TEST_ROOM_ID, "@someone:ROOM"))
  32. def test_filtering(self) -> None:
  33. #
  34. # The events to be filtered consist of 10 membership events (it doesn't
  35. # really matter if they are joins or leaves, so let's make them joins).
  36. # One of those membership events is going to be for a user on the
  37. # server we are filtering for (so we can check the filtering is doing
  38. # the right thing).
  39. #
  40. # before we do that, we persist some other events to act as state.
  41. self.get_success(self._inject_visibility("@admin:hs", "joined"))
  42. for i in range(0, 10):
  43. self.get_success(self._inject_room_member("@resident%i:hs" % i))
  44. events_to_filter = []
  45. for i in range(0, 10):
  46. user = "@user%i:%s" % (i, "test_server" if i == 5 else "other_server")
  47. evt = self.get_success(
  48. self._inject_room_member(user, extra_content={"a": "b"})
  49. )
  50. events_to_filter.append(evt)
  51. filtered = self.get_success(
  52. filter_events_for_server(self.storage, "test_server", events_to_filter)
  53. )
  54. # the result should be 5 redacted events, and 5 unredacted events.
  55. for i in range(0, 5):
  56. self.assertEqual(events_to_filter[i].event_id, filtered[i].event_id)
  57. self.assertNotIn("a", filtered[i].content)
  58. for i in range(5, 10):
  59. self.assertEqual(events_to_filter[i].event_id, filtered[i].event_id)
  60. self.assertEqual(filtered[i].content["a"], "b")
  61. def test_erased_user(self) -> None:
  62. # 4 message events, from erased and unerased users, with a membership
  63. # change in the middle of them.
  64. events_to_filter = []
  65. evt = self.get_success(self._inject_message("@unerased:local_hs"))
  66. events_to_filter.append(evt)
  67. evt = self.get_success(self._inject_message("@erased:local_hs"))
  68. events_to_filter.append(evt)
  69. evt = self.get_success(self._inject_room_member("@joiner:remote_hs"))
  70. events_to_filter.append(evt)
  71. evt = self.get_success(self._inject_message("@unerased:local_hs"))
  72. events_to_filter.append(evt)
  73. evt = self.get_success(self._inject_message("@erased:local_hs"))
  74. events_to_filter.append(evt)
  75. # the erasey user gets erased
  76. self.get_success(
  77. self.hs.get_datastores().main.mark_user_erased("@erased:local_hs")
  78. )
  79. # ... and the filtering happens.
  80. filtered = self.get_success(
  81. filter_events_for_server(self.storage, "test_server", events_to_filter)
  82. )
  83. for i in range(0, len(events_to_filter)):
  84. self.assertEqual(
  85. events_to_filter[i].event_id,
  86. filtered[i].event_id,
  87. "Unexpected event at result position %i" % (i,),
  88. )
  89. for i in (0, 3):
  90. self.assertEqual(
  91. events_to_filter[i].content["body"],
  92. filtered[i].content["body"],
  93. "Unexpected event content at result position %i" % (i,),
  94. )
  95. for i in (1, 4):
  96. self.assertNotIn("body", filtered[i].content)
  97. def _inject_visibility(self, user_id: str, visibility: str) -> EventBase:
  98. content = {"history_visibility": visibility}
  99. builder = self.event_builder_factory.for_room_version(
  100. RoomVersions.V1,
  101. {
  102. "type": "m.room.history_visibility",
  103. "sender": user_id,
  104. "state_key": "",
  105. "room_id": TEST_ROOM_ID,
  106. "content": content,
  107. },
  108. )
  109. event, context = self.get_success(
  110. self.event_creation_handler.create_new_client_event(builder)
  111. )
  112. self.get_success(self.storage.persistence.persist_event(event, context))
  113. return event
  114. def _inject_room_member(
  115. self,
  116. user_id: str,
  117. membership: str = "join",
  118. extra_content: Optional[JsonDict] = None,
  119. ) -> EventBase:
  120. content = {"membership": membership}
  121. content.update(extra_content or {})
  122. builder = self.event_builder_factory.for_room_version(
  123. RoomVersions.V1,
  124. {
  125. "type": "m.room.member",
  126. "sender": user_id,
  127. "state_key": user_id,
  128. "room_id": TEST_ROOM_ID,
  129. "content": content,
  130. },
  131. )
  132. event, context = self.get_success(
  133. self.event_creation_handler.create_new_client_event(builder)
  134. )
  135. self.get_success(self.storage.persistence.persist_event(event, context))
  136. return event
  137. def _inject_message(
  138. self, user_id: str, content: Optional[JsonDict] = None
  139. ) -> EventBase:
  140. if content is None:
  141. content = {"body": "testytest", "msgtype": "m.text"}
  142. builder = self.event_builder_factory.for_room_version(
  143. RoomVersions.V1,
  144. {
  145. "type": "m.room.message",
  146. "sender": user_id,
  147. "room_id": TEST_ROOM_ID,
  148. "content": content,
  149. },
  150. )
  151. event, context = self.get_success(
  152. self.event_creation_handler.create_new_client_event(builder)
  153. )
  154. self.get_success(self.storage.persistence.persist_event(event, context))
  155. return event
  156. class FilterEventsForClientTestCase(unittest.FederatingHomeserverTestCase):
  157. def test_out_of_band_invite_rejection(self):
  158. # this is where we have received an invite event over federation, and then
  159. # rejected it.
  160. invite_pdu = {
  161. "room_id": "!room:id",
  162. "depth": 1,
  163. "auth_events": [],
  164. "prev_events": [],
  165. "origin_server_ts": 1,
  166. "sender": "@someone:" + self.OTHER_SERVER_NAME,
  167. "type": "m.room.member",
  168. "state_key": "@user:test",
  169. "content": {"membership": "invite"},
  170. }
  171. self.add_hashes_and_signatures(invite_pdu)
  172. invite_event_id = make_event_from_dict(invite_pdu, RoomVersions.V9).event_id
  173. self.get_success(
  174. self.hs.get_federation_server().on_invite_request(
  175. self.OTHER_SERVER_NAME,
  176. invite_pdu,
  177. "9",
  178. )
  179. )
  180. # stub out do_remotely_reject_invite so that we fall back to a locally-
  181. # generated rejection
  182. with patch.object(
  183. self.hs.get_federation_handler(),
  184. "do_remotely_reject_invite",
  185. side_effect=Exception(),
  186. ):
  187. reject_event_id, _ = self.get_success(
  188. self.hs.get_room_member_handler().remote_reject_invite(
  189. invite_event_id,
  190. txn_id=None,
  191. requester=create_requester("@user:test"),
  192. content={},
  193. )
  194. )
  195. invite_event, reject_event = self.get_success(
  196. self.hs.get_datastores().main.get_events_as_list(
  197. [invite_event_id, reject_event_id]
  198. )
  199. )
  200. # the invited user should be able to see both the invite and the rejection
  201. self.assertEqual(
  202. self.get_success(
  203. filter_events_for_client(
  204. self.hs.get_storage(), "@user:test", [invite_event, reject_event]
  205. )
  206. ),
  207. [invite_event, reject_event],
  208. )
  209. # other users should see neither
  210. self.assertEqual(
  211. self.get_success(
  212. filter_events_for_client(
  213. self.hs.get_storage(), "@other:test", [invite_event, reject_event]
  214. )
  215. ),
  216. [],
  217. )