Skip to content

Commit

Permalink
Add/Edit Tests + Fixtures
Browse files Browse the repository at this point in the history
Add/Edit Fixutres for get_current_user
Add Test get_current_active_admin
  • Loading branch information
BraunRudolf committed Oct 9, 2024
1 parent 0761b45 commit 90a3102
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 25 deletions.
38 changes: 20 additions & 18 deletions backend-app/tests/unit/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ def mock_user():


@pytest.fixture
def mock_user_is_active():
def mock_user_is_active_not_admin():
"""Fixture for mocking a user."""
user = User(
id=1,
Expand All @@ -211,7 +211,7 @@ def mock_user_is_active():


@pytest.fixture
def mock_user_is_not_active():
def mock_user_not_active_not_admin():
"""Fixture for mocking a user."""
user = User(
id=1,
Expand All @@ -238,7 +238,7 @@ def mock_user_is_active_is_admin():


@pytest.fixture
def mock_user_is_not_active_is_admin():
def mock_user_not_active_is_admin():
"""Fixture for mocking a user."""
user = User(
id=1,
Expand All @@ -252,32 +252,34 @@ def mock_user_is_not_active_is_admin():


@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
def mock_get_current_user_is_active_not_admin(mock_user_is_active_not_admin):
"""Fixture for mocking get_current_user to return the mock user."""
mock_function = MagicMock()
mock_function.return_value = mock_user_is_active_not_admin
return mock_function


@pytest.fixture
def mock_get_current_user_not_active_not_admin(mock_user_not_active_not_admin):
"""Fixture for mocking get_current_user to return the mock user."""
mock_function = MagicMock()
mock_function.return_value = mock_user_not_active_not_admin
return mock_function


@pytest.fixture
def mock_get_current_user_active(mock_user_is_active):
def mock_get_current_user_is_active_is_admin(mock_user_is_active_is_admin):
"""Fixture for mocking get_current_user to return the mock user."""
mock_function = MagicMock()
mock_function.return_value = mock_user_is_active
mock_function.return_value = mock_user_is_active_is_admin
return mock_function


@pytest.fixture
def mock_get_current_user_not_active(mock_user_is_not_active):
def mock_get_current_user_not_active_is_admin(mock_user_not_active_is_admin):
"""Fixture for mocking get_current_user to return the mock user."""
mock_function = MagicMock()
mock_function.return_value = mock_user_is_not_active
mock_function.return_value = mock_user_not_active_is_admin
return mock_function


Expand Down
51 changes: 44 additions & 7 deletions backend-app/tests/unit/test_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,23 +204,60 @@ async def test_get_current_user_user_not_found(

@pytest.mark.asyncio
async def test_get_current_active_user_success(
mock_user_is_active, mock_get_current_user_active, monkeypatch
mock_user_is_active_not_admin, mock_get_current_user_is_active_not_admin, monkeypatch
):
monkeypatch.setattr("app.api.auth.get_current_user", mock_get_current_user_active)
monkeypatch.setattr("app.api.auth.get_current_user", mock_get_current_user_is_active_not_admin)

user = await get_current_active_user(mock_user_is_active)
user = await get_current_active_user(mock_user_is_active_not_admin)

assert user.id == mock_user_is_active.id
assert user.id == mock_user_is_active_not_admin.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
mock_user_not_active_not_admin, mock_get_current_user_not_active_not_admin, monkeypatch
):
monkeypatch.setattr("app.api.auth.get_current_user", mock_get_current_user_not_active)
monkeypatch.setattr("app.api.auth.get_current_user", mock_get_current_user_not_active_not_admin)

with pytest.raises(HTTPException) as exc_info:
await get_current_active_user(mock_user_is_not_active)
await get_current_active_user(mock_user_not_active_not_admin)

assert exc_info.value.status_code == 400
assert exc_info.value.detail == "Inactive user"


@pytest.mark.asyncio
async def test_get_current_active_admin_success(
mock_user_is_active_is_admin, mock_get_current_user_is_active_is_admin, monkeypatch
):
monkeypatch.setattr("app.api.auth.get_current_user", mock_get_current_user_is_active_is_admin)

user = await get_current_active_user(mock_user_is_active_is_admin)

assert user.id == mock_user_is_active_is_admin.id


@pytest.mark.asyncio
async def test_get_current_active_admin_not_active_is_admin(
mock_user_not_active_is_admin, mock_get_current_user_not_active_is_admin, monkeypatch
):
monkeypatch.setattr("app.api.auth.get_current_user", mock_get_current_user_not_active_is_admin)

with pytest.raises(HTTPException) as exc_info:
await get_current_active_user(mock_user_not_active_is_admin)

assert exc_info.value.status_code == 400
assert exc_info.value.detail == "Inactive user"


@pytest.mark.asyncio
async def test_get_current_admin_not_active_not_admin(
mock_user_not_active_not_admin, mock_get_current_user_not_active_not_admin, monkeypatch
):
monkeypatch.setattr("app.api.auth.get_current_user", mock_get_current_user_not_active_not_admin)

with pytest.raises(HTTPException) as exc_info:
await get_current_active_user(mock_user_not_active_not_admin)

assert exc_info.value.status_code == 400
assert exc_info.value.detail == "Inactive user"

0 comments on commit 90a3102

Please sign in to comment.