Svarer på det største enkeltfunnet i investor-statusrapporten (nesten ingen automatisert testdekning utenfor HCP-motoren). 17 nye integrasjonstester mot en automatisk opprettet/migrert/nedrevet scratch-database (scripts/run_backend_tests.sh), som kaller de faktiske router-/auth-funksjonene direkte -- ikke en SQL-gjenimplementering. Se ADR-058 og CHANGELOG punkt 67 for full detalj. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
82 lines
2.9 KiB
Python
82 lines
2.9 KiB
Python
"""
|
|
13-årsgrensen (2026-08-10, ADR) håndheves server-side i update_profile --
|
|
eneste vei inn i appen. Kaller den faktiske funksjonen direkte, ikke en
|
|
gjenimplementering av alderslogikken."""
|
|
|
|
from datetime import date, timedelta
|
|
|
|
import pytest
|
|
from fastapi import HTTPException
|
|
|
|
from app import db as app_db
|
|
from app.auth import CurrentUser
|
|
from app.routers.auth import ProfileUpdate, update_profile
|
|
|
|
from tests.conftest import create_user
|
|
|
|
|
|
def _birth_date_for_age(years: int, extra_days: int = 0) -> date:
|
|
"""extra_days=0 gir eksakt `years` år gammel i dag; extra_days=+1 gir
|
|
én dag YNGRE enn `years` (dvs. fyller `years` år i MORGEN, ikke i dag --
|
|
fødselsdatoen flyttes én dag SENERE enn "eksakt `years` i dag")."""
|
|
today = date.today()
|
|
try:
|
|
return today.replace(year=today.year - years) + timedelta(days=extra_days)
|
|
except ValueError:
|
|
# 29. februar-kant -- ikke relevant for testens formål, men trygt.
|
|
return today.replace(year=today.year - years, day=28) + timedelta(days=extra_days)
|
|
|
|
|
|
async def test_exactly_13_today_is_allowed(pool):
|
|
user_id = await create_user()
|
|
out = await update_profile(
|
|
ProfileUpdate(birth_date=_birth_date_for_age(13)),
|
|
user=CurrentUser(user_id=user_id),
|
|
)
|
|
assert out.birth_date == _birth_date_for_age(13)
|
|
|
|
|
|
async def test_one_day_short_of_13_is_rejected(pool):
|
|
user_id = await create_user()
|
|
with pytest.raises(HTTPException) as exc_info:
|
|
await update_profile(
|
|
ProfileUpdate(birth_date=_birth_date_for_age(13, extra_days=1)),
|
|
user=CurrentUser(user_id=user_id),
|
|
)
|
|
assert exc_info.value.status_code == 400
|
|
assert exc_info.value.detail["code"] == "UNDER_MINIMUM_AGE"
|
|
|
|
# Og bekreft at feilen faktisk stoppet skrivingen -- ingen delvis
|
|
# oppdatering av fødselsdato ble liggende igjen.
|
|
async with app_db.plain_connection() as conn:
|
|
row = await conn.fetchrow("SELECT birth_date FROM app_user WHERE id = $1", user_id)
|
|
assert row["birth_date"] is None
|
|
|
|
|
|
async def test_well_under_13_is_rejected(pool):
|
|
user_id = await create_user()
|
|
with pytest.raises(HTTPException) as exc_info:
|
|
await update_profile(
|
|
ProfileUpdate(birth_date=_birth_date_for_age(8)),
|
|
user=CurrentUser(user_id=user_id),
|
|
)
|
|
assert exc_info.value.status_code == 400
|
|
assert exc_info.value.detail["code"] == "UNDER_MINIMUM_AGE"
|
|
|
|
|
|
async def test_updating_other_fields_without_birth_date_skips_age_check(pool):
|
|
user_id = await create_user()
|
|
out = await update_profile(
|
|
ProfileUpdate(bio="Elsker links-golf"),
|
|
user=CurrentUser(user_id=user_id),
|
|
)
|
|
assert out.bio == "Elsker links-golf"
|
|
|
|
|
|
async def test_adult_birth_date_is_allowed(pool):
|
|
user_id = await create_user()
|
|
out = await update_profile(
|
|
ProfileUpdate(birth_date=_birth_date_for_age(35)),
|
|
user=CurrentUser(user_id=user_id),
|
|
)
|
|
assert out.birth_date == _birth_date_for_age(35)
|