1
0

test_purge.py 4.6 KB

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