|
| 1 | +import pytest |
| 2 | +from pathlib import Path |
| 3 | + |
| 4 | +from alephclient.api import AlephAPI |
| 5 | + |
| 6 | + |
| 7 | +class TestSignedURLUpload: |
| 8 | + fake_url = "http://aleph.test/api/2/" |
| 9 | + |
| 10 | + def setup_method(self): |
| 11 | + self.api = AlephAPI(host=self.fake_url, api_key="fake_key") |
| 12 | + |
| 13 | + def test_signed_url_upload_end_to_end_happy_path(self, mocker, tmp_path): |
| 14 | + collection_id = "8" |
| 15 | + file_path = tmp_path / "doc.txt" |
| 16 | + file_path.write_text("hello world") |
| 17 | + |
| 18 | + # Step 1: /file/uploadUrl returns URL and ID |
| 19 | + upload_id = "1234-upload-id" |
| 20 | + signed_url = "http://upload.test/signed-url" |
| 21 | + |
| 22 | + def fake_request(method, url, **kwargs): |
| 23 | + if url == f"{self.fake_url}file/uploadUrl" and method == "POST": |
| 24 | + return {"url": signed_url, "id": upload_id} |
| 25 | + if url.startswith(f"{self.fake_url}collections/{collection_id}/document"): |
| 26 | + assert method == "POST" |
| 27 | + # The payload must use capital-M "Meta" to match Alfred. |
| 28 | + body = kwargs["json"] |
| 29 | + assert body["upload_id"] == upload_id |
| 30 | + assert "Meta" in body |
| 31 | + assert body["Meta"]["file_name"] == file_path.name |
| 32 | + assert "mime_type" in body["Meta"] |
| 33 | + return {"status": "ok", "id": "42"} |
| 34 | + raise AssertionError(f"Unexpected request: {method} {url}") |
| 35 | + |
| 36 | + mocker.patch.object(self.api, "_request", side_effect=fake_request) |
| 37 | + |
| 38 | + # Step 2: PUT to signed URL |
| 39 | + put = mocker.patch.object(self.api.session, "put") |
| 40 | + put.return_value.raise_for_status.return_value = None |
| 41 | + |
| 42 | + result = self.api.signed_url_upload(collection_id, Path(file_path)) |
| 43 | + |
| 44 | + # Ensure we got back the document creation response |
| 45 | + assert result["status"] == "ok" |
| 46 | + assert result["id"] == "42" |
| 47 | + |
| 48 | + # Ensure we PUT to the signed URL once with the file data |
| 49 | + put.assert_called_once() |
| 50 | + args, kwargs = put.call_args |
| 51 | + assert args[0] == signed_url |
| 52 | + |
0 commit comments