mirror of
https://github.com/Hestia-Homes/Model.git
synced 2026-07-12 13:29:04 +00:00
66 lines
2 KiB
Python
66 lines
2 KiB
Python
from collections.abc import Iterator
|
|
|
|
import pytest
|
|
from moto import mock_aws
|
|
|
|
from infrastructure.s3.s3_client import S3Client
|
|
from tests.infrastructure import make_boto_client
|
|
|
|
BUCKET = "test-bucket"
|
|
|
|
|
|
@pytest.fixture
|
|
def s3_client() -> Iterator[S3Client]:
|
|
with mock_aws():
|
|
boto_client = make_boto_client("s3")
|
|
boto_client.create_bucket(Bucket=BUCKET)
|
|
yield S3Client(boto_client, BUCKET)
|
|
|
|
|
|
def test_put_object_returns_s3_uri(s3_client: S3Client) -> None:
|
|
# act
|
|
uri = s3_client.put_object("folder/data.bin", b"payload")
|
|
# assert
|
|
assert uri == f"s3://{BUCKET}/folder/data.bin"
|
|
|
|
|
|
def test_get_object_returns_bytes_written_by_put_object(s3_client: S3Client) -> None:
|
|
# arrange
|
|
s3_client.put_object("round/trip.bin", b"hello world")
|
|
# act / assert
|
|
assert s3_client.get_object("round/trip.bin") == b"hello world"
|
|
|
|
|
|
def test_bucket_property_exposes_configured_bucket(s3_client: S3Client) -> None:
|
|
# act / assert
|
|
assert s3_client.bucket == BUCKET
|
|
|
|
|
|
def test_generate_presigned_url_signs_a_get_for_the_object(s3_client: S3Client) -> None:
|
|
# arrange
|
|
s3_client.put_object("bulk-downloads/pkg.zip", b"zip-bytes")
|
|
|
|
# act
|
|
url = s3_client.generate_presigned_url("bulk-downloads/pkg.zip", expires_in=3600)
|
|
|
|
# assert — a signed GET URL for this bucket + key
|
|
assert BUCKET in url
|
|
assert "bulk-downloads/pkg.zip" in url
|
|
assert "Signature" in url
|
|
|
|
|
|
def test_upload_file_streams_a_local_file_to_s3(
|
|
s3_client: S3Client, tmp_path: object
|
|
) -> None:
|
|
# arrange — a file on local disk (stands in for the /tmp ZIP)
|
|
from pathlib import Path
|
|
|
|
local = Path(str(tmp_path)) / "package.zip"
|
|
local.write_bytes(b"ZIP-CONTENT")
|
|
|
|
# act
|
|
uri = s3_client.upload_file(str(local), "bulk-downloads/package.zip")
|
|
|
|
# assert — the object landed with the file's bytes
|
|
assert uri == f"s3://{BUCKET}/bulk-downloads/package.zip"
|
|
assert s3_client.get_object("bulk-downloads/package.zip") == b"ZIP-CONTENT"
|