Skip to content

Commit b2596e2

Browse files
committed
Add test cases for set_rate_limit_group
1 parent cbb0f52 commit b2596e2

File tree

1 file changed

+97
-0
lines changed

1 file changed

+97
-0
lines changed
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
import pytest
2+
from aikido_zen.context import get_current_context, Context
3+
from aikido_zen.thread.thread_cache import get_cache
4+
from .set_rate_limit_group import set_rate_limit_group
5+
6+
7+
@pytest.fixture(autouse=True)
8+
def run_around_tests():
9+
get_cache().reset()
10+
yield
11+
# Reset context and cache after every test
12+
from aikido_zen.context import current_context
13+
14+
current_context.set(None)
15+
get_cache().reset()
16+
17+
18+
def set_context_and_lifecycle():
19+
wsgi_request = {
20+
"REQUEST_METHOD": "GET",
21+
"HTTP_HEADER_1": "header 1 value",
22+
"HTTP_HEADER_2": "Header 2 value",
23+
"RANDOM_VALUE": "Random value",
24+
"HTTP_COOKIE": "sessionId=abc123xyz456;",
25+
"wsgi.url_scheme": "http",
26+
"HTTP_HOST": "localhost:8080",
27+
"PATH_INFO": "/hello",
28+
"QUERY_STRING": "user=JohnDoe&age=30&age=35",
29+
"CONTENT_TYPE": "application/json",
30+
"REMOTE_ADDR": "198.51.100.23",
31+
}
32+
context = Context(
33+
req=wsgi_request,
34+
body=None,
35+
source="flask",
36+
)
37+
context.set_as_current_context()
38+
return context
39+
40+
41+
def test_set_rate_limit_group_valid_group_id(caplog):
42+
context1 = set_context_and_lifecycle()
43+
assert context1.rate_limit_group is None
44+
set_rate_limit_group("group1")
45+
assert context1.rate_limit_group == "group1"
46+
assert "Group ID cannot be empty." not in caplog.text
47+
assert "was called without a context" not in caplog.text
48+
assert "must be called before the Zen middleware is executed" not in caplog.text
49+
50+
51+
def test_set_rate_limit_group_empty_group_id(caplog):
52+
context1 = set_context_and_lifecycle()
53+
assert context1.rate_limit_group is None
54+
set_rate_limit_group("")
55+
assert context1.rate_limit_group is None
56+
assert "Group ID cannot be empty." in caplog.text
57+
58+
59+
def test_set_rate_limit_group_none_group_id(caplog):
60+
context1 = set_context_and_lifecycle()
61+
assert context1.rate_limit_group is None
62+
set_rate_limit_group(None)
63+
assert context1.rate_limit_group is None
64+
assert "Group ID cannot be empty." in caplog.text
65+
66+
67+
def test_set_rate_limit_group_no_context(caplog):
68+
from aikido_zen.context import current_context
69+
70+
current_context.set(None)
71+
set_rate_limit_group("group1")
72+
assert "was called without a context" in caplog.text
73+
74+
75+
def test_set_rate_limit_group_middleware_already_executed(caplog):
76+
context1 = set_context_and_lifecycle()
77+
context1.executed_middleware = True
78+
set_rate_limit_group("group1")
79+
assert "must be called before the Zen middleware is executed" in caplog.text
80+
assert context1.rate_limit_group is "group1"
81+
82+
83+
def test_set_rate_limit_group_non_string_group_id(caplog):
84+
context1 = set_context_and_lifecycle()
85+
assert context1.rate_limit_group is None
86+
set_rate_limit_group(123)
87+
assert context1.rate_limit_group is None
88+
assert "Group ID cannot be empty." in caplog.text
89+
90+
91+
def test_set_rate_limit_group_overwrite_existing_group():
92+
context1 = set_context_and_lifecycle()
93+
assert context1.rate_limit_group is None
94+
set_rate_limit_group("group1")
95+
assert context1.rate_limit_group == "group1"
96+
set_rate_limit_group("group2")
97+
assert context1.rate_limit_group == "group2"

0 commit comments

Comments
 (0)