Files
ozon-seller-kit/server/tests/test_export.py
T

53 lines
1.9 KiB
Python

"""单图导出响应头回归测试。"""
from __future__ import annotations
import io
import sys
import unittest
import zipfile
from pathlib import Path
from unittest.mock import AsyncMock, patch
SERVER_DIR = Path(__file__).resolve().parents[1]
if str(SERVER_DIR) not in sys.path:
sys.path.insert(0, str(SERVER_DIR))
from api.export import ExportImageItem, ExportImagesRequest, export_image, export_images
class ExportImageTests(unittest.IsolatedAsyncioTestCase):
async def test_chinese_filename_uses_rfc5987_header(self) -> None:
image = ExportImageItem(
url="https://example.com/image.png",
key="尺寸标注图",
variantName="蓝色",
)
with patch("api.export._download", new=AsyncMock(return_value=(b"image", "image/png"))):
response = await export_image(image)
disposition = response.headers["content-disposition"]
self.assertIn('filename="_____-__.png"', disposition)
self.assertIn("filename*=UTF-8''%E5%B0%BA%E5%AF%B8%E6%A0%87%E6%B3%A8%E5%9B%BE-%E8%93%9D%E8%89%B2.png", disposition)
async def test_flat_export_places_generated_images_in_product_root(self) -> None:
request = ExportImagesRequest(
title="商品",
flat=True,
images=[
ExportImageItem(url="https://example.com/single.png", groupName="单张生图", key="single"),
ExportImageItem(url="https://example.com/suite.png", groupName="套图结果", key="suite"),
],
)
with patch("api.export._download", new=AsyncMock(return_value=(b"image", "image/png"))):
response = await export_images(request)
payload = b"".join([chunk async for chunk in response.body_iterator])
self.assertEqual(
sorted(zipfile.ZipFile(io.BytesIO(payload)).namelist()),
["商品/single.png", "商品/suite.png"],
)
if __name__ == "__main__":
unittest.main()