Skip to content

Commit

Permalink
Code coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
kapyteinaikido committed Dec 12, 2024
1 parent 8c3a372 commit f112108
Showing 1 changed file with 40 additions and 2 deletions.
42 changes: 40 additions & 2 deletions aikido_zen/sources/flask_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@
"HTTP_HEADER_1": "header 1 value",
"HTTP_HEADER_2": "Header 2 value",
"RANDOM_VALUE": "Random value",
"HTTP_COOKIE": "sessionId=abc123xyz456;",
"HTTP_COOKIE": "sessionklalkdlkjasdlkj@@as2@@d;a@@2sd=asd@@2;as@2d;'asd'aksksId=abc123xyz456;",
"wsgi.url_scheme": "https",
"HTTP_HOST": "example.com",
"PATH_INFO": "/hello",
"QUERY_STRING": "user=JohnDoe&age=30&age=35",
"body": "username=JohnDoe&age=30&age=35",
"CONTENT_TYPE": "application/x-www-form-urlencoded",
"REMOTE_ADDR": "198.51.100.23",
}
Expand Down Expand Up @@ -45,6 +46,43 @@ def timeout_handler(signum, frame):

signal.signal(signal.SIGALRM, timeout_handler)

def test_flask_all_3_func_with_invalid_body():
"""When the flask body can not be parsed (because it contains invalid json for example), we should still parse the cookies of the endpoint"""
with patch(

Check warning on line 51 in aikido_zen/sources/flask_test.py

View check run for this annotation

Codecov / codecov/patch

aikido_zen/sources/flask_test.py#L51

Added line #L51 was not covered by tests
"aikido_zen.sources.functions.request_handler.request_handler"
) as mock_request_handler:
reset_comms()
current_context.set(None)
mock_request_handler.return_value = None

Check warning on line 56 in aikido_zen/sources/flask_test.py

View check run for this annotation

Codecov / codecov/patch

aikido_zen/sources/flask_test.py#L54-L56

Added lines #L54 - L56 were not covered by tests

from flask import Flask

Check warning on line 58 in aikido_zen/sources/flask_test.py

View check run for this annotation

Codecov / codecov/patch

aikido_zen/sources/flask_test.py#L58

Added line #L58 was not covered by tests

app = Flask(__name__)
try:
signal.alarm(1)
app(sample_environ_malformed_json, lambda x, y: x)
app.run()
except TimeoutException:
pass

Check warning on line 66 in aikido_zen/sources/flask_test.py

View check run for this annotation

Codecov / codecov/patch

aikido_zen/sources/flask_test.py#L60-L66

Added lines #L60 - L66 were not covered by tests

assert get_current_context().method == "POST"
assert (

Check warning on line 69 in aikido_zen/sources/flask_test.py

View check run for this annotation

Codecov / codecov/patch

aikido_zen/sources/flask_test.py#L68-L69

Added lines #L68 - L69 were not covered by tests
get_current_context().body == None
) # body is None since it's invalid json
assert get_current_context().headers == {

Check warning on line 72 in aikido_zen/sources/flask_test.py

View check run for this annotation

Codecov / codecov/patch

aikido_zen/sources/flask_test.py#L72

Added line #L72 was not covered by tests
"COOKIE": "sessionId=abc123xyz456;",
"HEADER_1": "header 1 value",
"HEADER_2": "Header 2 value",
"HOST": "example.com",
"CONTENT_TYPE": "application/json",
}
calls = mock_request_handler.call_args_list
assert len(calls) == 3
assert calls[0][1]["stage"] == "init"
assert calls[1][1]["stage"] == "pre_response"
assert calls[2][1]["stage"] == "post_response"
assert calls[2][1]["status_code"] == 404

Check warning on line 84 in aikido_zen/sources/flask_test.py

View check run for this annotation

Codecov / codecov/patch

aikido_zen/sources/flask_test.py#L79-L84

Added lines #L79 - L84 were not covered by tests


def test_flask_all_3_func_with_invalid_body():
"""When the flask body can not be parsed (because it contains invalid json for example), we should still parse the cookies of the endpoint"""
Expand Down Expand Up @@ -104,7 +142,7 @@ def test_flask_all_3_func():
assert get_current_context().method == "POST"
assert get_current_context().body == None
assert get_current_context().headers == {
"COOKIE": "sessionId=abc123xyz456;",
"COOKIE": "sessionklalkdlkjasdlkj@@as2@@d;a@@2sd=asd@@2;as@2d;'asd'aksksId=abc123xyz456;",
"HEADER_1": "header 1 value",
"HEADER_2": "Header 2 value",
"HOST": "example.com",
Expand Down

0 comments on commit f112108

Please sign in to comment.