|
| 1 | +""" |
| 2 | +End-to-end API workflow tests for critical user journeys (issue #519). |
| 3 | +
|
| 4 | +Each test mirrors a scenario in e2e-tests/scenarios.yaml and chains multiple |
| 5 | +API calls to validate full workflows without external services. |
| 6 | +""" |
| 7 | +import csv |
| 8 | +from io import StringIO |
| 9 | + |
| 10 | +import pytest |
| 11 | +import responses |
| 12 | +from django.contrib.auth import get_user_model |
| 13 | +from django.urls import reverse |
| 14 | +from rest_framework import status |
| 15 | +from rest_framework.test import APIClient |
| 16 | + |
| 17 | +from soroscan.ingest.models import AuditLog, IngestError, TrackedContract, WebhookSubscription |
| 18 | +from soroscan.ingest.tests.factories import ContractEventFactory, TrackedContractFactory |
| 19 | + |
| 20 | +User = get_user_model() |
| 21 | + |
| 22 | + |
| 23 | +@pytest.fixture |
| 24 | +def api_client(): |
| 25 | + return APIClient() |
| 26 | + |
| 27 | + |
| 28 | +@pytest.mark.django_db |
| 29 | +def test_e2e_user_signup_to_viewing_events(api_client): |
| 30 | + """Signup -> register contract -> view events.""" |
| 31 | + user = User.objects.create_user( |
| 32 | + username="e2e_signup_user", |
| 33 | + email="e2e_signup@example.com", |
| 34 | + password="secret", |
| 35 | + ) |
| 36 | + api_client.force_authenticate(user=user) |
| 37 | + |
| 38 | + contract_payload = { |
| 39 | + "contract_id": "C" + "A" * 55, |
| 40 | + "name": "E2E Onboarding Contract", |
| 41 | + "description": "Created during signup workflow", |
| 42 | + "is_active": True, |
| 43 | + } |
| 44 | + create_response = api_client.post(reverse("contract-list"), contract_payload) |
| 45 | + assert create_response.status_code == status.HTTP_201_CREATED |
| 46 | + |
| 47 | + contract = TrackedContract.objects.get(pk=create_response.data["id"]) |
| 48 | + ContractEventFactory.create_batch(3, contract=contract, event_type="transfer") |
| 49 | + |
| 50 | + events_response = api_client.get(reverse("contract-events", args=[contract.id])) |
| 51 | + assert events_response.status_code == status.HTTP_200_OK |
| 52 | + assert len(events_response.data) == 3 |
| 53 | + |
| 54 | + list_response = api_client.get(reverse("contract-list")) |
| 55 | + assert list_response.status_code == status.HTTP_200_OK |
| 56 | + assert any(item["id"] == contract.id for item in list_response.data["results"]) |
| 57 | + |
| 58 | + |
| 59 | +@pytest.mark.django_db |
| 60 | +@responses.activate |
| 61 | +def test_e2e_webhook_subscription_lifecycle(api_client): |
| 62 | + """Create webhook -> list -> test delivery -> delete.""" |
| 63 | + user = User.objects.create_user(username="e2e_webhook_user", password="secret") |
| 64 | + contract = TrackedContractFactory(owner=user) |
| 65 | + api_client.force_authenticate(user=user) |
| 66 | + |
| 67 | + target_url = "https://example.com/e2e-webhook" |
| 68 | + create_response = api_client.post( |
| 69 | + reverse("webhook-list"), |
| 70 | + { |
| 71 | + "contract": contract.id, |
| 72 | + "event_type": "swap", |
| 73 | + "target_url": target_url, |
| 74 | + "is_active": True, |
| 75 | + }, |
| 76 | + ) |
| 77 | + assert create_response.status_code == status.HTTP_201_CREATED |
| 78 | + webhook_id = create_response.data["id"] |
| 79 | + |
| 80 | + list_response = api_client.get(reverse("webhook-list")) |
| 81 | + assert list_response.status_code == status.HTTP_200_OK |
| 82 | + assert any(item["id"] == webhook_id for item in list_response.data["results"]) |
| 83 | + |
| 84 | + responses.add(responses.POST, target_url, status=200) |
| 85 | + test_response = api_client.post(reverse("webhook-test", args=[webhook_id])) |
| 86 | + assert test_response.status_code == status.HTTP_200_OK |
| 87 | + assert test_response.data["status"] == "test_webhook_queued" |
| 88 | + |
| 89 | + delete_response = api_client.delete(reverse("webhook-detail", args=[webhook_id])) |
| 90 | + assert delete_response.status_code == status.HTTP_204_NO_CONTENT |
| 91 | + assert not WebhookSubscription.objects.filter(pk=webhook_id).exists() |
| 92 | + |
| 93 | + |
| 94 | +@pytest.mark.django_db |
| 95 | +def test_e2e_compliance_data_export(api_client): |
| 96 | + """Staff user exports compliance audit trail CSV.""" |
| 97 | + staff_user = User.objects.create_user( |
| 98 | + username="e2e_staff_exporter", |
| 99 | + password="secret", |
| 100 | + is_staff=True, |
| 101 | + ) |
| 102 | + api_client.force_authenticate(user=staff_user) |
| 103 | + |
| 104 | + AuditLog.objects.create( |
| 105 | + user=staff_user, |
| 106 | + action="create", |
| 107 | + model_name="TrackedContract", |
| 108 | + object_id="1", |
| 109 | + ip_address="127.0.0.1", |
| 110 | + changes={"name": "E2E Contract"}, |
| 111 | + ) |
| 112 | + |
| 113 | + export_response = api_client.get(reverse("compliance-export")) |
| 114 | + assert export_response.status_code == status.HTTP_200_OK |
| 115 | + assert export_response["Content-Type"] == "text/csv" |
| 116 | + |
| 117 | + content = b"".join(export_response.streaming_content).decode("utf-8") |
| 118 | + rows = list(csv.reader(StringIO(content))) |
| 119 | + assert rows[0] == [ |
| 120 | + "id", |
| 121 | + "timestamp", |
| 122 | + "user", |
| 123 | + "action", |
| 124 | + "model_name", |
| 125 | + "object_id", |
| 126 | + "ip_address", |
| 127 | + "changes", |
| 128 | + ] |
| 129 | + assert len(rows) >= 2 |
| 130 | + assert rows[1][2] == staff_user.username |
| 131 | + |
| 132 | + |
| 133 | +@pytest.mark.django_db |
| 134 | +def test_e2e_admin_ingest_error_review(api_client): |
| 135 | + """Staff user reviews grouped ingest errors.""" |
| 136 | + staff_user = User.objects.create_user( |
| 137 | + username="e2e_staff_admin", |
| 138 | + password="secret", |
| 139 | + is_staff=True, |
| 140 | + ) |
| 141 | + api_client.force_authenticate(user=staff_user) |
| 142 | + |
| 143 | + IngestError.objects.create( |
| 144 | + error_type="decode_error", |
| 145 | + contract_id="C" + "B" * 55, |
| 146 | + error_message="Failed to decode XDR", |
| 147 | + ledger=1000, |
| 148 | + ) |
| 149 | + IngestError.objects.create( |
| 150 | + error_type="decode_error", |
| 151 | + contract_id="C" + "B" * 55, |
| 152 | + error_message="Another decode error", |
| 153 | + ledger=1001, |
| 154 | + ) |
| 155 | + |
| 156 | + response = api_client.get(reverse("admin-ingest-errors")) |
| 157 | + assert response.status_code == status.HTTP_200_OK |
| 158 | + data = response.json() |
| 159 | + assert len(data) == 1 |
| 160 | + assert data[0]["count"] == 2 |
| 161 | + assert data[0]["error_type"] == "decode_error" |
| 162 | + |
| 163 | + |
| 164 | +def test_e2e_framework_files_exist(): |
| 165 | + """Sanity check that the E2E scenario catalog is present.""" |
| 166 | + from pathlib import Path |
| 167 | + |
| 168 | + repo_root = Path(__file__).resolve().parents[4] |
| 169 | + assert (repo_root / "e2e-tests/scenarios.yaml").is_file() |
| 170 | + assert (repo_root / "e2e-tests/scenarios.py").is_file() |
| 171 | + assert (repo_root / ".github/workflows/e2e-tests.yml").is_file() |
0 commit comments