fix(platform): 持久化并完善斗鱼兑换结果图片
This commit is contained in:
@@ -0,0 +1,183 @@
|
||||
"""斗鱼兑换结果图片持久化到文件存储并绑定履约任务。"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import io
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
from PIL import Image
|
||||
|
||||
from web.backend.models import (
|
||||
Account,
|
||||
DouyuTask,
|
||||
FileAsset,
|
||||
PlatformFulfillmentTask,
|
||||
User,
|
||||
)
|
||||
from web.backend.services.douyu_exchange_result_image import (
|
||||
ExchangeResultImageError,
|
||||
save_exchange_result_image,
|
||||
)
|
||||
from web.backend.services.file_storage import reset_object_storage_cache
|
||||
from web.backend.services.platform_fulfillment import service as fulfillment_service
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def local_storage(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> Path:
|
||||
monkeypatch.setenv("OSS_MODE", "local")
|
||||
monkeypatch.setenv("OSS_LOCAL_ROOT", str(tmp_path))
|
||||
monkeypatch.setenv("OSS_MAX_UPLOAD_SIZE_MB", "10")
|
||||
reset_object_storage_cache()
|
||||
yield tmp_path
|
||||
reset_object_storage_cache()
|
||||
|
||||
|
||||
def _png_bytes() -> bytes:
|
||||
buffer = io.BytesIO()
|
||||
Image.new("RGB", (320, 470), (20, 60, 120)).save(buffer, "PNG")
|
||||
return buffer.getvalue()
|
||||
|
||||
|
||||
def _records(db) -> tuple[User, Account, PlatformFulfillmentTask, DouyuTask]:
|
||||
user = User(username="exchange-image-user", password_hash="h", role="support")
|
||||
db.add(user)
|
||||
db.flush()
|
||||
account = Account(
|
||||
username="exchange-image-account",
|
||||
password="p",
|
||||
email="exchange-image@example.com",
|
||||
email_password="ep",
|
||||
status="active",
|
||||
assigned_to=user.id,
|
||||
)
|
||||
db.add(account)
|
||||
db.flush()
|
||||
platform_task = PlatformFulfillmentTask(
|
||||
source_order_id="exchange-order",
|
||||
source_order_item_id="exchange-item",
|
||||
internal_sku_key="套装-花羊椰椰",
|
||||
sku_name="套装-花羊椰椰",
|
||||
target_platform="douyu",
|
||||
account_id=account.id,
|
||||
binding_status="bound",
|
||||
execution_status="bound",
|
||||
exchange_status="eligible",
|
||||
)
|
||||
db.add(platform_task)
|
||||
db.flush()
|
||||
douyu_task = DouyuTask(
|
||||
batch_id="exchange-image-batch",
|
||||
account_id=account.id,
|
||||
account_assigned_to=user.id,
|
||||
task_type="exchange_goods",
|
||||
handbook_scope="elite",
|
||||
status="success",
|
||||
message="兑换成功",
|
||||
result={"goods": {"commodityName": "套装-花羊椰椰"}},
|
||||
created_by=user.id,
|
||||
platform_task_id=platform_task.id,
|
||||
)
|
||||
db.add(douyu_task)
|
||||
db.commit()
|
||||
return user, account, platform_task, douyu_task
|
||||
|
||||
|
||||
def test_saves_image_and_binds_it_to_both_tasks(db, local_storage: Path):
|
||||
user, _account, platform_task, douyu_task = _records(db)
|
||||
|
||||
saved = save_exchange_result_image(
|
||||
db,
|
||||
douyu_task,
|
||||
data=_png_bytes(),
|
||||
filename="兑换结果.png",
|
||||
uploader_id=user.id,
|
||||
platform_task=platform_task,
|
||||
)
|
||||
|
||||
assert saved["objectKey"].startswith("exchange-result/")
|
||||
assert saved["url"].startswith("/api/files/object?key=")
|
||||
db.refresh(douyu_task)
|
||||
db.refresh(platform_task)
|
||||
assert douyu_task.result["exchange_result_image"]["object_key"] == saved["objectKey"]
|
||||
assert platform_task.exchange_task_id == douyu_task.id
|
||||
assert platform_task.exchange_result_image_key == saved["objectKey"]
|
||||
assert platform_task.exchange_result_image_saved_at is not None
|
||||
asset = db.query(FileAsset).one()
|
||||
assert asset.object_key == saved["objectKey"]
|
||||
assert asset.scene == "exchange-result"
|
||||
assert asset.uploader_id == str(user.id)
|
||||
stored = [path for path in local_storage.rglob("*") if path.is_file()]
|
||||
assert len(stored) == 3
|
||||
|
||||
payload = fulfillment_service._task_out(db, platform_task)
|
||||
assert payload["exchangeResultImage"]["objectKey"] == saved["objectKey"]
|
||||
assert payload["exchangeResultImage"]["url"] == saved["url"]
|
||||
|
||||
|
||||
def test_saving_is_idempotent(db, local_storage: Path):
|
||||
user, _account, platform_task, douyu_task = _records(db)
|
||||
first = save_exchange_result_image(
|
||||
db,
|
||||
douyu_task,
|
||||
data=_png_bytes(),
|
||||
uploader_id=user.id,
|
||||
platform_task=platform_task,
|
||||
)
|
||||
|
||||
second = save_exchange_result_image(
|
||||
db,
|
||||
douyu_task,
|
||||
data=_png_bytes(),
|
||||
uploader_id=user.id,
|
||||
platform_task=platform_task,
|
||||
)
|
||||
|
||||
assert second == first
|
||||
assert db.query(FileAsset).count() == 1
|
||||
|
||||
|
||||
def test_exchange_operation_links_the_douyu_task(db):
|
||||
user, _account, platform_task, douyu_task = _records(db)
|
||||
|
||||
fulfillment_service.record_douyu_operation_started(
|
||||
db,
|
||||
platform_task,
|
||||
current=user,
|
||||
batch_id=douyu_task.batch_id,
|
||||
task_type="exchange_goods",
|
||||
douyu_task_ids=[douyu_task.id],
|
||||
)
|
||||
|
||||
db.refresh(platform_task)
|
||||
assert platform_task.exchange_task_id == douyu_task.id
|
||||
assert fulfillment_service._task_out(db, platform_task)["exchangeTaskId"] == douyu_task.id
|
||||
|
||||
|
||||
def test_detail_recovers_legacy_exchange_task_by_platform_task_id(db):
|
||||
_user, _account, platform_task, douyu_task = _records(db)
|
||||
platform_task.exchange_task_id = None
|
||||
db.commit()
|
||||
|
||||
payload = fulfillment_service._task_out(
|
||||
db,
|
||||
platform_task,
|
||||
include_exchange_task=True,
|
||||
)
|
||||
|
||||
assert payload["exchangeTaskId"] == douyu_task.id
|
||||
|
||||
|
||||
def test_rejects_non_exchange_or_unfinished_task(db, local_storage: Path):
|
||||
user, _account, platform_task, douyu_task = _records(db)
|
||||
douyu_task.task_type = "query_elite_status"
|
||||
db.commit()
|
||||
|
||||
with pytest.raises(ExchangeResultImageError, match="不是兑换任务"):
|
||||
save_exchange_result_image(
|
||||
db,
|
||||
douyu_task,
|
||||
data=_png_bytes(),
|
||||
uploader_id=user.id,
|
||||
platform_task=platform_task,
|
||||
)
|
||||
Reference in New Issue
Block a user