Completed
Push — master ( fb464e...f55f23 )
by Ali-Akber
27s
created

test_custom_key_prefix_with_headers()   B

Complexity

Conditions 5

Size

Total Lines 32

Duplication

Lines 4
Ratio 12.5 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
c 1
b 0
f 0
dl 4
loc 32
rs 8.0894

1 Method

Rating   Name   Duplication   Size   Complexity  
A RegressionTests.t2() 0 4 1
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
    def xest_redis_request_slower_than_fixed_window(self):
14
        app, limiter = self.build_app({
15
            C.GLOBAL_LIMITS: "5 per second",
16
            C.STORAGE_URL: "redis://localhost:6379",
17
            C.STRATEGY: "fixed-window",
18
            C.HEADERS_ENABLED: True
19
        })
20
21
        @app.route("/t1")
22
        def t1():
23
            time.sleep(1.1)
24
            return "t1"
25
26
        with app.test_client() as cli:
27
            resp = cli.get("/t1")
28
            self.assertEqual(resp.headers["X-RateLimit-Remaining"], '5')
29
30
    def test_redis_request_slower_than_moving_window(self):
31
        app, limiter = self.build_app({
32
            C.GLOBAL_LIMITS: "5 per second",
33
            C.STORAGE_URL: "redis://localhost:6379",
34
            C.STRATEGY: "moving-window",
35
            C.HEADERS_ENABLED: True
36
        })
37
38
        @app.route("/t1")
39
        def t1():
40
            time.sleep(1.1)
41
            return "t1"
42
43
        with app.test_client() as cli:
44
            resp = cli.get("/t1")
45
            self.assertEqual(resp.headers["X-RateLimit-Remaining"], '5')
46
47
    def test_dynamic_limits(self):
48
        app, limiter = self.build_app({
49
            C.STRATEGY: "moving-window",
50
            C.HEADERS_ENABLED: True
51
        })
52
53
        def func(*a):
54
            return "1/second; 2/minute"
55
56
        @app.route("/t1")
57
        @limiter.limit(func)
58
        def t1():
59
            return "t1"
60
61
        with hiro.Timeline().freeze() as timeline:
62
            with app.test_client() as cli:
63
                self.assertEqual(cli.get("/t1").status_code, 200)
64
                self.assertEqual(cli.get("/t1").status_code, 429)
65
                timeline.forward(2)
66
                self.assertEqual(cli.get("/t1").status_code, 200)
67
                self.assertEqual(cli.get("/t1").status_code, 429)
68
69
    def test_invalid_ratelimit_key(self):
70
        app, limiter = self.build_app({C.HEADERS_ENABLED: True})
71
72 View Code Duplication
        def func(*a):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
73
            return None
74
75
        @app.route("/t1")
76
        @limiter.limit("2/second", key_func=func)
77
        def t1():
78
            return "t1"
79
80
        with app.test_client() as cli:
81
            cli.get("/t1")
82
            cli.get("/t1")
83
            cli.get("/t1")
84
            self.assertEqual(cli.get("/t1").status_code, 200)
85
            limiter.limit("1/second", key_func=lambda: "key")(t1)
86
            cli.get("/t1")
87
            self.assertEqual(cli.get("/t1").status_code, 429)
88
89
    def test_custom_key_prefix_with_headers(self):
90
        app1, limiter1 = self.build_app(key_prefix="moo", storage_uri="redis://localhost:6379", headers_enabled=True)
91
        app2, limiter2 = self.build_app(key_prefix="cow", storage_uri="redis://localhost:6379", headers_enabled=True)
92
93
        @app1.route("/test")
94
        @limiter1.limit("1/minute")
95
        def t1():
96
            return "app1 test"
97
98
        @app2.route("/test")
99
        @limiter2.limit("1/minute")
100
        def t2():
101
            return "app2 test"
102
103
        with app1.test_client() as cli:
104
            resp = cli.get("/test")
105
            self.assertEqual(200, resp.status_code)
106
            resp = cli.get("/test")
107
            self.assertEqual(
108
                resp.headers.get('Retry-After'),
109
                str(60)
110
            )
111
            self.assertEqual(429, resp.status_code)
112
        with app2.test_client() as cli:
113
            resp = cli.get("/test")
114
            self.assertEqual(200, resp.status_code)
115
            resp = cli.get("/test")
116
            self.assertEqual(
117
                resp.headers.get('Retry-After'),
118
                str(60)
119
            )
120
            self.assertEqual(429, resp.status_code)
121