File size: 3,124 Bytes
b84ea83
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
89
90
91
92
93
94
95
96
97
98
"""
Tests for Presigned URL endpoints β€” Direct Upload Architecture

Tests:
    1. GET  /photos/presigned-url β†’ 501 when boto3 not installed
    2. POST /photos/confirm       β†’ 422 when missing required fields
    3. POST /photos/confirm       β†’ 404 when visit_id doesn't exist
    4. POST /photos/confirm       β†’ 200 when valid payload with real visit

These tests hit the live database (requires running PostgreSQL).
"""

import pytest
import httpx

BASE = "http://localhost:5000/api/v1"


@pytest.fixture
def client():
    """Create a reusable httpx client."""
    with httpx.Client(base_url=BASE, timeout=10) as c:
        yield c


def test_presigned_url_returns_501_without_boto3(client):
    """GET /photos/presigned-url should return 501 when boto3 is not installed."""
    r = client.get("/photos/presigned-url", params={"ext": "jpg"})
    assert r.status_code == 501
    body = r.json()
    assert body["detail"]["code"] == "STORAGE_NOT_CONFIGURED"


def test_confirm_requires_fields(client):
    """POST /photos/confirm should return 422 when missing required fields."""
    r = client.post("/photos/confirm", json={})
    assert r.status_code == 422
    body = r.json()
    assert body["detail"]["code"] == "VALIDATION_ERROR"


def test_confirm_rejects_unknown_visit(client):
    """POST /photos/confirm should return 404 for non-existent visit_id."""
    r = client.post("/photos/confirm", json={
        "storage_url": "https://bucket.s3.amazonaws.com/test.jpg",
        "visit_id": "00000000-0000-0000-0000-000000000000",
        "file_name": "test.jpg",
    })
    assert r.status_code == 404


def test_confirm_succeeds_with_real_visit(client):
    """POST /photos/confirm should succeed with a real visit_id."""
    # First, get a real visit_id from the database
    visits = client.get("/visits", params={"page": 1, "per_page": 1})
    assert visits.status_code == 200
    data = visits.json()["data"]

    if not data:
        pytest.skip("No visits in database β€” can't test confirm flow")

    visit_id = data[0]["id"]

    r = client.post("/photos/confirm", json={
        "storage_url": "https://test-bucket.s3.amazonaws.com/photos/test_ci.jpg",
        "visit_id": visit_id,
        "file_name": "test_ci.jpg",
        "file_size": 12345,
    })
    assert r.status_code == 200
    body = r.json()
    assert body["status"] == "success"
    assert "id" in body["data"]
    assert body["data"]["storage_url"] == "https://test-bucket.s3.amazonaws.com/photos/test_ci.jpg"


def test_existing_endpoints_still_work(client):
    """Regression: existing endpoints must not be broken by new routes."""
    # Health
    r = client.get("/health")
    assert r.status_code == 200
    assert r.json()["data"]["status"] == "healthy"

    # Visits
    r = client.get("/visits", params={"page": 1, "per_page": 1})
    assert r.status_code == 200
    assert "data" in r.json()

    # Outlets
    r = client.get("/outlets")
    assert r.status_code == 200
    assert "data" in r.json()

    # Analytics
    r = client.get("/analytics/summary")
    assert r.status_code == 200
    assert "data" in r.json()