1
0

test_admin.py 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. # Copyright 2019 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 collections import Counter
  15. from unittest.mock import Mock
  16. from twisted.test.proto_helpers import MemoryReactor
  17. import synapse.rest.admin
  18. import synapse.storage
  19. from synapse.api.constants import EventTypes, JoinRules
  20. from synapse.api.room_versions import RoomVersions
  21. from synapse.rest.client import knock, login, room
  22. from synapse.server import HomeServer
  23. from synapse.util import Clock
  24. from tests import unittest
  25. class ExfiltrateData(unittest.HomeserverTestCase):
  26. servlets = [
  27. synapse.rest.admin.register_servlets_for_client_rest_resource,
  28. login.register_servlets,
  29. room.register_servlets,
  30. knock.register_servlets,
  31. ]
  32. def prepare(self, reactor: MemoryReactor, clock: Clock, hs: HomeServer) -> None:
  33. self.admin_handler = hs.get_admin_handler()
  34. self.user1 = self.register_user("user1", "password")
  35. self.token1 = self.login("user1", "password")
  36. self.user2 = self.register_user("user2", "password")
  37. self.token2 = self.login("user2", "password")
  38. def test_single_public_joined_room(self) -> None:
  39. """Test that we write *all* events for a public room"""
  40. room_id = self.helper.create_room_as(
  41. self.user1, tok=self.token1, is_public=True
  42. )
  43. self.helper.send(room_id, body="Hello!", tok=self.token1)
  44. self.helper.join(room_id, self.user2, tok=self.token2)
  45. self.helper.send(room_id, body="Hello again!", tok=self.token1)
  46. writer = Mock()
  47. self.get_success(self.admin_handler.export_user_data(self.user2, writer))
  48. writer.write_events.assert_called()
  49. # Since we can see all events there shouldn't be any extremities, so no
  50. # state should be written
  51. writer.write_state.assert_not_called()
  52. # Collect all events that were written
  53. written_events = []
  54. for (called_room_id, events), _ in writer.write_events.call_args_list:
  55. self.assertEqual(called_room_id, room_id)
  56. written_events.extend(events)
  57. # Check that the right number of events were written
  58. counter = Counter(
  59. (event.type, getattr(event, "state_key", None)) for event in written_events
  60. )
  61. self.assertEqual(counter[(EventTypes.Message, None)], 2)
  62. self.assertEqual(counter[(EventTypes.Member, self.user1)], 1)
  63. self.assertEqual(counter[(EventTypes.Member, self.user2)], 1)
  64. def test_single_private_joined_room(self) -> None:
  65. """Tests that we correctly write state when we can't see all events in
  66. a room.
  67. """
  68. room_id = self.helper.create_room_as(self.user1, tok=self.token1)
  69. self.helper.send_state(
  70. room_id,
  71. EventTypes.RoomHistoryVisibility,
  72. body={"history_visibility": "joined"},
  73. tok=self.token1,
  74. )
  75. self.helper.send(room_id, body="Hello!", tok=self.token1)
  76. self.helper.join(room_id, self.user2, tok=self.token2)
  77. self.helper.send(room_id, body="Hello again!", tok=self.token1)
  78. writer = Mock()
  79. self.get_success(self.admin_handler.export_user_data(self.user2, writer))
  80. writer.write_events.assert_called()
  81. # Since we can't see all events there should be one extremity.
  82. writer.write_state.assert_called_once()
  83. # Collect all events that were written
  84. written_events = []
  85. for (called_room_id, events), _ in writer.write_events.call_args_list:
  86. self.assertEqual(called_room_id, room_id)
  87. written_events.extend(events)
  88. # Check that the right number of events were written
  89. counter = Counter(
  90. (event.type, getattr(event, "state_key", None)) for event in written_events
  91. )
  92. self.assertEqual(counter[(EventTypes.Message, None)], 1)
  93. self.assertEqual(counter[(EventTypes.Member, self.user1)], 1)
  94. self.assertEqual(counter[(EventTypes.Member, self.user2)], 1)
  95. def test_single_left_room(self) -> None:
  96. """Tests that we don't see events in the room after we leave."""
  97. room_id = self.helper.create_room_as(self.user1, tok=self.token1)
  98. self.helper.send(room_id, body="Hello!", tok=self.token1)
  99. self.helper.join(room_id, self.user2, tok=self.token2)
  100. self.helper.send(room_id, body="Hello again!", tok=self.token1)
  101. self.helper.leave(room_id, self.user2, tok=self.token2)
  102. self.helper.send(room_id, body="Helloooooo!", tok=self.token1)
  103. writer = Mock()
  104. self.get_success(self.admin_handler.export_user_data(self.user2, writer))
  105. writer.write_events.assert_called()
  106. # Since we can see all events there shouldn't be any extremities, so no
  107. # state should be written
  108. writer.write_state.assert_not_called()
  109. written_events = []
  110. for (called_room_id, events), _ in writer.write_events.call_args_list:
  111. self.assertEqual(called_room_id, room_id)
  112. written_events.extend(events)
  113. # Check that the right number of events were written
  114. counter = Counter(
  115. (event.type, getattr(event, "state_key", None)) for event in written_events
  116. )
  117. self.assertEqual(counter[(EventTypes.Message, None)], 2)
  118. self.assertEqual(counter[(EventTypes.Member, self.user1)], 1)
  119. self.assertEqual(counter[(EventTypes.Member, self.user2)], 2)
  120. def test_single_left_rejoined_private_room(self) -> None:
  121. """Tests that see the correct events in private rooms when we
  122. repeatedly join and leave.
  123. """
  124. room_id = self.helper.create_room_as(self.user1, tok=self.token1)
  125. self.helper.send_state(
  126. room_id,
  127. EventTypes.RoomHistoryVisibility,
  128. body={"history_visibility": "joined"},
  129. tok=self.token1,
  130. )
  131. self.helper.send(room_id, body="Hello!", tok=self.token1)
  132. self.helper.join(room_id, self.user2, tok=self.token2)
  133. self.helper.send(room_id, body="Hello again!", tok=self.token1)
  134. self.helper.leave(room_id, self.user2, tok=self.token2)
  135. self.helper.send(room_id, body="Helloooooo!", tok=self.token1)
  136. self.helper.join(room_id, self.user2, tok=self.token2)
  137. self.helper.send(room_id, body="Helloooooo!!", tok=self.token1)
  138. writer = Mock()
  139. self.get_success(self.admin_handler.export_user_data(self.user2, writer))
  140. writer.write_events.assert_called_once()
  141. # Since we joined/left/joined again we expect there to be two gaps.
  142. self.assertEqual(writer.write_state.call_count, 2)
  143. written_events = []
  144. for (called_room_id, events), _ in writer.write_events.call_args_list:
  145. self.assertEqual(called_room_id, room_id)
  146. written_events.extend(events)
  147. # Check that the right number of events were written
  148. counter = Counter(
  149. (event.type, getattr(event, "state_key", None)) for event in written_events
  150. )
  151. self.assertEqual(counter[(EventTypes.Message, None)], 2)
  152. self.assertEqual(counter[(EventTypes.Member, self.user1)], 1)
  153. self.assertEqual(counter[(EventTypes.Member, self.user2)], 3)
  154. def test_invite(self) -> None:
  155. """Tests that pending invites get handled correctly."""
  156. room_id = self.helper.create_room_as(self.user1, tok=self.token1)
  157. self.helper.send(room_id, body="Hello!", tok=self.token1)
  158. self.helper.invite(room_id, self.user1, self.user2, tok=self.token1)
  159. writer = Mock()
  160. self.get_success(self.admin_handler.export_user_data(self.user2, writer))
  161. writer.write_events.assert_not_called()
  162. writer.write_state.assert_not_called()
  163. writer.write_invite.assert_called_once()
  164. args = writer.write_invite.call_args[0]
  165. self.assertEqual(args[0], room_id)
  166. self.assertEqual(args[1].content["membership"], "invite")
  167. self.assertTrue(args[2]) # Assert there is at least one bit of state
  168. def test_knock(self) -> None:
  169. """Tests that knock get handled correctly."""
  170. # create a knockable v7 room
  171. room_id = self.helper.create_room_as(
  172. self.user1, room_version=RoomVersions.V7.identifier, tok=self.token1
  173. )
  174. self.helper.send_state(
  175. room_id,
  176. EventTypes.JoinRules,
  177. {"join_rule": JoinRules.KNOCK},
  178. tok=self.token1,
  179. )
  180. self.helper.send(room_id, body="Hello!", tok=self.token1)
  181. self.helper.knock(room_id, self.user2, tok=self.token2)
  182. writer = Mock()
  183. self.get_success(self.admin_handler.export_user_data(self.user2, writer))
  184. writer.write_events.assert_not_called()
  185. writer.write_state.assert_not_called()
  186. writer.write_knock.assert_called_once()
  187. args = writer.write_knock.call_args[0]
  188. self.assertEqual(args[0], room_id)
  189. self.assertEqual(args[1].content["membership"], "knock")
  190. self.assertTrue(args[2]) # Assert there is at least one bit of state