Fixed unit tests

This commit is contained in:
2025-09-26 21:04:47 +02:00
parent 1f7ef200e7
commit 42db3daa31
2 changed files with 35 additions and 19 deletions

View File

@@ -140,7 +140,13 @@ MyDocManager/
│ └── frontend/ │ └── frontend/
│ ├── Dockerfile │ ├── Dockerfile
│ ├── package.json │ ├── package.json
│ ├── index.html
│ └── src/ │ └── src/
│ ├── assets/
│ ├── App.css
│ ├── App.jsx
│ ├── main.css
│ └── main.jsx
├── tests/ ├── tests/
│ ├── file-processor/ │ ├── file-processor/
│ │ ├── test_auth/ │ │ ├── test_auth/

View File

@@ -1,9 +1,12 @@
from datetime import datetime
from unittest.mock import MagicMock from unittest.mock import MagicMock
import pytest import pytest
from fastapi import status, HTTPException from fastapi import status, HTTPException
from fastapi.testclient import TestClient 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.main import app # Assuming you have FastAPI app defined in app/main.py
from app.models.auth import UserRole from app.models.auth import UserRole
from app.models.types import PyObjectId from app.models.types import PyObjectId
@@ -26,8 +29,8 @@ def fake_user():
role=UserRole.USER, role=UserRole.USER,
is_active=True, is_active=True,
hashed_password="hashed-secret", hashed_password="hashed-secret",
created_at="2023-01-01T00:00:00", created_at=datetime(2025, 1, 1),
updated_at="2023-01-01T00:00:00", updated_at=datetime(2025, 1, 2),
) )
@@ -51,14 +54,23 @@ def override_get_current_user(fake_user):
return _override return _override
def override_get_database():
def _override():
client = MongoClient()
db = client.test_database
return db
return _override
# ---------------------- TESTS FOR /auth/login ---------------------- # ---------------------- TESTS FOR /auth/login ----------------------
class TestLogin: 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() auth_service = override_auth_service()
user_service = override_user_service(fake_user) user_service = override_user_service(fake_user)
monkeypatch.setattr("app.api.routes.auth.get_auth_service", lambda: auth_service) client.app.dependency_overrides[get_auth_service] = lambda: auth_service
monkeypatch.setattr("app.api.routes.auth.get_user_service", lambda: user_service) client.app.dependency_overrides[get_user_service] = lambda: user_service
response = client.post( response = client.post(
"/auth/login", "/auth/login",
@@ -70,13 +82,13 @@ class TestLogin:
assert "access_token" in data assert "access_token" in data
assert data["user"]["username"] == "testuser" 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() auth_service = override_auth_service()
user_service = MagicMock(spec=UserService) user_service = MagicMock(spec=UserService)
user_service.get_user_by_username.return_value = None user_service.get_user_by_username.return_value = None
monkeypatch.setattr("app.api.routes.auth.get_auth_service", lambda: auth_service) client.app.dependency_overrides[get_auth_service] = lambda: auth_service
monkeypatch.setattr("app.api.routes.auth.get_user_service", lambda: user_service) client.app.dependency_overrides[get_user_service] = lambda: user_service
response = client.post( response = client.post(
"/auth/login", "/auth/login",
@@ -85,13 +97,12 @@ class TestLogin:
assert response.status_code == status.HTTP_401_UNAUTHORIZED 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 fake_user.is_active = False
auth_service = override_auth_service() auth_service = override_auth_service()
user_service = override_user_service(fake_user) user_service = override_user_service(fake_user)
client.app.dependency_overrides[get_auth_service] = lambda: auth_service
monkeypatch.setattr("app.api.routes.auth.get_auth_service", lambda: auth_service) client.app.dependency_overrides[get_user_service] = lambda: user_service
monkeypatch.setattr("app.api.routes.auth.get_user_service", lambda: user_service)
response = client.post( response = client.post(
"/auth/login", "/auth/login",
@@ -100,13 +111,12 @@ class TestLogin:
assert response.status_code == status.HTTP_401_UNAUTHORIZED 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 = override_auth_service()
auth_service.verify_user_password.return_value = False auth_service.verify_user_password.return_value = False
user_service = override_user_service(fake_user) user_service = override_user_service(fake_user)
client.app.dependency_overrides[get_auth_service] = lambda: auth_service
monkeypatch.setattr("app.api.routes.auth.get_auth_service", lambda: auth_service) client.app.dependency_overrides[get_user_service] = lambda: user_service
monkeypatch.setattr("app.api.routes.auth.get_user_service", lambda: user_service)
response = client.post( response = client.post(
"/auth/login", "/auth/login",
@@ -118,8 +128,8 @@ class TestLogin:
# ---------------------- TESTS FOR /auth/me ---------------------- # ---------------------- TESTS FOR /auth/me ----------------------
class TesteMe: class TesteMe:
def test_i_can_get_current_user_profile(self, client, fake_user, monkeypatch): def test_i_can_get_current_user_profile(self, client, fake_user):
monkeypatch.setattr("app.api.routes.auth.get_current_user", override_get_current_user(fake_user)) client.app.dependency_overrides[get_current_user] = override_get_current_user(fake_user)
response = client.get("/auth/me") response = client.get("/auth/me")
@@ -132,7 +142,7 @@ class TesteMe:
def raise_http_exception(): def raise_http_exception():
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED) 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") response = client.get("/auth/me")