Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
137 changes: 103 additions & 34 deletions tests/unit/app/endpoints/test_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,13 @@
)
from utils.types import ToolCallSummary, TurnSummary

MOCK_AUTH = ("mock_user_id", "mock_username", False, "mock_token")
# User ID must be proper UUID
MOCK_AUTH = (
"00000001-0001-0001-0001-000000000001",
"mock_username",
False,
"mock_token",
)
Comment on lines +41 to +47
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion | 🟠 Major

Restore the shared MOCK_AUTH tuple

Per the test guidelines we need to keep using the shared auth mock tuple ("mock_user_id", "mock_username", False, "mock_token"). Overriding it with a bespoke UUID here will desynchronize these tests from the rest of the suite. Please restore the shared value (or import the shared constant) so we continue to meet the convention.
Based on learnings

-MOCK_AUTH = (
-    "00000001-0001-0001-0001-000000000001",
+MOCK_AUTH = (
+    "mock_user_id",
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
# User ID must be proper UUID
MOCK_AUTH = (
"00000001-0001-0001-0001-000000000001",
"mock_username",
False,
"mock_token",
)
# User ID must be proper UUID
MOCK_AUTH = (
"mock_user_id",
"mock_username",
False,
"mock_token",
)
🤖 Prompt for AI Agents
In tests/unit/app/endpoints/test_query.py around lines 41 to 47, the test
overrides the shared MOCK_AUTH tuple with a bespoke UUID tuple which
desynchronizes this file from the rest of the test suite; restore the shared
value by replacing the custom tuple with the canonical tuple ("mock_user_id",
"mock_username", False, "mock_token") or import the shared MOCK_AUTH constant
from the common test utilities, ensuring the file uses the same mock auth used
across the suite.



@pytest.fixture
Expand Down Expand Up @@ -93,6 +99,9 @@ def setup_configuration_fixture():
},
"mcp_servers": [],
"customization": None,
"conversation_cache": {
"type": "noop",
},
}
cfg = AppConfig()
cfg.init_from_dict(config_dict)
Expand Down Expand Up @@ -172,7 +181,7 @@ async def _test_query_endpoint_handler(
)
],
)
conversation_id = "fake_conversation_id"
conversation_id = "00000000-0000-0000-0000-000000000000"
query = "What is OpenStack?"
referenced_documents = []

