"""OSS compatibility regressions: real SDK request preparation, no external I/O.""" from datetime import timedelta from unittest.mock import AsyncMock from urllib.parse import urlsplit import aioboto3 import pytest from botocore.exceptions import ClientError from fastapi import HTTPException from apps.base.models import FileCodes, StorageReservation, UploadChunk from core.settings import settings from core.storage import S3FileStorage from core.utils import get_now @pytest.fixture def storage(monkeypatch): monkeypatch.setattr(settings, "user_config", { **settings.user_config, "file_storage": "s3", "uploadSize": 1024, "chunk_expire_hours": 24, }) storage = S3FileStorage.__new__(S3FileStorage) storage.signature_version = "s3v4" storage.addressing_style = "virtual" storage.bucket_name = "test-bucket" # Reserved .invalid domain; before-send hooks stop requests before any I/O. storage.endpoint_url = "https://oss-cn-hangzhou.example.invalid" storage.region_name = "oss-cn-hangzhou" storage.aws_session_token = None storage.session = aioboto3.Session( aws_access_key_id="testing", aws_secret_access_key="testing" ) return storage def test_checksum_config_preserves_signature_and_addressing(storage): config = storage._client_config() assert config.request_checksum_calculation == "when_required" assert config.response_checksum_validation == "when_required" assert config.signature_version == "s3v4" assert config.s3["addressing_style"] == "virtual" @pytest.mark.asyncio @pytest.mark.parametrize("operation", ["PutObject", "UploadPart"]) async def test_sdk_upload_has_no_flexible_checksum_trailer(storage, monkeypatch, operation): monkeypatch.setenv("AWS_REQUEST_CHECKSUM_CALCULATION", "when_supported") monkeypatch.setenv("AWS_RESPONSE_CHECKSUM_VALIDATION", "when_supported") captured = [] class RequestCaptured(Exception): pass def intercept(request, **kwargs): captured.append(request) raise RequestCaptured # Register on the session so save_chunk's real _client() inherits the hook. storage.session.events.register(f"before-send.s3.{operation}", intercept) with pytest.raises(RequestCaptured): if operation == "PutObject": await storage.save_chunk("up", 0, b"payload", "hash", "share/file.bin") else: async with storage._client() as client: assert client.meta.config.response_checksum_validation == "when_required" await client.upload_part( Bucket=storage.bucket_name, Key="share/file.bin", UploadId="up", PartNumber=1, Body=b"payload", ) assert len(captured) == 1 request = captured[0] headers = {key.lower(): value for key, value in request.headers.items()} assert "x-amz-trailer" not in headers assert not any(key.startswith("x-amz-checksum-") for key in headers) assert "x-amz-sdk-checksum-algorithm" not in headers assert b"aws-chunked" not in headers.get("content-encoding", b"") assert b"TRAILER" not in headers.get("x-amz-content-sha256", b"") assert urlsplit(request.url).hostname == "test-bucket.oss-cn-hangzhou.example.invalid" assert not urlsplit(request.url).path.startswith("/test-bucket/")