244 lines
15 KiB
Python
244 lines
15 KiB
Python
import pytest
|
|
from httpx import ASGITransport, AsyncClient
|
|
|
|
from router.router import app
|
|
|
|
REGION = "1144000000"
|
|
|
|
|
|
def client():
|
|
return AsyncClient(transport=ASGITransport(app=app), base_url="http://test")
|
|
|
|
|
|
async def _signup(c, login_id="mina_01", nickname="망원동주민", agree=True):
|
|
"""가입하고, 댓글을 쓸 수 있게 이용약관에도 동의한다 (agree=False 면 안 함)."""
|
|
body = (await c.post("/v1/auth/signup", json={"login_id": login_id, "password": "password1", "nickname": nickname})).json()
|
|
if agree and body.get("access_token"):
|
|
await c.post("/v1/auth/terms", headers={"Authorization": f"Bearer {body['access_token']}"})
|
|
return body
|
|
|
|
|
|
@pytest.mark.anyio
|
|
async def test_signup_login_refresh_me():
|
|
async with client() as c:
|
|
body = await _signup(c)
|
|
assert body["result"]["success"] is True and body["me"]["nickname"] == "망원동주민"
|
|
dup = await _signup(c)
|
|
assert dup["result"]["desc"] == "AUTH_DUPLICATE_ID"
|
|
bad = (await c.post("/v1/auth/login", json={"login_id": "mina_01", "password": "wrong-pass"})).json()
|
|
assert bad["result"]["desc"] == "AUTH_FAILED"
|
|
ok = (await c.post("/v1/auth/login", json={"login_id": "mina_01", "password": "password1"})).json()
|
|
me = (await c.get("/v1/auth/me", headers={"Authorization": f"Bearer {ok['access_token']}"})).json()
|
|
assert me["me"]["login_id"] == "mina_01"
|
|
again = (await c.post("/v1/auth/refresh", json={"refresh_token": ok["refresh_token"]})).json()
|
|
assert again["access_token"]
|
|
assert (await c.post("/v1/auth/refresh", json={"refresh_token": ok["access_token"]})).json()["result"]["desc"] == "AUTH_TOKEN_INVALID"
|
|
|
|
|
|
@pytest.mark.anyio
|
|
async def test_password_over_72_bytes_is_rejected_not_500():
|
|
long_pw = "비밀번호" * 7 # 84바이트
|
|
async with client() as c:
|
|
r = await c.post("/v1/auth/signup", json={"login_id": "mina_long", "password": long_pw, "nickname": "긴비번"})
|
|
assert r.status_code == 200 and r.json()["result"]["desc"] == "INVALID_INPUT"
|
|
await _signup(c, login_id="mina_02")
|
|
r = await c.post("/v1/auth/login", json={"login_id": "mina_02", "password": long_pw})
|
|
assert r.status_code == 200 and r.json()["result"]["desc"] == "AUTH_FAILED"
|
|
|
|
|
|
@pytest.mark.anyio
|
|
async def test_signup_rejects_bad_input():
|
|
async with client() as c:
|
|
body = (await c.post("/v1/auth/signup", json={"login_id": "AB", "password": "short", "nickname": "x"})).json()
|
|
assert body["result"]["desc"] == "INVALID_INPUT"
|
|
|
|
|
|
@pytest.mark.anyio
|
|
async def test_comment_without_login_is_anonymous_and_reactions_update_feed(offline):
|
|
async with client() as c:
|
|
feed = (await c.get("/v1/feed", params={"region": REGION})).json()
|
|
post_id = feed["posts"][0]["post_id"]
|
|
anon = (await c.post(f"/v1/reaction/post/{feed['posts'][1]['post_id']}/comment", json={"body": "안녕"})).json()
|
|
assert anon["result"]["success"] is True and anon["comment"]["nickname"] == "익명"
|
|
|
|
token = (await _signup(c))["access_token"]
|
|
h = {"Authorization": f"Bearer {token}"}
|
|
liked = (await c.post(f"/v1/reaction/post/{post_id}/like", headers=h)).json()
|
|
again = (await c.post(f"/v1/reaction/post/{post_id}/like", headers=h)).json()
|
|
assert liked["like_count"] == 1 and again["like_count"] == 1
|
|
assert (await c.post(f"/v1/reaction/post/{post_id}/save", headers=h)).json()["saved"] is True
|
|
|
|
comment = (await c.post(f"/v1/reaction/post/{post_id}/comment", headers=h, json={"body": " 빨래 지금 널어요! "})).json()["comment"]
|
|
assert comment["body"] == "빨래 지금 널어요!" and comment["mine"] is True
|
|
too_long = (await c.post(f"/v1/reaction/post/{post_id}/comment", headers=h, json={"body": "가" * 61})).json()
|
|
assert too_long["result"]["desc"] == "INVALID_INPUT"
|
|
assert (await c.post(f"/v1/reaction/comment/{comment['comment_id']}/like", headers=h)).json()["like_count"] == 1
|
|
|
|
listed = (await c.get(f"/v1/reaction/post/{post_id}/comment/list")).json()
|
|
assert [x["body"] for x in listed["comments"]] == ["빨래 지금 널어요!"] and listed["comments"][0]["mine"] is False
|
|
|
|
mine = (await c.get("/v1/feed", params={"region": REGION}, headers=h)).json()
|
|
p = next(x for x in mine["posts"] if x["post_id"] == post_id)
|
|
assert p["liked"] is True and p["saved"] is True and p["like_count"] == 1 and p["comment_count"] == 1
|
|
|
|
other = (await _signup(c, "other_02", "다른사람"))["access_token"]
|
|
assert (await c.delete(f"/v1/reaction/comment/{comment['comment_id']}", headers={"Authorization": f"Bearer {other}"})).json()["result"]["desc"] == "NOT_FOUND"
|
|
assert (await c.delete(f"/v1/reaction/comment/{comment['comment_id']}", headers=h)).json()["result"]["success"] is True
|
|
assert (await c.get("/v1/reaction/save/list", headers=h)).json()["posts"][0]["post_id"] == post_id
|
|
|
|
|
|
@pytest.mark.anyio
|
|
async def test_bad_token_is_401():
|
|
async with client() as c:
|
|
res = await c.get("/v1/auth/me", headers={"Authorization": "Bearer not-a-token"})
|
|
assert res.status_code == 401
|
|
|
|
|
|
@pytest.mark.anyio
|
|
async def test_update_me_changes_character_and_nickname_and_shows_on_comments(offline):
|
|
async with client() as c:
|
|
token = (await _signup(c))["access_token"]
|
|
h = {"Authorization": f"Bearer {token}"}
|
|
assert (await c.get("/v1/auth/me", headers=h)).json()["me"]["character"] == "rabbit"
|
|
me = (await c.patch("/v1/auth/me", headers=h, json={"character": "bear", "nickname": "곰돌이", "color": "blue"})).json()["me"]
|
|
assert (me["character"], me["nickname"], me["color"]) == ("bear", "곰돌이", "blue")
|
|
assert (await c.patch("/v1/auth/me", headers=h, json={"color": "gold"})).json()["result"]["desc"] == "INVALID_INPUT"
|
|
assert (await c.patch("/v1/auth/me", headers=h, json={"mood": "thunder"})).json()["me"]["mood"] == "thunder"
|
|
assert (await c.patch("/v1/auth/me", headers=h, json={"mood": "sleepy"})).json()["result"]["desc"] == "INVALID_INPUT"
|
|
assert (await c.patch("/v1/auth/me", headers=h, json={"character": "dragon"})).json()["result"]["desc"] == "INVALID_INPUT"
|
|
assert (await c.patch("/v1/auth/me", json={"character": "bear"})).status_code == 401
|
|
|
|
post_id = (await c.get("/v1/feed", params={"region": REGION})).json()["posts"][0]["post_id"]
|
|
await c.post(f"/v1/reaction/post/{post_id}/comment", headers=h, json={"body": "안녕"})
|
|
comments = (await c.get(f"/v1/reaction/post/{post_id}/comment/list")).json()["comments"]
|
|
assert (comments[0]["character"], comments[0]["color"], comments[0]["nickname"]) == ("bear", "blue", "곰돌이")
|
|
assert comments[0]["mood"] == "thunder"
|
|
assert "mood" not in (await c.patch("/v1/auth/me", headers=h, json={"mood": ""})).json()["me"]
|
|
|
|
|
|
@pytest.mark.anyio
|
|
async def test_logout_invalidates_access_and_refresh_tokens():
|
|
async with client() as c:
|
|
body = await _signup(c, login_id="mina_out")
|
|
h = {"Authorization": f"Bearer {body['access_token']}"}
|
|
assert (await c.post("/v1/auth/logout", headers=h)).json()["result"]["success"] is True
|
|
assert (await c.get("/v1/auth/me", headers=h)).status_code == 401
|
|
assert (await c.post("/v1/auth/refresh", json={"refresh_token": body["refresh_token"]})).json()["result"]["desc"] == "AUTH_TOKEN_INVALID"
|
|
again = (await c.post("/v1/auth/login", json={"login_id": "mina_out", "password": "password1"})).json()
|
|
assert (await c.get("/v1/auth/me", headers={"Authorization": f"Bearer {again['access_token']}"})).status_code == 200
|
|
|
|
|
|
@pytest.mark.anyio
|
|
async def test_withdraw_deletes_account_and_reactions(offline):
|
|
async with client() as c:
|
|
body = await _signup(c, login_id="mina_bye")
|
|
h = {"Authorization": f"Bearer {body['access_token']}"}
|
|
other = {"Authorization": f"Bearer {(await _signup(c, 'other_bye', '남는사람'))['access_token']}"}
|
|
post_id = (await c.get("/v1/feed", params={"region": REGION})).json()["posts"][0]["post_id"]
|
|
await c.post(f"/v1/reaction/post/{post_id}/like", headers=h)
|
|
await c.post(f"/v1/reaction/post/{post_id}/save", headers=h)
|
|
mine = (await c.post(f"/v1/reaction/post/{post_id}/comment", headers=h, json={"body": "곧 떠나요"})).json()["comment"]
|
|
kept = (await c.post(f"/v1/reaction/post/{post_id}/comment", headers=other, json={"body": "남아요"})).json()["comment"]
|
|
await c.post(f"/v1/reaction/comment/{kept['comment_id']}/like", headers=h)
|
|
await c.post(f"/v1/reaction/comment/{mine['comment_id']}/like", headers=other)
|
|
|
|
assert (await c.delete("/v1/auth/me")).status_code == 401
|
|
assert (await c.delete("/v1/auth/me", headers=h)).json()["result"]["success"] is True
|
|
|
|
assert (await c.get("/v1/auth/me", headers=h)).status_code == 401
|
|
assert (await c.post("/v1/auth/refresh", json={"refresh_token": body["refresh_token"]})).json()["result"]["desc"] == "AUTH_TOKEN_INVALID"
|
|
assert (await c.post("/v1/auth/login", json={"login_id": "mina_bye", "password": "password1"})).json()["result"]["desc"] == "AUTH_FAILED"
|
|
comments = (await c.get(f"/v1/reaction/post/{post_id}/comment/list")).json()["comments"]
|
|
assert [x["body"] for x in comments] == ["남아요"] and comments[0]["like_count"] == 0
|
|
p = next(x for x in (await c.get("/v1/feed", params={"region": REGION})).json()["posts"] if x["post_id"] == post_id)
|
|
assert p["like_count"] == 0 and p["comment_count"] == 1
|
|
# 같은 아이디로 다시 가입할 수 있다
|
|
assert (await _signup(c, login_id="mina_bye"))["result"]["success"] is True
|
|
|
|
|
|
def _h(body):
|
|
return {"Authorization": f"Bearer {body['access_token']}"}
|
|
|
|
|
|
async def _comment(c, post_id, h, text):
|
|
return (await c.post(f"/v1/reaction/post/{post_id}/comment", headers=h, json={"body": text})).json()
|
|
|
|
|
|
async def _bodies(c, post_id, h=None):
|
|
return [x["body"] for x in (await c.get(f"/v1/reaction/post/{post_id}/comment/list", headers=h or {})).json()["comments"]]
|
|
|
|
|
|
@pytest.mark.anyio
|
|
async def test_comment_needs_terms_agreement(offline):
|
|
async with client() as c:
|
|
h = _h(await _signup(c, agree=False))
|
|
post_id = (await c.get("/v1/feed", params={"region": REGION})).json()["posts"][0]["post_id"]
|
|
assert (await c.get("/v1/auth/me", headers=h)).json()["me"]["terms_agreed"] is False
|
|
assert (await _comment(c, post_id, h, "안녕"))["result"]["desc"] == "TERMS_REQUIRED"
|
|
assert (await c.post("/v1/auth/terms", headers=h)).json()["me"]["terms_agreed"] is True
|
|
assert (await _comment(c, post_id, h, "안녕"))["comment"]["body"] == "안녕"
|
|
|
|
|
|
@pytest.mark.anyio
|
|
async def test_report_hides_for_reporter_then_for_all_at_three(offline):
|
|
async with client() as c:
|
|
writer = _h(await _signup(c, "writer_1", "쓴사람"))
|
|
r = [_h(await _signup(c, f"reporter_{i}", f"신고자{i}")) for i in range(3)]
|
|
post_id = (await c.get("/v1/feed", params={"region": REGION})).json()["posts"][0]["post_id"]
|
|
bad = (await _comment(c, post_id, writer, "광고입니다"))["comment"]
|
|
url = f"/v1/reaction/comment/{bad['comment_id']}/report"
|
|
|
|
assert (await c.post(url, headers=writer, json={"reason": 2})).json()["result"]["desc"] == "INVALID_INPUT"
|
|
assert (await c.post(url, headers=r[0], json={"reason": 9})).status_code == 422
|
|
assert (await c.post(url, headers=r[0], json={"reason": 2})).json()["result"]["success"] is True
|
|
assert (await c.post(url, headers=r[0], json={"reason": 2})).json()["result"]["success"] is True # 두 번 신고해도 한 번
|
|
assert await _bodies(c, post_id, r[0]) == [] and await _bodies(c, post_id, r[1]) == ["광고입니다"]
|
|
|
|
await c.post(url, headers=r[1], json={"reason": 1})
|
|
assert await _bodies(c, post_id) == ["광고입니다"]
|
|
await c.post(url, headers=r[2], json={"reason": 1})
|
|
assert await _bodies(c, post_id) == [] and await _bodies(c, post_id, writer) == []
|
|
p = next(x for x in (await c.get("/v1/feed", params={"region": REGION})).json()["posts"] if x["post_id"] == post_id)
|
|
assert p["comment_count"] == 0
|
|
# 숨긴 댓글을 지워도 댓글 수가 한 번 더 줄지 않는다
|
|
assert (await c.delete(f"/v1/reaction/comment/{bad['comment_id']}", headers=writer)).json()["result"]["success"] is True
|
|
p = next(x for x in (await c.get("/v1/feed", params={"region": REGION})).json()["posts"] if x["post_id"] == post_id)
|
|
assert p["comment_count"] == 0
|
|
|
|
|
|
@pytest.mark.anyio
|
|
async def test_block_hides_author_comments_and_can_be_undone(offline):
|
|
async with client() as c:
|
|
me = _h(await _signup(c))
|
|
other = _h(await _signup(c, "rude_1", "무례한"))
|
|
post_id = (await c.get("/v1/feed", params={"region": REGION})).json()["posts"][0]["post_id"]
|
|
rude = (await _comment(c, post_id, other, "흥"))["comment"]
|
|
mine = (await _comment(c, post_id, me, "좋아요"))["comment"]
|
|
|
|
assert (await c.post(f"/v1/reaction/comment/{mine['comment_id']}/block", headers=me)).json()["result"]["desc"] == "INVALID_INPUT"
|
|
assert (await c.post(f"/v1/reaction/comment/{rude['comment_id']}/block")).status_code == 401
|
|
assert (await c.post(f"/v1/reaction/comment/{rude['comment_id']}/block", headers=me)).json()["result"]["success"] is True
|
|
assert await _bodies(c, post_id, me) == ["좋아요"] and await _bodies(c, post_id) == ["흥", "좋아요"]
|
|
|
|
blocked = (await c.get("/v1/reaction/block/list", headers=me)).json()["users"]
|
|
assert [u["nickname"] for u in blocked] == ["무례한"]
|
|
assert (await c.delete(f"/v1/reaction/block/{blocked[0]['user_id']}", headers=me)).json()["result"]["success"] is True
|
|
assert (await c.delete(f"/v1/reaction/block/{blocked[0]['user_id']}", headers=me)).json()["result"]["desc"] == "NOT_FOUND"
|
|
assert await _bodies(c, post_id, me) == ["흥", "좋아요"]
|
|
|
|
|
|
@pytest.mark.anyio
|
|
async def test_withdraw_removes_reports_and_blocks(offline):
|
|
async with client() as c:
|
|
leaver = _h(await _signup(c, "leaver_1", "떠날사람"))
|
|
stay = _h(await _signup(c, "stay_1", "남는사람"))
|
|
post_id = (await c.get("/v1/feed", params={"region": REGION})).json()["posts"][0]["post_id"]
|
|
kept = (await _comment(c, post_id, stay, "남아요"))["comment"]
|
|
gone = (await _comment(c, post_id, leaver, "떠나요"))["comment"]
|
|
await c.post(f"/v1/reaction/comment/{kept['comment_id']}/report", headers=leaver, json={"reason": 4})
|
|
await c.post(f"/v1/reaction/comment/{kept['comment_id']}/block", headers=leaver)
|
|
await c.post(f"/v1/reaction/comment/{gone['comment_id']}/block", headers=stay)
|
|
assert (await c.delete("/v1/auth/me", headers=leaver)).json()["result"]["success"] is True
|
|
assert (await c.get("/v1/reaction/block/list", headers=stay)).json()["users"] == []
|
|
assert await _bodies(c, post_id, stay) == ["남아요"]
|