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