|
| 1 | +import time |
| 2 | +from datetime import datetime, timezone |
| 3 | + |
| 4 | +import pytest |
| 5 | +import requests |
| 6 | + |
| 7 | +from yepcode_run import YepCodeStorage |
| 8 | +from yepcode_run.api.yepcode_api import YepCodeApiError |
| 9 | + |
| 10 | + |
| 11 | +TEST_NAME = "test-run-sdk.txt" |
| 12 | +TEST_CONTENT = b"hello signed url" |
| 13 | + |
| 14 | + |
| 15 | +@pytest.fixture |
| 16 | +def storage(): |
| 17 | + return YepCodeStorage() |
| 18 | + |
| 19 | + |
| 20 | +@pytest.fixture |
| 21 | +def uploaded_file(storage): |
| 22 | + storage.upload(TEST_NAME, TEST_CONTENT) |
| 23 | + yield TEST_NAME |
| 24 | + try: |
| 25 | + storage.delete(TEST_NAME) |
| 26 | + except Exception: |
| 27 | + pass |
| 28 | + |
| 29 | + |
| 30 | +def _parse_iso(value: str) -> float: |
| 31 | + return datetime.fromisoformat(value.replace("Z", "+00:00")).timestamp() |
| 32 | + |
| 33 | + |
| 34 | +@pytest.mark.skip(reason="Requires the signed-urls endpoint deployed in the target environment") |
| 35 | +def test_create_signed_url_default_expiry(storage, uploaded_file): |
| 36 | + result = storage.create_signed_url(uploaded_file) |
| 37 | + |
| 38 | + assert isinstance(result.url, str) and len(result.url) > 0 |
| 39 | + assert result.path == uploaded_file |
| 40 | + |
| 41 | + expires_at = _parse_iso(result.expires_at) |
| 42 | + expected_expiry = time.time() + 3600 |
| 43 | + assert abs(expires_at - expected_expiry) < 60 |
| 44 | + |
| 45 | + |
| 46 | +@pytest.mark.skip(reason="Requires the signed-urls endpoint deployed in the target environment") |
| 47 | +def test_create_signed_url_custom_expiry(storage, uploaded_file): |
| 48 | + result = storage.create_signed_url(uploaded_file, expires_in_seconds=60) |
| 49 | + |
| 50 | + expires_at = _parse_iso(result.expires_at) |
| 51 | + expected_expiry = time.time() + 60 |
| 52 | + assert abs(expires_at - expected_expiry) < 30 |
| 53 | + |
| 54 | + |
| 55 | +@pytest.mark.skip(reason="Requires the signed-urls endpoint deployed in the target environment") |
| 56 | +def test_create_signed_url_returns_fetchable_url(storage, uploaded_file): |
| 57 | + result = storage.create_signed_url(uploaded_file) |
| 58 | + |
| 59 | + response = requests.get(result.url, timeout=30) |
| 60 | + assert response.ok |
| 61 | + assert response.content == TEST_CONTENT |
| 62 | + |
| 63 | + |
| 64 | +@pytest.mark.skip(reason="Requires the signed-urls endpoint deployed in the target environment") |
| 65 | +def test_create_signed_url_missing_file_raises_404(storage): |
| 66 | + with pytest.raises(YepCodeApiError) as exc_info: |
| 67 | + storage.create_signed_url("does-not-exist.txt") |
| 68 | + assert exc_info.value.status == 404 |
| 69 | + |
| 70 | + |
| 71 | +@pytest.mark.skip(reason="Requires the signed-urls endpoint deployed in the target environment") |
| 72 | +def test_create_signed_url_out_of_range_expiry_raises_400(storage, uploaded_file): |
| 73 | + with pytest.raises(YepCodeApiError) as exc_info: |
| 74 | + storage.create_signed_url(uploaded_file, expires_in_seconds=999999) |
| 75 | + assert exc_info.value.status == 400 |
0 commit comments