test_purge.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 = self.hs.get_storage()
  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.purge_events.purge_history(self.room_id, token_str, True)
  44. )
  45. # 1-3 should fail and last will succeed, meaning that 1-3 are deleted
  46. # and last is not.
  47. self.get_failure(self.store.get_event(first["event_id"]), NotFoundError)
  48. self.get_failure(self.store.get_event(second["event_id"]), NotFoundError)
  49. self.get_failure(self.store.get_event(third["event_id"]), NotFoundError)
  50. self.get_success(self.store.get_event(last["event_id"]))
  51. def test_purge_history_wont_delete_extrems(self):
  52. """
  53. Purging a room history will delete everything before the topological point.
  54. """
  55. # Send four messages to the room
  56. first = self.helper.send(self.room_id, body="test1")
  57. second = self.helper.send(self.room_id, body="test2")
  58. third = self.helper.send(self.room_id, body="test3")
  59. last = self.helper.send(self.room_id, body="test4")
  60. # Set the topological token higher than it should be
  61. token = self.get_success(
  62. self.store.get_topological_token_for_event(last["event_id"])
  63. )
  64. event = f"t{token.topological + 1}-{token.stream + 1}"
  65. # Purge everything before this topological token
  66. f = self.get_failure(
  67. self.storage.purge_events.purge_history(self.room_id, event, True),
  68. SynapseError,
  69. )
  70. self.assertIn("greater than forward", f.value.args[0])
  71. # Try and get the events
  72. self.get_success(self.store.get_event(first["event_id"]))
  73. self.get_success(self.store.get_event(second["event_id"]))
  74. self.get_success(self.store.get_event(third["event_id"]))
  75. self.get_success(self.store.get_event(last["event_id"]))
  76. def test_purge_room(self):
  77. """
  78. Purging a room will delete everything about it.
  79. """
  80. # Send four messages to the room
  81. first = self.helper.send(self.room_id, body="test1")
  82. # Get the current room state.
  83. create_event = self.get_success(
  84. self.store.get_current_state_event(self.room_id, "m.room.create", "")
  85. )
  86. self.assertIsNotNone(create_event)
  87. # Purge everything before this topological token
  88. self.get_success(self.storage.purge_events.purge_room(self.room_id))
  89. # The events aren't found.
  90. self.store._invalidate_get_event_cache(create_event.event_id)
  91. self.get_failure(self.store.get_event(create_event.event_id), NotFoundError)
  92. self.get_failure(self.store.get_event(first["event_id"]), NotFoundError)