fix(file-assets): 预签名下载改用浏览器可访问的 MinIO 地址
This commit is contained in:
1 parent
00c52fdaae
commit
dfd48e08e7
9 files changed
+174
-30
No files matched your search
@@ -0,0 +1,79 @@
|
||||
"""
|
||||
backend/tests/services/file_assets/test_storage_presign.py
|
||||
|
||||
用途:验证业务文件预签名下载地址使用浏览器可访问的 MinIO 主机。
|
||||
依赖:file_assets.storage、minio。
|
||||
逻辑:正式环境内网端点与公网基址不同;断言签名 URL 的主机是公网基址,读写客户端仍是内网端点。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import pytest
|
||||
|
||||
from app.services.file_assets import storage as storage_mod
|
||||
from app.services.file_assets.storage import resolve_presign_endpoint
|
||||
|
||||
pytestmark = pytest.mark.pure_unit
|
||||
|
||||
|
||||
def _patch_minio_config(monkeypatch, *, public_base: str, secure: bool = False) -> None:
|
||||
"""把存储配置钉成正式环境形态:容器内网端点 + 浏览器公网基址。"""
|
||||
monkeypatch.setattr(storage_mod._cfg, "MINIO_ENDPOINT", "host.docker.internal:9000")
|
||||
monkeypatch.setattr(storage_mod._cfg, "MINIO_ACCESS_KEY", "test-access")
|
||||
monkeypatch.setattr(storage_mod._cfg, "MINIO_SECRET_KEY", "test-secret")
|
||||
monkeypatch.setattr(storage_mod._cfg, "MINIO_SECURE", secure)
|
||||
monkeypatch.setattr(storage_mod._cfg, "MINIO_PUBLIC_BASE_URL", public_base)
|
||||
monkeypatch.setattr(storage_mod._cfg, "MINIO_FILE_ASSETS_BUCKET", "hxops-file-assets")
|
||||
|
||||
|
||||
def test_resolve_presign_endpoint_keeps_explicit_port():
|
||||
"""公网基址的非默认端口必须留在预签名端点里。"""
|
||||
endpoint, secure = resolve_presign_endpoint(
|
||||
"http://ai.well-trust.com:9000",
|
||||
"host.docker.internal:9000",
|
||||
False,
|
||||
)
|
||||
# [断言意图] 正式环境浏览器入口是 9000,不能收成 80。
|
||||
assert endpoint == "ai.well-trust.com:9000"
|
||||
assert secure is False
|
||||
|
||||
|
||||
def test_resolve_presign_endpoint_falls_back_when_public_base_empty():
|
||||
"""公网基址为空时继续用内网端点,避免签出空主机。"""
|
||||
endpoint, secure = resolve_presign_endpoint("", "192.168.100.192:9000", False)
|
||||
# [断言意图] 未配置公网基址时行为与改前一致。
|
||||
assert endpoint == "192.168.100.192:9000"
|
||||
assert secure is False
|
||||
|
||||
|
||||
def test_presigned_get_url_uses_public_host(monkeypatch):
|
||||
"""正式环境下载链接的主机必须是 MINIO_PUBLIC_BASE_URL。"""
|
||||
_patch_minio_config(monkeypatch, public_base="http://ai.well-trust.com:9000")
|
||||
store = storage_mod.FileAssetStorage()
|
||||
monkeypatch.setattr(store, "_ensure_bucket_exists", lambda: None)
|
||||
|
||||
url = store.presigned_get_url(
|
||||
"shipping/outbound-reports/2026/10/1/abc.xlsx",
|
||||
expires_seconds=300,
|
||||
download_name="CRANE_20261020_1815.xlsx",
|
||||
)
|
||||
|
||||
# [断言意图] 浏览器打开的是公网 MinIO,签名里不能再出现容器内网主机。
|
||||
assert url.startswith(
|
||||
"http://ai.well-trust.com:9000/hxops-file-assets/shipping/outbound-reports/2026/10/1/abc.xlsx?"
|
||||
)
|
||||
assert "host.docker.internal" not in url
|
||||
assert "X-Amz-Signature=" in url
|
||||
assert "response-content-disposition=" in url
|
||||
# [断言意图] 读写仍走容器内网端点,避免把公网主机写进上传客户端。
|
||||
assert store.client is not store.presign_client
|
||||
assert store.client._base_url.host == "host.docker.internal:9000"
|
||||
assert store.presign_client._base_url.host == "ai.well-trust.com:9000"
|
||||
|
||||
|
||||
def test_presigned_get_url_reuses_client_when_hosts_match(monkeypatch):
|
||||
"""内网端点与公网基址相同时不另建客户端。"""
|
||||
_patch_minio_config(monkeypatch, public_base="http://host.docker.internal:9000")
|
||||
store = storage_mod.FileAssetStorage()
|
||||
# [断言意图] 开发环境两端点一致,预签名与读写共用同一客户端。
|
||||
assert store.presign_client is store.client
|
||||
Reference in new issue
Block a user