Open source video conferencing app powered by LiveKit. Built with Django and React.
✅(summary) test the S3 FileClient service
briquet62a2515c6bd0
diff --git a/src/summary/tests/unit/test_file_service.py b/src/summary/tests/unit/test_file_service.pyindex 33f89b37..2941052d 100644--- a/src/summary/tests/unit/test_file_service.py+++ b/src/summary/tests/unit/test_file_service.py@@ -1,17 +1,22 @@"""Unit tests for the file service."""import json+from collections.abc import Callable, Iteratorfrom pathlib import Pathfrom unittest.mock import Mock+from urllib.parse import parse_qs, urlparseimport pytest+from botocore.stub import Stubberfrom summary.core import file_servicefrom summary.core.file_service import (+ FileService,MediaInfo,extract_audio_from_media,get_media_info,)+from summary.core.shared_models import WhisperXResponseBASE_PATH = Path(__file__).parent.parent / "assets"@@ -24,6 +29,46 @@ MEDIA_INFO_SAMPLE_VISIO = MediaInfo()+S3Settings = Callable[..., None]+++def s3_settings(monkeypatch: pytest.MonkeyPatch) -> S3Settings:+ """Configure the S3 settings read by the file service.++ The returned function overrides some of them on top of the current ones.+ The (frozen) settings are replaced by a copy, restored after the test.+ """++ def override(**values) -> None:+ monkeypatch.setattr(+ file_service, "settings", file_service.settings.model_copy(update=values)+ )++ override(+ aws_s3_endpoint_url="garage:9000",+ aws_s3_secure_access=False,+ aws_s3_region_name="fr-par",+ aws_storage_bucket_name="meet-media-storage",+ )+ return override+++def s3_stubber(+ monkeypatch: pytest.MonkeyPatch, s3_settings: S3Settings+) -> Iterator[Stubber]:+ """Stub the S3 client built by the file service.++ An unexpected S3 call fails the test instead of reaching the network.+ """+ stubber = Stubber(file_service._build_s3_client())+ stubber.activate()+ monkeypatch.setattr(file_service, "_build_s3_client", lambda: stubber.client)+ yield stubber+ stubber.assert_no_pending_responses()++@pytest.mark.parametrize("media_info",[@@ -149,3 +194,92 @@ def test_extract_audio_from_video():assert path.name.endswith(".m4a")finally:path.unlink(missing_ok=True)+++ ("endpoint_url", "secure_access", "expected_endpoint_url"),+ [+ ("garage:9000", False, "http://garage:9000"),+ ("http://garage:9000/", False, "http://garage:9000"),+ ("s3.example.com", True, "https://s3.example.com"),+ ("http://s3.example.com", True, "https://s3.example.com"),+ ],+)+def test_s3_client_endpoint_follows_secure_access(+ s3_settings: S3Settings,+ endpoint_url: str,+ secure_access: bool,+ expected_endpoint_url: str,+) -> None:+ """The endpoint scheme is taken from aws_s3_secure_access, not from the URL."""+ s3_settings(aws_s3_endpoint_url=endpoint_url, aws_s3_secure_access=secure_access)++ assert FileService()._s3_client.meta.endpoint_url == expected_endpoint_url++[email protected]("region_name", ["fr-par", None])+def test_s3_client_region_is_passed_as_is(+ monkeypatch: pytest.MonkeyPatch,+ s3_settings: S3Settings,+ region_name: str | None,+) -> None:+ """The configured region goes straight to boto3, even when it is unset."""+ s3_settings(aws_s3_region_name=region_name)+ boto3_client = Mock()+ monkeypatch.setattr(file_service.boto3, "client", boto3_client)++ assert FileService()._s3_client is boto3_client.return_value++ boto3_client.assert_called_once()+ assert boto3_client.call_args.kwargs["region_name"] == region_name+++def test_store_transcript(s3_stubber: Stubber) -> None:+ """The transcript is stored as JSON under the transcripts path."""+ transcript = WhisperXResponse(segments=())+ s3_stubber.add_response(+ "put_object",+ {},+ {+ "Bucket": "meet-media-storage",+ "Key": "transcripts/job-1.json",+ "Body": transcript.model_dump_json().encode(),+ },+ )++ FileService().store_transcript(transcript=transcript, job_id="job-1")+++def test_store_summary(s3_stubber: Stubber) -> None:+ """The summary is stored as text under the summaries path."""+ s3_stubber.add_response(+ "put_object",+ {},+ {+ "Bucket": "meet-media-storage",+ "Key": "summaries/job-1.txt",+ "Body": b"The summary",+ },+ )++ FileService().store_summary(summary="The summary", job_id="job-1")+++ ("method", "expected_path"),+ [+ ("get_transcript_signed_url", "/meet-media-storage/transcripts/job-1.json"),+ ("get_summary_signed_url", "/meet-media-storage/summaries/job-1.txt"),+ ],+)+def test_signed_urls(s3_stubber: Stubber, method: str, expected_path: str) -> None:+ """Signed URLs are path-style, SigV4-signed for the region, valid for a day."""+ url = urlparse(getattr(FileService(), method)("job-1"))+ query = parse_qs(url.query)++ assert url.scheme == "http"+ assert url.netloc == "garage:9000"+ assert url.path == expected_path+ assert query["X-Amz-Algorithm"] == ["AWS4-HMAC-SHA256"]+ assert "/fr-par/s3/aws4_request" in query["X-Amz-Credential"][0]+ assert query["X-Amz-Expires"] == ["86400"]