-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathinit_test.py
More file actions
298 lines (227 loc) · 9.9 KB
/
init_test.py
File metadata and controls
298 lines (227 loc) · 9.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
import pytest
import pickle
import json
from aikido_zen.context import Context, get_current_context, current_context
basic_wsgi_req = {
"REQUEST_METHOD": "GET",
"HTTP_HEADER_1": "header 1 value",
"HTTP_HEADER_2": "Header 2 value",
"RANDOM_VALUE": "Random value",
"HTTP_COOKIE": "sessionId=abc123xyz456;",
"wsgi.url_scheme": "http",
"HTTP_HOST": "localhost:8080",
"PATH_INFO": "/hello",
"QUERY_STRING": "user=JohnDoe&age=30&age=35",
"CONTENT_TYPE": "application/json",
"REMOTE_ADDR": "198.51.100.23",
"HTTP_USER_AGENT": "Mozilla/5.0",
}
@pytest.fixture(autouse=True)
def run_around_tests():
yield
# Make sure to reset context after every test so it does not
# interfere with other tests
current_context.set(None)
def test_get_current_context_no_context():
# Test get_current_context() when no context is set
assert get_current_context() is None
def test_wsgi_context_1():
wsgi_request = {
"REQUEST_METHOD": "POST",
"HTTP_HEADER_1": "header 1 value",
"HTTP_HEADER_2": "Header 2 value",
"RANDOM_VALUE": "Random value",
"HTTP_COOKIE": "sessionId=abc123xyz456;",
"wsgi.url_scheme": "https",
"HTTP_HOST": "example.com",
"PATH_INFO": "/hello",
"QUERY_STRING": "user=JohnDoe&age=30&age=35",
"CONTENT_TYPE": "application/x-www-form-urlencoded",
"REMOTE_ADDR": "198.51.100.23",
}
context = Context(req=wsgi_request, body=123, source="django")
assert context.__dict__ == {
"source": "django",
"method": "POST",
"headers": {
"HEADER_1": ["header 1 value"],
"HEADER_2": ["Header 2 value"],
"COOKIE": ["sessionId=abc123xyz456;"],
"HOST": ["example.com"],
"CONTENT_TYPE": ["application/x-www-form-urlencoded"],
},
"cookies": {"sessionId": "abc123xyz456"},
"url": "https://example.com/hello",
"query": {"user": ["JohnDoe"], "age": ["30", "35"]},
"body": 123,
"route": "/hello",
"subdomains": [],
"user": None,
"rate_limit_group": None,
"remote_address": "198.51.100.23",
"parsed_userinput": {},
"xml": {},
"outgoing_req_redirects": [],
"executed_middleware": False,
"route_params": [],
"protection_forced_off": None,
}
assert context.get_user_agent() is None
def test_wsgi_context_2():
context = Context(req=basic_wsgi_req, body={"test": True}, source="flask")
assert context.__dict__ == {
"source": "flask",
"method": "GET",
"headers": {
"HEADER_1": ["header 1 value"],
"HEADER_2": ["Header 2 value"],
"COOKIE": ["sessionId=abc123xyz456;"],
"HOST": ["localhost:8080"],
"CONTENT_TYPE": ["application/json"],
"USER_AGENT": ["Mozilla/5.0"],
},
"cookies": {"sessionId": "abc123xyz456"},
"url": "http://localhost:8080/hello",
"query": {"user": ["JohnDoe"], "age": ["30", "35"]},
"body": {"test": True},
"route": "/hello",
"subdomains": [],
"user": None,
"rate_limit_group": None,
"remote_address": "198.51.100.23",
"parsed_userinput": {},
"xml": {},
"outgoing_req_redirects": [],
"executed_middleware": False,
"route_params": [],
"protection_forced_off": None,
}
assert context.get_user_agent() == "Mozilla/5.0"
def test_set_as_current_context(mocker):
# Test set_as_current_context() method
context = Context(req=basic_wsgi_req, body=12, source="flask")
context.set_as_current_context()
assert get_current_context() == context
def test_get_current_context_with_context(mocker):
# Test get_current_context() when a context is set
context = Context(req=basic_wsgi_req, body=456, source="flask")
context.set_as_current_context()
assert get_current_context() == context
def test_context_is_picklable(mocker):
context = Context(req=basic_wsgi_req, body=123, source="flask")
pickled_obj = pickle.dumps(context)
unpickled_obj = pickle.loads(pickled_obj)
assert unpickled_obj.source == "flask"
assert unpickled_obj.method == "GET"
assert unpickled_obj.remote_address == "198.51.100.23"
assert unpickled_obj.url == "http://localhost:8080/hello"
assert unpickled_obj.body == 123
assert unpickled_obj.headers == {
"HEADER_1": ["header 1 value"],
"HEADER_2": ["Header 2 value"],
"COOKIE": ["sessionId=abc123xyz456;"],
"HOST": ["localhost:8080"],
"CONTENT_TYPE": ["application/json"],
"USER_AGENT": ["Mozilla/5.0"],
}
assert unpickled_obj.query == {"user": ["JohnDoe"], "age": ["30", "35"]}
assert unpickled_obj.cookies == {"sessionId": "abc123xyz456"}
def test_set_valid_dict():
valid_body = {"key": "value"}
context = Context(req=basic_wsgi_req, body=valid_body, source="flask")
assert context.body == valid_body
def test_set_valid_list():
valid_body = [1, 2, 3]
context = Context(req=basic_wsgi_req, body=valid_body, source="flask")
assert context.body == valid_body
def test_set_valid_string():
valid_body = "This is a valid string"
context = Context(req=basic_wsgi_req, body=valid_body, source="flask")
assert context.body == valid_body
def test_set_empty_byte_string():
invalid_body = b""
context = Context(req=basic_wsgi_req, body=invalid_body, source="flask")
assert context.body is None
def test_set_normal_byte_string():
body = b"Hello World!"
context = Context(req=basic_wsgi_req, body=body, source="flask")
assert context.body == "Hello World!"
def test_set_byte_string_wrong_encoding():
body = "hello world! 😊".encode("utf-16") # UTF-16 unique character
context = Context(req=basic_wsgi_req, body=body, source="flask")
assert context.body == body # Body remains unchanged because utf-8 failed.
def test_set_none():
context = Context(req=basic_wsgi_req, body=None, source="flask")
assert context.body is None
def test_set_valid_nested_json_string():
context = Context(req=basic_wsgi_req, body=None, source="flask")
context.set_body('{"key": {"nested_key": "nested_value"}}')
assert context.body == {"key": {"nested_key": "nested_value"}}
def test_set_invalid_json_with_unmatched_quotes():
context = Context(req=basic_wsgi_req, body=None, source="flask")
context.set_body('{"key": "value\'s}')
assert context.body == '{"key": "value\'s}' # Should remain as string
def test_set_valid_json_with_array():
context = Context(req=basic_wsgi_req, body=None, source="flask")
context.set_body('{"key": [1, 2, 3]}')
assert context.body == {"key": [1, 2, 3]}
def test_set_valid_json_with_spaces():
context = Context(req=basic_wsgi_req, body=None, source="flask")
context.set_body(' {"key": [1, 2, 3]} ')
assert context.body == {"key": [1, 2, 3]}
def test_valid_json_string_with_newlines():
context = Context(req=basic_wsgi_req, body=None, source="flask")
context.set_body('\r\n\r\n"hello"\r\n\r\n')
assert context.body == "hello"
def test_valid_json_string_with_spaces():
context = Context(req=basic_wsgi_req, body=None, source="flask")
context.set_body('" hello "')
assert context.body == " hello "
def test_set_valid_json_with_newlines():
context = Context(req=basic_wsgi_req, body=None, source="flask")
context.set_body('\r\n\r\n{"key": [1, 2, 3]}\r\n\r\n')
assert context.body == {"key": [1, 2, 3]}
def test_set_valid_json_with_spaces_and_array():
context = Context(req=basic_wsgi_req, body=None, source="flask")
context.set_body(" [1, 2, 3] ")
assert context.body == [1, 2, 3]
context.set_body(" (1, 2, 3) ")
assert context.body == " (1, 2, 3) "
def test_set_valid_json_with_spaces_and_array_bytes():
context = Context(req=basic_wsgi_req, body=None, source="flask")
context.set_body(b" [1, 2, 3] ")
assert context.body == [1, 2, 3]
context.set_body(b" (1, 2, 3) ")
assert context.body == " (1, 2, 3) "
def test_set_valid_json_with_complex_array():
context = Context(req=basic_wsgi_req, body=None, source="flask")
context.set_body(
' [{"hello": "world", "one": 123}, 2, "hiya"] '
)
assert context.body == [{"hello": "world", "one": 123}, 2, "hiya"]
def test_set_valid_json_with_complex_array_bytes():
context = Context(req=basic_wsgi_req, body=None, source="flask")
context.set_body(
b' [{"hello": "world", "one": 123}, 2, "hiya"] '
)
assert context.body == [{"hello": "world", "one": 123}, 2, "hiya"]
def test_empty_string_becomes_none():
context = Context(req=basic_wsgi_req, body=None, source="flask")
context.set_body("")
assert context.body is None
def test_set_valid_json_with_special_characters():
context = Context(req=basic_wsgi_req, body=None, source="flask")
context.set_body('{"key": "value with special characters !@#$%^&*()"}')
assert context.body == {"key": "value with special characters !@#$%^&*()"}
def test_set_valid_json_with_special_characters_bytes():
context = Context(req=basic_wsgi_req, body=None, source="flask")
context.set_body(b'{"key": "value with special characters !@#$%^&*()"}')
assert context.body == {"key": "value with special characters !@#$%^&*()"}
def test_set_protection_forced_off():
context = Context(req=basic_wsgi_req, body=None, source="flask")
context.set_force_protection_off(True)
assert context.protection_forced_off is True
context.set_force_protection_off(False)
assert context.protection_forced_off is False
context.set_force_protection_off(None)
assert context.protection_forced_off is None