import io import pytest from httpx import ASGITransport, AsyncClient from PIL import Image from router.router import app REGION = "1144000000" GPS_IFD = 0x8825 def client(): return AsyncClient(transport=ASGITransport(app=app), base_url="http://test") async def _login(c, login_id: str) -> dict: body = (await c.post("/v1/auth/signup", json={"login_id": login_id, "password": "password1", "nickname": login_id})).json() return {"Authorization": f"Bearer {body['access_token']}"} def _jpeg_with_gps(w: int = 2400, h: int = 1600) -> bytes: exif = Image.Exif() exif[0x010F] = "PhoneMaker" exif[GPS_IFD] = {1: "N", 2: (37.0, 33.0, 0.0), 3: "E", 4: (126.0, 58.0, 0.0)} buf = io.BytesIO() Image.new("RGB", (w, h), (120, 180, 230)).save(buf, "JPEG", exif=exif) return buf.getvalue() @pytest.mark.anyio async def test_upload_strips_location_resizes_and_attaches_to_talk(): src = _jpeg_with_gps() assert Image.open(io.BytesIO(src)).getexif().get_ifd(GPS_IFD) async with client() as c: assert (await c.post("/v1/talk/image", files={"file": ("a.jpg", src, "image/jpeg")})).status_code == 401 h = await _login(c, "photo_01") url = (await c.post("/v1/talk/image", files={"file": ("a.jpg", src, "image/jpeg")}, headers=h)).json()["image"] got = await c.get(url) assert got.status_code == 200 and got.headers["content-type"] == "image/webp" img = Image.open(io.BytesIO(got.content)) assert max(img.size) == 1080 assert not img.getexif() talk = (await c.post("/v1/talk", json={"region_code": REGION, "body": "하늘 사진", "image": url}, headers=h)).json()["talk"] assert talk["image"] == url listed = (await c.get("/v1/talk/list")).json()["talks"][0] assert listed["image"] == url @pytest.mark.anyio async def test_rejects_non_images_and_foreign_paths(): async with client() as c: h = await _login(c, "photo_02") bad = (await c.post("/v1/talk/image", files={"file": ("a.txt", b"not an image", "text/plain")}, headers=h)).json() assert bad["result"]["desc"] == "INVALID_INPUT" for image in ("https://evil.example/a.webp", "/v1/talk/image/../../etc/passwd", "/v1/talk/image/" + "0" * 32 + ".webp"): res = (await c.post("/v1/talk", json={"region_code": REGION, "body": "안녕", "image": image}, headers=h)).json() assert res["result"]["desc"] == "INVALID_INPUT" assert (await c.get("/v1/talk/image/" + "0" * 32 + ".webp")).status_code == 404