Files
o2o-weather/backend/tests/test_anon_like.py
T

175 lines
10 KiB
Python

import pytest
from httpx import ASGITransport, AsyncClient
from router.router import app
REGION = "1144000000"
IP_A = {"X-Forwarded-For": "203.0.113.10"}
IP_B = {"X-Forwarded-For": "203.0.113.20"}
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()
h = {"Authorization": f"Bearer {body['access_token']}"}
await c.post("/v1/auth/terms", headers=h) # 로그인한 사람은 약관에 동의해야 댓글을 쓴다
return h
@pytest.mark.anyio
async def test_anon_post_like_counts_once_per_ip(offline):
async with client() as c:
post_id = (await c.get("/v1/feed", params={"region": REGION})).json()["posts"][0]["post_id"]
url = f"/v1/reaction/post/{post_id}/like"
assert (await c.post(url, headers=IP_A)).json()["like_count"] == 1
assert (await c.post(url, headers=IP_A)).json()["like_count"] == 1
assert (await c.post(url, headers=IP_B)).json()["like_count"] == 2
h = await _login(c, "anon_like_01")
assert (await c.post(url, headers={**h, **IP_A})).json()["like_count"] == 3
feed_a = (await c.get("/v1/feed", params={"region": REGION}, headers=IP_A)).json()
p = next(x for x in feed_a["posts"] if x["post_id"] == post_id)
assert p["liked"] is True and p["like_count"] == 3 and "saved" not in p
other = (await c.get("/v1/feed", params={"region": REGION}, headers={"X-Forwarded-For": "203.0.113.30"})).json()
assert next(x for x in other["posts"] if x["post_id"] == post_id)["liked"] is False
assert (await c.delete(url, headers=IP_A)).json()["like_count"] == 2
assert (await c.delete(url, headers=IP_A)).json()["like_count"] == 2
@pytest.mark.anyio
async def test_only_last_forwarded_ip_counts(offline):
"""클라이언트가 X-Forwarded-For 앞쪽을 바꿔도 nginx 가 붙인 마지막 주소가 같으면 같은 사람이다."""
async with client() as c:
post_id = (await c.get("/v1/feed", params={"region": REGION})).json()["posts"][0]["post_id"]
url = f"/v1/reaction/post/{post_id}/like"
assert (await c.post(url, headers={"X-Forwarded-For": "1.1.1.1, 203.0.113.10"})).json()["like_count"] == 1
assert (await c.post(url, headers={"X-Forwarded-For": "2.2.2.2, 203.0.113.10"})).json()["like_count"] == 1
@pytest.mark.anyio
async def test_anon_comment_like(offline):
async with client() as c:
post_id = (await c.get("/v1/feed", params={"region": REGION})).json()["posts"][0]["post_id"]
h = await _login(c, "anon_like_02")
comment_id = (await c.post(f"/v1/reaction/post/{post_id}/comment", headers=h, json={"body": "맑아요"})).json()["comment"]["comment_id"]
url = f"/v1/reaction/comment/{comment_id}/like"
assert (await c.post(url, headers=IP_A)).json()["like_count"] == 1
assert (await c.post(url, headers=IP_A)).json()["like_count"] == 1
assert (await c.post(url, headers=IP_B)).json()["like_count"] == 2
assert (await c.post(url, headers=h)).json()["like_count"] == 3
listed = (await c.get(f"/v1/reaction/post/{post_id}/comment/list", headers=IP_A)).json()["comments"]
assert listed[0]["liked"] is True
listed = (await c.get(f"/v1/reaction/post/{post_id}/comment/list", headers={"X-Forwarded-For": "203.0.113.30"})).json()["comments"]
assert listed[0]["liked"] is False
assert (await c.delete(url, headers=IP_B)).json()["like_count"] == 2
@pytest.mark.anyio
async def test_anon_talk_like():
async with client() as c:
h = await _login(c, "anon_like_03")
talk_id = (await c.post("/v1/talk", json={"region_code": REGION, "body": "바람 불어요"}, headers=h)).json()["talk"]["talk_id"]
url = f"/v1/talk/{talk_id}/like"
assert (await c.post(url, headers=IP_A)).json()["like_count"] == 1
assert (await c.post(url, headers=IP_A)).json()["like_count"] == 1
assert (await c.post(url, headers=IP_B)).json()["like_count"] == 2
assert (await c.post(url, headers=h)).json()["like_count"] == 3
talks = (await c.get("/v1/talk/list", headers=IP_A)).json()["talks"]
assert next(x for x in talks if x["talk_id"] == talk_id)["liked"] is True
talks = (await c.get("/v1/talk/list", headers={"X-Forwarded-For": "203.0.113.30"})).json()["talks"]
assert next(x for x in talks if x["talk_id"] == talk_id)["liked"] is False
assert (await c.delete(url, headers=IP_A)).json()["like_count"] == 2
@pytest.mark.anyio
async def test_anon_comment_shows_region_and_only_same_ip_deletes(offline):
async with client() as c:
post_id = (await c.get("/v1/feed", params={"region": REGION})).json()["posts"][0]["post_id"]
url = f"/v1/reaction/post/{post_id}/comment"
made = (await c.post(url, headers=IP_A, json={"body": " 우산 챙겨요 ", "region": REGION})).json()["comment"]
assert made["body"] == "우산 챙겨요" and made["nickname"] == "마포구 익명" and made["mine"] is True
assert (await c.post(url, headers=IP_A, json={"body": "www.example.com 보세요"})).json()["result"]["desc"] == "LINK_NOT_ALLOWED"
no_region = (await c.post(url, headers=IP_A, json={"body": "안녕"})).json()["comment"]
assert no_region["nickname"] == "익명"
listed = (await c.get(f"{url}/list", headers=IP_B)).json()["comments"]
assert [(x["nickname"], x["mine"]) for x in listed] == [("마포구 익명", False), ("익명", False)]
assert next(x for x in (await c.get(f"{url}/list", headers=IP_A)).json()["comments"] if x["comment_id"] == made["comment_id"])["mine"] is True
h = await _login(c, "anon_cmt_01")
delete = f"/v1/reaction/comment/{made['comment_id']}"
assert (await c.delete(delete, headers=IP_B)).json()["result"]["desc"] == "NOT_FOUND"
assert (await c.delete(delete, headers={**h, **IP_A})).json()["result"]["desc"] == "NOT_FOUND"
assert (await c.delete(delete, headers=IP_A)).json()["result"]["success"] is True
feed = (await c.get("/v1/feed", params={"region": REGION})).json()
assert next(x for x in feed["posts"] if x["post_id"] == post_id)["comment_count"] == 1
@pytest.mark.anyio
async def test_anon_comment_daily_limit_per_ip(offline):
async with client() as c:
post_id = (await c.get("/v1/feed", params={"region": REGION})).json()["posts"][0]["post_id"]
url = f"/v1/reaction/post/{post_id}/comment"
ip = {"X-Forwarded-For": "203.0.113.40"}
for i in range(20):
assert (await c.post(url, headers=ip, json={"body": f"댓글{i}"})).json()["result"]["success"] is True
assert (await c.post(url, headers=ip, json={"body": "하나 더"})).json()["result"]["desc"] == "DAILY_LIMIT"
assert (await c.post(url, headers=IP_B, json={"body": "다른 사람"})).json()["result"]["success"] is True
h = await _login(c, "anon_cmt_02")
assert (await c.post(url, headers={**h, **ip}, json={"body": "로그인하면 따로"})).json()["result"]["success"] is True
@pytest.mark.anyio
async def test_hero_and_today_gak_take_likes_and_comments(offline):
async with client() as c:
feed = (await c.get("/v1/feed", params={"region": REGION})).json()
hero, gak = feed["hero_post"]["post_id"], feed["gak_post"]["post_id"]
assert hero != gak and {hero, gak}.isdisjoint(p["post_id"] for p in feed["posts"])
assert (await c.post(f"/v1/reaction/post/{hero}/like", headers=IP_A)).json()["like_count"] == 1
assert (await c.post(f"/v1/reaction/post/{gak}/comment", headers=IP_A, json={"body": "좋다"})).json()["result"]["success"] is True
mine = (await c.get("/v1/feed", params={"region": REGION}, headers=IP_A)).json()
assert mine["hero_post"] == {"post_id": hero, "like_count": 1, "comment_count": 0, "liked": True}
assert mine["gak_post"] == {"post_id": gak, "like_count": 0, "comment_count": 1, "liked": False}
assert (await c.get("/v1/feed", params={"region": REGION}, headers=IP_B)).json()["hero_post"]["liked"] is False
@pytest.mark.anyio
async def test_hero_tomorrow_and_tomorrow_slots(offline):
async with client() as c:
hero = (await c.get("/v1/feed", params={"region": REGION})).json()["hero"]
assert set(hero["tomorrow"]["weather_slots"]) <= {"morning", "day", "evening", "night"}
res = (await c.get("/v1/weather/today", params={"region": REGION, "day": "tomorrow"})).json()
assert res["day"] == hero["tomorrow"]["day"]
@pytest.mark.anyio
async def test_anon_comment_can_be_reported_not_blocked(offline):
"""로그인 없이 쓴 댓글도 신고는 되고(신고한 사람에겐 숨김), 차단할 사람이 없어 차단은 안 된다. 다른 사람을 차단해도 익명 댓글은 그대로 보인다."""
async with client() as c:
post_id = (await c.get("/v1/feed", params={"region": REGION})).json()["posts"][0]["post_id"]
url = f"/v1/reaction/post/{post_id}/comment"
listing = f"{url}/list"
anon_id = (await c.post(url, headers=IP_A, json={"body": "익명 댓글"})).json()["comment"]["comment_id"]
writer = await _login(c, "anon_mod_01")
writer_id = (await c.post(url, headers=writer, json={"body": "회원 댓글"})).json()["comment"]["comment_id"]
viewer = await _login(c, "anon_mod_02")
blocked = (await c.post(f"/v1/reaction/comment/{writer_id}/block", headers=viewer)).json()
assert blocked["result"]["success"] is True
ids = [x["comment_id"] for x in (await c.get(listing, headers=viewer)).json()["comments"]]
assert anon_id in ids and writer_id not in ids
assert (await c.post(f"/v1/reaction/comment/{anon_id}/block", headers=viewer)).json()["result"]["desc"] == "INVALID_INPUT"
assert (await c.post(f"/v1/reaction/comment/{anon_id}/report", headers=viewer, json={"reason": 1})).json()["result"]["success"] is True
assert anon_id not in [x["comment_id"] for x in (await c.get(listing, headers=viewer)).json()["comments"]]
assert anon_id in [x["comment_id"] for x in (await c.get(listing, headers=IP_B)).json()["comments"]]