File size: 2,504 Bytes
6c9c901
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from typing import Dict

from fastapi.testclient import TestClient


def register_user(client: TestClient, payload: Dict[str, str]):
    response = client.post("/api/auth/signup", json=payload)
    assert response.status_code == 200
    return response.json()


def login_user(client: TestClient, payload: Dict[str, str]):
    response = client.post("/api/auth/login", json=payload)
    assert response.status_code == 200
    return response.json()


def test_signup_and_me_endpoint(client: TestClient):
    payload = {
        "email": "user@example.com",
        "password": "securepass",
        "display_name": "Marine User",
        "organization": "Ocean Org",
        "role": "citizen",
    }

    signup_response = register_user(client, payload)
    token = signup_response["access_token"]

    me_response = client.get(
        "/api/auth/me",
        headers={"Authorization": f"Bearer {token}"},
    )

    assert me_response.status_code == 200
    me_data = me_response.json()
    assert me_data["email"] == payload["email"]
    assert me_data["display_name"] == payload["display_name"]


def test_login_returns_new_token(client: TestClient):
    credentials = {
        "email": "login@example.com",
        "password": "strongpassword",
        "display_name": "Login User",
        "organization": "Ocean Org",
        "role": "citizen",
    }

    register_user(client, credentials)

    login_response = login_user(client, credentials)

    assert login_response["access_token"]
    assert login_response["user"]["email"] == credentials["email"]


def test_incident_classification_flow(client: TestClient):
    credentials = {
        "email": "incident@example.com",
        "password": "password123",
        "display_name": "Reporter",
        "organization": "Ocean Org",
        "role": "citizen",
    }

    signup_response = register_user(client, credentials)
    token = signup_response["access_token"]

    response = client.post(
        "/api/incidents/classify",
        data={
            "description": "A major oil spill observed near the coast",
            "latitude": "12.34",
            "longitude": "56.78",
        },
        files={
            "image": ("evidence.jpg", b"fake-image-bytes", "image/jpeg"),
        },
        headers={"Authorization": f"Bearer {token}"},
    )

    assert response.status_code == 200
    data = response.json()
    assert data["incident_class"] == "Incident"
    assert data["severity"] == "high"
    assert "incident_id" in data