* 禁用 OSS 非必需校验尾部,改用精确会话前缀逐对象清理 * 保护上传终态与分享事务,清理失败保留会话并支持安全重试 * 统一下载响应头,兼容旧下载器并防止文件名注入 * 限流改用单调时钟并限制 IP 记录,缓存模板与静态资源策略 * 统一 ORM 初始化与异常处理,新增查询索引及 SQLite 性能参数 * 固定 uvloop/httptools,统一 Docker keep-alive 与内存回收配置 * 同步版本与升级文档,补充 OSS、清理、缓存及数据库回归验证
82 lines
3.3 KiB
Python
82 lines
3.3 KiB
Python
"""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/")
|