diff --git a/backend-app/tests/unit/conftest.py b/backend-app/tests/unit/conftest.py index edacacb0..a7a6d327 100644 --- a/backend-app/tests/unit/conftest.py +++ b/backend-app/tests/unit/conftest.py @@ -13,7 +13,7 @@ from app.models import user as api_m from app.models.database import User as db_user from app.models.file_db import create_file_table_class, update_schema -from app.models.user import UserCreate, UserInDB +from app.models.user import User, UserCreate, UserInDB from app.sql_db.crud import create_user, get_db, update_is_active, update_is_admin from app.sql_db.database import Base from app.sql_db.file_crud import create_update_table, insert_data @@ -197,6 +197,90 @@ def mock_user(): return user +@pytest.fixture +def mock_user_is_active(): + """Fixture for mocking a user.""" + user = User( + id=1, + email="user1@example.com", + password="test1", + hashed_password="test1fake_hash", + is_active=True, + ) + return user + + +@pytest.fixture +def mock_user_is_not_active(): + """Fixture for mocking a user.""" + user = User( + id=1, + email="user1@example.com", + password="test1", + hashed_password="test1fake_hash", + is_active=False, + ) + return user + + +@pytest.fixture +def mock_user_is_active_is_admin(): + """Fixture for mocking a user.""" + user = User( + id=1, + email="user1@example.com", + password="test1", + hashed_password="test1fake_hash", + is_active=True, + is_admin=True, + ) + return user + + +@pytest.fixture +def mock_user_is_not_active_is_admin(): + """Fixture for mocking a user.""" + user = User( + id=1, + email="user1@example.com", + password="test1", + hashed_password="test1fake_hash", + is_active=False, + is_admin=True, + ) + return user + + +@pytest.fixture +def mock_user_is_not_active_is_not_admin(): + """Fixture for mocking a user.""" + user = User( + id=1, + email="user1@example.com", + password="test1", + hashed_password="test1fake_hash", + is_active=False, + is_admin=False, + ) + return user + + +@pytest.fixture +def mock_get_current_user_active(mock_user_is_active): + """Fixture for mocking get_current_user to return the mock user.""" + mock_function = MagicMock() + mock_function.return_value = mock_user_is_active + return mock_function + + +@pytest.fixture +def mock_get_current_user_not_active(mock_user_is_not_active): + """Fixture for mocking get_current_user to return the mock user.""" + mock_function = MagicMock() + mock_function.return_value = mock_user_is_not_active + return mock_function + + @pytest.fixture def mock_get_user_by_email_success(mock_user): """Fixture for mocking get_user_by_email to return the mock user.""" diff --git a/backend-app/tests/unit/test_auth.py b/backend-app/tests/unit/test_auth.py index d3411cff..f80dc044 100644 --- a/backend-app/tests/unit/test_auth.py +++ b/backend-app/tests/unit/test_auth.py @@ -4,7 +4,12 @@ from fastapi.exceptions import HTTPException from jose import JWTError, jwt -from app.api.auth import authenticate_user, create_access_token, get_current_user +from app.api.auth import ( + authenticate_user, + create_access_token, + get_current_active_user, + get_current_user, +) def test_authenticate_user_success(mock_db, mock_get_user_by_email_success, mock_user, monkeypatch): @@ -195,3 +200,27 @@ async def test_get_current_user_user_not_found( assert exc_info.value.detail == "Could not validate credentials" mock_jwt_decode.assert_called_once_with(valid_token, TEST_SECRET_KEY, algorithms=["HS256"]) mock_get_user_by_email_none.assert_called_once_with(mock_db, email=valid_token_payload["sub"]) + + +@pytest.mark.asyncio +async def test_get_current_active_user_success( + mock_user_is_active, mock_get_current_user_active, monkeypatch +): + monkeypatch.setattr("app.api.auth.get_current_user", mock_get_current_user_active) + + user = await get_current_active_user(mock_user_is_active) + + assert user.id == mock_user_is_active.id + + +@pytest.mark.asyncio +async def test_get_current_active_user_not_active( + mock_user_is_not_active, mock_get_current_user_not_active, monkeypatch +): + monkeypatch.setattr("app.api.auth.get_current_user", mock_get_current_user_not_active) + + with pytest.raises(HTTPException) as exc_info: + await get_current_active_user(mock_user_is_not_active) + + assert exc_info.value.status_code == 400 + assert exc_info.value.detail == "Inactive user"