11import pytest
2- from aikido_zen .context import get_current_context , Context
32from aikido_zen .thread .thread_cache import get_cache
3+ import aikido_zen .test_utils as test_utils
44from .set_rate_limit_group import set_rate_limit_group
55
66
@@ -15,31 +15,8 @@ def run_around_tests():
1515 get_cache ().reset ()
1616
1717
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-
4118def test_set_rate_limit_group_valid_group_id (caplog ):
42- context1 = set_context_and_lifecycle ()
19+ context1 = test_utils . generate_and_set_context ()
4320 assert context1 .rate_limit_group is None
4421 set_rate_limit_group ("group1" )
4522 assert context1 .rate_limit_group == "group1"
@@ -49,15 +26,15 @@ def test_set_rate_limit_group_valid_group_id(caplog):
4926
5027
5128def test_set_rate_limit_group_empty_group_id (caplog ):
52- context1 = set_context_and_lifecycle ()
29+ context1 = test_utils . generate_and_set_context ()
5330 assert context1 .rate_limit_group is None
5431 set_rate_limit_group ("" )
5532 assert context1 .rate_limit_group is None
5633 assert "Group ID cannot be empty." in caplog .text
5734
5835
5936def test_set_rate_limit_group_none_group_id (caplog ):
60- context1 = set_context_and_lifecycle ()
37+ context1 = test_utils . generate_and_set_context ()
6138 assert context1 .rate_limit_group is None
6239 set_rate_limit_group (None )
6340 assert context1 .rate_limit_group is None
@@ -73,30 +50,30 @@ def test_set_rate_limit_group_no_context(caplog):
7350
7451
7552def test_set_rate_limit_group_middleware_already_executed (caplog ):
76- context1 = set_context_and_lifecycle ()
53+ context1 = test_utils . generate_and_set_context ()
7754 context1 .executed_middleware = True
7855 set_rate_limit_group ("group1" )
7956 assert "must be called before the Zen middleware is executed" in caplog .text
8057 assert context1 .rate_limit_group is "group1"
8158
8259
8360def test_set_rate_limit_group_non_string_group_id (caplog ):
84- context1 = set_context_and_lifecycle ()
61+ context1 = test_utils . generate_and_set_context ()
8562 assert context1 .rate_limit_group is None
8663 set_rate_limit_group (123 )
8764 assert context1 .rate_limit_group == "123"
8865
8966
9067def test_set_rate_limit_group_non_string_group_id_non_number (caplog ):
91- context1 = set_context_and_lifecycle ()
68+ context1 = test_utils . generate_and_set_context ()
9269 assert context1 .rate_limit_group is None
9370 set_rate_limit_group ({"a" : "b" })
9471 assert context1 .rate_limit_group is None
9572 assert "Group ID must be a string or a number" in caplog .text
9673
9774
9875def test_set_rate_limit_group_overwrite_existing_group ():
99- context1 = set_context_and_lifecycle ()
76+ context1 = test_utils . generate_and_set_context ()
10077 assert context1 .rate_limit_group is None
10178 set_rate_limit_group ("group1" )
10279 assert context1 .rate_limit_group == "group1"
0 commit comments