test_retention.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2019 New Vector Ltd
  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 mock import Mock
  16. from synapse.api.constants import EventTypes
  17. from synapse.rest import admin
  18. from synapse.rest.client.v1 import login, room
  19. from synapse.visibility import filter_events_for_client
  20. from tests import unittest
  21. one_hour_ms = 3600000
  22. one_day_ms = one_hour_ms * 24
  23. class RetentionTestCase(unittest.HomeserverTestCase):
  24. servlets = [
  25. admin.register_servlets,
  26. login.register_servlets,
  27. room.register_servlets,
  28. ]
  29. def make_homeserver(self, reactor, clock):
  30. config = self.default_config()
  31. config["retention"] = {
  32. "enabled": True,
  33. "default_policy": {
  34. "min_lifetime": one_day_ms,
  35. "max_lifetime": one_day_ms * 3,
  36. },
  37. "allowed_lifetime_min": one_day_ms,
  38. "allowed_lifetime_max": one_day_ms * 3,
  39. }
  40. self.hs = self.setup_test_homeserver(config=config)
  41. return self.hs
  42. def prepare(self, reactor, clock, homeserver):
  43. self.user_id = self.register_user("user", "password")
  44. self.token = self.login("user", "password")
  45. def test_retention_state_event(self):
  46. """Tests that the server configuration can limit the values a user can set to the
  47. room's retention policy.
  48. """
  49. room_id = self.helper.create_room_as(self.user_id, tok=self.token)
  50. self.helper.send_state(
  51. room_id=room_id,
  52. event_type=EventTypes.Retention,
  53. body={"max_lifetime": one_day_ms * 4},
  54. tok=self.token,
  55. expect_code=400,
  56. )
  57. self.helper.send_state(
  58. room_id=room_id,
  59. event_type=EventTypes.Retention,
  60. body={"max_lifetime": one_hour_ms},
  61. tok=self.token,
  62. expect_code=400,
  63. )
  64. def test_retention_event_purged_with_state_event(self):
  65. """Tests that expired events are correctly purged when the room's retention policy
  66. is defined by a state event.
  67. """
  68. room_id = self.helper.create_room_as(self.user_id, tok=self.token)
  69. # Set the room's retention period to 2 days.
  70. lifetime = one_day_ms * 2
  71. self.helper.send_state(
  72. room_id=room_id,
  73. event_type=EventTypes.Retention,
  74. body={"max_lifetime": lifetime},
  75. tok=self.token,
  76. )
  77. self._test_retention_event_purged(room_id, one_day_ms * 1.5)
  78. def test_retention_event_purged_without_state_event(self):
  79. """Tests that expired events are correctly purged when the room's retention policy
  80. is defined by the server's configuration's default retention policy.
  81. """
  82. room_id = self.helper.create_room_as(self.user_id, tok=self.token)
  83. self._test_retention_event_purged(room_id, one_day_ms * 2)
  84. def test_visibility(self):
  85. """Tests that synapse.visibility.filter_events_for_client correctly filters out
  86. outdated events
  87. """
  88. store = self.hs.get_datastore()
  89. storage = self.hs.get_storage()
  90. room_id = self.helper.create_room_as(self.user_id, tok=self.token)
  91. events = []
  92. # Send a first event, which should be filtered out at the end of the test.
  93. resp = self.helper.send(room_id=room_id, body="1", tok=self.token)
  94. # Get the event from the store so that we end up with a FrozenEvent that we can
  95. # give to filter_events_for_client. We need to do this now because the event won't
  96. # be in the database anymore after it has expired.
  97. events.append(self.get_success(store.get_event(resp.get("event_id"))))
  98. # Advance the time by 2 days. We're using the default retention policy, therefore
  99. # after this the first event will still be valid.
  100. self.reactor.advance(one_day_ms * 2 / 1000)
  101. # Send another event, which shouldn't get filtered out.
  102. resp = self.helper.send(room_id=room_id, body="2", tok=self.token)
  103. valid_event_id = resp.get("event_id")
  104. events.append(self.get_success(store.get_event(valid_event_id)))
  105. # Advance the time by anothe 2 days. After this, the first event should be
  106. # outdated but not the second one.
  107. self.reactor.advance(one_day_ms * 2 / 1000)
  108. # Run filter_events_for_client with our list of FrozenEvents.
  109. filtered_events = self.get_success(
  110. filter_events_for_client(storage, self.user_id, events)
  111. )
  112. # We should only get one event back.
  113. self.assertEqual(len(filtered_events), 1, filtered_events)
  114. # That event should be the second, not outdated event.
  115. self.assertEqual(filtered_events[0].event_id, valid_event_id, filtered_events)
  116. def _test_retention_event_purged(self, room_id, increment):
  117. # Get the create event to, later, check that we can still access it.
  118. message_handler = self.hs.get_message_handler()
  119. create_event = self.get_success(
  120. message_handler.get_room_data(self.user_id, room_id, EventTypes.Create)
  121. )
  122. # Send a first event to the room. This is the event we'll want to be purged at the
  123. # end of the test.
  124. resp = self.helper.send(room_id=room_id, body="1", tok=self.token)
  125. expired_event_id = resp.get("event_id")
  126. # Check that we can retrieve the event.
  127. expired_event = self.get_event(room_id, expired_event_id)
  128. self.assertEqual(
  129. expired_event.get("content", {}).get("body"), "1", expired_event
  130. )
  131. # Advance the time.
  132. self.reactor.advance(increment / 1000)
  133. # Send another event. We need this because the purge job won't purge the most
  134. # recent event in the room.
  135. resp = self.helper.send(room_id=room_id, body="2", tok=self.token)
  136. valid_event_id = resp.get("event_id")
  137. # Advance the time again. Now our first event should have expired but our second
  138. # one should still be kept.
  139. self.reactor.advance(increment / 1000)
  140. # Check that the event has been purged from the database.
  141. self.get_event(room_id, expired_event_id, expected_code=404)
  142. # Check that the event that hasn't been purged can still be retrieved.
  143. valid_event = self.get_event(room_id, valid_event_id)
  144. self.assertEqual(valid_event.get("content", {}).get("body"), "2", valid_event)
  145. # Check that we can still access state events that were sent before the event that
  146. # has been purged.
  147. self.get_event(room_id, create_event.event_id)
  148. def get_event(self, room_id, event_id, expected_code=200):
  149. url = "/_matrix/client/r0/rooms/%s/event/%s" % (room_id, event_id)
  150. request, channel = self.make_request("GET", url, access_token=self.token)
  151. self.render(request)
  152. self.assertEqual(channel.code, expected_code, channel.result)
  153. return channel.json_body
  154. class RetentionNoDefaultPolicyTestCase(unittest.HomeserverTestCase):
  155. servlets = [
  156. admin.register_servlets,
  157. login.register_servlets,
  158. room.register_servlets,
  159. ]
  160. def make_homeserver(self, reactor, clock):
  161. config = self.default_config()
  162. config["retention"] = {
  163. "enabled": True,
  164. }
  165. mock_federation_client = Mock(spec=["backfill"])
  166. self.hs = self.setup_test_homeserver(
  167. config=config, federation_client=mock_federation_client,
  168. )
  169. return self.hs
  170. def prepare(self, reactor, clock, homeserver):
  171. self.user_id = self.register_user("user", "password")
  172. self.token = self.login("user", "password")
  173. def test_no_default_policy(self):
  174. """Tests that an event doesn't get expired if there is neither a default retention
  175. policy nor a policy specific to the room.
  176. """
  177. room_id = self.helper.create_room_as(self.user_id, tok=self.token)
  178. self._test_retention(room_id)
  179. def test_state_policy(self):
  180. """Tests that an event gets correctly expired if there is no default retention
  181. policy but there's a policy specific to the room.
  182. """
  183. room_id = self.helper.create_room_as(self.user_id, tok=self.token)
  184. # Set the maximum lifetime to 35 days so that the first event gets expired but not
  185. # the second one.
  186. self.helper.send_state(
  187. room_id=room_id,
  188. event_type=EventTypes.Retention,
  189. body={"max_lifetime": one_day_ms * 35},
  190. tok=self.token,
  191. )
  192. self._test_retention(room_id, expected_code_for_first_event=404)
  193. def _test_retention(self, room_id, expected_code_for_first_event=200):
  194. # Send a first event to the room. This is the event we'll want to be purged at the
  195. # end of the test.
  196. resp = self.helper.send(room_id=room_id, body="1", tok=self.token)
  197. first_event_id = resp.get("event_id")
  198. # Check that we can retrieve the event.
  199. expired_event = self.get_event(room_id, first_event_id)
  200. self.assertEqual(
  201. expired_event.get("content", {}).get("body"), "1", expired_event
  202. )
  203. # Advance the time by a month.
  204. self.reactor.advance(one_day_ms * 30 / 1000)
  205. # Send another event. We need this because the purge job won't purge the most
  206. # recent event in the room.
  207. resp = self.helper.send(room_id=room_id, body="2", tok=self.token)
  208. second_event_id = resp.get("event_id")
  209. # Advance the time by another month.
  210. self.reactor.advance(one_day_ms * 30 / 1000)
  211. # Check if the event has been purged from the database.
  212. first_event = self.get_event(
  213. room_id, first_event_id, expected_code=expected_code_for_first_event
  214. )
  215. if expected_code_for_first_event == 200:
  216. self.assertEqual(
  217. first_event.get("content", {}).get("body"), "1", first_event
  218. )
  219. # Check that the event that hasn't been purged can still be retrieved.
  220. second_event = self.get_event(room_id, second_event_id)
  221. self.assertEqual(second_event.get("content", {}).get("body"), "2", second_event)
  222. def get_event(self, room_id, event_id, expected_code=200):
  223. url = "/_matrix/client/r0/rooms/%s/event/%s" % (room_id, event_id)
  224. request, channel = self.make_request("GET", url, access_token=self.token)
  225. self.render(request)
  226. self.assertEqual(channel.code, expected_code, channel.result)
  227. return channel.json_body