test_retention.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. # Copyright 2019 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. from typing import Any, Dict
  15. from unittest.mock import Mock
  16. from twisted.test.proto_helpers import MemoryReactor
  17. from synapse.api.constants import EventTypes
  18. from synapse.rest import admin
  19. from synapse.rest.client import login, room
  20. from synapse.server import HomeServer
  21. from synapse.types import JsonDict, create_requester
  22. from synapse.util import Clock
  23. from synapse.visibility import filter_events_for_client
  24. from tests import unittest
  25. from tests.unittest import override_config
  26. one_hour_ms = 3600000
  27. one_day_ms = one_hour_ms * 24
  28. class RetentionTestCase(unittest.HomeserverTestCase):
  29. servlets = [
  30. admin.register_servlets,
  31. login.register_servlets,
  32. room.register_servlets,
  33. ]
  34. def make_homeserver(self, reactor: MemoryReactor, clock: Clock) -> HomeServer:
  35. config = self.default_config()
  36. # merge this default retention config with anything that was specified in
  37. # @override_config
  38. retention_config = {
  39. "enabled": True,
  40. "default_policy": {
  41. "min_lifetime": one_day_ms,
  42. "max_lifetime": one_day_ms * 3,
  43. },
  44. "allowed_lifetime_min": one_day_ms,
  45. "allowed_lifetime_max": one_day_ms * 3,
  46. }
  47. retention_config.update(config.get("retention", {}))
  48. config["retention"] = retention_config
  49. self.hs = self.setup_test_homeserver(config=config)
  50. return self.hs
  51. def prepare(self, reactor: MemoryReactor, clock: Clock, hs: HomeServer) -> None:
  52. self.user_id = self.register_user("user", "password")
  53. self.token = self.login("user", "password")
  54. self.store = self.hs.get_datastores().main
  55. self.serializer = self.hs.get_event_client_serializer()
  56. self.clock = self.hs.get_clock()
  57. def test_retention_event_purged_with_state_event(self) -> None:
  58. """Tests that expired events are correctly purged when the room's retention policy
  59. is defined by a state event.
  60. """
  61. room_id = self.helper.create_room_as(self.user_id, tok=self.token)
  62. # Set the room's retention period to 2 days.
  63. lifetime = one_day_ms * 2
  64. self.helper.send_state(
  65. room_id=room_id,
  66. event_type=EventTypes.Retention,
  67. body={"max_lifetime": lifetime},
  68. tok=self.token,
  69. )
  70. self._test_retention_event_purged(room_id, one_day_ms * 1.5)
  71. def test_retention_event_purged_with_state_event_outside_allowed(self) -> None:
  72. """Tests that the server configuration can override the policy for a room when
  73. running the purge jobs.
  74. """
  75. room_id = self.helper.create_room_as(self.user_id, tok=self.token)
  76. # Set a max_lifetime higher than the maximum allowed value.
  77. self.helper.send_state(
  78. room_id=room_id,
  79. event_type=EventTypes.Retention,
  80. body={"max_lifetime": one_day_ms * 4},
  81. tok=self.token,
  82. )
  83. # Check that the event is purged after waiting for the maximum allowed duration
  84. # instead of the one specified in the room's policy.
  85. self._test_retention_event_purged(room_id, one_day_ms * 1.5)
  86. # Set a max_lifetime lower than the minimum allowed value.
  87. self.helper.send_state(
  88. room_id=room_id,
  89. event_type=EventTypes.Retention,
  90. body={"max_lifetime": one_hour_ms},
  91. tok=self.token,
  92. )
  93. # Check that the event is purged after waiting for the minimum allowed duration
  94. # instead of the one specified in the room's policy.
  95. self._test_retention_event_purged(room_id, one_day_ms * 0.5)
  96. def test_retention_event_purged_without_state_event(self) -> None:
  97. """Tests that expired events are correctly purged when the room's retention policy
  98. is defined by the server's configuration's default retention policy.
  99. """
  100. room_id = self.helper.create_room_as(self.user_id, tok=self.token)
  101. self._test_retention_event_purged(room_id, one_day_ms * 2)
  102. @override_config({"retention": {"purge_jobs": [{"interval": "5d"}]}})
  103. def test_visibility(self) -> None:
  104. """Tests that synapse.visibility.filter_events_for_client correctly filters out
  105. outdated events, even if the purge job hasn't got to them yet.
  106. We do this by setting a very long time between purge jobs.
  107. """
  108. store = self.hs.get_datastores().main
  109. storage_controllers = self.hs.get_storage_controllers()
  110. room_id = self.helper.create_room_as(self.user_id, tok=self.token)
  111. # Send a first event, which should be filtered out at the end of the test.
  112. resp = self.helper.send(room_id=room_id, body="1", tok=self.token)
  113. first_event_id = resp.get("event_id")
  114. # Advance the time by 2 days. We're using the default retention policy, therefore
  115. # after this the first event will still be valid.
  116. self.reactor.advance(one_day_ms * 2 / 1000)
  117. # Send another event, which shouldn't get filtered out.
  118. resp = self.helper.send(room_id=room_id, body="2", tok=self.token)
  119. valid_event_id = resp.get("event_id")
  120. # Advance the time by another 2 days. After this, the first event should be
  121. # outdated but not the second one.
  122. self.reactor.advance(one_day_ms * 2 / 1000)
  123. # Fetch the events, and run filter_events_for_client on them
  124. events = self.get_success(
  125. store.get_events_as_list([first_event_id, valid_event_id])
  126. )
  127. self.assertEqual(2, len(events), "events retrieved from database")
  128. filtered_events = self.get_success(
  129. filter_events_for_client(storage_controllers, self.user_id, events)
  130. )
  131. # We should only get one event back.
  132. self.assertEqual(len(filtered_events), 1, filtered_events)
  133. # That event should be the second, not outdated event.
  134. self.assertEqual(filtered_events[0].event_id, valid_event_id, filtered_events)
  135. def _test_retention_event_purged(self, room_id: str, increment: float) -> None:
  136. """Run the following test scenario to test the message retention policy support:
  137. 1. Send event 1
  138. 2. Increment time by `increment`
  139. 3. Send event 2
  140. 4. Increment time by `increment`
  141. 5. Check that event 1 has been purged
  142. 6. Check that event 2 has not been purged
  143. 7. Check that state events that were sent before event 1 aren't purged.
  144. The main reason for sending a second event is because currently Synapse won't
  145. purge the latest message in a room because it would otherwise result in a lack of
  146. forward extremities for this room. It's also a good thing to ensure the purge jobs
  147. aren't too greedy and purge messages they shouldn't.
  148. Args:
  149. room_id: The ID of the room to test retention in.
  150. increment: The number of milliseconds to advance the clock each time. Must be
  151. defined so that events in the room aren't purged if they are `increment`
  152. old but are purged if they are `increment * 2` old.
  153. """
  154. # Get the create event to, later, check that we can still access it.
  155. message_handler = self.hs.get_message_handler()
  156. create_event = self.get_success(
  157. message_handler.get_room_data(
  158. create_requester(self.user_id), room_id, EventTypes.Create, state_key=""
  159. )
  160. )
  161. # Send a first event to the room. This is the event we'll want to be purged at the
  162. # end of the test.
  163. resp = self.helper.send(room_id=room_id, body="1", tok=self.token)
  164. expired_event_id = resp.get("event_id")
  165. assert expired_event_id is not None
  166. # Check that we can retrieve the event.
  167. expired_event = self.get_event(expired_event_id)
  168. self.assertEqual(
  169. expired_event.get("content", {}).get("body"), "1", expired_event
  170. )
  171. # Advance the time.
  172. self.reactor.advance(increment / 1000)
  173. # Send another event. We need this because the purge job won't purge the most
  174. # recent event in the room.
  175. resp = self.helper.send(room_id=room_id, body="2", tok=self.token)
  176. valid_event_id = resp.get("event_id")
  177. assert valid_event_id is not None
  178. # Advance the time again. Now our first event should have expired but our second
  179. # one should still be kept.
  180. self.reactor.advance(increment / 1000)
  181. # Check that the first event has been purged from the database, i.e. that we
  182. # can't retrieve it anymore, because it has expired.
  183. self.get_event(expired_event_id, expect_none=True)
  184. # Check that the event that hasn't expired can still be retrieved.
  185. valid_event = self.get_event(valid_event_id)
  186. self.assertEqual(valid_event.get("content", {}).get("body"), "2", valid_event)
  187. # Check that we can still access state events that were sent before the event that
  188. # has been purged.
  189. self.get_event(room_id, create_event.event_id)
  190. def get_event(self, event_id: str, expect_none: bool = False) -> JsonDict:
  191. event = self.get_success(self.store.get_event(event_id, allow_none=True))
  192. if expect_none:
  193. self.assertIsNone(event)
  194. return {}
  195. self.assertIsNotNone(event)
  196. time_now = self.clock.time_msec()
  197. serialized = self.serializer.serialize_event(event, time_now)
  198. return serialized
  199. class RetentionNoDefaultPolicyTestCase(unittest.HomeserverTestCase):
  200. servlets = [
  201. admin.register_servlets,
  202. login.register_servlets,
  203. room.register_servlets,
  204. ]
  205. def default_config(self) -> Dict[str, Any]:
  206. config = super().default_config()
  207. retention_config = {
  208. "enabled": True,
  209. }
  210. # Update this config with what's in the default config so that
  211. # override_config works as expected.
  212. retention_config.update(config.get("retention", {}))
  213. config["retention"] = retention_config
  214. return config
  215. def make_homeserver(self, reactor: MemoryReactor, clock: Clock) -> HomeServer:
  216. mock_federation_client = Mock(spec=["backfill"])
  217. self.hs = self.setup_test_homeserver(
  218. federation_client=mock_federation_client,
  219. )
  220. return self.hs
  221. def prepare(self, reactor: MemoryReactor, clock: Clock, hs: HomeServer) -> None:
  222. self.user_id = self.register_user("user", "password")
  223. self.token = self.login("user", "password")
  224. def test_no_default_policy(self) -> None:
  225. """Tests that an event doesn't get expired if there is neither a default retention
  226. policy nor a policy specific to the room.
  227. """
  228. room_id = self.helper.create_room_as(self.user_id, tok=self.token)
  229. self._test_retention(room_id)
  230. def test_state_policy(self) -> None:
  231. """Tests that an event gets correctly expired if there is no default retention
  232. policy but there's a policy specific to the room.
  233. """
  234. room_id = self.helper.create_room_as(self.user_id, tok=self.token)
  235. # Set the maximum lifetime to 35 days so that the first event gets expired but not
  236. # the second one.
  237. self.helper.send_state(
  238. room_id=room_id,
  239. event_type=EventTypes.Retention,
  240. body={"max_lifetime": one_day_ms * 35},
  241. tok=self.token,
  242. )
  243. self._test_retention(room_id, expected_code_for_first_event=404)
  244. @unittest.override_config({"retention": {"enabled": False}})
  245. def test_visibility_when_disabled(self) -> None:
  246. """Retention policies should be ignored when the retention feature is disabled."""
  247. room_id = self.helper.create_room_as(self.user_id, tok=self.token)
  248. self.helper.send_state(
  249. room_id=room_id,
  250. event_type=EventTypes.Retention,
  251. body={"max_lifetime": one_day_ms},
  252. tok=self.token,
  253. )
  254. resp = self.helper.send(room_id=room_id, body="test", tok=self.token)
  255. self.reactor.advance(one_day_ms * 2 / 1000)
  256. self.get_event(room_id, resp["event_id"])
  257. def _test_retention(
  258. self, room_id: str, expected_code_for_first_event: int = 200
  259. ) -> None:
  260. # Send a first event to the room. This is the event we'll want to be purged at the
  261. # end of the test.
  262. resp = self.helper.send(room_id=room_id, body="1", tok=self.token)
  263. first_event_id = resp.get("event_id")
  264. assert first_event_id is not None
  265. # Check that we can retrieve the event.
  266. expired_event = self.get_event(room_id, first_event_id)
  267. self.assertEqual(
  268. expired_event.get("content", {}).get("body"), "1", expired_event
  269. )
  270. # Advance the time by a month.
  271. self.reactor.advance(one_day_ms * 30 / 1000)
  272. # Send another event. We need this because the purge job won't purge the most
  273. # recent event in the room.
  274. resp = self.helper.send(room_id=room_id, body="2", tok=self.token)
  275. second_event_id = resp.get("event_id")
  276. assert second_event_id is not None
  277. # Advance the time by another month.
  278. self.reactor.advance(one_day_ms * 30 / 1000)
  279. # Check if the event has been purged from the database.
  280. first_event = self.get_event(
  281. room_id, first_event_id, expected_code=expected_code_for_first_event
  282. )
  283. if expected_code_for_first_event == 200:
  284. self.assertEqual(
  285. first_event.get("content", {}).get("body"), "1", first_event
  286. )
  287. # Check that the event that hasn't been purged can still be retrieved.
  288. second_event = self.get_event(room_id, second_event_id)
  289. self.assertEqual(second_event.get("content", {}).get("body"), "2", second_event)
  290. def get_event(
  291. self, room_id: str, event_id: str, expected_code: int = 200
  292. ) -> JsonDict:
  293. url = "/_matrix/client/r0/rooms/%s/event/%s" % (room_id, event_id)
  294. channel = self.make_request("GET", url, access_token=self.token)
  295. self.assertEqual(channel.code, expected_code, channel.result)
  296. return channel.json_body