diff --git a/proofline/client.py b/proofline/client.py index 1d8c1519..fd8c0503 100644 --- a/proofline/client.py +++ b/proofline/client.py @@ -189,6 +189,20 @@ def _pagination_limit(resource: str = "Review") -> ProoflineError: ) +def _export_path(organization_id: UUID, dataset: ExportDataset) -> str: + # This path is built by hand rather than by a generated operation, so the + # organization must become one canonical UUID segment. Any other text could + # add segments, leave the organization through dot segments, or replace the + # dataset through "?" or "#", and the page would still decode as `dataset`. + try: + organization = ( + organization_id if isinstance(organization_id, UUID) else UUID(organization_id) + ) + except (TypeError, ValueError, AttributeError): + raise ValueError("Export organization_id must be a UUID.") from None + return f"/api/v1/organizations/{organization}/exports/{dataset}" + + def _export_page_limit(max_pages: int) -> None: if isinstance(max_pages, bool) or not isinstance(max_pages, int) or not 1 <= max_pages <= 1_000: raise ValueError("Export pagination requires max_pages 1..1000.") @@ -290,6 +304,7 @@ def export_reviews( raise ValueError("Unknown export dataset.") if format not in ("csv", "ndjson"): raise ValueError("Export format must be csv or ndjson.") + path = _export_path(organization_id, dataset) params = {"from": from_time, "to": to_time, "format": format} if cursor is not None: params["cursor"] = cursor @@ -297,7 +312,7 @@ def export_reviews( params["group_by"] = group_by try: response = self._http.get( - f"/api/v1/organizations/{organization_id}/exports/{dataset}", + path, params=params, headers={"Accept": "text/csv" if format == "csv" else "application/x-ndjson"}, ) @@ -660,6 +675,7 @@ async def export_reviews( raise ValueError("Unknown export dataset.") if format not in ("csv", "ndjson"): raise ValueError("Export format must be csv or ndjson.") + path = _export_path(organization_id, dataset) params = {"from": from_time, "to": to_time, "format": format} if cursor is not None: params["cursor"] = cursor @@ -667,7 +683,7 @@ async def export_reviews( params["group_by"] = group_by try: response = await self._http.get( - f"/api/v1/organizations/{organization_id}/exports/{dataset}", + path, params=params, headers={"Accept": "text/csv" if format == "csv" else "application/x-ndjson"}, ) diff --git a/tests/test_http.py b/tests/test_http.py index 0af9d60f..3f0a348c 100644 --- a/tests/test_http.py +++ b/tests/test_http.py @@ -1992,6 +1992,81 @@ async def test_export_iterator_validates_bound_before_requests(mode: str, bound: assert not server.requests +EXPORT_WINDOW: dict[str, Any] = { + "from_time": "2026-10-01T00:00:00Z", + "to_time": "2026-10-25T00:00:00Z", +} + + +# Each value would otherwise change the request target: dot segments leave the +# organization, a slash adds path segments, and "?" or "#" replace the dataset. +@MODES +@pytest.mark.parametrize( + "organization_id", + [ + "..", + ".", + "", + f"{ORG}/../../reviews", + f"{ORG}/exports/spend?x=", + f"{ORG}#", + "not-a-uuid", + ORG.int, + None, + ], +) +async def test_export_refuses_an_organization_id_that_is_not_a_uuid( + mode: str, organization_id: Any +) -> None: + with Server(lambda request, index: Reply(status=404, body={"detail": "no"})) as server: + async with session(mode, server.url) as client: + with pytest.raises(ValueError, match="organization_id"): + await call(client, "export_reviews", organization_id, "reviews", **EXPORT_WINDOW) + iterator = client.iter_export_pages(organization_id, "reviews", **EXPORT_WINDOW) + with pytest.raises(ValueError, match="organization_id"): + await next_export(iterator) + assert not server.requests + + +# ORG has no hex letters, so its upper-case spelling would equal its string. +LETTERED_ORG = UUID("3fa85f64-5717-4562-b3fc-2c963f66afa6") +ORG_SPELLINGS = [ + LETTERED_ORG, + str(LETTERED_ORG), + str(LETTERED_ORG).upper(), + f"{{{LETTERED_ORG}}}", + LETTERED_ORG.hex.upper(), +] + + +def test_organization_spellings_are_distinct() -> None: + assert len({repr(spelling) for spelling in ORG_SPELLINGS}) == len(ORG_SPELLINGS) + + +@MODES +@pytest.mark.parametrize("organization_id", ORG_SPELLINGS) +async def test_export_sends_the_canonical_organization_path( + mode: str, organization_id: Any +) -> None: + def respond(request: Request, index: int) -> Reply: + return Reply( + body=b'{"dataset_version":1}\n', + headers={ + "Content-Type": "application/x-ndjson", + "X-Proofline-Export-Version": "1", + "X-Proofline-Export-Settled-Until": "2026-10-25T00:00:00Z", + }, + ) + + with Server(respond) as server: + async with session(mode, server.url) as client: + page = await call(client, "export_reviews", organization_id, "reviews", **EXPORT_WINDOW) + assert page.rows == [{"dataset_version": 1}] + assert urlsplit(server.requests[0].path).path == ( + f"/api/v1/organizations/{LETTERED_ORG}/exports/reviews" + ) + + @MODES @pytest.mark.parametrize( ("cursors", "resume", "yielded"),