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

90 lines
4.1 KiB
Python

import pytest
from httpx import ASGITransport, AsyncClient
from router.router import app
from services.talk_service import DAILY_MAX, HIDE_AT, weather_line
REGION = "1144000000"
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="망원동 하늘 진짜 맑아요"):
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