You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

112 lines
4.0 KiB

  1. # -*- coding: utf-8 -*-
  2. # Copyright 2018 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 twisted.internet import defer
  16. from synapse.rest.client.v1 import room
  17. from tests.unittest import HomeserverTestCase
  18. class PurgeTests(HomeserverTestCase):
  19. user_id = "@red:server"
  20. servlets = [room.register_servlets]
  21. def make_homeserver(self, reactor, clock):
  22. hs = self.setup_test_homeserver("server", http_client=None)
  23. return hs
  24. def prepare(self, reactor, clock, hs):
  25. self.room_id = self.helper.create_room_as(self.user_id)
  26. def test_purge(self):
  27. """
  28. Purging a room will delete everything before the topological point.
  29. """
  30. # Send four messages to the room
  31. first = self.helper.send(self.room_id, body="test1")
  32. second = self.helper.send(self.room_id, body="test2")
  33. third = self.helper.send(self.room_id, body="test3")
  34. last = self.helper.send(self.room_id, body="test4")
  35. store = self.hs.get_datastore()
  36. storage = self.hs.get_storage()
  37. # Get the topological token
  38. event = store.get_topological_token_for_event(last["event_id"])
  39. self.pump()
  40. event = self.successResultOf(event)
  41. # Purge everything before this topological token
  42. purge = defer.ensureDeferred(
  43. storage.purge_events.purge_history(self.room_id, event, True)
  44. )
  45. self.pump()
  46. self.assertEqual(self.successResultOf(purge), None)
  47. # Try and get the events
  48. get_first = store.get_event(first["event_id"])
  49. get_second = store.get_event(second["event_id"])
  50. get_third = store.get_event(third["event_id"])
  51. get_last = store.get_event(last["event_id"])
  52. self.pump()
  53. # 1-3 should fail and last will succeed, meaning that 1-3 are deleted
  54. # and last is not.
  55. self.failureResultOf(get_first)
  56. self.failureResultOf(get_second)
  57. self.failureResultOf(get_third)
  58. self.successResultOf(get_last)
  59. def test_purge_wont_delete_extrems(self):
  60. """
  61. Purging a room will delete everything before the topological point.
  62. """
  63. # Send four messages to the room
  64. first = self.helper.send(self.room_id, body="test1")
  65. second = self.helper.send(self.room_id, body="test2")
  66. third = self.helper.send(self.room_id, body="test3")
  67. last = self.helper.send(self.room_id, body="test4")
  68. storage = self.hs.get_datastore()
  69. # Set the topological token higher than it should be
  70. event = storage.get_topological_token_for_event(last["event_id"])
  71. self.pump()
  72. event = self.successResultOf(event)
  73. event = "t{}-{}".format(
  74. *list(map(lambda x: x + 1, map(int, event[1:].split("-"))))
  75. )
  76. # Purge everything before this topological token
  77. purge = defer.ensureDeferred(storage.purge_history(self.room_id, event, True))
  78. self.pump()
  79. f = self.failureResultOf(purge)
  80. self.assertIn("greater than forward", f.value.args[0])
  81. # Try and get the events
  82. get_first = storage.get_event(first["event_id"])
  83. get_second = storage.get_event(second["event_id"])
  84. get_third = storage.get_event(third["event_id"])
  85. get_last = storage.get_event(last["event_id"])
  86. self.pump()
  87. # Nothing is deleted.
  88. self.successResultOf(get_first)
  89. self.successResultOf(get_second)
  90. self.successResultOf(get_third)
  91. self.successResultOf(get_last)