""" Tests pour UserRepository. """ import pytest from sqlalchemy.ext.asyncio import AsyncSession pytestmark = pytest.mark.unit class TestUserRepository: """Tests pour UserRepository.""" @pytest.mark.asyncio async def test_create_user(self, db_session: AsyncSession): """Création d'un utilisateur.""" from app.crud.user import UserRepository repo = UserRepository(db_session) user = await repo.create( username="testuser", hashed_password="hashed_password_here", email="test@example.com", role="admin" ) await db_session.commit() assert user.id is not None assert user.username == "testuser" assert user.email == "test@example.com" assert user.role == "admin" @pytest.mark.asyncio async def test_get_by_username(self, db_session: AsyncSession): """Récupération par nom d'utilisateur.""" from app.crud.user import UserRepository repo = UserRepository(db_session) await repo.create(username="findme", hashed_password="hash", role="admin") await db_session.commit() found = await repo.get_by_username("findme") assert found is not None assert found.username == "findme" @pytest.mark.asyncio async def test_get_by_username_not_found(self, db_session: AsyncSession): """Utilisateur non trouvé.""" from app.crud.user import UserRepository repo = UserRepository(db_session) found = await repo.get_by_username("nonexistent") assert found is None @pytest.mark.asyncio async def test_get_by_email(self, db_session: AsyncSession): """Récupération par email.""" from app.crud.user import UserRepository repo = UserRepository(db_session) await repo.create( username="emailuser", hashed_password="hash", email="unique@example.com", role="admin" ) await db_session.commit() found = await repo.get_by_email("unique@example.com") assert found is not None assert found.username == "emailuser" @pytest.mark.asyncio async def test_get_by_id(self, db_session: AsyncSession): """Récupération par ID.""" from app.crud.user import UserRepository repo = UserRepository(db_session) user = await repo.create(username="iduser", hashed_password="hash", role="admin") await db_session.commit() found = await repo.get(user.id) assert found is not None assert found.username == "iduser" @pytest.mark.asyncio async def test_list_users(self, db_session: AsyncSession): """Liste des utilisateurs.""" from app.crud.user import UserRepository repo = UserRepository(db_session) await repo.create(username="user1", hashed_password="hash", role="admin") await repo.create(username="user2", hashed_password="hash", role="operator") await db_session.commit() users = await repo.list(limit=10, offset=0) assert len(users) >= 2 @pytest.mark.asyncio async def test_count_users(self, db_session: AsyncSession): """Comptage des utilisateurs.""" from app.crud.user import UserRepository repo = UserRepository(db_session) initial_count = await repo.count() await repo.create(username="countuser", hashed_password="hash", role="admin") await db_session.commit() new_count = await repo.count() assert new_count == initial_count + 1 @pytest.mark.asyncio async def test_update_user(self, db_session: AsyncSession): """Mise à jour d'un utilisateur.""" from app.crud.user import UserRepository repo = UserRepository(db_session) user = await repo.create(username="updateme", hashed_password="hash", role="admin") await db_session.commit() updated = await repo.update(user, display_name="Updated Name") await db_session.commit() assert updated.display_name == "Updated Name" @pytest.mark.asyncio async def test_update_password(self, db_session: AsyncSession): """Mise à jour du mot de passe.""" from app.crud.user import UserRepository repo = UserRepository(db_session) user = await repo.create(username="pwduser", hashed_password="old_hash", role="admin") await db_session.commit() old_changed_at = user.password_changed_at updated = await repo.update_password(user, "new_hash") await db_session.commit() assert updated.hashed_password == "new_hash" assert updated.password_changed_at >= old_changed_at @pytest.mark.asyncio async def test_update_last_login(self, db_session: AsyncSession): """Mise à jour de la dernière connexion.""" from app.crud.user import UserRepository repo = UserRepository(db_session) user = await repo.create(username="loginuser", hashed_password="hash", role="admin") await db_session.commit() assert user.last_login is None updated = await repo.update_last_login(user) await db_session.commit() assert updated.last_login is not None @pytest.mark.asyncio async def test_soft_delete(self, db_session: AsyncSession): """Suppression douce.""" from app.crud.user import UserRepository repo = UserRepository(db_session) user = await repo.create(username="deleteme", hashed_password="hash", role="admin") await db_session.commit() deleted = await repo.soft_delete(user.id) await db_session.commit() assert deleted is True # Should not find with default (exclude deleted) found = await repo.get(user.id) assert found is None # Should find with include_deleted found_deleted = await repo.get(user.id, include_deleted=True) assert found_deleted is not None assert found_deleted.is_active is False @pytest.mark.asyncio async def test_exists_any_empty(self, db_session: AsyncSession): """Vérification qu'aucun utilisateur n'existe.""" from app.crud.user import UserRepository repo = UserRepository(db_session) # Note: other tests may have created users, so we just check the method works result = await repo.exists_any() assert isinstance(result, bool) @pytest.mark.asyncio async def test_exists_any_with_user(self, db_session: AsyncSession): """Vérification qu'un utilisateur existe.""" from app.crud.user import UserRepository repo = UserRepository(db_session) await repo.create(username="existsuser", hashed_password="hash", role="admin") await db_session.commit() result = await repo.exists_any() assert result is True @pytest.mark.asyncio async def test_hard_delete(self, db_session: AsyncSession): """Suppression définitive.""" from app.crud.user import UserRepository repo = UserRepository(db_session) user = await repo.create(username="harddelete", hashed_password="hash", role="admin") await db_session.commit() user_id = user.id deleted = await repo.hard_delete(user_id) await db_session.commit() assert deleted is True # Should not find even with include_deleted found = await repo.get(user_id, include_deleted=True) assert found is None @pytest.mark.asyncio async def test_hard_delete_nonexistent(self, db_session: AsyncSession): """Suppression définitive d'un utilisateur inexistant.""" from app.crud.user import UserRepository repo = UserRepository(db_session) deleted = await repo.hard_delete(99999) assert deleted is False