Skip to content

Commit 4836a3c

Browse files
committed
add an e2e test case for django asgi uvicorn
1 parent c0bdc78 commit 4836a3c

2 files changed

Lines changed: 60 additions & 0 deletions

File tree

.github/workflows/end2end.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ jobs:
2929
- { name: django-mysql, testfile: end2end/django_mysql_test.py }
3030
- { name: django-mysql-gunicorn, testfile: end2end/django_mysql_gunicorn_test.py }
3131
- { name: django-postgres-gunicorn, testfile: end2end/django_postgres_gunicorn_test.py }
32+
- { name: django-asgi-uvicorn, testfile: end2end/django_asgi_uvicorn_test.py }
3233
- { name: flask-mongo, testfile: end2end/flask_mongo_test.py }
3334
- { name: flask-mysql, testfile: end2end/flask_mysql_test.py }
3435
- { name: flask-mysql-uwsgi, testfile: end2end/flask_mysql_uwsgi_test.py }
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import pytest
2+
import requests
3+
import time
4+
from .server.check_events_from_mock import fetch_events_from_mock, validate_started_event, filter_on_event_type, \
5+
clear_events_from_mock
6+
7+
# e2e tests for django_postgres_gunicorn sample app
8+
post_url_fw = "http://localhost:8114/create"
9+
post_url_nofw = "http://localhost:8115/create"
10+
11+
def test_firewall_started_okay():
12+
events = fetch_events_from_mock("http://localhost:5000")
13+
started_events = filter_on_event_type(events, "started")
14+
assert len(started_events) == 1
15+
validate_started_event(started_events[0], ["gunicorn", "django", "psycopg2-binary"])
16+
17+
def test_safe_response_with_firewall():
18+
dog_name = "Bobby Tables"
19+
res = requests.post(post_url_fw, data={'dog_name': dog_name})
20+
assert res.status_code == 200
21+
22+
23+
def test_safe_response_without_firewall():
24+
dog_name = "Bobby Tables"
25+
res = requests.post(post_url_nofw, data={'dog_name': dog_name})
26+
assert res.status_code == 200
27+
28+
29+
def test_dangerous_response_with_firewall():
30+
clear_events_from_mock("http://localhost:5000")
31+
dog_name = "Dangerous bobby', TRUE); -- "
32+
res = requests.post(post_url_fw, data={'dog_name': dog_name})
33+
assert res.status_code == 500
34+
35+
time.sleep(5) # Wait for attack to be reported
36+
events = fetch_events_from_mock("http://localhost:5000")
37+
attacks = filter_on_event_type(events, "detected_attack")
38+
39+
assert len(attacks) == 1
40+
del attacks[0]["attack"]["stack"]
41+
assert attacks[0]["attack"] == {
42+
"blocked": True,
43+
"kind": "sql_injection",
44+
'metadata': {
45+
'dialect': "postgres",
46+
'sql': "INSERT INTO sample_app_Dogs (dog_name, is_admin) VALUES ('Dangerous bobby', TRUE); -- ', FALSE)"
47+
},
48+
'operation': "psycopg2.Connection.Cursor.execute",
49+
'pathToPayload': '.dog_name.[0]',
50+
'payload': "\"Dangerous bobby', TRUE); -- \"",
51+
'source': "body",
52+
'user': None
53+
}
54+
55+
def test_dangerous_response_without_firewall():
56+
dog_name = "Dangerous bobby', TRUE); -- "
57+
res = requests.post(post_url_nofw, data={'dog_name': dog_name})
58+
assert res.status_code == 200
59+

0 commit comments

Comments
 (0)