summary refs log tree commit diff
path: root/tests/rest/client/test_retention.py
blob: 41ea9db689be6f752e794bc4b0e2d0f57e22f492 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
# -*- coding: utf-8 -*-
# Copyright 2019 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.
from mock import Mock

from synapse.api.constants import EventTypes
from synapse.rest import admin
from synapse.rest.client.v1 import login, room
from synapse.visibility import filter_events_for_client

from tests import unittest

one_hour_ms = 3600000
one_day_ms = one_hour_ms * 24


class RetentionTestCase(unittest.HomeserverTestCase):
    servlets = [
        admin.register_servlets,
        login.register_servlets,
        room.register_servlets,
    ]

    def make_homeserver(self, reactor, clock):
        config = self.default_config()
        config["default_room_version"] = "1"
        config["retention"] = {
            "enabled": True,
            "default_policy": {
                "min_lifetime": one_day_ms,
                "max_lifetime": one_day_ms * 3,
            },
            "allowed_lifetime_min": one_day_ms,
            "allowed_lifetime_max": one_day_ms * 3,
        }

        self.hs = self.setup_test_homeserver(config=config)
        return self.hs

    def prepare(self, reactor, clock, homeserver):
        self.user_id = self.register_user("user", "password")
        self.token = self.login("user", "password")

    def test_retention_state_event(self):
        """Tests that the server configuration can limit the values a user can set to the
        room's retention policy.
        """
        room_id = self.helper.create_room_as(self.user_id, tok=self.token)

        self.helper.send_state(
            room_id=room_id,
            event_type=EventTypes.Retention,
            body={
                "max_lifetime": one_day_ms * 4,
            },
            tok=self.token,
            expect_code=400,
        )

        self.helper.send_state(
            room_id=room_id,
            event_type=EventTypes.Retention,
            body={
                "max_lifetime": one_hour_ms,
            },
            tok=self.token,
            expect_code=400,
        )

    def test_retention_event_purged_with_state_event(self):
        """Tests that expired events are correctly purged when the room's retention policy
        is defined by a state event.
        """
        room_id = self.helper.create_room_as(self.user_id, tok=self.token)

        # Set the room's retention period to 2 days.
        lifetime = one_day_ms * 2
        self.helper.send_state(
            room_id=room_id,
            event_type=EventTypes.Retention,
            body={
                "max_lifetime": lifetime,
            },
            tok=self.token,
        )

        self._test_retention_event_purged(room_id, one_day_ms * 1.5)

    def test_retention_event_purged_without_state_event(self):
        """Tests that expired events are correctly purged when the room's retention policy
        is defined by the server's configuration's default retention policy.
        """
        room_id = self.helper.create_room_as(self.user_id, tok=self.token)

        self._test_retention_event_purged(room_id, one_day_ms * 2)

    def test_visibility(self):
        """Tests that synapse.visibility.filter_events_for_client correctly filters out
        outdated events
        """
        store = self.hs.get_datastore()
        storage = self.hs.get_storage()
        room_id = self.helper.create_room_as(self.user_id, tok=self.token)
        events = []

        # Send a first event, which should be filtered out at the end of the test.
        resp = self.helper.send(
            room_id=room_id,
            body="1",
            tok=self.token,
        )

        # Get the event from the store so that we end up with a FrozenEvent that we can
        # give to filter_events_for_client. We need to do this now because the event won't
        # be in the database anymore after it has expired.
        events.append(self.get_success(
            store.get_event(
                resp.get("event_id")
            )
        ))

        # Advance the time by 2 days. We're using the default retention policy, therefore
        # after this the first event will still be valid.
        self.reactor.advance(one_day_ms * 2 / 1000)

        # Send another event, which shouldn't get filtered out.
        resp = self.helper.send(
            room_id=room_id,
            body="2",
            tok=self.token,
        )

        valid_event_id = resp.get("event_id")

        events.append(self.get_success(
            store.get_event(
                valid_event_id
            )
        ))

        # Advance the time by anothe 2 days. After this, the first event should be
        # outdated but not the second one.
        self.reactor.advance(one_day_ms * 2 / 1000)

        # Run filter_events_for_client with our list of FrozenEvents.
        filtered_events = self.get_success(filter_events_for_client(
            storage, self.user_id, events
        ))

        # We should only get one event back.
        self.assertEqual(len(filtered_events), 1, filtered_events)
        # That event should be the second, not outdated event.
        self.assertEqual(filtered_events[0].event_id, valid_event_id, filtered_events)

    def _test_retention_event_purged(self, room_id, increment):
        # Send a first event to the room. This is the event we'll want to be purged at the
        # end of the test.
        resp = self.helper.send(
            room_id=room_id,
            body="1",
            tok=self.token,
        )

        expired_event_id = resp.get("event_id")

        # Check that we can retrieve the event.
        expired_event = self.get_event(room_id, expired_event_id)
        self.assertEqual(expired_event.get("content", {}).get("body"), "1", expired_event)

        # Advance the time.
        self.reactor.advance(increment / 1000)

        # Send another event. We need this because the purge job won't purge the most
        # recent event in the room.
        resp = self.helper.send(
            room_id=room_id,
            body="2",
            tok=self.token,
        )

        valid_event_id = resp.get("event_id")

        # Advance the time again. Now our first event should have expired but our second
        # one should still be kept.
        self.reactor.advance(increment / 1000)

        # Check that the event has been purged from the database.
        self.get_event(room_id, expired_event_id, expected_code=404)

        # Check that the event that hasn't been purged can still be retrieved.
        valid_event = self.get_event(room_id, valid_event_id)
        self.assertEqual(valid_event.get("content", {}).get("body"), "2", valid_event)

    def get_event(self, room_id, event_id, expected_code=200):
        url = "/_matrix/client/r0/rooms/%s/event/%s" % (room_id, event_id)

        request, channel = self.make_request("GET", url, access_token=self.token)
        self.render(request)

        self.assertEqual(channel.code, expected_code, channel.result)

        return channel.json_body


