Merge remote-tracking branch 'origin/main' into feat/apps-in-toss

This commit is contained in:
minaChoi committed 2026-10-26 15:33:39 +09:00
commit 2627dd2685
200 files changed
+2956 -843

No files matched your search

+1 -1
View File
@@ -43,7 +43,7 @@ def anyio_backend():
_VOLATILE = (
"weather_hourly", "weather_daily", "cloud_hourly", "openmeteo_hourly", "air_station", "air_quality", "weather_warning", "sun_time",
"sky_score", "index_score", "post", "post_like", "post_like_anon", "post_save", "comment", "comment_like", "comment_like_anon", "holiday", "solar_term", "users", "card_set", "card_image",
"sky_score", "index_score", "post", "post_like", "post_like_anon", "post_save", "comment", "comment_like", "comment_like_anon", "holiday", "solar_term", "users", "card_set", "card_image", "reel",
)
+2 -2
View File
@@ -112,7 +112,7 @@ async def test_instagram_publish_carousel_and_store_media_id(offline, admin_key,
assert data["access_token"] == "tok"
if request.url.path.endswith("/1789/media"):
if data.get("media_type") == "CAROUSEL":
assert data["children"] == "c1,c2,c3" and data["caption"].startswith("오늘의 썸 타로")
assert data["children"] == "c1,c2,c3" and data["caption"] == "오늘의 썸 타로\n\n한 장 골라 보세요\n\n#날씨각 #날씨타로"
return httpx.Response(200, json={"id": "car"})
assert data["image_url"].startswith("https://weather.example/v1/card/") and data["is_carousel_item"] == "true"
return httpx.Response(200, json={"id": f"c{len([c for c in calls if 'is_carousel_item' in c[1]])}"})
@@ -126,7 +126,7 @@ async def test_instagram_publish_carousel_and_store_media_id(offline, admin_key,
real = httpx.AsyncClient
monkeypatch.setattr(instagram.httpx, "AsyncClient", lambda **kw: real(transport=httpx.MockTransport(handler)))
async with client() as c:
made = (await c.post("/v1/card", json=body(publish=True), headers=admin_key)).json()
made = (await c.post("/v1/card", json=body(publish=True, hashtags="#날씨각 #날씨타로"), headers=admin_key)).json()
res = (await c.post(f"/v1/card/{made['card_id']}/instagram", headers=admin_key)).json()
again = (await c.post(f"/v1/card/{made['card_id']}/instagram", headers=admin_key)).json()
listed = (await c.get("/v1/card", headers=admin_key)).json()["items"][0]
+18
View File
@@ -0,0 +1,18 @@
from starlette.requests import Request
from common.enums import PostType
from router.v1.feed.feed import TOSS_KNOWN_MAX, _from_toss
def _req(headers: dict) -> Request:
return Request({"type": "http", "headers": [(k.lower().encode(), v.encode()) for k, v in headers.items()]})
def test_from_toss_by_referer_or_user_agent():
assert _from_toss(_req({"referer": "https://nalssigak.apps.tossmini.com/"}))
assert _from_toss(_req({"user-agent": "Mozilla/5.0 ... AppsInToss TossApp/5.282.1"}))
assert not _from_toss(_req({"referer": "https://weather.o2o.kr/", "user-agent": "Mozilla/5.0 Safari"}))
def test_card_is_hidden_from_old_toss_bundle():
assert PostType.CARD.value > TOSS_KNOWN_MAX >= PostType.UV.value
+103
View File
@@ -0,0 +1,103 @@
import base64
import pytest
from httpx import ASGITransport, AsyncClient
from config.settings import settings
from router.router import app
KEY = "test-admin-key"
# 가장 작은 mp4 머리 (서버는 'ftyp' 상자만 본다)
MP4_BYTES = b"\x00\x00\x00\x18ftypisom" + b"0" * 200
MP4 = base64.b64encode(MP4_BYTES).decode()
JPEG = base64.b64encode(b"\xff\xd8\xff\xe0" + b"0" * 64).decode()
def client():
return AsyncClient(transport=ASGITransport(app=app), base_url="http://test")
def body(**kw):
return {"title": "날씨각이 뭐예요?", "caption": "오늘 날씨에 맞는 할 일을 점수로", "hashtags": "#날씨각 #릴스", "video": MP4, **kw}
@pytest.fixture
def admin_key(monkeypatch):
monkeypatch.setattr(settings, "admin_api_key", KEY)
return {"X-Admin-Key": KEY}
@pytest.mark.anyio
async def test_reel_needs_key_and_mp4(admin_key):
async with client() as c:
assert (await c.post("/v1/reel", json=body())).status_code == 401
bad = await c.post("/v1/reel", json=body(video=JPEG), headers=admin_key)
assert bad.status_code == 400
@pytest.mark.anyio
async def test_reel_saved_and_served_with_range(admin_key):
async with client() as c:
made = (await c.post("/v1/reel", json=body(cover=JPEG), headers=admin_key)).json()
assert made["result"]["success"] and made["video_url"] == f"/v1/reel/{made['reel_id']}.mp4" and made["cover_url"].endswith("/cover.jpg")
full = await c.get(made["video_url"])
assert full.status_code == 200 and full.headers["content-type"] == "video/mp4" and full.content == MP4_BYTES
part = await c.get(made["video_url"], headers={"Range": "bytes=4-7"})
assert part.status_code == 206 and part.content == b"ftyp" and part.headers["content-range"] == f"bytes 4-7/{len(MP4_BYTES)}"
cover = await c.get(made["cover_url"])
assert cover.status_code == 200 and cover.headers["content-type"] == "image/jpeg"
listed = (await c.get("/v1/reel", headers=admin_key)).json()["items"]
assert listed[0]["reel_id"] == made["reel_id"] and "video" not in listed[0]
assert (await c.get("/v1/reel/999999.mp4")).status_code == 404
@pytest.mark.anyio
async def test_reel_instagram_publish_and_store_media_id(admin_key, monkeypatch):
import httpx
from services.external import instagram
monkeypatch.setattr(settings, "instagram_access_token", "tok")
monkeypatch.setattr(settings, "instagram_user_id", "1789")
monkeypatch.setattr(settings, "public_base_url", "https://weather.example")
monkeypatch.setattr(instagram, "VIDEO_STATUS_WAIT_SEC", 0.0)
calls: list[tuple[str, dict]] = []
polls = {"n": 0}
def handler(request: httpx.Request) -> httpx.Response:
data = dict(httpx.QueryParams(request.content.decode())) if request.method == "POST" else dict(request.url.params)
calls.append((request.url.path, data))
assert data["access_token"] == "tok"
if request.url.path.endswith("/1789/media"):
assert data["media_type"] == "REELS" and data["share_to_feed"] == "true"
assert data["video_url"].startswith("https://weather.example/v1/reel/") and data["video_url"].endswith(".mp4")
assert data["cover_url"].endswith("/cover.jpg")
assert data["caption"] == "날씨각이 뭐예요?\n\n오늘 날씨에 맞는 할 일을 점수로\n\n#날씨각 #릴스"
return httpx.Response(200, json={"id": "vid"})
if request.url.path.endswith("/vid"):
polls["n"] += 1
return httpx.Response(200, json={"status_code": "IN_PROGRESS" if polls["n"] < 3 else "FINISHED"})
if request.url.path.endswith("/media_publish"):
assert data["creation_id"] == "vid"
return httpx.Response(200, json={"id": "reel77"})
return httpx.Response(404, json={"error": {"message": "no"}})
real = httpx.AsyncClient
monkeypatch.setattr(instagram.httpx, "AsyncClient", lambda **kw: real(transport=httpx.MockTransport(handler)))
async with client() as c:
made = (await c.post("/v1/reel", json=body(cover=JPEG), headers=admin_key)).json()
res = (await c.post(f"/v1/reel/{made['reel_id']}/instagram", headers=admin_key)).json()
again = (await c.post(f"/v1/reel/{made['reel_id']}/instagram", headers=admin_key)).json()
listed = (await c.get("/v1/reel", headers=admin_key)).json()["items"][0]
assert res["result"]["success"] and res["ig_media_id"] == "reel77" and polls["n"] == 3
assert again["ig_media_id"] == "reel77" and len([p for p, _ in calls if p.endswith("/media_publish")]) == 1
assert listed["ig_media_id"] == "reel77"
@pytest.mark.anyio
async def test_reel_instagram_requires_token(admin_key, monkeypatch):
monkeypatch.setattr(settings, "instagram_access_token", "")
async with client() as c:
made = (await c.post("/v1/reel", json=body(), headers=admin_key)).json()
res = (await c.post(f"/v1/reel/{made['reel_id']}/instagram", headers=admin_key)).json()
assert res["result"]["success"] is False and "INSTAGRAM" in res["msg"]