1
0

test_admin.py 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2019 The Matrix.org Foundation C.I.C.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. from collections import Counter
  16. from mock import Mock
  17. import synapse.api.errors
  18. import synapse.handlers.admin
  19. import synapse.rest.admin
  20. import synapse.storage
  21. from synapse.api.constants import EventTypes
  22. from synapse.rest.client.v1 import login, room
  23. from tests import unittest
  24. class ExfiltrateData(unittest.HomeserverTestCase):
  25. servlets = [
  26. synapse.rest.admin.register_servlets_for_client_rest_resource,
  27. login.register_servlets,
  28. room.register_servlets,
  29. ]
  30. def prepare(self, reactor, clock, hs):
  31. self.admin_handler = hs.get_handlers().admin_handler
  32. self.user1 = self.register_user("user1", "password")
  33. self.token1 = self.login("user1", "password")
  34. self.user2 = self.register_user("user2", "password")
  35. self.token2 = self.login("user2", "password")
  36. def test_single_public_joined_room(self):
  37. """Test that we write *all* events for a public room
  38. """
  39. room_id = self.helper.create_room_as(
  40. self.user1, tok=self.token1, is_public=True
  41. )
  42. self.helper.send(room_id, body="Hello!", tok=self.token1)
  43. self.helper.join(room_id, self.user2, tok=self.token2)
  44. self.helper.send(room_id, body="Hello again!", tok=self.token1)
  45. writer = Mock()
  46. self.get_success(self.admin_handler.export_user_data(self.user2, writer))
  47. writer.write_events.assert_called()
  48. # Since we can see all events there shouldn't be any extremities, so no
  49. # state should be written
  50. writer.write_state.assert_not_called()
  51. # Collect all events that were written
  52. written_events = []
  53. for (called_room_id, events), _ in writer.write_events.call_args_list:
  54. self.assertEqual(called_room_id, room_id)
  55. written_events.extend(events)
  56. # Check that the right number of events were written
  57. counter = Counter(
  58. (event.type, getattr(event, "state_key", None)) for event in written_events
  59. )
  60. self.assertEqual(counter[(EventTypes.Message, None)], 2)
  61. self.assertEqual(counter[(EventTypes.Member, self.user1)], 1)
  62. self.assertEqual(counter[(EventTypes.Member, self.user2)], 1)
  63. def test_single_private_joined_room(self):
  64. """Tests that we correctly write state when we can't see all events in
  65. a room.
  66. """
  67. room_id = self.helper.create_room_as(self.user1, tok=self.token1)
  68. self.helper.send_state(
  69. room_id,
  70. EventTypes.RoomHistoryVisibility,
  71. body={"history_visibility": "joined"},
  72. tok=self.token1,
  73. )
  74. self.helper.send(room_id, body="Hello!", tok=self.token1)
  75. self.helper.join(room_id, self.user2, tok=self.token2)
  76. self.helper.send(room_id, body="Hello again!", tok=self.token1)
  77. writer = Mock()
  78. self.get_success(self.admin_handler.export_user_data(self.user2, writer))
  79. writer.write_events.assert_called()
  80. # Since we can't see all events there should be one extremity.
  81. writer.write_state.assert_called_once()
  82. # Collect all events that were written
  83. written_events = []
  84. for (called_room_id, events), _ in writer.write_events.call_args_list:
  85. self.assertEqual(called_room_id, room_id)
  86. written_events.extend(events)
  87. # Check that the right number of events were written
  88. counter = Counter(
  89. (event.type, getattr(event, "state_key", None)) for event in written_events
  90. )
  91. self.assertEqual(counter[(EventTypes.Message, None)], 1)
  92. self.assertEqual(counter[(EventTypes.Member, self.user1)], 1)
  93. self.assertEqual(counter[(EventTypes.Member, self.user2)], 1)
  94. def test_single_left_room(self):
  95. """Tests that we don't see events in the room after we leave.
  96. """
  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):
  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):
  155. """Tests that pending invites get handled correctly.
  156. """
  157. room_id = self.helper.create_room_as(self.user1, tok=self.token1)
  158. self.helper.send(room_id, body="Hello!", tok=self.token1)
  159. self.helper.invite(room_id, self.user1, self.user2, tok=self.token1)
  160. writer = Mock()
  161. self.get_success(self.admin_handler.export_user_data(self.user2, writer))
  162. writer.write_events.assert_not_called()
  163. writer.write_state.assert_not_called()
  164. writer.write_invite.assert_called_once()
  165. args = writer.write_invite.call_args[0]
  166. self.assertEqual(args[0], room_id)
  167. self.assertEqual(args[1].content["membership"], "invite")
  168. self.assertTrue(args[2]) # Assert there is at least one bit of state