Enforce 18+ age gate at account registration #23

Merged
reviewer-bot merged 6 commits from agent/7fedeb122f7e into main 2026-08-18 16:36:43 +00:00
3 changed files with 67 additions and 0 deletions
Showing only changes of commit ef646a8e54 - Show all commits
+3
View File
@@ -0,0 +1,3 @@
[pytest]
asyncio_mode = auto
testpaths = tests
View File
+64
View File
@@ -0,0 +1,64 @@
"""Tests for the 18+ age gate on user registration."""
from datetime import date, timedelta, datetime
from unittest.mock import patch
import pytest
from app.routers.auth import calculate_age
class TestCalculateAge:
"""Unit tests for the calculate_age helper."""
def test_exactly_18(self):
"""A person born exactly 18 years ago today is 18."""
today = datetime.now().date()
dob = today.replace(year=today.year - 18)
assert calculate_age(dob) == 18
def test_just_turned_18(self):
"""A person who turned 18 yesterday is 18."""
today = datetime.now().date()
dob = today.replace(year=today.year - 18) - timedelta(days=1)
assert calculate_age(dob) == 18
def test_one_day_short_of_18(self):
"""A person born one day after their 18th birthday is 17."""
today = datetime.now().date()
dob = today.replace(year=today.year - 18) + timedelta(days=1)
assert calculate_age(dob) == 17
def test_17_years_old(self):
"""A person who is 17 years and 364 days old is 17."""
today = datetime.now().date()
dob = today.replace(year=today.year - 17) + timedelta(days=1)
assert calculate_age(dob) == 17
def test_65_years_old(self):
"""A person who is 65 is correctly calculated."""
today = datetime.now().date()
dob = today.replace(year=today.year - 65)
assert calculate_age(dob) == 65
def test_newborn(self):
"""A newborn is 0."""
today = datetime.now().date()
dob = today
assert calculate_age(dob) == 0
def test_birthday_not_yet_this_year(self):
"""Person born Dec 31, 2000 — on Jan 1, 2025 they are still 24."""
dob = date(2000, 12, 31)
with patch("app.routers.auth.datetime") as mock_dt:
mock_dt.now.return_value = datetime(2025, 1, 1)
mock_dt.now.return_value.date.return_value = date(2025, 1, 1)
assert calculate_age(dob) == 24
def test_birthday_today(self):
"""Person born Dec 31, 2000 — on Dec 31, 2024 they turn 24."""
dob = date(2000, 12, 31)
with patch("app.routers.auth.datetime") as mock_dt:
mock_dt.now.return_value = datetime(2024, 12, 31)
mock_dt.now.return_value.date.return_value = date(2024, 12, 31)
assert calculate_age(dob) == 24