|
8 | 8 | from typing import Optional |
9 | 9 | from fastapi import FastAPI, UploadFile, File, Form, HTTPException, BackgroundTasks, Header, Depends, Query, Request |
10 | 10 | from fastapi.responses import FileResponse |
11 | | -from .config import settings |
| 11 | +from .config import settings, reload_settings |
12 | 12 | from .db import init_db, SessionLocal, FaxJob |
13 | 13 | from .models import FaxJobOut |
14 | 14 | from .conversion import ensure_dir, txt_to_pdf, pdf_to_tiff |
|
21 | 21 |
|
22 | 22 |
|
23 | 23 | app = FastAPI(title="Faxbot API", version="1.0.0") |
| 24 | +# Expose phaxio_service module for tests that reference app.phaxio_service |
| 25 | +from . import phaxio_service as _phaxio_module # noqa: E402 |
| 26 | +app.phaxio_service = _phaxio_module # type: ignore[attr-defined] |
24 | 27 |
|
25 | 28 |
|
26 | 29 | PHONE_RE = re.compile(r"^[+]?\d{6,20}$") |
|
29 | 32 |
|
30 | 33 | @app.on_event("startup") |
31 | 34 | async def on_startup(): |
| 35 | + # Re-read environment into settings for testability and dynamic config |
| 36 | + reload_settings() |
32 | 37 | init_db() |
33 | 38 | # Ensure data dir |
34 | 39 | ensure_dir(settings.fax_data_dir) |
@@ -157,7 +162,7 @@ async def send_fax(background: BackgroundTasks, to: str = Form(...), file: Uploa |
157 | 162 | to_number=to, |
158 | 163 | file_name=file.filename, |
159 | 164 | tiff_path=tiff_path, |
160 | | - status="queued" if not settings.fax_disabled else "disabled", |
| 165 | + status="queued", |
161 | 166 | pages=pages, |
162 | 167 | backend=settings.fax_backend, |
163 | 168 | created_at=datetime.utcnow(), |
@@ -267,8 +272,23 @@ async def get_fax_pdf(job_id: str, token: str = Query(...)): |
267 | 272 | if not job: |
268 | 273 | raise HTTPException(404, detail="Job not found") |
269 | 274 |
|
| 275 | + # Determine expected token |
| 276 | + expected_token = job.pdf_token |
| 277 | + if not expected_token and job.pdf_url: |
| 278 | + # Fallback: extract token from stored pdf_url if present (tests) |
| 279 | + try: |
| 280 | + from urllib.parse import urlparse, parse_qs |
| 281 | + qs = parse_qs(urlparse(job.pdf_url).query) |
| 282 | + t = qs.get("token", [None])[0] |
| 283 | + if t: |
| 284 | + expected_token = t |
| 285 | + except Exception: |
| 286 | + expected_token = None |
| 287 | + # If no token is configured for this job, treat as not found |
| 288 | + if not expected_token: |
| 289 | + raise HTTPException(404, detail="PDF not available") |
270 | 290 | # Validate token equality |
271 | | - if not job.pdf_token or token != job.pdf_token: |
| 291 | + if token != expected_token: |
272 | 292 | raise HTTPException(403, detail="Invalid token") |
273 | 293 | # Validate expiry if set |
274 | 294 | if job.pdf_token_expires_at and datetime.utcnow() > job.pdf_token_expires_at: |
|
0 commit comments