File size: 7,973 Bytes
dc4e6da
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
"""
Functional tests β€” POST /generate/pdf endpoint
================================================
Tests for the synchronous PDF generation endpoint.

Key behaviours exercised:
  β€’ Schema validation (422) for missing / bad fields
  β€’ 404 when request_id not in Supabase
  β€’ Response headers contract (Content-Type, Content-Disposition)
  β€’ num_solutions validation bounds (1–5)
  β€’ Swagger-default token sanitisation ("string" β†’ None)
  β€’ seed_images validator (empty list β†’ 422)
"""
import json
import pytest
import requests
from tests.conftest import (
    BASE_URL, TIMEOUT, SEED_IMAGE_URL,
    NONEXISTENT_REQUEST_ID, MINIMAL_GENERATE_PAYLOAD,
)

ENDPOINT = f"{BASE_URL}/generate/pdf"


# ---------------------------------------------------------------------------
# Helpers
# ---------------------------------------------------------------------------

def make_payload(**overrides):
    import copy
    payload = copy.deepcopy(MINIMAL_GENERATE_PAYLOAD)
    payload.update(overrides)
    return payload


def make_prompt_override(**kw):
    import copy
    pp = copy.deepcopy(MINIMAL_GENERATE_PAYLOAD["prompt_params"])
    pp.update(kw)
    return pp


# ---------------------------------------------------------------------------
# 1. Schema / Input Validation (expects 422 from FastAPI)
# ---------------------------------------------------------------------------

class TestGeneratePdfInputValidation:
    """FastAPI must reject malformed requests with 422 Unprocessable Entity."""

    def test_missing_request_id_returns_422(self, http):
        payload = {
            "seed_images": [SEED_IMAGE_URL],
            "prompt_params": MINIMAL_GENERATE_PAYLOAD["prompt_params"],
        }
        r = http.post(ENDPOINT, json=payload, timeout=TIMEOUT)
        assert r.status_code == 422, (
            f"Expected 422 for missing request_id, got {r.status_code}"
        )

    def test_empty_seed_images_returns_422(self, http):
        payload = make_payload(seed_images=[])
        r = http.post(ENDPOINT, json=payload, timeout=TIMEOUT)
        assert r.status_code == 422, (
            f"Expected 422 for empty seed_images, got {r.status_code}"
        )

    def test_too_many_seed_images_returns_422(self, http):
        payload = make_payload(seed_images=[SEED_IMAGE_URL] * 11)
        r = http.post(ENDPOINT, json=payload, timeout=TIMEOUT)
        assert r.status_code == 422, (
            f"Expected 422 for 11 seed_images (max 10), got {r.status_code}"
        )

    def test_invalid_seed_image_url_returns_422(self, http):
        payload = make_payload(seed_images=["not-a-url"])
        r = http.post(ENDPOINT, json=payload, timeout=TIMEOUT)
        assert r.status_code == 422, (
            f"Expected 422 for non-URL seed image, got {r.status_code}"
        )

    def test_num_solutions_below_min_returns_422(self, http):
        pp = make_prompt_override(num_solutions=0)
        payload = make_payload(prompt_params=pp)
        r = http.post(ENDPOINT, json=payload, timeout=TIMEOUT)
        assert r.status_code == 422, (
            f"Expected 422 for num_solutions=0, got {r.status_code}"
        )

    def test_num_solutions_above_max_returns_422(self, http):
        pp = make_prompt_override(num_solutions=6)
        payload = make_payload(prompt_params=pp)
        r = http.post(ENDPOINT, json=payload, timeout=TIMEOUT)
        assert r.status_code == 422, (
            f"Expected 422 for num_solutions=6, got {r.status_code}"
        )

    def test_handwriting_ratio_out_of_range_returns_422(self, http):
        pp = make_prompt_override(handwriting_ratio=1.5)
        payload = make_payload(prompt_params=pp)
        r = http.post(ENDPOINT, json=payload, timeout=TIMEOUT)
        assert r.status_code == 422, (
            f"Expected 422 for handwriting_ratio=1.5, got {r.status_code}"
        )

    def test_non_json_body_returns_422(self, http):
        r = http.post(ENDPOINT, data="this is not json", timeout=TIMEOUT,
                      headers={"Content-Type": "application/json"})
        assert r.status_code == 422, (
            f"Expected 422 for non-JSON body, got {r.status_code}"
        )

    def test_empty_body_returns_422(self, http):
        r = http.post(ENDPOINT, json={}, timeout=TIMEOUT)
        assert r.status_code == 422, (
            f"Expected 422 for empty body, got {r.status_code}"
        )


