Skip to content

Commit d07b300

Browse files
committed
set_rate_limit_group_test use context generator utils
1 parent d87cbb7 commit d07b300

File tree

2 files changed

+11
-31
lines changed

2 files changed

+11
-31
lines changed

aikido_zen/middleware/set_rate_limit_group_test.py

Lines changed: 8 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import pytest
2-
from aikido_zen.context import get_current_context, Context
32
from aikido_zen.thread.thread_cache import get_cache
3+
import aikido_zen.test_utils as test_utils
44
from .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-
4118
def 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

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

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

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

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

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

9875
def 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"

aikido_zen/test_utils/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,6 @@ def __init__(self):
3131
self.source = "flask"
3232
self.route = "/"
3333
self.parsed_userinput = {}
34+
self.user = None
35+
self.rate_limit_group = None
36+
self.executed_middleware = False

0 commit comments

Comments
 (0)