Completed
Push — master ( 57b903...efd80c )
by Ali-Akber
24s
created

RegressionTests.build_app()   A

Complexity

Conditions 2

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 2
dl 0
loc 9
rs 9.6666
c 2
b 0
f 0
1
"""
2
3
"""
4
import time
5
6
import hiro
7
8
from flask_limiter.extension import C
9
from tests import FlaskLimiterTestCase
10
11
12
class RegressionTests(FlaskLimiterTestCase):
13
14
15
    def test_redis_request_slower_than_fixed_window(self):
16
        app, limiter = self.build_app({
17
            C.GLOBAL_LIMITS: "5 per second",
18
            C.STORAGE_URL: "redis://localhost:6379",
19
            C.STRATEGY: "fixed-window",
20
            C.HEADERS_ENABLED: True
21
        })
22
23
        @app.route("/t1")
24
        def t1():
25
            time.sleep(1.1)
26
            return "t1"
27
28
        with app.test_client() as cli:
29
            resp = cli.get("/t1")
30
            self.assertEqual(
31
                    resp.headers["X-RateLimit-Remaining"],
32
                    '5'
33
            )
34
35
    def test_redis_request_slower_than_moving_window(self):
36
        app, limiter = self.build_app({
37
            C.GLOBAL_LIMITS: "5 per second",
38
            C.STORAGE_URL: "redis://localhost:6379",
39
            C.STRATEGY: "moving-window",
40
            C.HEADERS_ENABLED: True
41
        })
42
43
        @app.route("/t1")
44
        def t1():
45
            time.sleep(1.1)
46
            return "t1"
47
48
        with app.test_client() as cli:
49
            resp = cli.get("/t1")
50
            self.assertEqual(
51
                    resp.headers["X-RateLimit-Remaining"],
52
                    '5'
53
            )
54
55
    def test_dynamic_limits(self):
56
        app, limiter = self.build_app({
57
            C.STRATEGY: "moving-window",
58
            C.HEADERS_ENABLED: True
59
        })
60
61
        def func(*a):
62
            return "1/second; 2/minute"
63
64
        @app.route("/t1")
65
        @limiter.limit(func)
66
        def t1():
67
            return "t1"
68
69
        with hiro.Timeline().freeze() as timeline:
70
            with app.test_client() as cli:
71
                self.assertEqual(cli.get("/t1").status_code, 200)
72 View Code Duplication
                self.assertEqual(cli.get("/t1").status_code, 429)
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
73
                timeline.forward(2)
74
                self.assertEqual(cli.get("/t1").status_code, 200)
75
                self.assertEqual(cli.get("/t1").status_code, 429)
76
77
    def test_invalid_ratelimit_key(self):
78
        app, limiter = self.build_app(
79
            {C.HEADERS_ENABLED: True}
80
        )
81
82
        def func(*a):
83
            return None
84
85
        @app.route("/t1")
86
        @limiter.limit("2/second", key_func=func)
87
        def t1():
88
            return "t1"
89
90
        with app.test_client() as cli:
91
            cli.get("/t1")
92
            cli.get("/t1")
93
            cli.get("/t1")
94
            self.assertEqual(cli.get("/t1").status_code, 200)
95
            limiter.limit("1/second", key_func=lambda: "key")(t1)
96
            cli.get("/t1")
97
            self.assertEqual(cli.get("/t1").status_code, 429)
98