# ---------------------------------------------------------------------------
# 2. Business-logic validation (valid JSON but non-existent request_id β†’ 404)
# ---------------------------------------------------------------------------

class TestGeneratePdfBusinessLogic:
    """Valid schema, but request_id not in Supabase β†’ 404."""

    def test_nonexistent_request_id_returns_404(self, http):
        r = http.post(ENDPOINT, json=MINIMAL_GENERATE_PAYLOAD, timeout=TIMEOUT)
        assert r.status_code == 404, (
            f"Expected 404 for unknown request_id, got {r.status_code}: {r.text}"
        )

    def test_nonexistent_request_id_error_is_json(self, http):
        r = http.post(ENDPOINT, json=MINIMAL_GENERATE_PAYLOAD, timeout=TIMEOUT)
        assert "application/json" in r.headers.get("Content-Type", ""), (
            "Error response must be JSON"
        )

    def test_nonexistent_request_id_has_detail(self, http):
        r = http.post(ENDPOINT, json=MINIMAL_GENERATE_PAYLOAD, timeout=TIMEOUT)
        body = r.json()
        assert "detail" in body, f"Error body must have 'detail' field. Got: {body}"

    def test_swagger_string_token_is_sanitised(self, http):
        """
        The frontend Swagger UI sends literal "string" as token defaults.
        The API should accept the request (not 422) and treat "string" as None.
        The result is still 404 because the request_id doesn't exist β€” but NOT 422.
        """
        payload = make_payload(
            google_drive_token="string",
            google_drive_refresh_token="string",
        )
        r = http.post(ENDPOINT, json=payload, timeout=TIMEOUT)
        assert r.status_code != 422, (
            "API should NOT reject 'string' tokens with 422 β€” it should sanitise them."
        )

    def test_none_google_tokens_are_accepted(self, http):
        """Explicitly null tokens are valid (Google Drive is optional)."""
        payload = make_payload(
            google_drive_token=None,
            google_drive_refresh_token=None,
        )
        r = http.post(ENDPOINT, json=payload, timeout=TIMEOUT)
        # Still 404 (no real request_id) but schema accepted β†’ not 422
        assert r.status_code != 422

    def test_valid_num_solutions_boundary_values_accepted(self, http):
        """num_solutions=1 and num_solutions=5 should pass schema validation."""
        for n in [1, 5]:
            pp = make_prompt_override(num_solutions=n)
            payload = make_payload(prompt_params=pp)
            r = http.post(ENDPOINT, json=payload, timeout=TIMEOUT)
            assert r.status_code != 422, (
                f"num_solutions={n} should be schema-valid, got {r.status_code}"
            )

    def test_missing_prompt_params_uses_defaults(self, http):
        """prompt_params is optional (has defaults); omitting it must not raise 422."""
        payload = {"request_id": NONEXISTENT_REQUEST_ID}
        r = http.post(ENDPOINT, json=payload, timeout=TIMEOUT)
        assert r.status_code != 422, (
            f"Missing prompt_params should use defaults, got {r.status_code}"
        )

    def test_request_id_with_user_prefix_is_accepted(self, http):
        """request_id supports 'user_id/request_id' format."""
        payload = make_payload(request_id=f"user_42/{NONEXISTENT_REQUEST_ID}")
        r = http.post(ENDPOINT, json=payload, timeout=TIMEOUT)
        # The format is valid; server parses and looks up uuid β†’ 404
        assert r.status_code in (404, 500), (
            f"Prefixed request_id should parse fine, got {r.status_code}"
        )