144 lines
7.2 KiB
Python
144 lines
7.2 KiB
Python
from datetime import timedelta
|
|
|
|
import pytest
|
|
from httpx import ASGITransport, AsyncClient
|
|
from sqlalchemy import select
|
|
|
|
from common.database.db_session_manager import DB_SESSION_MNG
|
|
from common.database.model.models import PointLog, User
|
|
from common.utils.kma_time import now_kst
|
|
from router.router import app
|
|
from services.level_service import LEVELS, is_unlocked, level_of
|
|
|
|
REGION = "1144000000"
|
|
pytestmark = pytest.mark.usefixtures("no_bot_comments")
|
|
|
|
|
|
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
|
|
|
|
|
|
def _earned(level: dict, action: str) -> int:
|
|
return next(a["earned"] for a in level["today"] if a["action"] == action)
|
|
|
|
|
|
def test_level_thresholds_and_unlocks():
|
|
assert [need for need, _ in LEVELS] == [0, 30, 100, 200, 400, 700, 1000]
|
|
assert level_of(0) == 1 and level_of(29) == 1 and level_of(30) == 2 and level_of(199) == 3 and level_of(200) == 4 and level_of(5000) == 7
|
|
assert not is_unlocked(199, "saju_love") and is_unlocked(200, "saju_love")
|
|
assert not is_unlocked(399, "saju_match") and is_unlocked(400, "saju_match")
|
|
|
|
|
|
@pytest.mark.anyio
|
|
async def test_actions_award_once_and_respect_daily_caps(offline):
|
|
async with client() as c:
|
|
assert (await c.get("/v1/level/me")).status_code == 401
|
|
h = await _login(c, "level_01")
|
|
me = (await c.get("/v1/level/me", headers=h)).json()
|
|
assert me["points"] == 0 and me["level"] == 1 and me["name"] == "이슬" and me["next_points"] == 30
|
|
|
|
# 출석은 하루 한 번
|
|
assert (await c.post("/v1/level/attend", headers=h)).json()["points"] == 10
|
|
assert (await c.post("/v1/level/attend", headers=h)).json()["points"] == 10
|
|
|
|
# 좋아요·댓글은 5각씩 하루 10각, 같은 글을 다시 눌러도 더 안 줌
|
|
posts = [p["post_id"] for p in (await c.get("/v1/feed", params={"region": REGION})).json()["posts"][:3]]
|
|
await c.post(f"/v1/reaction/post/{posts[0]}/like", headers=h)
|
|
await c.delete(f"/v1/reaction/post/{posts[0]}/like", headers=h)
|
|
await c.post(f"/v1/reaction/post/{posts[0]}/like", headers=h)
|
|
assert _earned((await c.get("/v1/level/me", headers=h)).json(), "react") == 5
|
|
await c.post(f"/v1/reaction/post/{posts[1]}/comment", json={"body": "오늘 노을 기대돼요"}, headers=h)
|
|
await c.post(f"/v1/reaction/post/{posts[2]}/like", headers=h)
|
|
me = (await c.get("/v1/level/me", headers=h)).json()
|
|
assert _earned(me, "react") == 10 and me["points"] == 20
|
|
|
|
# 오늘 타로는 하루 한 번, 동네 이야기 글 20각 → 50각이면 Lv.2
|
|
await c.post("/v1/fortune/draw", json={"region_code": REGION}, headers=h)
|
|
await c.post("/v1/fortune/draw", json={"region_code": REGION}, headers=h)
|
|
await c.post("/v1/talk", json={"region_code": REGION, "body": "오늘 하늘 맑아요"}, headers=h)
|
|
me = (await c.get("/v1/level/me", headers=h)).json()
|
|
assert _earned(me, "tarot") == 10 and _earned(me, "talk") == 20
|
|
assert me["points"] == 50 and me["level"] == 2 and me["name"] == "햇살" and me["floor"] == 30 and me["next_points"] == 100
|
|
|
|
# 저녁 체크인·공유도 하루 한 번
|
|
await c.put("/v1/fortune/log", json={"region_code": REGION, "mood": 2, "went_out": False}, headers=h)
|
|
await c.put("/v1/fortune/log", json={"region_code": REGION, "mood": 3, "went_out": False}, headers=h)
|
|
await c.post("/v1/level/share", headers=h)
|
|
assert (await c.post("/v1/level/share", headers=h)).json()["points"] == 70
|
|
|
|
# 로그인 안 한 좋아요는 각이 없다 (다른 사람 계정에도 안 쌓임)
|
|
await c.post(f"/v1/reaction/post/{posts[0]}/like", headers={"X-Forwarded-For": "203.0.113.50"})
|
|
assert (await c.get("/v1/level/me", headers=h)).json()["points"] == 70
|
|
|
|
|
|
@pytest.mark.anyio
|
|
async def test_seven_day_attendance_bonus(offline):
|
|
async with client() as c:
|
|
h = await _login(c, "level_02")
|
|
await c.post("/v1/level/attend", headers=h)
|
|
today = now_kst().date()
|
|
|
|
async def backfill(s):
|
|
uid = await s.scalar(select(User.user_id).where(User.login_id == "level_02"))
|
|
s.add_all([PointLog(user_id=uid, day=today - timedelta(days=d), action="attend", ref="", points=10) for d in range(1, 7)])
|
|
|
|
await DB_SESSION_MNG.execute_lambda_write(backfill)
|
|
# 오늘 출석이 이미 있으니 다시 불러도 보너스가 생기지 않는다 → 다음 날처럼 오늘 기록을 지우고 다시 출석
|
|
async def drop_today(s):
|
|
uid = await s.scalar(select(User.user_id).where(User.login_id == "level_02"))
|
|
row = await s.scalar(select(PointLog).where(PointLog.user_id == uid, PointLog.day == today))
|
|
await s.delete(row)
|
|
user = await s.get(User, uid)
|
|
user.points -= 10
|
|
|
|
await DB_SESSION_MNG.execute_lambda_write(drop_today)
|
|
me = (await c.post("/v1/level/attend", headers=h)).json()
|
|
# 지난 6일은 장부만 넣어 총량엔 없다: 오늘 출석 10 + 7일 연속 50
|
|
assert me["streak"] == 7 and me["points"] == 60
|
|
|
|
|
|
@pytest.mark.anyio
|
|
async def test_game_rounds_award_two_each_up_to_twenty(offline):
|
|
import time
|
|
|
|
from services.level_service import GAME_ROUND_MIN_SEC, game_token
|
|
|
|
async with client() as c:
|
|
h = await _login(c, "level_03")
|
|
other = await _login(c, "level_04")
|
|
uid = await DB_SESSION_MNG.execute_lambda(lambda s: s.scalar(select(User.user_id).where(User.login_id == "level_03")))
|
|
uid = uid[1]
|
|
|
|
play = (await c.post("/v1/level/game/start", headers=h)).json()["play"]
|
|
# 시작하자마자 깬 라운드는 받지 않는다
|
|
me = (await c.post("/v1/level/game/round", json={"play": play, "round": 1}, headers=h)).json()
|
|
assert _earned(me, "game") == 0
|
|
|
|
# 충분히 지난 판: 라운드마다 2각, 같은 라운드는 한 번
|
|
old = game_token(uid, int(time.time()) - GAME_ROUND_MIN_SEC * 30)
|
|
await c.post("/v1/level/game/round", json={"play": old, "round": 1}, headers=h)
|
|
me = (await c.post("/v1/level/game/round", json={"play": old, "round": 1}, headers=h)).json()
|
|
assert _earned(me, "game") == 2 and me["points"] == 2
|
|
|
|
# 남의 판·위조한 판은 받지 않는다
|
|
me = (await c.post("/v1/level/game/round", json={"play": old, "round": 2}, headers=other)).json()
|
|
assert _earned(me, "game") == 0
|
|
forged = old.split(".")[0] + ".0000000000000000"
|
|
me = (await c.post("/v1/level/game/round", json={"play": forged, "round": 2}, headers=h)).json()
|
|
assert _earned(me, "game") == 2
|
|
|
|
# 하루 20각까지
|
|
for r in range(2, 15):
|
|
me = (await c.post("/v1/level/game/round", json={"play": old, "round": r}, headers=h)).json()
|
|
assert _earned(me, "game") == 20 and me["points"] == 20
|
|
|
|
# 로그인 안 하면 판을 열 수 없다
|
|
assert (await c.post("/v1/level/game/start")).status_code == 401
|