cb85801061
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
97 lines
3.1 KiB
Python
97 lines
3.1 KiB
Python
import io
|
|
import pytest
|
|
from pathlib import Path
|
|
|
|
|
|
def _make_jpeg(width: int = 200, height: int = 200) -> bytes:
|
|
try:
|
|
from PIL import Image
|
|
img = Image.new("RGB", (width, height), color=(120, 80, 40))
|
|
buf = io.BytesIO()
|
|
img.save(buf, format="JPEG")
|
|
return buf.getvalue()
|
|
except ImportError:
|
|
pytest.skip("Pillow non disponible localement (Python 3.14) — OK en Docker")
|
|
|
|
|
|
async def test_upload_image_retourne_chemins(client, tmp_path, monkeypatch):
|
|
from app.services import media as media_svc
|
|
monkeypatch.setattr(media_svc, "UPLOAD_DIR", tmp_path)
|
|
|
|
response = await client.post(
|
|
"/api/media/upload",
|
|
files={"file": ("photo.jpg", _make_jpeg(), "image/jpeg")},
|
|
params={"context": "note"},
|
|
)
|
|
assert response.status_code == 200
|
|
data = response.json()
|
|
assert "file_id" in data
|
|
assert data["file_path"].startswith("images/originals/")
|
|
assert data["thumbnail_path"].startswith("images/thumbnails/")
|
|
|
|
|
|
async def test_upload_format_invalide_retourne_400(client, tmp_path, monkeypatch):
|
|
from app.services import media as media_svc
|
|
monkeypatch.setattr(media_svc, "UPLOAD_DIR", tmp_path)
|
|
|
|
response = await client.post(
|
|
"/api/media/upload",
|
|
files={"file": ("doc.pdf", b"contenu pdf", "application/pdf")},
|
|
)
|
|
assert response.status_code == 400
|
|
|
|
|
|
async def test_miniature_produit_max_150px(client, tmp_path, monkeypatch):
|
|
try:
|
|
from PIL import Image
|
|
except ImportError:
|
|
pytest.skip("Pillow non disponible localement")
|
|
from app.services import media as media_svc
|
|
monkeypatch.setattr(media_svc, "UPLOAD_DIR", tmp_path)
|
|
|
|
response = await client.post(
|
|
"/api/media/upload",
|
|
files={"file": ("prod.jpg", _make_jpeg(800, 600), "image/jpeg")},
|
|
params={"context": "product"},
|
|
)
|
|
assert response.status_code == 200
|
|
data = response.json()
|
|
thumb = Image.open(tmp_path / data["thumbnail_path"])
|
|
assert thumb.width <= 150
|
|
assert thumb.height <= 150
|
|
|
|
|
|
async def test_upload_audio_retourne_chemin(client, tmp_path, monkeypatch):
|
|
from app.services import media as media_svc
|
|
monkeypatch.setattr(media_svc, "UPLOAD_DIR", tmp_path)
|
|
|
|
response = await client.post(
|
|
"/api/media/upload",
|
|
files={"file": ("enreg.webm", b"fake webm bytes", "audio/webm")},
|
|
params={"context": "note"},
|
|
)
|
|
assert response.status_code == 200
|
|
data = response.json()
|
|
assert data["file_path"].startswith("audio/")
|
|
assert data["thumbnail_path"] is None
|
|
|
|
|
|
async def test_suppression_media(client, tmp_path, monkeypatch):
|
|
from app.services import media as media_svc
|
|
monkeypatch.setattr(media_svc, "UPLOAD_DIR", tmp_path)
|
|
|
|
up = await client.post(
|
|
"/api/media/upload",
|
|
files={"file": ("enreg.webm", b"fake webm bytes", "audio/webm")},
|
|
params={"context": "note"},
|
|
)
|
|
data = up.json()
|
|
file_id = data["file_id"]
|
|
|
|
response = await client.delete(
|
|
f"/api/media/{file_id}",
|
|
params={"file_path": data["file_path"]},
|
|
)
|
|
assert response.status_code == 204
|
|
assert not (tmp_path / data["file_path"]).exists()
|