Skip to content

Commit 20b198e

Browse files
committed
middleware/init_test.py use context generation utils
1 parent fda5d23 commit 20b198e

File tree

2 files changed

+13
-31
lines changed

2 files changed

+13
-31
lines changed

aikido_zen/middleware/init_test.py

Lines changed: 9 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
from unittest.mock import patch, MagicMock
22

33
import 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
67
from . import should_block_request
78
from .. 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-
4626
def 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

5232
def 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

7858
def 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
}

aikido_zen/test_utils/__init__.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ def generate_and_set_context(*args, **kwargs) -> Context:
88
return context
99

1010

11-
def generate_context(value=None, query_value=None, user=None) -> Context:
11+
def generate_context(value=None, query_value=None, user=None, route=None) -> Context:
1212
context = MockTestContext()
1313

1414
if value is not None:
@@ -17,6 +17,8 @@ def generate_context(value=None, query_value=None, user=None) -> Context:
1717
context.query["key1"] = query_value
1818
if user is not None:
1919
context.user = user
20+
if route is not None:
21+
context.route = route
2022

2123
return context
2224

@@ -27,7 +29,7 @@ def __init__(self):
2729
self.headers = Headers()
2830
self.remote_address = "1.1.1.1"
2931
self.method = "POST"
30-
self.url = "url"
32+
self.url = "http://localhost:8080/"
3133
self.body = {}
3234
self.query = {}
3335
self.source = "flask"

0 commit comments

Comments
 (0)