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="망원동주민"): return (await c.post("/v1/auth/signup", json={"login_id": login_id, "password": "password1", "nickname": nickname})).json() @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_reactions_require_login_and_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"] assert (await c.post(f"/v1/reaction/post/{post_id}/like")).status_code == 401 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