2018-08-30 16:19:58 +02:00
|
|
|
# Copyright 2018 New Vector Ltd
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
|
2021-03-08 15:21:36 +01:00
|
|
|
from synapse.api.errors import NotFoundError, SynapseError
|
2021-08-17 13:57:58 +02:00
|
|
|
from synapse.rest.client import room
|
2018-08-30 16:19:58 +02:00
|
|
|
|
|
|
|
from tests.unittest import HomeserverTestCase
|
|
|
|
|
|
|
|
|
|
|
|
class PurgeTests(HomeserverTestCase):
|
|
|
|
|
|
|
|
user_id = "@red:server"
|
|
|
|
servlets = [room.register_servlets]
|
|
|
|
|
|
|
|
def make_homeserver(self, reactor, clock):
|
2020-12-02 17:09:24 +01:00
|
|
|
hs = self.setup_test_homeserver("server", federation_http_client=None)
|
2018-08-30 16:19:58 +02:00
|
|
|
return hs
|
|
|
|
|
|
|
|
def prepare(self, reactor, clock, hs):
|
|
|
|
self.room_id = self.helper.create_room_as(self.user_id)
|
|
|
|
|
2021-03-08 15:21:36 +01:00
|
|
|
self.store = hs.get_datastore()
|
|
|
|
self.storage = self.hs.get_storage()
|
|
|
|
|
|
|
|
def test_purge_history(self):
|
2018-08-30 16:19:58 +02:00
|
|
|
"""
|
2021-03-08 15:21:36 +01:00
|
|
|
Purging a room history will delete everything before the topological point.
|
2018-08-30 16:19:58 +02:00
|
|
|
"""
|
|
|
|
# Send four messages to the room
|
|
|
|
first = self.helper.send(self.room_id, body="test1")
|
|
|
|
second = self.helper.send(self.room_id, body="test2")
|
|
|
|
third = self.helper.send(self.room_id, body="test3")
|
|
|
|
last = self.helper.send(self.room_id, body="test4")
|
|
|
|
|
|
|
|
# Get the topological token
|
2020-09-30 21:29:19 +02:00
|
|
|
token = self.get_success(
|
2021-03-08 15:21:36 +01:00
|
|
|
self.store.get_topological_token_for_event(last["event_id"])
|
2020-07-28 22:09:53 +02:00
|
|
|
)
|
2020-09-30 21:29:19 +02:00
|
|
|
token_str = self.get_success(token.to_string(self.hs.get_datastore()))
|
2018-08-30 16:19:58 +02:00
|
|
|
|
2020-08-17 13:24:46 +02:00
|
|
|
# Purge everything before this topological token
|
2020-09-30 21:29:19 +02:00
|
|
|
self.get_success(
|
2021-03-08 15:21:36 +01:00
|
|
|
self.storage.purge_events.purge_history(self.room_id, token_str, True)
|
2020-09-30 21:29:19 +02:00
|
|
|
)
|
2018-08-30 16:19:58 +02:00
|
|
|
|
|
|
|
# 1-3 should fail and last will succeed, meaning that 1-3 are deleted
|
|
|
|
# and last is not.
|
2021-03-08 15:21:36 +01:00
|
|
|
self.get_failure(self.store.get_event(first["event_id"]), NotFoundError)
|
|
|
|
self.get_failure(self.store.get_event(second["event_id"]), NotFoundError)
|
|
|
|
self.get_failure(self.store.get_event(third["event_id"]), NotFoundError)
|
|
|
|
self.get_success(self.store.get_event(last["event_id"]))
|
2018-08-30 16:19:58 +02:00
|
|
|
|
2021-03-08 15:21:36 +01:00
|
|
|
def test_purge_history_wont_delete_extrems(self):
|
2018-08-30 16:19:58 +02:00
|
|
|
"""
|
2021-03-08 15:21:36 +01:00
|
|
|
Purging a room history will delete everything before the topological point.
|
2018-08-30 16:19:58 +02:00
|
|
|
"""
|
|
|
|
# Send four messages to the room
|
|
|
|
first = self.helper.send(self.room_id, body="test1")
|
|
|
|
second = self.helper.send(self.room_id, body="test2")
|
|
|
|
third = self.helper.send(self.room_id, body="test3")
|
|
|
|
last = self.helper.send(self.room_id, body="test4")
|
|
|
|
|
|
|
|
# Set the topological token higher than it should be
|
2020-09-29 22:48:33 +02:00
|
|
|
token = self.get_success(
|
2021-03-08 15:21:36 +01:00
|
|
|
self.store.get_topological_token_for_event(last["event_id"])
|
2020-08-17 13:24:46 +02:00
|
|
|
)
|
2021-07-13 12:43:15 +02:00
|
|
|
event = f"t{token.topological + 1}-{token.stream + 1}"
|
2018-08-30 16:19:58 +02:00
|
|
|
|
|
|
|
# Purge everything before this topological token
|
2021-03-08 15:21:36 +01:00
|
|
|
f = self.get_failure(
|
|
|
|
self.storage.purge_events.purge_history(self.room_id, event, True),
|
|
|
|
SynapseError,
|
|
|
|
)
|
2018-08-30 16:19:58 +02:00
|
|
|
self.assertIn("greater than forward", f.value.args[0])
|
|
|
|
|
|
|
|
# Try and get the events
|
2021-03-08 15:21:36 +01:00
|
|
|
self.get_success(self.store.get_event(first["event_id"]))
|
|
|
|
self.get_success(self.store.get_event(second["event_id"]))
|
|
|
|
self.get_success(self.store.get_event(third["event_id"]))
|
|
|
|
self.get_success(self.store.get_event(last["event_id"]))
|
|
|
|
|
|
|
|
def test_purge_room(self):
|
|
|
|
"""
|
|
|
|
Purging a room will delete everything about it.
|
|
|
|
"""
|
|
|
|
# Send four messages to the room
|
|
|
|
first = self.helper.send(self.room_id, body="test1")
|
|
|
|
|
|
|
|
# Get the current room state.
|
|
|
|
state_handler = self.hs.get_state_handler()
|
|
|
|
create_event = self.get_success(
|
|
|
|
state_handler.get_current_state(self.room_id, "m.room.create", "")
|
|
|
|
)
|
|
|
|
self.assertIsNotNone(create_event)
|
|
|
|
|
|
|
|
# Purge everything before this topological token
|
|
|
|
self.get_success(self.storage.purge_events.purge_room(self.room_id))
|
|
|
|
|
|
|
|
# The events aren't found.
|
|
|
|
self.store._invalidate_get_event_cache(create_event.event_id)
|
|
|
|
self.get_failure(self.store.get_event(create_event.event_id), NotFoundError)
|
|
|
|
self.get_failure(self.store.get_event(first["event_id"]), NotFoundError)
|