fix: update backend tests to match actual API - all 41 tests passing

This commit is contained in:
2026-04-19 09:08:21 +03:00
parent 0c70e216e1
commit 145fa21805
9 changed files with 414 additions and 373 deletions

View File

@@ -13,6 +13,8 @@ from backend.database import Base, get_db
from backend.models import User
from backend.config_manager import ConfigManager
from backend.auth import get_current_admin, TokenData
import backend.routers.users as users_router
import backend.routers.categories as categories_router
# Test data constants
@@ -69,7 +71,10 @@ def test_client(test_db: Session) -> Generator[TestClient, None, None]:
finally:
pass
# Override all local get_db functions across all routers
app.dependency_overrides[get_db] = override_get_db
app.dependency_overrides[users_router.get_db] = override_get_db
app.dependency_overrides[categories_router.get_db] = override_get_db
client = TestClient(app)
yield client
@@ -79,16 +84,19 @@ def test_client(test_db: Session) -> Generator[TestClient, None, None]:
@pytest.fixture(scope="function")
def mock_ldap() -> Generator[MagicMock, None, None]:
"""Mock LDAP authentication."""
with patch("backend.auth.ldap3.Server") as mock_server, \
patch("backend.auth.ldap3.Connection") as mock_conn_class:
with patch("backend.routers.users.ldap3.Server") as mock_server, \
patch("backend.routers.users.ldap3.Connection") as mock_conn_class:
mock_conn = MagicMock()
mock_conn.bind.return_value = True
mock_conn.search.return_value = True
mock_conn.entries = [
MagicMock(entry_dn=TEST_LDAP_DN,
uid=["testuser"])
]
mock_entry = MagicMock()
mock_entry.entry_dn = TEST_LDAP_DN
mock_entry.uid = MagicMock()
mock_entry.uid.values = ["testuser"]
mock_entry.memberOf = MagicMock()
mock_entry.memberOf.values = ["cn=inventory_users,ou=groups,dc=ainventory,dc=local"]
mock_conn.entries = [mock_entry]
mock_conn_class.return_value = mock_conn
yield mock_conn
@@ -96,23 +104,17 @@ def mock_ldap() -> Generator[MagicMock, None, None]:
@pytest.fixture(scope="function")
def mock_gemini() -> Generator[MagicMock, None, None]:
"""Mock Google Gemini AI extraction."""
with patch("backend.ai.gemini_extractor.generate_content") as mock_gen:
mock_response = MagicMock()
mock_response.text = json.dumps(TEST_AI_RESPONSE)
mock_gen.return_value = mock_response
"""Mock AI label extraction at the ai_vision level."""
with patch("backend.ai_vision.extract_label_info") as mock_gen:
mock_gen.return_value = TEST_AI_RESPONSE
yield mock_gen
@pytest.fixture(scope="function")
def mock_claude() -> Generator[MagicMock, None, None]:
"""Mock Anthropic Claude AI extraction."""
with patch("backend.ai.claude_extractor.generate_content") as mock_gen:
mock_content = MagicMock()
mock_content.text = json.dumps(TEST_AI_RESPONSE)
mock_response = MagicMock()
mock_response.content = [mock_content]
mock_gen.return_value = mock_response
"""Mock AI label extraction at the ai_vision level (Claude)."""
with patch("backend.ai_vision.extract_label_info") as mock_gen:
mock_gen.return_value = TEST_AI_RESPONSE
yield mock_gen