296 lines
11 KiB
Python
296 lines
11 KiB
Python
"""Integration tests for query endpoints (DB API interaction only).
|
|
|
|
Note: These tests check DB API integration for user settings retrieval.
|
|
RAG backend calls are not tested here (require actual RAG infrastructure).
|
|
"""
|
|
|
|
import pytest
|
|
from unittest.mock import patch, AsyncMock, MagicMock
|
|
|
|
|
|
@pytest.mark.integration
|
|
class TestQueryDBApiIntegration:
|
|
"""Integration tests for query endpoints - DB API interaction only."""
|
|
|
|
def test_bench_query_retrieves_user_settings(self, client, auth_headers):
|
|
"""Test that bench query retrieves user settings from DB API."""
|
|
# First, set up user settings for bench mode
|
|
settings_data = {
|
|
"settings": {
|
|
"ift": {
|
|
"apiMode": "bench",
|
|
"bearerToken": "test-token",
|
|
"systemPlatform": "test-platform",
|
|
"systemPlatformUser": "test-user",
|
|
"platformUserId": "user-123",
|
|
"platformId": "platform-123",
|
|
"withClassify": False,
|
|
"resetSessionMode": True
|
|
}
|
|
}
|
|
}
|
|
|
|
client.put("/api/v1/settings", json=settings_data, headers=auth_headers)
|
|
|
|
# Mock RAG service to avoid actual RAG calls
|
|
with patch('app.api.v1.query.RagService') as MockRagService:
|
|
mock_rag = AsyncMock()
|
|
mock_rag.send_bench_query = AsyncMock(return_value={
|
|
"answers": [{"answer": "Test answer", "docs": []}]
|
|
})
|
|
mock_rag.close = AsyncMock()
|
|
MockRagService.return_value = mock_rag
|
|
|
|
# Send bench query
|
|
query_data = {
|
|
"environment": "ift",
|
|
"questions": [{"body": "Test question", "with_docs": True}]
|
|
}
|
|
|
|
response = client.post(
|
|
"/api/v1/query/bench",
|
|
json=query_data,
|
|
headers=auth_headers
|
|
)
|
|
|
|
# Should succeed (settings retrieved from DB API)
|
|
assert response.status_code == 200
|
|
|
|
# Verify RAG service was called with correct settings
|
|
mock_rag.send_bench_query.assert_called_once()
|
|
call_kwargs = mock_rag.send_bench_query.call_args[1]
|
|
assert call_kwargs["environment"] == "ift"
|
|
assert "user_settings" in call_kwargs
|
|
user_settings = call_kwargs["user_settings"]
|
|
assert user_settings["bearerToken"] == "test-token"
|
|
|
|
def test_backend_query_retrieves_user_settings(self, client, auth_headers):
|
|
"""Test that backend query retrieves user settings from DB API."""
|
|
# Set up user settings for backend mode
|
|
settings_data = {
|
|
"settings": {
|
|
"psi": {
|
|
"apiMode": "backend",
|
|
"bearerToken": "backend-token",
|
|
"systemPlatform": "backend-platform",
|
|
"systemPlatformUser": "backend-user",
|
|
"platformUserId": "user-456",
|
|
"platformId": "platform-456",
|
|
"withClassify": True,
|
|
"resetSessionMode": False
|
|
}
|
|
}
|
|
}
|
|
|
|
client.put("/api/v1/settings", json=settings_data, headers=auth_headers)
|
|
|
|
# Mock RAG service
|
|
with patch('app.api.v1.query.RagService') as MockRagService:
|
|
mock_rag = AsyncMock()
|
|
mock_rag.send_backend_query = AsyncMock(return_value=[
|
|
{"answer": "Test answer", "confidence": 0.95}
|
|
])
|
|
mock_rag.close = AsyncMock()
|
|
MockRagService.return_value = mock_rag
|
|
|
|
# Send backend query
|
|
query_data = {
|
|
"environment": "psi",
|
|
"questions": [{"body": "Test question", "with_docs": True}],
|
|
"reset_session": False
|
|
}
|
|
|
|
response = client.post(
|
|
"/api/v1/query/backend",
|
|
json=query_data,
|
|
headers=auth_headers
|
|
)
|
|
|
|
assert response.status_code == 200
|
|
|
|
# Verify RAG service was called with correct settings
|
|
mock_rag.send_backend_query.assert_called_once()
|
|
call_kwargs = mock_rag.send_backend_query.call_args[1]
|
|
assert call_kwargs["environment"] == "psi"
|
|
assert call_kwargs["reset_session"] is False
|
|
user_settings = call_kwargs["user_settings"]
|
|
assert user_settings["bearerToken"] == "backend-token"
|
|
assert user_settings["withClassify"] is True
|
|
|
|
def test_bench_query_wrong_api_mode(self, client, auth_headers):
|
|
"""Test bench query fails when settings configured for backend mode."""
|
|
# Set up settings for backend mode
|
|
settings_data = {
|
|
"settings": {
|
|
"ift": {
|
|
"apiMode": "backend", # Wrong mode for bench query
|
|
"bearerToken": "token",
|
|
"systemPlatform": "platform",
|
|
"systemPlatformUser": "user",
|
|
"platformUserId": "user-id",
|
|
"platformId": "platform-id",
|
|
"withClassify": False,
|
|
"resetSessionMode": True
|
|
}
|
|
}
|
|
}
|
|
|
|
client.put("/api/v1/settings", json=settings_data, headers=auth_headers)
|
|
|
|
# Try bench query
|
|
query_data = {
|
|
"environment": "ift",
|
|
"questions": [{"body": "Test", "with_docs": True}]
|
|
}
|
|
|
|
response = client.post(
|
|
"/api/v1/query/bench",
|
|
json=query_data,
|
|
headers=auth_headers
|
|
)
|
|
|
|
# Should fail due to wrong API mode
|
|
assert response.status_code in [400, 500]
|
|
|
|
def test_backend_query_wrong_api_mode(self, client, auth_headers):
|
|
"""Test backend query fails when settings configured for bench mode."""
|
|
# Set up settings for bench mode
|
|
settings_data = {
|
|
"settings": {
|
|
"prod": {
|
|
"apiMode": "bench", # Wrong mode for backend query
|
|
"bearerToken": "token",
|
|
"systemPlatform": "platform",
|
|
"systemPlatformUser": "user",
|
|
"platformUserId": "user-id",
|
|
"platformId": "platform-id",
|
|
"withClassify": False,
|
|
"resetSessionMode": True
|
|
}
|
|
}
|
|
}
|
|
|
|
client.put("/api/v1/settings", json=settings_data, headers=auth_headers)
|
|
|
|
# Try backend query
|
|
query_data = {
|
|
"environment": "prod",
|
|
"questions": [{"body": "Test", "with_docs": True}],
|
|
"reset_session": True
|
|
}
|
|
|
|
response = client.post(
|
|
"/api/v1/query/backend",
|
|
json=query_data,
|
|
headers=auth_headers
|
|
)
|
|
|
|
# Should fail due to wrong API mode
|
|
assert response.status_code in [400, 500]
|
|
|
|
def test_query_invalid_environment(self, client, auth_headers):
|
|
"""Test query with invalid environment name."""
|
|
query_data = {
|
|
"environment": "invalid_env",
|
|
"questions": [{"body": "Test", "with_docs": True}]
|
|
}
|
|
|
|
# Bench query
|
|
response = client.post(
|
|
"/api/v1/query/bench",
|
|
json=query_data,
|
|
headers=auth_headers
|
|
)
|
|
assert response.status_code == 400
|
|
|
|
# Backend query
|
|
query_data["reset_session"] = True
|
|
response = client.post(
|
|
"/api/v1/query/backend",
|
|
json=query_data,
|
|
headers=auth_headers
|
|
)
|
|
assert response.status_code == 400
|
|
|
|
def test_query_requires_authentication(self, client):
|
|
"""Test that query endpoints require authentication."""
|
|
query_data = {
|
|
"environment": "ift",
|
|
"questions": [{"body": "Test", "with_docs": True}]
|
|
}
|
|
|
|
# Bench without auth
|
|
response = client.post("/api/v1/query/bench", json=query_data)
|
|
assert response.status_code == 401
|
|
|
|
# Backend without auth
|
|
query_data["reset_session"] = True
|
|
response = client.post("/api/v1/query/backend", json=query_data)
|
|
assert response.status_code == 401
|
|
|
|
def test_settings_update_affects_query(self, client, auth_headers):
|
|
"""Test that updating settings affects subsequent queries."""
|
|
# Initial settings
|
|
initial_settings = {
|
|
"settings": {
|
|
"ift": {
|
|
"apiMode": "bench",
|
|
"bearerToken": "initial-token",
|
|
"systemPlatform": "initial",
|
|
"systemPlatformUser": "initial",
|
|
"platformUserId": "initial",
|
|
"platformId": "initial",
|
|
"withClassify": False,
|
|
"resetSessionMode": True
|
|
}
|
|
}
|
|
}
|
|
|
|
client.put("/api/v1/settings", json=initial_settings, headers=auth_headers)
|
|
|
|
# Mock RAG service
|
|
with patch('app.api.v1.query.RagService') as MockRagService:
|
|
mock_rag = AsyncMock()
|
|
mock_rag.send_bench_query = AsyncMock(return_value={"answers": []})
|
|
mock_rag.close = AsyncMock()
|
|
MockRagService.return_value = mock_rag
|
|
|
|
# First query
|
|
query_data = {
|
|
"environment": "ift",
|
|
"questions": [{"body": "Test", "with_docs": True}]
|
|
}
|
|
|
|
client.post("/api/v1/query/bench", json=query_data, headers=auth_headers)
|
|
|
|
# Check first call
|
|
first_call = mock_rag.send_bench_query.call_args[1]
|
|
assert first_call["user_settings"]["bearerToken"] == "initial-token"
|
|
|
|
# Update settings
|
|
updated_settings = {
|
|
"settings": {
|
|
"ift": {
|
|
"apiMode": "bench",
|
|
"bearerToken": "updated-token",
|
|
"systemPlatform": "updated",
|
|
"systemPlatformUser": "updated",
|
|
"platformUserId": "updated",
|
|
"platformId": "updated",
|
|
"withClassify": True,
|
|
"resetSessionMode": False
|
|
}
|
|
}
|
|
}
|
|
|
|
client.put("/api/v1/settings", json=updated_settings, headers=auth_headers)
|
|
|
|
# Second query
|
|
mock_rag.send_bench_query.reset_mock()
|
|
client.post("/api/v1/query/bench", json=query_data, headers=auth_headers)
|
|
|
|
# Check second call uses updated settings
|
|
second_call = mock_rag.send_bench_query.call_args[1]
|
|
assert second_call["user_settings"]["bearerToken"] == "updated-token"
|
|
assert second_call["user_settings"]["withClassify"] is True
|