11from unittest .mock import patch , MagicMock
22
33import pytest
4- from aikido_zen .context import current_context , Context , get_current_context
5- from aikido_zen .thread .thread_cache import ThreadCache , get_cache
4+ import aikido_zen .test_utils as test_utils
5+ from aikido_zen .context import current_context , get_current_context
6+ from aikido_zen .thread .thread_cache import get_cache
67from . import should_block_request
78from .. import set_rate_limit_group
89
@@ -22,35 +23,14 @@ def test_without_context():
2223 assert should_block_request () == {"block" : False }
2324
2425
25- def set_context (user = None , executed_middleware = False ):
26- Context (
27- context_obj = {
28- "remote_address" : "::1" ,
29- "method" : "POST" ,
30- "url" : "http://localhost:4000" ,
31- "query" : {
32- "abc" : "def" ,
33- },
34- "headers" : {},
35- "body" : None ,
36- "cookies" : {},
37- "source" : "flask" ,
38- "route" : "/posts/:id" ,
39- "user" : user ,
40- "rate_limit_group" : None ,
41- "executed_middleware" : executed_middleware ,
42- }
43- ).set_as_current_context ()
44-
45-
4626def test_with_context_without_cache ():
47- set_context ()
27+ test_utils . generate_and_set_context ()
4828 get_cache ().cache = None
4929 assert should_block_request () == {"block" : False }
5030
5131
5232def test_with_context_with_cache ():
53- set_context (user = {"id" : "123" })
33+ test_utils . generate_and_set_context (user = {"id" : "123" })
5434 thread_cache = get_cache ()
5535
5636 thread_cache .config .blocked_uids = ["123" ]
@@ -76,7 +56,7 @@ def test_with_context_with_cache():
7656
7757
7858def test_cache_comms_with_endpoints ():
79- set_context (user = {"id" : "456" })
59+ test_utils . generate_and_set_context (user = {"id" : "456" }, route = "/posts/:id" )
8060 set_rate_limit_group ("my_group" )
8161 thread_cache = get_cache ()
8262 thread_cache .config .blocked_uids = ["123" ]
@@ -145,11 +125,11 @@ def test_cache_comms_with_endpoints():
145125 "route_metadata" : {
146126 "method" : "POST" ,
147127 "route" : "/posts/:id" ,
148- "url" : "http://localhost:4000 " ,
128+ "url" : "http://localhost:8080/ " ,
149129 },
150130 "user" : {"id" : "456" },
151131 "group" : "my_group" ,
152- "remote_address" : ":: 1" ,
132+ "remote_address" : "1.1.1. 1" ,
153133 },
154134 receive = True ,
155135 timeout_in_sec = 0.01 ,
@@ -168,7 +148,7 @@ def test_cache_comms_with_endpoints():
168148 assert thread_cache .stats .rate_limited_hits == 0
169149 assert should_block_request () == {
170150 "block" : True ,
171- "ip" : ":: 1" ,
151+ "ip" : "1.1.1. 1" ,
172152 "type" : "ratelimited" ,
173153 "trigger" : "my_trigger" ,
174154 }
0 commit comments