@@ -109,6 +109,27 @@ def test_delayed_state_events_are_sent_on_timeout(self) -> None:
109
109
)
110
110
self .assertEqual (setter_expected , content .get (setter_key ), content )
111
111
112
+ @unittest .override_config (
113
+ {"rc_delayed_event_mgmt" : {"per_second" : 0.5 , "burst_count" : 1 }}
114
+ )
115
+ def test_get_delayed_events_ratelimit (self ) -> None :
116
+ args = ("GET" , PATH_PREFIX )
117
+
118
+ channel = self .make_request (* args )
119
+ self .assertEqual (HTTPStatus .OK , channel .code , channel .result )
120
+
121
+ channel = self .make_request (* args )
122
+ self .assertEqual (HTTPStatus .TOO_MANY_REQUESTS , channel .code , channel .result )
123
+
124
+ # Add the current user to the ratelimit overrides, allowing them no ratelimiting.
125
+ self .get_success (
126
+ self .hs .get_datastores ().main .set_ratelimit_for_user (self .user_id , 0 , 0 )
127
+ )
128
+
129
+ # Test that the request isn't ratelimited anymore.
130
+ channel = self .make_request (* args )
131
+ self .assertEqual (HTTPStatus .OK , channel .code , channel .result )
132
+
112
133
def test_update_delayed_event_without_id (self ) -> None :
113
134
channel = self .make_request (
114
135
"POST" ,
@@ -206,6 +227,46 @@ def test_cancel_delayed_state_event(self) -> None:
206
227
expect_code = HTTPStatus .NOT_FOUND ,
207
228
)
208
229
230
+ @unittest .override_config (
231
+ {"rc_delayed_event_mgmt" : {"per_second" : 0.5 , "burst_count" : 1 }}
232
+ )
233
+ def test_cancel_delayed_event_ratelimit (self ) -> None :
234
+ delay_ids = []
235
+ for _ in range (2 ):
236
+ channel = self .make_request (
237
+ "POST" ,
238
+ _get_path_for_delayed_send (self .room_id , _EVENT_TYPE , 100000 ),
239
+ {},
240
+ )
241
+ self .assertEqual (HTTPStatus .OK , channel .code , channel .result )
242
+ delay_id = channel .json_body .get ("delay_id" )
243
+ self .assertIsNotNone (delay_id )
244
+ delay_ids .append (delay_id )
245
+
246
+ channel = self .make_request (
247
+ "POST" ,
248
+ f"{ PATH_PREFIX } /{ delay_ids .pop (0 )} " ,
249
+ {"action" : "cancel" },
250
+ )
251
+ self .assertEqual (HTTPStatus .OK , channel .code , channel .result )
252
+
253
+ args = (
254
+ "POST" ,
255
+ f"{ PATH_PREFIX } /{ delay_ids .pop (0 )} " ,
256
+ {"action" : "cancel" },
257
+ )
258
+ channel = self .make_request (* args )
259
+ self .assertEqual (HTTPStatus .TOO_MANY_REQUESTS , channel .code , channel .result )
260
+
261
+ # Add the current user to the ratelimit overrides, allowing them no ratelimiting.
262
+ self .get_success (
263
+ self .hs .get_datastores ().main .set_ratelimit_for_user (self .user_id , 0 , 0 )
264
+ )
265
+
266
+ # Test that the request isn't ratelimited anymore.
267
+ channel = self .make_request (* args )
268
+ self .assertEqual (HTTPStatus .OK , channel .code , channel .result )
269
+
209
270
def test_send_delayed_state_event (self ) -> None :
210
271
state_key = "to_send_on_request"
211
272
@@ -250,6 +311,44 @@ def test_send_delayed_state_event(self) -> None:
250
311
)
251
312
self .assertEqual (setter_expected , content .get (setter_key ), content )
252
313
314
+ @unittest .override_config ({"rc_message" : {"per_second" : 3.5 , "burst_count" : 4 }})
315
+ def test_send_delayed_event_ratelimit (self ) -> None :
316
+ delay_ids = []
317
+ for _ in range (2 ):
318
+ channel = self .make_request (
319
+ "POST" ,
320
+ _get_path_for_delayed_send (self .room_id , _EVENT_TYPE , 100000 ),
321
+ {},
322
+ )
323
+ self .assertEqual (HTTPStatus .OK , channel .code , channel .result )
324
+ delay_id = channel .json_body .get ("delay_id" )
325
+ self .assertIsNotNone (delay_id )
326
+ delay_ids .append (delay_id )
327
+
328
+ channel = self .make_request (
329
+ "POST" ,
330
+ f"{ PATH_PREFIX } /{ delay_ids .pop (0 )} " ,
331
+ {"action" : "send" },
332
+ )
333
+ self .assertEqual (HTTPStatus .OK , channel .code , channel .result )
334
+
335
+ args = (
336
+ "POST" ,
337
+ f"{ PATH_PREFIX } /{ delay_ids .pop (0 )} " ,
338
+ {"action" : "send" },
339
+ )
340
+ channel = self .make_request (* args )
341
+ self .assertEqual (HTTPStatus .TOO_MANY_REQUESTS , channel .code , channel .result )
342
+
343
+ # Add the current user to the ratelimit overrides, allowing them no ratelimiting.
344
+ self .get_success (
345
+ self .hs .get_datastores ().main .set_ratelimit_for_user (self .user_id , 0 , 0 )
346
+ )
347
+
348
+ # Test that the request isn't ratelimited anymore.
349
+ channel = self .make_request (* args )
350
+ self .assertEqual (HTTPStatus .OK , channel .code , channel .result )
351
+
253
352
def test_restart_delayed_state_event (self ) -> None :
254
353
state_key = "to_send_on_restarted_timeout"
255
354
@@ -309,6 +408,46 @@ def test_restart_delayed_state_event(self) -> None:
309
408
)
310
409
self .assertEqual (setter_expected , content .get (setter_key ), content )
311
410
411
+ @unittest .override_config (
412
+ {"rc_delayed_event_mgmt" : {"per_second" : 0.5 , "burst_count" : 1 }}
413
+ )
414
+ def test_restart_delayed_event_ratelimit (self ) -> None :
415
+ delay_ids = []
416
+ for _ in range (2 ):
417
+ channel = self .make_request (
418
+ "POST" ,
419
+ _get_path_for_delayed_send (self .room_id , _EVENT_TYPE , 100000 ),
420
+ {},
421
+ )
422
+ self .assertEqual (HTTPStatus .OK , channel .code , channel .result )
423
+ delay_id = channel .json_body .get ("delay_id" )
424
+ self .assertIsNotNone (delay_id )
425
+ delay_ids .append (delay_id )
426
+
427
+ channel = self .make_request (
428
+ "POST" ,
429
+ f"{ PATH_PREFIX } /{ delay_ids .pop (0 )} " ,
430
+ {"action" : "restart" },
431
+ )
432
+ self .assertEqual (HTTPStatus .OK , channel .code , channel .result )
433
+
434
+ args = (
435
+ "POST" ,
436
+ f"{ PATH_PREFIX } /{ delay_ids .pop (0 )} " ,
437
+ {"action" : "restart" },
438
+ )
439
+ channel = self .make_request (* args )
440
+ self .assertEqual (HTTPStatus .TOO_MANY_REQUESTS , channel .code , channel .result )
441
+
442
+ # Add the current user to the ratelimit overrides, allowing them no ratelimiting.
443
+ self .get_success (
444
+ self .hs .get_datastores ().main .set_ratelimit_for_user (self .user_id , 0 , 0 )
445
+ )
446
+
447
+ # Test that the request isn't ratelimited anymore.
448
+ channel = self .make_request (* args )
449
+ self .assertEqual (HTTPStatus .OK , channel .code , channel .result )
450
+
312
451
def test_delayed_state_events_are_cancelled_by_more_recent_state (self ) -> None :
313
452
state_key = "to_be_cancelled"
314
453
@@ -374,3 +513,7 @@ def _get_path_for_delayed_state(
374
513
room_id : str , event_type : str , state_key : str , delay_ms : int
375
514
) -> str :
376
515
return f"rooms/{ room_id } /state/{ event_type } /{ state_key } ?org.matrix.msc4140.delay={ delay_ms } "
516
+
517
+
518
+ def _get_path_for_delayed_send (room_id : str , event_type : str , delay_ms : int ) -> str :
519
+ return f"rooms/{ room_id } /send/{ event_type } ?org.matrix.msc4140.delay={ delay_ms } "
0 commit comments