-
-
Notifications
You must be signed in to change notification settings - Fork 126
/
test_decorators.py
988 lines (768 loc) · 29.5 KB
/
test_decorators.py
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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
863
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
import asyncio
import logging
from functools import wraps
from unittest import mock
import hiro
from flask import Blueprint, Flask, current_app, g, make_response, request
from werkzeug.exceptions import BadRequest
from flask_limiter import ExemptionScope, Limiter
from flask_limiter.util import get_remote_address
def get_ip_from_header():
return request.headers.get("Test-IP") or "127.0.0.1"
def test_multiple_decorators(extension_factory):
app, limiter = extension_factory(key_func=get_ip_from_header)
@app.route("/t1")
@limiter.limit(
"100 per minute", key_func=lambda: "test"
) # effectively becomes a limit for all users
@limiter.limit("50/minute") # per ip as per default key_func
def t1():
return "test"
with hiro.Timeline().freeze():
with app.test_client() as cli:
for i in range(0, 100):
assert (200 if i < 50 else 429) == cli.get(
"/t1", headers={"Test-IP": "127.0.0.2"}
).status_code
for i in range(50):
assert 200 == cli.get("/t1").status_code
assert 429 == cli.get("/t1").status_code
assert 429 == cli.get("/t1", headers={"Test-IP": "127.0.0.3"}).status_code
def test_exempt_routes(extension_factory):
app, limiter = extension_factory(
default_limits=["1/minute"], application_limits=["2/minute"]
)
@app.route("/t1")
def t1():
return "test"
@app.route("/t2")
@limiter.exempt
def t2():
return "test"
@app.route("/t3")
@limiter.exempt(flags=ExemptionScope.APPLICATION)
def t3():
return "test"
@app.route("/t4")
def t4():
return "test"
with app.test_client() as cli:
assert cli.get("/t1").status_code == 200
assert cli.get("/t1").status_code == 429
# exempt from default application
assert cli.get("/t2").status_code == 200
assert cli.get("/t2").status_code == 200
# exempt from application
assert cli.get("/t3").status_code == 200
assert cli.get("/t3").status_code == 429
# 2/minute for application is now taken up
assert cli.get("/t4").status_code == 429
def test_decorated_limit_with_scope(extension_factory):
app, limiter = extension_factory()
@app.route("/t/<path:path>")
@limiter.limit("1/second", scope=lambda _: request.view_args["path"])
def t(path):
return "test"
with hiro.Timeline():
with app.test_client() as cli:
assert cli.get("/t/1").status_code == 200
assert cli.get("/t/1").status_code == 429
assert cli.get("/t/2").status_code == 200
assert cli.get("/t/2").status_code == 429
def test_decorated_limit_with_conditional_deduction(extension_factory):
app, limiter = extension_factory()
@app.route("/t/<path:path>")
@limiter.limit("1/second", deduct_when=lambda resp: resp.status_code == 200)
@limiter.limit("1/minute", deduct_when=lambda resp: resp.status_code == 400)
def t(path):
if path == "1":
return "test"
raise BadRequest()
with hiro.Timeline() as timeline:
with app.test_client() as cli:
assert cli.get("/t/1").status_code == 200
assert cli.get("/t/1").status_code == 429
timeline.forward(1)
assert cli.get("/t/2").status_code == 400
timeline.forward(1)
assert cli.get("/t/1").status_code == 429
assert cli.get("/t/2").status_code == 429
timeline.forward(60)
assert cli.get("/t/1").status_code == 200
def test_shared_limit_with_conditional_deduction(extension_factory):
app, limiter = extension_factory()
bp = Blueprint("main", __name__)
limit = limiter.shared_limit(
"2/minute",
"not_found",
deduct_when=lambda response: response.status_code == 400,
)
@app.route("/test/<path:path>")
@limit
def app_test(path):
if path != "1":
raise BadRequest()
return path
@bp.route("/test/<path:path>")
def bp_test(path):
if path != "1":
raise BadRequest()
return path
limit(bp)
app.register_blueprint(bp, url_prefix="/bp")
with hiro.Timeline() as timeline:
with app.test_client() as cli:
assert cli.get("/bp/test/1").status_code == 200
assert cli.get("/bp/test/1").status_code == 200
assert cli.get("/test/1").status_code == 200
assert cli.get("/bp/test/2").status_code == 400
assert cli.get("/test/2").status_code == 400
assert cli.get("/bp/test/2").status_code == 429
assert cli.get("/bp/test/1").status_code == 429
assert cli.get("/test/1").status_code == 429
assert cli.get("/test/2").status_code == 429
timeline.forward(60)
assert cli.get("/bp/test/1").status_code == 200
assert cli.get("/test/1").status_code == 200
def test_header_ordering_with_conditional_deductions(extension_factory):
app, limiter = extension_factory(default_limits=["3/second"], headers_enabled=True)
@app.route("/test_combined/<path:path>")
@limiter.limit(
"1/hour",
override_defaults=False,
deduct_when=lambda response: response.status_code != 200,
)
@limiter.limit(
"4/minute",
override_defaults=False,
deduct_when=lambda response: response.status_code == 200,
)
def app_test_combined(path):
if path != "1":
raise BadRequest()
return path
@app.route("/test/<path:path>")
@limiter.limit("2/hour", deduct_when=lambda response: response.status_code != 200)
def app_test(path):
if path != "1":
raise BadRequest()
return path
with hiro.Timeline() as timeline:
with app.test_client() as cli:
assert cli.get("/test_combined/1").status_code == 200
resp = cli.get("/test_combined/1")
assert resp.status_code == 200
assert resp.headers.get("X-RateLimit-Limit") == "3"
assert resp.headers.get("X-RateLimit-Remaining") == "1"
assert cli.get("/test_combined/2").status_code == 400
resp = cli.get("/test/1")
assert resp.headers.get("X-RateLimit-Limit") == "2"
assert resp.headers.get("X-RateLimit-Remaining") == "2"
resp = cli.get("/test/2")
assert resp.headers.get("X-RateLimit-Limit") == "2"
assert resp.headers.get("X-RateLimit-Remaining") == "1"
timeline.forward(1)
resp = cli.get("/test_combined/1")
assert resp.status_code == 429
assert resp.headers.get("X-RateLimit-Limit") == "1"
assert resp.headers.get("X-RateLimit-Remaining") == "0"
assert cli.get("/test_combined/2").status_code == 429
timeline.forward(60)
assert cli.get("/test_combined/1").status_code == 429
assert cli.get("/test_combined/2").status_code == 429
timeline.forward(3600)
assert cli.get("/test_combined/1").status_code == 200
def test_decorated_limits_with_combined_defaults(extension_factory):
app, limiter = extension_factory(default_limits=["2/minute"])
@app.route("/")
@limiter.limit("1/second", override_defaults=False)
def root():
return "root"
with hiro.Timeline() as timeline:
with app.test_client() as cli:
assert 200 == cli.get("/").status_code
assert 429 == cli.get("/").status_code
timeline.forward(60)
assert 200 == cli.get("/").status_code
timeline.forward(1)
assert 200 == cli.get("/").status_code
timeline.forward(1)
assert 429 == cli.get("/").status_code
def test_decorated_limit_with_combined_defaults_per_method(extension_factory):
app, limiter = extension_factory(
default_limits=["2/minute"], default_limits_per_method=True
)
@app.route("/", methods=["GET", "PUT"])
@limiter.limit("1/second", override_defaults=False, methods=["GET"])
def root():
return "root"
with hiro.Timeline() as timeline:
with app.test_client() as cli:
assert 200 == cli.get("/").status_code
assert 429 == cli.get("/").status_code
assert 200 == cli.put("/").status_code
assert 200 == cli.put("/").status_code
assert 429 == cli.put("/").status_code
timeline.forward(60)
assert 200 == cli.get("/").status_code
assert 200 == cli.put("/").status_code
timeline.forward(1)
assert 200 == cli.get("/").status_code
assert 200 == cli.put("/").status_code
timeline.forward(1)
assert 429 == cli.get("/").status_code
assert 429 == cli.put("/").status_code
def test_decorated_dynamic_limits(extension_factory):
app, limiter = extension_factory({"X": "2 per second"}, default_limits=["1/second"])
def request_context_limit():
limits = {"127.0.0.1": "10 per minute", "127.0.0.2": "1 per minute"}
remote_addr = request.headers.get("Test-IP").split(",")[0] or "127.0.0.1"
limit = limits.setdefault(remote_addr, "1 per minute")
return limit
@app.route("/t1")
@limiter.limit("20/day")
@limiter.limit(lambda: current_app.config.get("X"))
@limiter.limit(request_context_limit)
def t1():
return "42"
@app.route("/t2")
@limiter.limit(lambda: current_app.config.get("X"))
def t2():
return "42"
R1 = {"Test-IP": "127.0.0.1, 127.0.0.0"}
R2 = {"Test-IP": "127.0.0.2"}
with app.test_client() as cli:
with hiro.Timeline().freeze() as timeline:
for i in range(0, 10):
assert cli.get("/t1", headers=R1).status_code == 200
timeline.forward(1)
assert cli.get("/t1", headers=R1).status_code == 429
assert cli.get("/t1", headers=R2).status_code == 200
assert cli.get("/t1", headers=R2).status_code == 429
timeline.forward(60)
assert cli.get("/t1", headers=R2).status_code == 200
assert cli.get("/t2").status_code == 200
assert cli.get("/t2").status_code == 200
assert cli.get("/t2").status_code == 429
timeline.forward(1)
assert cli.get("/t2").status_code == 200
def test_invalid_decorated_dynamic_limits(caplog):
caplog.set_level(logging.INFO)
app = Flask(__name__)
app.config.setdefault("X", "2 per sec")
limiter = Limiter(get_ip_from_header, app=app, default_limits=["1/second"])
@app.route("/t1")
@limiter.limit(lambda: current_app.config.get("X"))
def t1():
return "42"
with app.test_client() as cli:
with hiro.Timeline().freeze():
assert cli.get("/t1").status_code == 200
assert cli.get("/t1").status_code == 429
# 2 for invalid limit, 1 for warning.
assert len(caplog.records) == 3
assert "failed to load ratelimit" in caplog.records[0].msg
assert "failed to load ratelimit" in caplog.records[1].msg
assert "exceeded at endpoint" in caplog.records[2].msg
assert caplog.records[2].levelname == "INFO"
def test_decorated_limit_empty_exempt(caplog):
app = Flask(__name__)
limiter = Limiter(get_remote_address, app=app)
@app.route("/t1")
@limiter.limit(lambda: "")
def t1():
return "42"
with app.test_client() as cli:
with hiro.Timeline().freeze():
assert cli.get("/t1").status_code == 200
assert cli.get("/t1").status_code == 200
assert not caplog.records
def test_invalid_decorated_static_limits(caplog):
caplog.set_level(logging.INFO)
app = Flask(__name__)
limiter = Limiter(get_ip_from_header, app=app, default_limits=["1/second"])
@app.route("/t1")
@limiter.limit("2/sec")
def t1():
return "42"
with app.test_client() as cli:
with hiro.Timeline().freeze():
assert cli.get("/t1").status_code == 200
assert cli.get("/t1").status_code == 429
assert "failed to load" in caplog.records[0].msg
assert "exceeded at endpoint" in caplog.records[-1].msg
def test_named_shared_limit(extension_factory):
app, limiter = extension_factory()
shared_limit_a = limiter.shared_limit("1/minute", scope="a")
shared_limit_b = limiter.shared_limit("1/minute", scope="b")
@app.route("/t1")
@shared_limit_a
def route1():
return "route1"
@app.route("/t2")
@shared_limit_a
def route2():
return "route2"
@app.route("/t3")
@shared_limit_b
def route3():
return "route3"
with hiro.Timeline().freeze():
with app.test_client() as cli:
assert 200 == cli.get("/t1").status_code
assert 200 == cli.get("/t3").status_code
assert 429 == cli.get("/t2").status_code
def test_dynamic_shared_limit(extension_factory):
app, limiter = extension_factory()
fn_a = mock.Mock()
fn_b = mock.Mock()
fn_a.return_value = "foo"
fn_b.return_value = "bar"
dy_limit_a = limiter.shared_limit("1/minute", scope=fn_a)
dy_limit_b = limiter.shared_limit("1/minute", scope=fn_b)
@app.route("/t1")
@dy_limit_a
def t1():
return "route1"
@app.route("/t2")
@dy_limit_a
def t2():
return "route2"
@app.route("/t3")
@dy_limit_b
def t3():
return "route3"
with hiro.Timeline().freeze():
with app.test_client() as cli:
assert 200 == cli.get("/t1").status_code
assert 200 == cli.get("/t3").status_code
assert 429 == cli.get("/t2").status_code
assert 429 == cli.get("/t3").status_code
assert 2 == fn_a.call_count
assert 2 == fn_b.call_count
fn_b.assert_called_with("t3")
fn_a.assert_has_calls([mock.call("t1"), mock.call("t2")])
def test_conditional_limits():
"""Test that the conditional activation of the limits work."""
app = Flask(__name__)
limiter = Limiter(get_ip_from_header, app=app)
@app.route("/limited")
@limiter.limit("1 per day")
def limited_route():
return "passed"
@app.route("/unlimited")
@limiter.limit("1 per day", exempt_when=lambda: True)
def never_limited_route():
return "should always pass"
is_exempt = False
@app.route("/conditional")
@limiter.limit("1 per day", exempt_when=lambda: is_exempt)
def conditionally_limited_route():
return "conditional"
with app.test_client() as cli:
assert cli.get("/limited").status_code == 200
assert cli.get("/limited").status_code == 429
assert cli.get("/unlimited").status_code == 200
assert cli.get("/unlimited").status_code == 200
assert cli.get("/conditional").status_code == 200
assert cli.get("/conditional").status_code == 429
is_exempt = True
assert cli.get("/conditional").status_code == 200
is_exempt = False
assert cli.get("/conditional").status_code == 429
def test_conditional_shared_limits():
"""Test that conditional shared limits work."""
app = Flask(__name__)
limiter = Limiter(get_ip_from_header, app=app)
@app.route("/limited")
@limiter.shared_limit("1 per day", "test_scope")
def limited_route():
return "passed"
@app.route("/unlimited")
@limiter.shared_limit("1 per day", "test_scope", exempt_when=lambda: True)
def never_limited_route():
return "should always pass"
is_exempt = False
@app.route("/conditional")
@limiter.shared_limit("1 per day", "test_scope", exempt_when=lambda: is_exempt)
def conditionally_limited_route():
return "conditional"
with app.test_client() as cli:
assert cli.get("/unlimited").status_code == 200
assert cli.get("/unlimited").status_code == 200
assert cli.get("/limited").status_code == 200
assert cli.get("/limited").status_code == 429
assert cli.get("/conditional").status_code == 429
is_exempt = True
assert cli.get("/conditional").status_code == 200
is_exempt = False
assert cli.get("/conditional").status_code == 429
def test_whitelisting():
app = Flask(__name__)
limiter = Limiter(
get_ip_from_header,
app=app,
default_limits=["1/minute"],
headers_enabled=True,
)
@app.route("/")
def t():
return "test"
@limiter.request_filter
def w():
if request.headers.get("internal", None) == "true":
return True
return False
with hiro.Timeline().freeze() as timeline:
with app.test_client() as cli:
assert cli.get("/").status_code == 200
assert cli.get("/").status_code == 429
timeline.forward(60)
assert cli.get("/").status_code == 200
for i in range(0, 10):
assert cli.get("/", headers={"internal": "true"}).status_code == 200
def test_separate_method_limits(extension_factory):
app, limiter = extension_factory()
@app.route("/", methods=["GET", "POST"])
@limiter.limit("1/second", per_method=True)
def root():
return "root"
with hiro.Timeline():
with app.test_client() as cli:
assert 200 == cli.get("/").status_code
assert 429 == cli.get("/").status_code
assert 200 == cli.post("/").status_code
assert 429 == cli.post("/").status_code
def test_explicit_method_limits(extension_factory):
app, limiter = extension_factory(default_limits=["2/second"])
@app.route("/", methods=["GET", "POST"])
@limiter.limit("1/second", methods=["GET"])
def root():
return "root"
with hiro.Timeline():
with app.test_client() as cli:
assert 200 == cli.get("/").status_code
assert 429 == cli.get("/").status_code
assert 200 == cli.post("/").status_code
assert 200 == cli.post("/").status_code
assert 429 == cli.post("/").status_code
def test_decorated_limit_immediate(extension_factory):
app, limiter = extension_factory(default_limits=["1/minute"])
def append_info(fn):
@wraps(fn)
def __inner(*args, **kwargs):
g.rate_limit = "2/minute"
return fn(*args, **kwargs)
return __inner
@app.route("/", methods=["GET", "POST"])
@append_info
@limiter.limit(lambda: g.rate_limit, per_method=True)
def root():
return "root"
with hiro.Timeline().freeze():
with app.test_client() as cli:
assert 200 == cli.get("/").status_code
assert 200 == cli.get("/").status_code
assert 429 == cli.get("/").status_code
def test_decorated_shared_limit_immediate(extension_factory):
app, limiter = extension_factory(default_limits=["1/minute"])
shared = limiter.shared_limit(lambda: g.rate_limit, "shared")
def append_info(fn):
@wraps(fn)
def __inner(*args, **kwargs):
g.rate_limit = "2/minute"
return fn(*args, **kwargs)
return __inner
@app.route("/", methods=["GET", "POST"])
@append_info
@shared
def root():
return "root"
@app.route("/other", methods=["GET", "POST"])
def other():
return "other"
with hiro.Timeline().freeze():
with app.test_client() as cli:
assert 200 == cli.get("/other").status_code
assert 429 == cli.get("/other").status_code
assert 200 == cli.get("/").status_code
assert 200 == cli.get("/").status_code
assert 429 == cli.get("/").status_code
def test_async_route(extension_factory):
app, limiter = extension_factory()
@app.route("/t1")
@limiter.limit("1/minute")
async def t1():
await asyncio.sleep(0.01)
return "test"
@app.route("/t2")
@limiter.limit("1/minute")
@limiter.exempt
async def t2():
await asyncio.sleep(0.01)
return "test"
with app.test_client() as cli:
assert cli.get("/t1").status_code == 200
assert cli.get("/t1").status_code == 429
assert cli.get("/t2").status_code == 200
assert cli.get("/t2").status_code == 200
def test_on_breach_callback_swallow_errors(extension_factory, caplog):
app, limiter = extension_factory(swallow_errors=True)
callbacks = []
def on_breach(request_limit):
callbacks.append(request_limit)
def failed_on_breach(request_limit):
1 / 0
@app.route("/")
@limiter.limit("1/second", on_breach=on_breach)
def root():
return "root"
@app.route("/other")
@limiter.limit("1/second")
def other():
return "other"
@app.route("/fail")
@limiter.limit("1/second", on_breach=failed_on_breach)
def fail():
return "fail"
with app.test_client() as cli:
assert cli.get("/").status_code == 200
assert cli.get("/").status_code == 429
assert cli.get("/other").status_code == 200
assert cli.get("/other").status_code == 429
assert cli.get("/fail").status_code == 200
assert cli.get("/fail").status_code == 429
assert len(callbacks) == 1
log = caplog.records[-1]
assert log.message == "on_breach callback failed with error division by zero"
assert log.levelname == "ERROR"
def test_on_breach_callback_custom_response(extension_factory):
def on_breach_no_response(request_limit):
pass
def on_breach_with_response(request_limit):
return make_response(
f"custom response {request_limit.limit} @ {request.path}", 429
)
def default_on_breach_with_response(request_limit):
return make_response(
f"default custom response {request_limit.limit} @ {request.path}", 429
)
def on_breach_invalid(): ...
def on_breach_fail(request_limit):
1 / 0
app, limiter = extension_factory(on_breach=default_on_breach_with_response)
@app.route("/")
@limiter.limit("1/second", on_breach=on_breach_no_response)
def root():
return "root"
@app.route("/t1")
@limiter.limit("1/second")
def t1():
return "t1"
@app.route("/t2")
@limiter.limit("1/second", on_breach=on_breach_with_response)
def t2():
return "t2"
@app.route("/t3")
@limiter.limit("1/second", on_breach=on_breach_invalid)
def t3():
return "t3"
@app.route("/t4")
@limiter.limit("1/second", on_breach=on_breach_fail)
def t4():
return "t4"
with app.test_client() as cli:
assert cli.get("/").status_code == 200
resp = cli.get("/")
assert resp.status_code == 429
assert resp.text == "default custom response 1 per 1 second @ /"
assert cli.get("/t1").status_code == 200
resp = cli.get("/t1")
assert resp.status_code == 429
assert resp.text == "default custom response 1 per 1 second @ /t1"
assert cli.get("/t2").status_code == 200
resp = cli.get("/t2")
assert resp.status_code == 429
assert resp.text == "custom response 1 per 1 second @ /t2"
resp = cli.get("/t3")
assert resp.status_code == 200
resp = cli.get("/t3")
assert resp.status_code == 500
resp = cli.get("/t4")
assert resp.status_code == 200
resp = cli.get("/t4")
assert resp.status_code == 500
def test_limit_multiple_cost(extension_factory):
app, limiter = extension_factory()
@app.route("/root")
@limiter.limit("4/second", cost=2)
def root():
return "root"
with hiro.Timeline().freeze():
with app.test_client() as cli:
assert 200 == cli.get("/root").status_code
assert 200 == cli.get("/root").status_code
assert 429 == cli.get("/root").status_code
def test_limit_multiple_cost_callable(extension_factory):
app, limiter = extension_factory()
@app.route("/root")
@limiter.limit("4/second", cost=lambda: 2)
def root():
return "root"
with hiro.Timeline().freeze():
with app.test_client() as cli:
assert 200 == cli.get("/root").status_code
assert 200 == cli.get("/root").status_code
assert 429 == cli.get("/root").status_code
def test_shared_limit_multiple_cost(extension_factory):
app, limiter = extension_factory()
shared_limit = limiter.shared_limit("4/minute", scope="a", cost=2)
@app.route("/t1")
@shared_limit
def route1():
return "route1"
@app.route("/t2")
@shared_limit
def route2():
return "route2"
with hiro.Timeline().freeze():
with app.test_client() as cli:
assert 200 == cli.get("/t1").status_code
assert 200 == cli.get("/t2").status_code
assert 429 == cli.get("/t2").status_code
def test_shared_limit_multiple_cost_callable(extension_factory):
app, limiter = extension_factory()
shared_limit = limiter.shared_limit("4/minute", scope="a", cost=lambda: 2)
@app.route("/t1")
@shared_limit
def route1():
return "route1"
@app.route("/t2")
@shared_limit
def route2():
return "route2"
with hiro.Timeline().freeze():
with app.test_client() as cli:
assert 200 == cli.get("/t1").status_code
assert 200 == cli.get("/t2").status_code
assert 429 == cli.get("/t2").status_code
def test_non_route_decoration_static_limits_override_defaults(extension_factory):
app, limiter = extension_factory(default_limits=["1/second"])
@limiter.limit("2/second")
def limited():
return "limited"
@app.route("/t1")
def route1():
return "t1"
@app.route("/t2")
@limiter.limit("2/second")
def route2():
return "t2"
@app.route("/t3")
def route3():
return limited()
@app.route("/t4")
def route4():
@limiter.limit("2/day", override_defaults=False)
def __inner():
return "inner"
return __inner()
@app.route("/t5/<int:param>")
def route5(param: int):
@limiter.limit("2/day", override_defaults=False)
def __inner1():
return "inner1"
@limiter.limit("3/day")
def __inner2():
return "inner2"
return __inner1() if param < 10 else __inner2()
with hiro.Timeline().freeze() as timeline:
with app.test_client() as cli:
assert 200 == cli.get("/t1").status_code
assert 429 == cli.get("/t1").status_code
for i in range(2):
assert 200 == cli.get("/t2").status_code
assert 429 == cli.get("/t2").status_code
for i in range(2):
assert 200 == cli.get("/t3").status_code, i
assert 429 == cli.get("/t3").status_code
assert 200 == cli.get("/t4").status_code
assert 429 == cli.get("/t4").status_code
timeline.forward(1)
assert 200 == cli.get("/t4").status_code
timeline.forward(1)
assert 429 == cli.get("/t4").status_code
assert 200 == cli.get("/t5/1").status_code
assert 429 == cli.get("/t5/1").status_code
timeline.forward(1)
assert 200 == cli.get("/t5/1").status_code
timeline.forward(1)
assert 429 == cli.get("/t5/1").status_code
timeline.forward(60 * 60 * 24)
assert 200 == cli.get("/t5/11").status_code
assert 200 == cli.get("/t5/11").status_code
assert 200 == cli.get("/t5/11").status_code
assert 429 == cli.get("/t5/11").status_code
def test_non_route_decoration_static_limits(extension_factory):
app, limiter = extension_factory()
@limiter.limit("1/second")
def limited():
return "limited"
@app.route("/t1")
def route1():
return limited()
with hiro.Timeline().freeze():
with app.test_client() as cli:
assert 200 == cli.get("/t1").status_code
assert 429 == cli.get("/t1").status_code
def test_non_route_decoration_dynamic_limits(extension_factory):
app, limiter = extension_factory()
def dynamic_limit_provider():
return "1/second"
@limiter.limit(dynamic_limit_provider)
def limited():
return "limited"
@app.route("/t1")
def route1():
return limited()
with hiro.Timeline().freeze():
with app.test_client() as cli:
assert 200 == cli.get("/t1").status_code
assert 429 == cli.get("/t1").status_code
def test_non_route_decoration_multiple_sequential_limits_per_request(extension_factory):
app, limiter = extension_factory()
@limiter.limit("10/second")
def l1():
return "l1"
@limiter.limit("1/second")
def l2():
return "l2"
@app.route("/t1")
def route1():
return l1() l2()
with hiro.Timeline().freeze():
with app.test_client() as cli:
assert 200 == cli.get("/t1").status_code
assert 429 == cli.get("/t1").status_code
def test_inner_function_decoration(extension_factory):
app, limiter = extension_factory()
@app.route("/t1")
def route1():
@limiter.limit("5/second")
def l1():
return "l1"
return l1()
@app.route("/t2")
def route2():
@limiter.limit("1/second")
def l1():
return "l1"
return l1()
with hiro.Timeline().freeze():
with app.test_client() as cli:
assert 200 == cli.get("/t1").status_code
assert 200 == cli.get("/t2").status_code
for _ in range(4):
assert 200 == cli.get("/t1").status_code
assert 429 == cli.get("/t1").status_code
assert 429 == cli.get("/t2").status_code