Files
MyAuth/tests/persistence/test_sqlite_user.py
Kodjo Sossouvi 0138ac247a Changed module name from my_auth to myauth
Changed encryption algorithm to argon2
Added unit tests
2025-10-19 23:17:38 +02:00

155 lines
6.4 KiB
Python

# tests/persistence/test_sqlite_user.py
import pytest
import json
from datetime import datetime
from myauth.persistence.sqlite import SQLiteUserRepository
from myauth.models.user import UserCreate, UserUpdate
from myauth.exceptions import UserAlreadyExistsError, UserNotFoundError
def test_i_can_create_and_retrieve_user_by_email(user_repository: SQLiteUserRepository,
test_user_data_create: UserCreate,
test_user_hashed_password: str):
"""Verifies user creation and successful retrieval by email."""
# 1. Create User
created_user = user_repository.create_user(
user_data=test_user_data_create,
hashed_password=test_user_hashed_password
)
# Assertions on creation
assert created_user is not None
assert created_user.email == test_user_data_create.email
assert created_user.hashed_password == test_user_hashed_password
assert created_user.is_active is True
assert created_user.is_verified is False
assert created_user.id is not None
assert isinstance(created_user.created_at, datetime)
# 2. Retrieve User
retrieved_user = user_repository.get_user_by_email(test_user_data_create.email)
# Assertions on retrieval
assert retrieved_user is not None
assert retrieved_user.id == created_user.id
assert retrieved_user.username == "TestUser"
def test_i_can_retrieve_user_by_id(user_repository: SQLiteUserRepository,
test_user_data_create: UserCreate,
test_user_hashed_password: str):
"""Verifies user retrieval by unique ID."""
created_user = user_repository.create_user(test_user_data_create, test_user_hashed_password)
retrieved_user = user_repository.get_user_by_id(created_user.id)
assert retrieved_user is not None
assert retrieved_user.email == created_user.email
def test_i_cannot_create_user_if_email_exists(user_repository: SQLiteUserRepository,
test_user_data_create: UserCreate,
test_user_hashed_password: str):
"""Ensures that creating a user with an existing email raises UserAlreadyExistsError."""
# First creation should succeed
user_repository.create_user(test_user_data_create, test_user_hashed_password)
# Second creation with same email should fail
with pytest.raises(UserAlreadyExistsError):
user_repository.create_user(test_user_data_create, test_user_hashed_password)
def test_i_can_check_if_email_exists(user_repository: SQLiteUserRepository,
test_user_data_create: UserCreate,
test_user_hashed_password: str):
"""Verifies the email_exists method returns correct boolean results."""
email = test_user_data_create.email
non_existent_email = "non.existent@example.com"
# 1. Check before creation (Should be False)
assert user_repository.email_exists(email) is False
user_repository.create_user(test_user_data_create, test_user_hashed_password)
# 2. Check after creation (Should be True)
assert user_repository.email_exists(email) is True
# 3. Check for another non-existent email
assert user_repository.email_exists(non_existent_email) is False
def test_i_can_update_username_and_roles(user_repository: SQLiteUserRepository,
test_user_data_create: UserCreate,
test_user_hashed_password: str):
"""Tests partial update of user fields (username and roles)."""
created_user = user_repository.create_user(test_user_data_create, test_user_hashed_password)
updates = UserUpdate(username="NewUsername", roles=["admin", "staff"])
updated_user = user_repository.update_user(created_user.id, updates)
assert updated_user.username == "NewUsername"
assert updated_user.roles == ["admin", "staff"]
# Check that unrelated fields remain the same
assert updated_user.email == created_user.email
# Check that update timestamp changed
assert updated_user.updated_at > created_user.updated_at
assert updated_user.is_verified == created_user.is_verified
def test_i_can_update_is_active_status(user_repository: SQLiteUserRepository,
test_user_data_create: UserCreate,
test_user_hashed_password: str):
"""Tests the specific update of the 'is_active' status."""
created_user = user_repository.create_user(test_user_data_create, test_user_hashed_password)
# Deactivate
updates_deactivate = UserUpdate(is_active=False)
deactivated_user = user_repository.update_user(created_user.id, updates_deactivate)
assert deactivated_user.is_active is False
# Reactivate
updates_activate = UserUpdate(is_active=True)
reactivated_user = user_repository.update_user(created_user.id, updates_activate)
assert reactivated_user.is_active is True
def test_i_cannot_update_non_existent_user(user_repository: SQLiteUserRepository):
"""Ensures that updating a user with an unknown ID raises UserNotFoundError."""
non_existent_id = "unknown_id_123"
updates = UserUpdate(username="Phantom")
with pytest.raises(UserNotFoundError):
user_repository.update_user(non_existent_id, updates)
def test_i_can_delete_user(user_repository: SQLiteUserRepository,
test_user_data_create: UserCreate,
test_user_hashed_password: str):
"""Verifies user deletion and subsequent failure to retrieve."""
created_user = user_repository.create_user(test_user_data_create, test_user_hashed_password)
# 1. Delete the user
was_deleted = user_repository.delete_user(created_user.id)
assert was_deleted is True
# 2. Verify deletion by attempting retrieval
retrieved_user = user_repository.get_user_by_id(created_user.id)
assert retrieved_user is None
# 3. Verify attempting to delete again returns False
was_deleted_again = user_repository.delete_user(created_user.id)
assert was_deleted_again is False
def test_i_cannot_retrieve_non_existent_user_by_email(user_repository: SQLiteUserRepository):
"""Ensures retrieval by email returns None for non-existent email."""
retrieved_user = user_repository.get_user_by_email("ghost@example.com")
assert retrieved_user is None