14
14
# limitations under the License.
15
15
16
16
import os .path
17
+ from unittest .mock import patch
17
18
19
+ from mock import Mock
20
+
21
+ import synapse .rest .admin
22
+ from synapse .api .constants import EventTypes
23
+ from synapse .rest .client .v1 import login , room
18
24
from synapse .storage import prepare_database
19
25
from synapse .types import Requester , UserID
20
26
@@ -225,6 +231,14 @@ def test_forked_graph_cleanup(self):
225
231
226
232
227
233
class CleanupExtremDummyEventsTestCase (HomeserverTestCase ):
234
+ CONSENT_VERSION = "1"
235
+ EXTREMITIES_COUNT = 50
236
+ servlets = [
237
+ synapse .rest .admin .register_servlets_for_client_rest_resource ,
238
+ login .register_servlets ,
239
+ room .register_servlets ,
240
+ ]
241
+
228
242
def make_homeserver (self , reactor , clock ):
229
243
config = self .default_config ()
230
244
config ["cleanup_extremities_with_dummy_events" ] = True
@@ -233,33 +247,148 @@ def make_homeserver(self, reactor, clock):
233
247
def prepare (self , reactor , clock , homeserver ):
234
248
self .store = homeserver .get_datastore ()
235
249
self .room_creator = homeserver .get_room_creation_handler ()
250
+ self .event_creator_handler = homeserver .get_event_creation_handler ()
236
251
237
252
# Create a test user and room
238
- self .user = UserID ("alice" , "test" )
253
+ self .user = UserID .from_string (self .register_user ("user1" , "password" ))
254
+ self .token1 = self .login ("user1" , "password" )
239
255
self .requester = Requester (self .user , None , False , None , None )
240
256
info = self .get_success (self .room_creator .create_room (self .requester , {}))
241
257
self .room_id = info ["room_id" ]
258
+ self .event_creator = homeserver .get_event_creation_handler ()
259
+ homeserver .config .user_consent_version = self .CONSENT_VERSION
242
260
243
261
def test_send_dummy_event (self ):
244
- # Create a bushy graph with 50 extremities.
245
-
246
- event_id_start = self .create_and_send_event (self .room_id , self .user )
262
+ self ._create_extremity_rich_graph ()
247
263
248
- for _ in range (50 ):
249
- self .create_and_send_event (
250
- self .room_id , self .user , prev_event_ids = [event_id_start ]
251
- )
264
+ # Pump the reactor repeatedly so that the background updates have a
265
+ # chance to run.
266
+ self .pump (10 * 60 )
252
267
253
268
latest_event_ids = self .get_success (
254
269
self .store .get_latest_event_ids_in_room (self .room_id )
255
270
)
256
- self .assertEqual (len (latest_event_ids ), 50 )
271
+ self .assertTrue (len (latest_event_ids ) < 10 , len ( latest_event_ids ) )
257
272
273
+ @patch ("synapse.handlers.message._DUMMY_EVENT_ROOM_EXCLUSION_EXPIRY" , new = 0 )
274
+ def test_send_dummy_events_when_insufficient_power (self ):
275
+ self ._create_extremity_rich_graph ()
276
+ # Criple power levels
277
+ self .helper .send_state (
278
+ self .room_id ,
279
+ EventTypes .PowerLevels ,
280
+ body = {"users" : {str (self .user ): - 1 }},
281
+ tok = self .token1 ,
282
+ )
258
283
# Pump the reactor repeatedly so that the background updates have a
259
284
# chance to run.
260
285
self .pump (10 * 60 )
261
286
287
+ latest_event_ids = self .get_success (
288
+ self .store .get_latest_event_ids_in_room (self .room_id )
289
+ )
290
+ # Check that the room has not been pruned
291
+ self .assertTrue (len (latest_event_ids ) > 10 )
292
+
293
+ # New user with regular levels
294
+ user2 = self .register_user ("user2" , "password" )
295
+ token2 = self .login ("user2" , "password" )
296
+ self .helper .join (self .room_id , user2 , tok = token2 )
297
+ self .pump (10 * 60 )
298
+
299
+ latest_event_ids = self .get_success (
300
+ self .store .get_latest_event_ids_in_room (self .room_id )
301
+ )
302
+ self .assertTrue (len (latest_event_ids ) < 10 , len (latest_event_ids ))
303
+
304
+ @patch ("synapse.handlers.message._DUMMY_EVENT_ROOM_EXCLUSION_EXPIRY" , new = 0 )
305
+ def test_send_dummy_event_without_consent (self ):
306
+ self ._create_extremity_rich_graph ()
307
+ self ._enable_consent_checking ()
308
+
309
+ # Pump the reactor repeatedly so that the background updates have a
310
+ # chance to run. Attempt to add dummy event with user that has not consented
311
+ # Check that dummy event send fails.
312
+ self .pump (10 * 60 )
313
+ latest_event_ids = self .get_success (
314
+ self .store .get_latest_event_ids_in_room (self .room_id )
315
+ )
316
+ self .assertTrue (len (latest_event_ids ) == self .EXTREMITIES_COUNT )
317
+
318
+ # Create new user, and add consent
319
+ user2 = self .register_user ("user2" , "password" )
320
+ token2 = self .login ("user2" , "password" )
321
+ self .get_success (
322
+ self .store .user_set_consent_version (user2 , self .CONSENT_VERSION )
323
+ )
324
+ self .helper .join (self .room_id , user2 , tok = token2 )
325
+
326
+ # Background updates should now cause a dummy event to be added to the graph
327
+ self .pump (10 * 60 )
328
+
262
329
latest_event_ids = self .get_success (
263
330
self .store .get_latest_event_ids_in_room (self .room_id )
264
331
)
265
332
self .assertTrue (len (latest_event_ids ) < 10 , len (latest_event_ids ))
333
+
334
+ @patch ("synapse.handlers.message._DUMMY_EVENT_ROOM_EXCLUSION_EXPIRY" , new = 250 )
335
+ def test_expiry_logic (self ):
336
+ """Simple test to ensure that _expire_rooms_to_exclude_from_dummy_event_insertion()
337
+ expires old entries correctly.
338
+ """
339
+ self .event_creator_handler ._rooms_to_exclude_from_dummy_event_insertion [
340
+ "1"
341
+ ] = 100000
342
+ self .event_creator_handler ._rooms_to_exclude_from_dummy_event_insertion [
343
+ "2"
344
+ ] = 200000
345
+ self .event_creator_handler ._rooms_to_exclude_from_dummy_event_insertion [
346
+ "3"
347
+ ] = 300000
348
+ self .event_creator_handler ._expire_rooms_to_exclude_from_dummy_event_insertion ()
349
+ # All entries within time frame
350
+ self .assertEqual (
351
+ len (
352
+ self .event_creator_handler ._rooms_to_exclude_from_dummy_event_insertion
353
+ ),
354
+ 3 ,
355
+ )
356
+ # Oldest room to expire
357
+ self .pump (1 )
358
+ self .event_creator_handler ._expire_rooms_to_exclude_from_dummy_event_insertion ()
359
+ self .assertEqual (
360
+ len (
361
+ self .event_creator_handler ._rooms_to_exclude_from_dummy_event_insertion
362
+ ),
363
+ 2 ,
364
+ )
365
+ # All rooms to expire
366
+ self .pump (2 )
367
+ self .assertEqual (
368
+ len (
369
+ self .event_creator_handler ._rooms_to_exclude_from_dummy_event_insertion
370
+ ),
371
+ 0 ,
372
+ )
373
+
374
+ def _create_extremity_rich_graph (self ):
375
+ """Helper method to create bushy graph on demand"""
376
+
377
+ event_id_start = self .create_and_send_event (self .room_id , self .user )
378
+
379
+ for _ in range (self .EXTREMITIES_COUNT ):
380
+ self .create_and_send_event (
381
+ self .room_id , self .user , prev_event_ids = [event_id_start ]
382
+ )
383
+
384
+ latest_event_ids = self .get_success (
385
+ self .store .get_latest_event_ids_in_room (self .room_id )
386
+ )
387
+ self .assertEqual (len (latest_event_ids ), 50 )
388
+
389
+ def _enable_consent_checking (self ):
390
+ """Helper method to enable consent checking"""
391
+ self .event_creator ._block_events_without_consent_error = "No consent from user"
392
+ consent_uri_builder = Mock ()
393
+ consent_uri_builder .build_user_consent_uri .return_value = "http://example.com"
394
+ self .event_creator ._consent_uri_builder = consent_uri_builder
0 commit comments