From 90a31028f384c1c5a2436fbedfe7ed0870d80d68 Mon Sep 17 00:00:00 2001 From: Rudolf Braun <48672663+BraunRudolf@users.noreply.github.com> Date: Wed, 9 Oct 2024 08:18:31 +0200 Subject: [PATCH] Add/Edit Tests + Fixtures Add/Edit Fixutres for get_current_user Add Test get_current_active_admin --- backend-app/tests/unit/conftest.py | 38 +++++++++++---------- backend-app/tests/unit/test_auth.py | 51 +++++++++++++++++++++++++---- 2 files changed, 64 insertions(+), 25 deletions(-) diff --git a/backend-app/tests/unit/conftest.py b/backend-app/tests/unit/conftest.py index a7a6d327..0110bcf1 100644 --- a/backend-app/tests/unit/conftest.py +++ b/backend-app/tests/unit/conftest.py @@ -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, @@ -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, @@ -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, @@ -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="user1@example.com", - 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 diff --git a/backend-app/tests/unit/test_auth.py b/backend-app/tests/unit/test_auth.py index f80dc044..a7378dcb 100644 --- a/backend-app/tests/unit/test_auth.py +++ b/backend-app/tests/unit/test_auth.py @@ -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"