1
0

test_admin.py 8.2 KB

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