fix /user/active tests

This commit is contained in:
msramalho
2025-02-06 20:40:40 +00:00
parent 9af48efe22
commit fed8543c30
2 changed files with 16 additions and 12 deletions

View File

@@ -8,7 +8,6 @@ from core.config import VERSION, BREAKING_CHANGES
from core.logging import log_error from core.logging import log_error
from db import crud from db import crud
from db.schemas import ActiveUser, UsageResponse from db.schemas import ActiveUser, UsageResponse
from db.database import get_db_dependency
from db.user_state import UserState from db.user_state import UserState
from web.security import get_user_auth, bearer_security, get_user_state from web.security import get_user_auth, bearer_security, get_user_state
from shared.user_groups import GroupPermissions from shared.user_groups import GroupPermissions

View File

@@ -1,4 +1,4 @@
from unittest.mock import AsyncMock, patch from unittest.mock import AsyncMock, MagicMock, patch
from fastapi.testclient import TestClient from fastapi.testclient import TestClient
import pytest import pytest
from core.config import VERSION from core.config import VERSION
@@ -49,22 +49,27 @@ def test_endpoint_active_no_auth(client, test_no_auth):
test_no_auth(client.get, "/user/active") test_no_auth(client.get, "/user/active")
def test_endpoint_active_true_user(client_with_auth): def test_endpoint_active(app):
r = client_with_auth.get("/user/active") m_user_state = MagicMock()
assert r.status_code == 200
assert r.json() == {"active": True}
from web.security import get_user_state
def test_endpoint_active_false_user(app): app.dependency_overrides[get_user_state] = lambda: m_user_state
from web.security import get_user_auth
# inactive user
app.dependency_overrides[get_user_auth] = lambda: "morty@not-recognized-group.com" m_user_state.active = False
client = TestClient(app) client = TestClient(app)
r = client.get("/user/active") r = client.get("/user/active")
assert r.status_code == 200 assert r.status_code == 200
assert r.json() == {"active": False} assert r.json() == {"active": False}
# active user
m_user_state.active = True
client = TestClient(app)
r = client.get("/user/active")
assert r.status_code == 200
assert r.json() == {"active": True}
def test_no_serve_local_archive_by_default(client_with_auth): def test_no_serve_local_archive_by_default(client_with_auth):
r = client_with_auth.get("/app/local_archive_test/temp.txt") r = client_with_auth.get("/app/local_archive_test/temp.txt")