Files
SneakySwole/tests/test_user_service.py
Phillip Tarrant 576d3bbb68 feat: replace admin auth with cookie-based profile picker
Remove all authentication (login, sessions, bcrypt, itsdangerous) since
the app runs on a private homelab LAN. Replace with a profile picker
landing page and cookie-based profile selection (1-year expiry).

- Add Alembic migration to drop password_hash/is_admin columns
- Delete auth service, auth routes, login template, and auth tests
- Rewrite app/utils/auth.py with NoProfileSelectedError and
  require_active_profile dependency
- Add profile creation flow (GET/POST /profiles/create)
- Rewrite home page as profile picker with card layout
- Update all route files to use profile dependency instead of admin auth
- Remove bcrypt and itsdangerous from requirements
- Remove admin_username/admin_password from config
- Update all tests for new profile-based access model

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-13 12:40:54 -05:00

78 lines
2.7 KiB
Python

"""Tests for the UserService class."""
from sqlmodel import SQLModel, Session, create_engine
from app.models.user import User
from app.services.user_service import UserService
class TestUserService:
"""Tests for user CRUD operations."""
def _setup(self):
"""Create an in-memory DB and service instance."""
engine = create_engine("sqlite:///:memory:")
SQLModel.metadata.create_all(engine)
session = Session(engine)
service = UserService(session)
return session, service
def test_create_user(self) -> None:
"""create_user should insert a new user and return it."""
session, service = self._setup()
user = service.create_user(
username="phil",
display_name="Phillip",
height="6'0\"",
weight="260 lbs",
goals="Muscle build",
)
assert user.id is not None
assert user.username == "phil"
session.close()
def test_get_user_by_id(self) -> None:
"""get_user_by_id should return the correct user."""
session, service = self._setup()
created = service.create_user(
username="test", display_name="Test"
)
found = service.get_user_by_id(created.id)
assert found is not None
assert found.username == "test"
session.close()
def test_get_user_by_username(self) -> None:
"""get_user_by_username should return the correct user."""
session, service = self._setup()
service.create_user(username="phil", display_name="Phillip")
found = service.get_user_by_username("phil")
assert found is not None
assert found.display_name == "Phillip"
session.close()
def test_get_user_by_username_not_found(self) -> None:
"""get_user_by_username should return None for missing users."""
session, service = self._setup()
found = service.get_user_by_username("nonexistent")
assert found is None
session.close()
def test_list_users(self) -> None:
"""list_users should return all users."""
session, service = self._setup()
service.create_user(username="a", display_name="A")
service.create_user(username="b", display_name="B")
users = service.list_users()
assert len(users) == 2
session.close()
def test_update_user(self) -> None:
"""update_user should modify the specified fields."""
session, service = self._setup()
user = service.create_user(username="u", display_name="Old")
updated = service.update_user(user.id, display_name="New", weight="250 lbs")
assert updated.display_name == "New"
assert updated.weight == "250 lbs"
session.close()