36 lines
987 B
Python
36 lines
987 B
Python
from io import BytesIO
|
|
|
|
import pytest
|
|
from fastapi import HTTPException, UploadFile
|
|
from PIL import Image
|
|
from starlette.datastructures import Headers
|
|
|
|
from app.services.image_service import ImageService
|
|
|
|
|
|
def make_image(size=(800, 800), color=(230, 210, 190)):
|
|
image = Image.new("RGB", size, color=color)
|
|
buffer = BytesIO()
|
|
image.save(buffer, format="JPEG")
|
|
buffer.seek(0)
|
|
return buffer
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_rejects_unsupported_content_type():
|
|
service = ImageService()
|
|
upload = UploadFile(filename="palm.txt", file=BytesIO(b"not image"), headers=Headers({"content-type": "text/plain"}))
|
|
|
|
with pytest.raises(HTTPException):
|
|
await service.validate_and_store(upload, "user-1")
|
|
|
|
|
|
def test_inspect_rejects_low_resolution():
|
|
service = ImageService()
|
|
data = make_image(size=(300, 300)).getvalue()
|
|
|
|
result = service._inspect_image(data)
|
|
|
|
assert result["is_acceptable"] is False
|
|
assert "分辨率" in result["reason"]
|