Files
habit-tracker/tests/test_pages_today.py
T
shinalok d0dd530e26 add retroactive check for yesterday's missed habits on /today
Users often only realize a habit was missed the next day; a same-day-only
check made it impossible to record it after the fact.
2026-07-17 20:15:38 +09:00

72 lines
2.8 KiB
Python

from datetime import date, timedelta
from app.models.habit import ALL_WEEKDAYS_MASK, HabitType
from app.models.habit_log import HabitLog
from app.schemas.habit import HabitCreate
from app.services import habit_service
def _make_habit(db_session, user_id, name="아침 운동"):
data = HabitCreate(name=name, habit_type=HabitType.BUILD, weekdays_mask=ALL_WEEKDAYS_MASK)
return habit_service.create_habit(db_session, user_id, data)
def test_today_page_redirects_to_login_when_not_authenticated(client):
response = client.get("/today", follow_redirects=False)
assert response.status_code == 303
assert response.headers["location"] == "/login"
def test_today_page_shows_created_habit(auth_client, db_session, test_user):
_make_habit(db_session, test_user.id, name="아침 운동")
response = auth_client.get("/today")
assert response.status_code == 200
assert "아침 운동" in response.text
def test_toggle_today_page_updates_checked_state(auth_client, db_session, test_user):
habit = _make_habit(db_session, test_user.id, name="아침 운동")
response = auth_client.post(f"/today/{habit.id}/toggle")
assert response.status_code == 200
# 서버가 실제로 로그를 남겼는지 today API로 재확인
today_items = auth_client.get("/api/today").json()
assert today_items[0]["checked"] is True
def test_toggle_today_page_for_other_users_habit_returns_404(auth_client, db_session, other_user):
others_habit = _make_habit(db_session, other_user.id, name="남의 습관")
response = auth_client.post(f"/today/{others_habit.id}/toggle")
assert response.status_code == 404
def test_today_page_shows_yesterday_missed_habit(auth_client, db_session, test_user):
_make_habit(db_session, test_user.id, name="어제 놓친 습관")
response = auth_client.get("/today")
assert response.status_code == 200
assert "어제 놓친 습관" in response.text
def test_toggle_yesterday_page_creates_log_for_yesterday(auth_client, db_session, test_user):
habit = _make_habit(db_session, test_user.id, name="어제 습관")
response = auth_client.post(f"/today/{habit.id}/toggle-yesterday")
assert response.status_code == 200
yesterday = date.today() - timedelta(days=1)
log = db_session.query(HabitLog).filter_by(habit_id=habit.id, log_date=yesterday).one()
assert log is not None
# 체크되었으니 더 이상 "어제 놓친 습관" 목록에 나오지 않아야 한다.
assert "어제 놓친 습관" not in response.text
def test_toggle_yesterday_page_for_other_users_habit_returns_404(auth_client, db_session, other_user):
others_habit = _make_habit(db_session, other_user.id, name="남의 습관")
response = auth_client.post(f"/today/{others_habit.id}/toggle-yesterday")
assert response.status_code == 404