- 공유 이미지 9:16·4:5·1:1, 위 로고·지역 날짜 / 카드 / 아래 주소 알약 (시안 A) - 2장 카드는 한 이미지에 묶음, 버튼만 빼고 링크(장소)는 남김 - 웹폰트는 카드 글자에 필요한 조각만 data URL 로 넣어 글꼴이 안 깨지게 - 문구 고르기·링크 복사 제거, 공유하기 한 번에 이미지 + 제목·주소 글 - 행사 사진은 백엔드 /v1/image 가 대신 받음 (IMAGE_PROXY_HOSTS 만 허용) - 공유 주소는 VITE_SITE_URL 하나로
94 lines
3.6 KiB
Python
94 lines
3.6 KiB
Python
import httpx
|
|
import pytest
|
|
from httpx import ASGITransport, AsyncClient
|
|
|
|
from router.router import app
|
|
from services.external import image_proxy
|
|
from services.external.image_proxy import ImageFetchFailed, allowed, fetch_image
|
|
|
|
OK_URL = "https://tong.visitkorea.or.kr/cms/resource/01/test_image.jpg"
|
|
|
|
|
|
@pytest.mark.parametrize("url,ok", [
|
|
(OK_URL, True),
|
|
("https://www.culture.go.kr/upload/test.jpg", True),
|
|
("http://tong.visitkorea.or.kr/cms/a.jpg", False),
|
|
("https://evil.example.com/a.jpg", False),
|
|
("https://tong.visitkorea.or.kr.evil.example.com/a.jpg", False),
|
|
("https://tong.visitkorea.or.kr:8443/a.jpg", False),
|
|
("https://user:pw@tong.visitkorea.or.kr/a.jpg", False),
|
|
("https://169.254.169.254/latest/meta-data", False),
|
|
("not a url", False),
|
|
])
|
|
def test_allowed(url, ok):
|
|
assert allowed(url) is ok
|
|
|
|
|
|
def _client(handler) -> httpx.AsyncClient:
|
|
return httpx.AsyncClient(transport=httpx.MockTransport(handler))
|
|
|
|
|
|
@pytest.mark.anyio
|
|
async def test_fetch_image_returns_bytes_and_type():
|
|
async with _client(lambda r: httpx.Response(200, content=b"\xff\xd8jpeg", headers={"content-type": "image/jpeg; charset=binary"})) as c:
|
|
assert await fetch_image(c, OK_URL) == (b"\xff\xd8jpeg", "image/jpeg")
|
|
|
|
|
|
@pytest.mark.anyio
|
|
@pytest.mark.parametrize("res", [
|
|
httpx.Response(302, headers={"location": "https://evil.example.com/a.jpg"}),
|
|
httpx.Response(404),
|
|
httpx.Response(200, content=b"<html>", headers={"content-type": "text/html"}),
|
|
httpx.Response(200, content=b"<svg/>", headers={"content-type": "image/svg+xml"}),
|
|
])
|
|
async def test_fetch_image_rejects_redirect_error_and_non_image(res):
|
|
async with _client(lambda r: res) as c:
|
|
with pytest.raises(ImageFetchFailed):
|
|
await fetch_image(c, OK_URL)
|
|
|
|
|
|
@pytest.mark.anyio
|
|
async def test_fetch_image_rejects_too_large(monkeypatch):
|
|
monkeypatch.setattr(image_proxy, "MAX_BYTES", 10)
|
|
async with _client(lambda r: httpx.Response(200, content=b"x" * 11, headers={"content-type": "image/png"})) as c:
|
|
with pytest.raises(ImageFetchFailed):
|
|
await fetch_image(c, OK_URL)
|
|
|
|
|
|
@pytest.mark.anyio
|
|
async def test_image_api_blocks_other_hosts():
|
|
async with AsyncClient(transport=ASGITransport(app=app), base_url="http://test") as c:
|
|
res = await c.get("/v1/image", params={"url": "https://evil.example.com/a.jpg"})
|
|
assert res.status_code == 400
|
|
|
|
|
|
@pytest.mark.anyio
|
|
async def test_image_api_serves_image_with_cache(monkeypatch):
|
|
async def fake(_client, url):
|
|
assert url == OK_URL
|
|
return b"png-bytes", "image/png"
|
|
monkeypatch.setattr("router.v1.image.image.fetch_image", fake)
|
|
async with AsyncClient(transport=ASGITransport(app=app), base_url="http://test") as c:
|
|
res = await c.get("/v1/image", params={"url": OK_URL})
|
|
assert res.status_code == 200
|
|
assert res.content == b"png-bytes"
|
|
assert res.headers["content-type"] == "image/png"
|
|
assert "max-age=86400" in res.headers["cache-control"]
|
|
|
|
|
|
@pytest.mark.anyio
|
|
async def test_image_api_reports_upstream_failure(monkeypatch):
|
|
async def fail(_client, _url):
|
|
raise ImageFetchFailed("status 404")
|
|
monkeypatch.setattr("router.v1.image.image.fetch_image", fail)
|
|
async with AsyncClient(transport=ASGITransport(app=app), base_url="http://test") as c:
|
|
res = await c.get("/v1/image", params={"url": OK_URL})
|
|
assert res.status_code == 502
|
|
|
|
|
|
def test_allowed_follows_settings(monkeypatch):
|
|
from config.settings import settings
|
|
monkeypatch.setattr(settings, "image_proxy_hosts", " cdn.example.org , ")
|
|
assert allowed("https://cdn.example.org/a.jpg") is True
|
|
assert allowed(OK_URL) is False
|