Spaces:
Running on CPU Upgrade
Running on CPU Upgrade
Download tests/test_api/test_file_source_url.py from DanielKretschmer/lighting-mvp: direct link, hf CLI and curl.
- Browser
- Download file 4.75 kB
-
https://huggingface.co/spaces/DanielKretschmer/lighting-mvp/resolve/main/tests/test_api/test_file_source_url.py
- Command line
-
hf download hf://spaces/DanielKretschmer/lighting-mvp/tests/test_api/test_file_source_url.py
-
curl -L -o test_file_source_url.py https://huggingface.co/spaces/DanielKretschmer/lighting-mvp/resolve/main/tests/test_api/test_file_source_url.py
4.75 kB
| """Tests for the source_url provenance flow on file uploads. | |
| Two paths exist: | |
| 1. Operator uploads bytes directly and optionally pastes a SharePoint | |
| (or any) URL alongside, which gets recorded on the row. | |
| 2. Operator pastes only a URL and the server downloads the file, | |
| storing both content and URL. | |
| Both paths converge on `uploaded_files.source_url`, which the UI uses | |
| to render a "source" link next to each file. | |
| """ | |
| from __future__ import annotations | |
| import io | |
| import httpx | |
| import pytest | |
| async def _create_job(client, name="source-url test") -> str: | |
| res = await client.post( | |
| "/api/v1/jobs", | |
| json={"job_name": name, "config_version": "v1.0", "created_by": "test"}, | |
| ) | |
| assert res.status_code == 201, res.text | |
| return res.json()["job_id"] | |
| async def test_upload_files_records_source_url_alongside_bytes(client): | |
| """File + URL together: URL is stored on the row, bytes are the | |
| working copy, the listing surfaces both.""" | |
| job_id = await _create_job(client) | |
| pdf_bytes = b"%PDF-1.4 dummy" | |
| res = await client.post( | |
| f"/api/v1/jobs/{job_id}/files", | |
| files={"files": ("schedule.pdf", io.BytesIO(pdf_bytes), "application/pdf")}, | |
| data={"source_urls": "https://example.sharepoint.com/sites/team/schedule.pdf"}, | |
| ) | |
| assert res.status_code == 200, res.text | |
| body = res.json() | |
| assert len(body["files"]) == 1 | |
| assert body["files"][0]["filename"] == "schedule.pdf" | |
| assert body["files"][0]["source_url"] == "https://example.sharepoint.com/sites/team/schedule.pdf" | |
| listing = await client.get(f"/api/v1/jobs/{job_id}/files") | |
| assert listing.status_code == 200 | |
| files = listing.json()["files"] | |
| assert len(files) == 1 | |
| assert files[0]["source_url"] == "https://example.sharepoint.com/sites/team/schedule.pdf" | |
| async def test_upload_files_without_source_url_works_as_before(client): | |
| """Backwards-compatible: omitting source_urls still works.""" | |
| job_id = await _create_job(client) | |
| res = await client.post( | |
| f"/api/v1/jobs/{job_id}/files", | |
| files={"files": ("schedule.pdf", io.BytesIO(b"%PDF-1.4"), "application/pdf")}, | |
| ) | |
| assert res.status_code == 200 | |
| listing = await client.get(f"/api/v1/jobs/{job_id}/files") | |
| assert listing.json()["files"][0]["source_url"] is None | |
| async def test_fetch_from_url_downloads_and_stores(client, monkeypatch): | |
| """URL-only path: server fetches the bytes and records the URL. | |
| The PDF download is mocked so the test stays hermetic — the real | |
| fetch logic is exercised indirectly via the same code path. | |
| """ | |
| job_id = await _create_job(client) | |
| fake_pdf = b"%PDF-1.4 fetched-via-url-test" | |
| url = "https://example.sharepoint.com/sites/team/schedule.pdf?download=1" | |
| async def fake_get(self, request_url, **kwargs): | |
| assert request_url == url | |
| return httpx.Response( | |
| 200, | |
| content=fake_pdf, | |
| headers={ | |
| "content-type": "application/pdf", | |
| "content-disposition": 'attachment; filename="schedule.pdf"', | |
| }, | |
| request=httpx.Request("GET", request_url), | |
| ) | |
| monkeypatch.setattr(httpx.AsyncClient, "get", fake_get) | |
| res = await client.post( | |
| f"/api/v1/jobs/{job_id}/files/fetch", | |
| data={"url": url}, | |
| ) | |
| assert res.status_code == 200, res.text | |
| body = res.json() | |
| assert body["files"][0]["filename"] == "schedule.pdf" | |
| assert body["files"][0]["source_url"] == url | |
| assert body["files"][0]["mime_type"] == "application/pdf" | |
| async def test_fetch_from_url_surfaces_auth_required_clearly(client, monkeypatch): | |
| """A 401 / 403 from SharePoint must produce a structured error so | |
| the UI can tell the operator how to fix it (re-share the link or | |
| upload directly), not a stack trace.""" | |
| job_id = await _create_job(client) | |
| url = "https://example.sharepoint.com/sites/private/schedule.pdf" | |
| async def fake_get(self, request_url, **kwargs): | |
| return httpx.Response(401, content=b"", request=httpx.Request("GET", request_url)) | |
| monkeypatch.setattr(httpx.AsyncClient, "get", fake_get) | |
| res = await client.post( | |
| f"/api/v1/jobs/{job_id}/files/fetch", | |
| data={"url": url}, | |
| ) | |
| assert res.status_code == 400 | |
| assert res.json()["detail"]["code"] == "SOURCE_URL_AUTH_REQUIRED" | |
| assert "sign-in" in res.json()["detail"]["message"] | |
| async def test_fetch_from_url_rejects_non_http_schemes(client): | |
| job_id = await _create_job(client) | |
| res = await client.post( | |
| f"/api/v1/jobs/{job_id}/files/fetch", | |
| data={"url": "file:///etc/passwd"}, | |
| ) | |
| assert res.status_code == 400 | |
| assert res.json()["detail"]["code"] == "SOURCE_URL_BAD_SCHEME" | |