From 42db3daa313d3e05fb07efb39a180fb40d49d741 Mon Sep 17 00:00:00 2001 From: Kodjo Sossouvi Date: Fri, 26 Sep 2025 21:04:47 +0200 Subject: [PATCH] Fixed unit tests --- Readme.md | 6 +++++ tests/api/test_auth_routes.py | 48 +++++++++++++++++++++-------------- 2 files changed, 35 insertions(+), 19 deletions(-) diff --git a/Readme.md b/Readme.md index dbd8095..2625f99 100644 --- a/Readme.md +++ b/Readme.md @@ -140,7 +140,13 @@ MyDocManager/ │ └── frontend/ │ ├── Dockerfile │ ├── package.json +│ ├── index.html │ └── src/ +│ ├── assets/ +│ ├── App.css +│ ├── App.jsx +│ ├── main.css +│ └── main.jsx ├── tests/ │ ├── file-processor/ │ │ ├── test_auth/ diff --git a/tests/api/test_auth_routes.py b/tests/api/test_auth_routes.py index dd63377..7345161 100644 --- a/tests/api/test_auth_routes.py +++ b/tests/api/test_auth_routes.py @@ -1,9 +1,12 @@ +from datetime import datetime from unittest.mock import MagicMock import pytest from fastapi import status, HTTPException from fastapi.testclient import TestClient +from mongomock.mongo_client import MongoClient +from app.api.dependencies import get_auth_service, get_user_service, get_current_user from app.main import app # Assuming you have FastAPI app defined in app/main.py from app.models.auth import UserRole from app.models.types import PyObjectId @@ -26,8 +29,8 @@ def fake_user(): role=UserRole.USER, is_active=True, hashed_password="hashed-secret", - created_at="2023-01-01T00:00:00", - updated_at="2023-01-01T00:00:00", + created_at=datetime(2025, 1, 1), + updated_at=datetime(2025, 1, 2), ) @@ -51,14 +54,23 @@ def override_get_current_user(fake_user): return _override +def override_get_database(): + def _override(): + client = MongoClient() + db = client.test_database + return db + + return _override + + # ---------------------- TESTS FOR /auth/login ---------------------- class TestLogin: - def test_i_can_login_with_valid_credentials(self, client, fake_user, monkeypatch): + def test_i_can_login_with_valid_credentials(self, client, fake_user): auth_service = override_auth_service() user_service = override_user_service(fake_user) - monkeypatch.setattr("app.api.routes.auth.get_auth_service", lambda: auth_service) - monkeypatch.setattr("app.api.routes.auth.get_user_service", lambda: user_service) + client.app.dependency_overrides[get_auth_service] = lambda: auth_service + client.app.dependency_overrides[get_user_service] = lambda: user_service response = client.post( "/auth/login", @@ -70,13 +82,13 @@ class TestLogin: assert "access_token" in data assert data["user"]["username"] == "testuser" - def test_i_cannot_login_with_invalid_username(self, client, monkeypatch): + def test_i_cannot_login_with_invalid_username(self, client): auth_service = override_auth_service() user_service = MagicMock(spec=UserService) user_service.get_user_by_username.return_value = None - monkeypatch.setattr("app.api.routes.auth.get_auth_service", lambda: auth_service) - monkeypatch.setattr("app.api.routes.auth.get_user_service", lambda: user_service) + client.app.dependency_overrides[get_auth_service] = lambda: auth_service + client.app.dependency_overrides[get_user_service] = lambda: user_service response = client.post( "/auth/login", @@ -85,13 +97,12 @@ class TestLogin: assert response.status_code == status.HTTP_401_UNAUTHORIZED - def test_i_cannot_login_with_inactive_user(self, client, fake_user, monkeypatch): + def test_i_cannot_login_with_inactive_user(self, client, fake_user): fake_user.is_active = False auth_service = override_auth_service() user_service = override_user_service(fake_user) - - monkeypatch.setattr("app.api.routes.auth.get_auth_service", lambda: auth_service) - monkeypatch.setattr("app.api.routes.auth.get_user_service", lambda: user_service) + client.app.dependency_overrides[get_auth_service] = lambda: auth_service + client.app.dependency_overrides[get_user_service] = lambda: user_service response = client.post( "/auth/login", @@ -100,13 +111,12 @@ class TestLogin: assert response.status_code == status.HTTP_401_UNAUTHORIZED - def test_i_cannot_login_with_wrong_password(self, client, fake_user, monkeypatch): + def test_i_cannot_login_with_wrong_password(self, client, fake_user): auth_service = override_auth_service() auth_service.verify_user_password.return_value = False user_service = override_user_service(fake_user) - - monkeypatch.setattr("app.api.routes.auth.get_auth_service", lambda: auth_service) - monkeypatch.setattr("app.api.routes.auth.get_user_service", lambda: user_service) + client.app.dependency_overrides[get_auth_service] = lambda: auth_service + client.app.dependency_overrides[get_user_service] = lambda: user_service response = client.post( "/auth/login", @@ -118,8 +128,8 @@ class TestLogin: # ---------------------- TESTS FOR /auth/me ---------------------- class TesteMe: - def test_i_can_get_current_user_profile(self, client, fake_user, monkeypatch): - monkeypatch.setattr("app.api.routes.auth.get_current_user", override_get_current_user(fake_user)) + def test_i_can_get_current_user_profile(self, client, fake_user): + client.app.dependency_overrides[get_current_user] = override_get_current_user(fake_user) response = client.get("/auth/me") @@ -132,7 +142,7 @@ class TesteMe: def raise_http_exception(): raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED) - monkeypatch.setattr("app.api.routes.auth.get_current_user", raise_http_exception) + client.app.dependency_overrides[get_current_user] = raise_http_exception response = client.get("/auth/me")