class RetentionNoDefaultPolicyTestCase(unittest.HomeserverTestCase):
    servlets = [
        admin.register_servlets,
        login.register_servlets,
        room.register_servlets,
    ]

    def make_homeserver(self, reactor, clock):
        config = self.default_config()
        config["default_room_version"] = "1"
        config["retention"] = {
            "enabled": True,
        }

        mock_federation_client = Mock(spec=["backfill"])

        self.hs = self.setup_test_homeserver(
            config=config,
            federation_client=mock_federation_client,
        )
        return self.hs

    def prepare(self, reactor, clock, homeserver):
        self.user_id = self.register_user("user", "password")
        self.token = self.login("user", "password")

    def test_no_default_policy(self):
        """Tests that an event doesn't get expired if there is neither a default retention
        policy nor a policy specific to the room.
        """
        room_id = self.helper.create_room_as(self.user_id, tok=self.token)

        self._test_retention(room_id)

    def test_state_policy(self):
        """Tests that an event gets correctly expired if there is no default retention
        policy but there's a policy specific to the room.
        """
        room_id = self.helper.create_room_as(self.user_id, tok=self.token)

        # Set the maximum lifetime to 35 days so that the first event gets expired but not
        # the second one.
        self.helper.send_state(
            room_id=room_id,
            event_type=EventTypes.Retention,
            body={
                "max_lifetime": one_day_ms * 35,
            },
            tok=self.token,
        )

        self._test_retention(room_id, expected_code_for_first_event=404)

    def _test_retention(self, room_id, expected_code_for_first_event=200):
        # Send a first event to the room. This is the event we'll want to be purged at the
        # end of the test.
        resp = self.helper.send(
            room_id=room_id,
            body="1",
            tok=self.token,
        )

        first_event_id = resp.get("event_id")

        # Check that we can retrieve the event.
        expired_event = self.get_event(room_id, first_event_id)
        self.assertEqual(expired_event.get("content", {}).get("body"), "1", expired_event)

        # Advance the time by a month.
        self.reactor.advance(one_day_ms * 30 / 1000)

        # Send another event. We need this because the purge job won't purge the most
        # recent event in the room.
        resp = self.helper.send(
            room_id=room_id,
            body="2",
            tok=self.token,
        )

        second_event_id = resp.get("event_id")

        # Advance the time by another month.
        self.reactor.advance(one_day_ms * 30 / 1000)

        # Check if the event has been purged from the database.
        first_event = self.get_event(
            room_id, first_event_id, expected_code=expected_code_for_first_event
        )

        if expected_code_for_first_event == 200:
            self.assertEqual(first_event.get("content", {}).get("body"), "1", first_event)

        # Check that the event that hasn't been purged can still be retrieved.
        second_event = self.get_event(room_id, second_event_id)
        self.assertEqual(second_event.get("content", {}).get("body"), "2", second_event)

    def get_event(self, room_id, event_id, expected_code=200):
        url = "/_matrix/client/r0/rooms/%s/event/%s" % (room_id, event_id)

        request, channel = self.make_request("GET", url, access_token=self.token)
        self.render(request)

        self.assertEqual(channel.code, expected_code, channel.result)

        return channel.json_body