test_visibility.py 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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 synapse.api.room_versions import RoomVersions
  17. from synapse.events import EventBase
  18. from synapse.types import JsonDict
  19. from synapse.visibility import filter_events_for_server
  20. from tests import unittest
  21. from tests.utils import create_room
  22. logger = logging.getLogger(__name__)
  23. TEST_ROOM_ID = "!TEST:ROOM"
  24. class FilterEventsForServerTestCase(unittest.HomeserverTestCase):
  25. def setUp(self) -> None:
  26. super(FilterEventsForServerTestCase, self).setUp()
  27. self.event_creation_handler = self.hs.get_event_creation_handler()
  28. self.event_builder_factory = self.hs.get_event_builder_factory()
  29. self.storage = self.hs.get_storage()
  30. self.get_success(create_room(self.hs, TEST_ROOM_ID, "@someone:ROOM"))
  31. def test_filtering(self) -> None:
  32. #
  33. # The events to be filtered consist of 10 membership events (it doesn't
  34. # really matter if they are joins or leaves, so let's make them joins).
  35. # One of those membership events is going to be for a user on the
  36. # server we are filtering for (so we can check the filtering is doing
  37. # the right thing).
  38. #
  39. # before we do that, we persist some other events to act as state.
  40. self.get_success(self._inject_visibility("@admin:hs", "joined"))
  41. for i in range(0, 10):
  42. self.get_success(self._inject_room_member("@resident%i:hs" % i))
  43. events_to_filter = []
  44. for i in range(0, 10):
  45. user = "@user%i:%s" % (i, "test_server" if i == 5 else "other_server")
  46. evt = self.get_success(
  47. self._inject_room_member(user, extra_content={"a": "b"})
  48. )
  49. events_to_filter.append(evt)
  50. filtered = self.get_success(
  51. filter_events_for_server(self.storage, "test_server", events_to_filter)
  52. )
  53. # the result should be 5 redacted events, and 5 unredacted events.
  54. for i in range(0, 5):
  55. self.assertEqual(events_to_filter[i].event_id, filtered[i].event_id)
  56. self.assertNotIn("a", filtered[i].content)
  57. for i in range(5, 10):
  58. self.assertEqual(events_to_filter[i].event_id, filtered[i].event_id)
  59. self.assertEqual(filtered[i].content["a"], "b")
  60. def test_erased_user(self) -> None:
  61. # 4 message events, from erased and unerased users, with a membership
  62. # change in the middle of them.
  63. events_to_filter = []
  64. evt = self.get_success(self._inject_message("@unerased:local_hs"))
  65. events_to_filter.append(evt)
  66. evt = self.get_success(self._inject_message("@erased:local_hs"))
  67. events_to_filter.append(evt)
  68. evt = self.get_success(self._inject_room_member("@joiner:remote_hs"))
  69. events_to_filter.append(evt)
  70. evt = self.get_success(self._inject_message("@unerased:local_hs"))
  71. events_to_filter.append(evt)
  72. evt = self.get_success(self._inject_message("@erased:local_hs"))
  73. events_to_filter.append(evt)
  74. # the erasey user gets erased
  75. self.get_success(self.hs.get_datastore().mark_user_erased("@erased:local_hs"))
  76. # ... and the filtering happens.
  77. filtered = self.get_success(
  78. filter_events_for_server(self.storage, "test_server", events_to_filter)
  79. )
  80. for i in range(0, len(events_to_filter)):
  81. self.assertEqual(
  82. events_to_filter[i].event_id,
  83. filtered[i].event_id,
  84. "Unexpected event at result position %i" % (i,),
  85. )
  86. for i in (0, 3):
  87. self.assertEqual(
  88. events_to_filter[i].content["body"],
  89. filtered[i].content["body"],
  90. "Unexpected event content at result position %i" % (i,),
  91. )
  92. for i in (1, 4):
  93. self.assertNotIn("body", filtered[i].content)
  94. def _inject_visibility(self, user_id: str, visibility: str) -> EventBase:
  95. content = {"history_visibility": visibility}
  96. builder = self.event_builder_factory.for_room_version(
  97. RoomVersions.V1,
  98. {
  99. "type": "m.room.history_visibility",
  100. "sender": user_id,
  101. "state_key": "",
  102. "room_id": TEST_ROOM_ID,
  103. "content": content,
  104. },
  105. )
  106. event, context = self.get_success(
  107. self.event_creation_handler.create_new_client_event(builder)
  108. )
  109. self.get_success(self.storage.persistence.persist_event(event, context))
  110. return event
  111. def _inject_room_member(
  112. self,
  113. user_id: str,
  114. membership: str = "join",
  115. extra_content: Optional[JsonDict] = None,
  116. ) -> EventBase:
  117. content = {"membership": membership}
  118. content.update(extra_content or {})
  119. builder = self.event_builder_factory.for_room_version(
  120. RoomVersions.V1,
  121. {
  122. "type": "m.room.member",
  123. "sender": user_id,
  124. "state_key": user_id,
  125. "room_id": TEST_ROOM_ID,
  126. "content": content,
  127. },
  128. )
  129. event, context = self.get_success(
  130. self.event_creation_handler.create_new_client_event(builder)
  131. )
  132. self.get_success(self.storage.persistence.persist_event(event, context))
  133. return event
  134. def _inject_message(
  135. self, user_id: str, content: Optional[JsonDict] = None
  136. ) -> EventBase:
  137. if content is None:
  138. content = {"body": "testytest", "msgtype": "m.text"}
  139. builder = self.event_builder_factory.for_room_version(
  140. RoomVersions.V1,
  141. {
  142. "type": "m.room.message",
  143. "sender": user_id,
  144. "room_id": TEST_ROOM_ID,
  145. "content": content,
  146. },
  147. )
  148. event, context = self.get_success(
  149. self.event_creation_handler.create_new_client_event(builder)
  150. )
  151. self.get_success(self.storage.persistence.persist_event(event, context))
  152. return event