Expand Down Expand Up @@ -209,7 +218,7 @@ async def _test_query_endpoint_handler(
# Assert the store_transcript function is called if transcripts are enabled
if store_transcript_to_file:
mock_transcript.assert_called_once_with(
user_id="mock_user_id",
user_id="00000001-0001-0001-0001-000000000001",
conversation_id=conversation_id,
model_id="fake_model_id",
provider_id="fake_provider_id",
Expand Down Expand Up @@ -458,7 +467,11 @@ async def test_retrieve_response_no_returned_message(prepare_agent_mocks, mocker
mocker.patch("app.endpoints.query.configuration", mock_config)
mocker.patch(
"app.endpoints.query.get_agent",
return_value=(mock_agent, "fake_conversation_id", "fake_session_id"),
return_value=(
mock_agent,
"00000000-0000-0000-0000-000000000000",
"fake_session_id",
),
)
mock_metrics(mocker)

Expand Down Expand Up @@ -490,7 +503,11 @@ async def test_retrieve_response_message_without_content(prepare_agent_mocks, mo
mocker.patch("app.endpoints.query.configuration", mock_config)
mocker.patch(
"app.endpoints.query.get_agent",
return_value=(mock_agent, "fake_conversation_id", "fake_session_id"),
return_value=(
mock_agent,
"00000000-0000-0000-0000-000000000000",
"fake_session_id",
),
)
mock_metrics(mocker)

Expand Down Expand Up @@ -523,7 +540,11 @@ async def test_retrieve_response_vector_db_available(prepare_agent_mocks, mocker
mocker.patch("app.endpoints.query.configuration", mock_config)
mocker.patch(
"app.endpoints.query.get_agent",
return_value=(mock_agent, "fake_conversation_id", "fake_session_id"),
return_value=(
mock_agent,
"00000000-0000-0000-0000-000000000000",
"fake_session_id",
),
)
mock_metrics(mocker)

Expand All @@ -538,7 +559,7 @@ async def test_retrieve_response_vector_db_available(prepare_agent_mocks, mocker
# Assert that the metric for validation errors is NOT incremented
mock_metric.inc.assert_not_called()
assert summary.llm_response == "LLM answer"
assert conversation_id == "fake_conversation_id"
assert conversation_id == "00000000-0000-0000-0000-000000000000"
mock_agent.create_turn.assert_called_once_with(
messages=[UserMessage(content="What is OpenStack?", role="user")],
session_id="fake_session_id",
Expand All @@ -562,7 +583,11 @@ async def test_retrieve_response_no_available_shields(prepare_agent_mocks, mocke
mocker.patch("app.endpoints.query.configuration", mock_config)
mocker.patch(
"app.endpoints.query.get_agent",
return_value=(mock_agent, "fake_conversation_id", "fake_session_id"),
return_value=(
mock_agent,
"00000000-0000-0000-0000-000000000000",
"fake_session_id",
),
)
mock_metrics(mocker)

Expand All @@ -575,7 +600,7 @@ async def test_retrieve_response_no_available_shields(prepare_agent_mocks, mocke
)

assert summary.llm_response == "LLM answer"
assert conversation_id == "fake_conversation_id"
assert conversation_id == "00000000-0000-0000-0000-000000000000"
mock_agent.create_turn.assert_called_once_with(
messages=[UserMessage(content="What is OpenStack?", role="user")],
session_id="fake_session_id",
Expand Down Expand Up @@ -612,7 +637,11 @@ def __repr__(self):
mocker.patch("app.endpoints.query.configuration", mock_config)
mocker.patch(
"app.endpoints.query.get_agent",
return_value=(mock_agent, "fake_conversation_id", "fake_session_id"),
return_value=(
mock_agent,
"00000000-0000-0000-0000-000000000000",
"fake_session_id",
),
)
mock_metrics(mocker)

Expand All @@ -625,7 +654,7 @@ def __repr__(self):
)

assert summary.llm_response == "LLM answer"
assert conversation_id == "fake_conversation_id"
assert conversation_id == "00000000-0000-0000-0000-000000000000"
mock_agent.create_turn.assert_called_once_with(
messages=[UserMessage(content="What is OpenStack?", role="user")],
session_id="fake_session_id",
Expand Down Expand Up @@ -665,7 +694,11 @@ def __repr__(self):
mocker.patch("app.endpoints.query.configuration", mock_config)
mocker.patch(
"app.endpoints.query.get_agent",
return_value=(mock_agent, "fake_conversation_id", "fake_session_id"),
return_value=(
mock_agent,
"00000000-0000-0000-0000-000000000000",
"fake_session_id",
),
)
mock_metrics(mocker)

Expand All @@ -678,7 +711,7 @@ def __repr__(self):
)

assert summary.llm_response == "LLM answer"
assert conversation_id == "fake_conversation_id"
assert conversation_id == "00000000-0000-0000-0000-000000000000"
mock_agent.create_turn.assert_called_once_with(
messages=[UserMessage(content="What is OpenStack?", role="user")],
session_id="fake_session_id",
Expand Down Expand Up @@ -720,7 +753,11 @@ def __repr__(self):
mocker.patch("app.endpoints.query.configuration", mock_config)
mock_get_agent = mocker.patch(
"app.endpoints.query.get_agent",
return_value=(mock_agent, "fake_conversation_id", "fake_session_id"),
return_value=(
mock_agent,
"00000000-0000-0000-0000-000000000000",
"fake_session_id",
),
)
mock_metrics(mocker)

Expand All @@ -733,7 +770,7 @@ def __repr__(self):
)

assert summary.llm_response == "LLM answer"
assert conversation_id == "fake_conversation_id"
assert conversation_id == "00000000-0000-0000-0000-000000000000"

# Verify get_agent was called with the correct parameters
mock_get_agent.assert_called_once_with(
Expand Down Expand Up @@ -777,7 +814,11 @@ async def test_retrieve_response_with_one_attachment(prepare_agent_mocks, mocker
]
mocker.patch(
"app.endpoints.query.get_agent",
return_value=(mock_agent, "fake_conversation_id", "fake_session_id"),
return_value=(
mock_agent,
"00000000-0000-0000-0000-000000000000",
"fake_session_id",
),
)
mock_metrics(mocker)

Expand All @@ -790,7 +831,7 @@ async def test_retrieve_response_with_one_attachment(prepare_agent_mocks, mocker
)

assert summary.llm_response == "LLM answer"
assert conversation_id == "fake_conversation_id"
assert conversation_id == "00000000-0000-0000-0000-000000000000"
mock_agent.create_turn.assert_called_once_with(
messages=[UserMessage(content="What is OpenStack?", role="user")],
session_id="fake_session_id",
Expand Down Expand Up @@ -832,7 +873,11 @@ async def test_retrieve_response_with_two_attachments(prepare_agent_mocks, mocke
]
mocker.patch(
"app.endpoints.query.get_agent",
return_value=(mock_agent, "fake_conversation_id", "fake_session_id"),
return_value=(
mock_agent,
"00000000-0000-0000-0000-000000000000",
"fake_session_id",
),
)
mock_metrics(mocker)

Expand All @@ -845,7 +890,7 @@ async def test_retrieve_response_with_two_attachments(prepare_agent_mocks, mocke
)

assert summary.llm_response == "LLM answer"
assert conversation_id == "fake_conversation_id"
assert conversation_id == "00000000-0000-0000-0000-000000000000"
mock_agent.create_turn.assert_called_once_with(
messages=[UserMessage(content="What is OpenStack?", role="user")],
session_id="fake_session_id",
Expand Down Expand Up @@ -1005,7 +1050,11 @@ async def test_retrieve_response_with_mcp_servers(prepare_agent_mocks, mocker):
mocker.patch("app.endpoints.query.configuration", mock_config)
mock_get_agent = mocker.patch(
"app.endpoints.query.get_agent",
return_value=(mock_agent, "fake_conversation_id", "fake_session_id"),
return_value=(
mock_agent,
"00000000-0000-0000-0000-000000000000",
"fake_session_id",
),
)
mock_metrics(mocker)

Expand All @@ -1018,7 +1067,7 @@ async def test_retrieve_response_with_mcp_servers(prepare_agent_mocks, mocker):
)

assert summary.llm_response == "LLM answer"
assert conversation_id == "fake_conversation_id"
assert conversation_id == "00000000-0000-0000-0000-000000000000"

# Verify get_agent was called with the correct parameters
mock_get_agent.assert_called_once_with(
Expand Down Expand Up @@ -1075,7 +1124,11 @@ async def test_retrieve_response_with_mcp_servers_empty_token(
mocker.patch("app.endpoints.query.configuration", mock_config)
mock_get_agent = mocker.patch(
"app.endpoints.query.get_agent",
return_value=(mock_agent, "fake_conversation_id", "fake_session_id"),
return_value=(
mock_agent,
"00000000-0000-0000-0000-000000000000",
"fake_session_id",
),
)
mock_metrics(mocker)

Expand All @@ -1088,7 +1141,7 @@ async def test_retrieve_response_with_mcp_servers_empty_token(
)

assert summary.llm_response == "LLM answer"
assert conversation_id == "fake_conversation_id"
assert conversation_id == "00000000-0000-0000-0000-000000000000"

# Verify get_agent was called with the correct parameters
mock_get_agent.assert_called_once_with(
Expand Down Expand Up @@ -1137,7 +1190,11 @@ async def test_retrieve_response_with_mcp_servers_and_mcp_headers(
mocker.patch("app.endpoints.query.configuration", mock_config)
mock_get_agent = mocker.patch(
"app.endpoints.query.get_agent",
return_value=(mock_agent, "fake_conversation_id", "fake_session_id"),
return_value=(
mock_agent,
"00000000-0000-0000-0000-000000000000",
"fake_session_id",
),
)
mock_metrics(mocker)

Expand All @@ -1164,7 +1221,7 @@ async def test_retrieve_response_with_mcp_servers_and_mcp_headers(
)

assert summary.llm_response == "LLM answer"
assert conversation_id == "fake_conversation_id"
assert conversation_id == "00000000-0000-0000-0000-000000000000"

# Verify get_agent was called with the correct parameters
mock_get_agent.assert_called_once_with(
Expand Down Expand Up @@ -1234,7 +1291,11 @@ async def test_retrieve_response_shield_violation(prepare_agent_mocks, mocker):
mocker.patch("app.endpoints.query.configuration", mock_config)
mocker.patch(
"app.endpoints.query.get_agent",
return_value=(mock_agent, "fake_conversation_id", "fake_session_id"),
return_value=(
mock_agent,
"00000000-0000-0000-0000-000000000000",
"fake_session_id",
),
)
mock_metrics(mocker)

Expand All @@ -1247,7 +1308,7 @@ async def test_retrieve_response_shield_violation(prepare_agent_mocks, mocker):
# Assert that the metric for validation errors is incremented
mock_metric.inc.assert_called_once()

assert conversation_id == "fake_conversation_id"
assert conversation_id == "00000000-0000-0000-0000-000000000000"
mock_agent.create_turn.assert_called_once_with(
messages=[UserMessage(content="What is OpenStack?", role="user")],
session_id="fake_session_id",
Expand Down Expand Up @@ -1325,7 +1386,7 @@ async def test_auth_tuple_unpacking_in_query_endpoint_handler(mocker, dummy_requ
)
mock_retrieve_response = mocker.patch(
"app.endpoints.query.retrieve_response",
return_value=(summary, "test_conversation_id", []),
return_value=(summary, "00000000-0000-0000-0000-000000000000", []),
)

mocker.patch(
Expand Down Expand Up @@ -1371,7 +1432,7 @@ async def test_query_endpoint_handler_no_tools_true(mocker, dummy_request):
)
],
)
conversation_id = "fake_conversation_id"
conversation_id = "00000000-0000-0000-0000-000000000000"
query = "What is OpenStack?"
referenced_documents = []

Expand Down Expand Up @@ -1423,7 +1484,7 @@ async def test_query_endpoint_handler_no_tools_false(mocker, dummy_request):
)
],
)
conversation_id = "fake_conversation_id"
conversation_id = "00000000-0000-0000-0000-000000000000"
query = "What is OpenStack?"
referenced_documents = []

Expand Down Expand Up @@ -1473,7 +1534,11 @@ async def test_retrieve_response_no_tools_bypasses_mcp_and_rag(
mocker.patch("app.endpoints.query.configuration", mock_config)
mocker.patch(
"app.endpoints.query.get_agent",
return_value=(mock_agent, "fake_conversation_id", "fake_session_id"),
return_value=(
mock_agent,
"00000000-0000-0000-0000-000000000000",
"fake_session_id",
),
)
mock_metrics(mocker)

Expand All @@ -1486,7 +1551,7 @@ async def test_retrieve_response_no_tools_bypasses_mcp_and_rag(
)

assert summary.llm_response == "LLM answer"
assert conversation_id == "fake_conversation_id"
assert conversation_id == "00000000-0000-0000-0000-000000000000"

# Verify that agent.extra_headers is empty (no MCP headers)
assert mock_agent.extra_headers == {}
Expand Down Expand Up @@ -1524,7 +1589,11 @@ async def test_retrieve_response_no_tools_false_preserves_functionality(
mocker.patch("app.endpoints.query.configuration", mock_config)
mocker.patch(
"app.endpoints.query.get_agent",
return_value=(mock_agent, "fake_conversation_id", "fake_session_id"),
return_value=(
mock_agent,
"00000000-0000-0000-0000-000000000000",
"fake_session_id",
),
)
mock_metrics(mocker)

Expand All @@ -1537,7 +1606,7 @@ async def test_retrieve_response_no_tools_false_preserves_functionality(
)

assert summary.llm_response == "LLM answer"
assert conversation_id == "fake_conversation_id"
assert conversation_id == "00000000-0000-0000-0000-000000000000"

# Verify that agent.extra_headers contains MCP headers
expected_extra_headers = {
Expand Down
Loading
Loading