1
0

test_purge.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. # Copyright 2018 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 twisted.test.proto_helpers import MemoryReactor
  15. from synapse.api.errors import NotFoundError, SynapseError
  16. from synapse.rest.client import room
  17. from synapse.server import HomeServer
  18. from synapse.util import Clock
  19. from tests.unittest import HomeserverTestCase
  20. class PurgeTests(HomeserverTestCase):
  21. user_id = "@red:server"
  22. servlets = [room.register_servlets]
  23. def make_homeserver(self, reactor: MemoryReactor, clock: Clock) -> HomeServer:
  24. hs = self.setup_test_homeserver("server", federation_http_client=None)
  25. return hs
  26. def prepare(self, reactor: MemoryReactor, clock: Clock, hs: HomeServer) -> None:
  27. self.room_id = self.helper.create_room_as(self.user_id)
  28. self.store = hs.get_datastores().main
  29. self._storage_controllers = self.hs.get_storage_controllers()
  30. def test_purge_history(self) -> None:
  31. """
  32. Purging a room history will delete everything before the topological point.
  33. """
  34. # Send four messages to the room
  35. first = self.helper.send(self.room_id, body="test1")
  36. second = self.helper.send(self.room_id, body="test2")
  37. third = self.helper.send(self.room_id, body="test3")
  38. last = self.helper.send(self.room_id, body="test4")
  39. # Get the topological token
  40. token = self.get_success(
  41. self.store.get_topological_token_for_event(last["event_id"])
  42. )
  43. token_str = self.get_success(token.to_string(self.hs.get_datastores().main))
  44. # Purge everything before this topological token
  45. self.get_success(
  46. self._storage_controllers.purge_events.purge_history(
  47. self.room_id, token_str, True
  48. )
  49. )
  50. # 1-3 should fail and last will succeed, meaning that 1-3 are deleted
  51. # and last is not.
  52. self.get_failure(self.store.get_event(first["event_id"]), NotFoundError)
  53. self.get_failure(self.store.get_event(second["event_id"]), NotFoundError)
  54. self.get_failure(self.store.get_event(third["event_id"]), NotFoundError)
  55. self.get_success(self.store.get_event(last["event_id"]))
  56. def test_purge_history_wont_delete_extrems(self) -> None:
  57. """
  58. Purging a room history will delete everything before the topological point.
  59. """
  60. # Send four messages to the room
  61. first = self.helper.send(self.room_id, body="test1")
  62. second = self.helper.send(self.room_id, body="test2")
  63. third = self.helper.send(self.room_id, body="test3")
  64. last = self.helper.send(self.room_id, body="test4")
  65. # Set the topological token higher than it should be
  66. token = self.get_success(
  67. self.store.get_topological_token_for_event(last["event_id"])
  68. )
  69. assert token.topological is not None
  70. event = f"t{token.topological + 1}-{token.stream + 1}"
  71. # Purge everything before this topological token
  72. f = self.get_failure(
  73. self._storage_controllers.purge_events.purge_history(
  74. self.room_id, event, True
  75. ),
  76. SynapseError,
  77. )
  78. self.assertIn("greater than forward", f.value.args[0])
  79. # Try and get the events
  80. self.get_success(self.store.get_event(first["event_id"]))
  81. self.get_success(self.store.get_event(second["event_id"]))
  82. self.get_success(self.store.get_event(third["event_id"]))
  83. self.get_success(self.store.get_event(last["event_id"]))
  84. def test_purge_room(self) -> None:
  85. """
  86. Purging a room will delete everything about it.
  87. """
  88. # Send four messages to the room
  89. first = self.helper.send(self.room_id, body="test1")
  90. # Get the current room state.
  91. create_event = self.get_success(
  92. self._storage_controllers.state.get_current_state_event(
  93. self.room_id, "m.room.create", ""
  94. )
  95. )
  96. assert create_event is not None
  97. # Purge everything before this topological token
  98. self.get_success(
  99. self._storage_controllers.purge_events.purge_room(self.room_id)
  100. )
  101. # The events aren't found.
  102. self.store._invalidate_local_get_event_cache(create_event.event_id)
  103. self.get_failure(self.store.get_event(create_event.event_id), NotFoundError)
  104. self.get_failure(self.store.get_event(first["event_id"]), NotFoundError)