Skip to content

Commit

Permalink
Add test + fixtures get_current_active_user
Browse files Browse the repository at this point in the history
  • Loading branch information
BraunRudolf committed Oct 8, 2024
1 parent 8c579ba commit 0761b45
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 2 deletions.
86 changes: 85 additions & 1 deletion backend-app/tests/unit/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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="[email protected]",
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="[email protected]",
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="[email protected]",
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="[email protected]",
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="[email protected]",
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."""
Expand Down
31 changes: 30 additions & 1 deletion backend-app/tests/unit/test_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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"

0 comments on commit 0761b45

Please sign in to comment.