from types import SimpleNamespace import pytest from httpx import ASGITransport, AsyncClient from router.router import app from services.talk_service import DAILY_MAX, HIDE_AT, parse_cursor, spread_authors, weather_line REGION = "1144000000" BUSAN = "2611000000" 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() return {"Authorization": f"Bearer {body['access_token']}"} async def _write(c, h, body="망원동 하늘 진짜 맑아요", region=REGION): return (await c.post("/v1/talk", json={"region_code": region, "body": body}, headers=h)).json() @pytest.mark.anyio async def test_write_list_like_delete(): async with client() as c: assert (await c.post("/v1/talk", json={"region_code": REGION, "body": "안녕"})).status_code == 401 h = await _login(c, "talk_01") t = (await _write(c, h, " 바람 많이 불어요 "))["talk"] assert t["body"] == "바람 많이 불어요" and t["mine"] is True and t["region_name"] anon = (await c.get("/v1/talk/list")).json()["talks"] mine = next(x for x in anon if x["talk_id"] == t["talk_id"]) assert mine["mine"] is False and mine["nickname"] == "talk_01" liked = (await c.post(f"/v1/talk/{t['talk_id']}/like", headers=h)).json() assert liked["liked"] is True and liked["like_count"] == 1 assert (await c.post(f"/v1/talk/{t['talk_id']}/like", headers=h)).json()["like_count"] == 1 assert (await c.delete(f"/v1/talk/{t['talk_id']}/like", headers=h)).json()["like_count"] == 0 other = await _login(c, "talk_02") assert (await c.delete(f"/v1/talk/{t['talk_id']}", headers=other)).json()["result"]["desc"] == "NOT_FOUND" assert (await c.delete(f"/v1/talk/{t['talk_id']}", headers=h)).json()["result"]["success"] is True assert t["talk_id"] not in [x["talk_id"] for x in (await c.get("/v1/talk/list")).json()["talks"]] @pytest.mark.anyio async def test_rejects_links_phone_long_and_daily_limit(): async with client() as c: h = await _login(c, "talk_03") assert (await _write(c, h, "여기 보세요 https://spam.example"))["result"]["desc"] == "LINK_NOT_ALLOWED" assert (await _write(c, h, "연락 주세요 010-1234-5678"))["result"]["desc"] == "LINK_NOT_ALLOWED" assert (await _write(c, h, "가" * 101))["result"]["desc"] == "INVALID_INPUT" bad_region = (await c.post("/v1/talk", json={"region_code": "0000000000", "body": "안녕"}, headers=h)).json() assert bad_region["result"]["desc"] == "INVALID_INPUT" for i in range(DAILY_MAX): assert (await _write(c, h, f"오늘 {i}번째"))["talk"] assert (await _write(c, h))["result"]["desc"] == "DAILY_LIMIT" @pytest.mark.anyio async def test_reports_hide_and_block_hides_author(): async with client() as c: author = await _login(c, "talk_04") t = (await _write(c, author))["talk"] assert (await c.post(f"/v1/talk/{t['talk_id']}/report", json={"reason": 1}, headers=author)).json()["result"]["desc"] == "INVALID_INPUT" viewer = await _login(c, "talk_05") assert (await c.post(f"/v1/talk/{t['talk_id']}/block", headers=viewer)).json()["result"]["success"] is True seen = [x["talk_id"] for x in (await c.get("/v1/talk/list", headers=viewer)).json()["talks"]] assert t["talk_id"] not in seen assert t["talk_id"] in [x["talk_id"] for x in (await c.get("/v1/talk/list")).json()["talks"]] for i in range(HIDE_AT): h = await _login(c, f"talk_r{i}") await c.post(f"/v1/talk/{t['talk_id']}/report", json={"reason": 2}, headers=h) await c.post(f"/v1/talk/{t['talk_id']}/report", json={"reason": 2}, headers=h) assert t["talk_id"] not in [x["talk_id"] for x in (await c.get("/v1/talk/list")).json()["talks"]] def test_weather_line(): class Row: temp_c, sky, rain_type = 18.4, 1, 1 class Wet: temp_c, sky, rain_type = 11.6, 3, 2 assert weather_line(Row()) == "18° 맑음" assert weather_line(Wet()) == "12° 비" assert weather_line(None) is None def _ids(body: dict) -> list[int]: return [x["talk_id"] for x in body["talks"]] @pytest.mark.anyio async def test_same_sido_talks_come_first_for_that_viewer(): async with client() as c: busan = (await _write(c, await _login(c, "talk_bs1"), "광안리 바람 시원해요", BUSAN))["talk"]["talk_id"] seoul = (await _write(c, await _login(c, "talk_se1"), "망원동 비 그쳤어요"))["talk"]["talk_id"] nation = _ids((await c.get("/v1/talk/list")).json()) local = _ids((await c.get("/v1/talk/list", params={"region": BUSAN})).json()) assert nation.index(seoul) < nation.index(busan) assert local.index(busan) < local.index(seoul) @pytest.mark.anyio async def test_pages_follow_cursor_without_repeats(): async with client() as c: mine = [] for i in range(5): h = await _login(c, f"talk_pg{i}") for j in range(DAILY_MAX): mine.append((await _write(c, h, f"페이지 글 {i}-{j}", BUSAN if j % 2 else REGION))["talk"]["talk_id"]) seen, cursor = [], None while True: body = (await c.get("/v1/talk/list", params={"region": BUSAN} | ({"cursor": cursor} if cursor else {}))).json() seen += _ids(body) cursor = body.get("next_cursor") if not cursor: break assert (await c.get("/v1/talk/list", params={"cursor": "abc"})).json()["result"]["desc"] == "INVALID_INPUT" assert len(seen) == len(set(seen)) and set(mine) <= set(seen) @pytest.mark.anyio async def test_mine_lists_only_my_talks_newest_first(): async with client() as c: assert (await c.get("/v1/talk/mine")).status_code == 401 h = await _login(c, "talk_mine") ids = [(await _write(c, h, f"내 글 {i}"))["talk"]["talk_id"] for i in range(3)] await _write(c, await _login(c, "talk_notmine"), "남의 글") await c.delete(f"/v1/talk/{ids[0]}", headers=h) body = (await c.get("/v1/talk/mine", headers=h)).json() assert _ids(body) == [ids[2], ids[1]] and all(x["mine"] for x in body["talks"]) def test_spread_authors_and_cursor(): v = lambda who: SimpleNamespace(author=SimpleNamespace(user_id=who)) # noqa: E731 assert [x.author.user_id for x in spread_authors([v("a"), v("a"), v("a"), v("b"), v("c")])] == ["a", "b", "a", "c", "a"] assert parse_cursor("1760000000_42") == (1760000000, 42) assert parse_cursor("12") is None and parse_cursor("x_1") is None