|
| 1 | +from unittest.mock import AsyncMock |
| 2 | + |
| 3 | +from fastapi.testclient import TestClient |
| 4 | + |
| 5 | +from src.app.core.dependencies import get_agent_workflow |
| 6 | +from src.app.main import app |
| 7 | + |
| 8 | +client = TestClient(app) |
| 9 | + |
| 10 | + |
| 11 | +def test_chat_byok_headers(): |
| 12 | + """ |
| 13 | + Test that Authorization and X-Provider headers are correctly |
| 14 | + extracted and passed to the workflow state. |
| 15 | + """ |
| 16 | + mock_workflow = AsyncMock() |
| 17 | + mock_workflow.ainvoke.return_value = { |
| 18 | + "question": "test question", |
| 19 | + "generation": "mock response", |
| 20 | + "documents": [], |
| 21 | + "steps": ["received_query"], |
| 22 | + "route": "direct", |
| 23 | + "retry_count": 0, |
| 24 | + } |
| 25 | + |
| 26 | + app.dependency_overrides[get_agent_workflow] = lambda: mock_workflow |
| 27 | + |
| 28 | + # 1. Test Bearer token in Authorization header and X-Provider header |
| 29 | + headers = { |
| 30 | + "Authorization": "Bearer my-custom-gemini-key", |
| 31 | + "X-Provider": "gemini", |
| 32 | + } |
| 33 | + response = client.post( |
| 34 | + "/api/v1/chat", |
| 35 | + json={"query": "test question"}, |
| 36 | + headers=headers, |
| 37 | + ) |
| 38 | + |
| 39 | + assert response.status_code == 200 |
| 40 | + assert mock_workflow.ainvoke.call_count == 1 |
| 41 | + call_state = mock_workflow.ainvoke.call_args[0][0] |
| 42 | + assert call_state["api_key"] == "my-custom-gemini-key" |
| 43 | + assert call_state["provider"] == "gemini" |
| 44 | + |
| 45 | + # 2. Test X-API-Key header and different casing for X-Provider |
| 46 | + mock_workflow.reset_mock() |
| 47 | + headers = { |
| 48 | + "X-API-Key": "my-custom-anthropic-key", |
| 49 | + "X-Provider": "Anthropic", |
| 50 | + } |
| 51 | + response = client.post( |
| 52 | + "/api/v1/chat", |
| 53 | + json={"query": "test question"}, |
| 54 | + headers=headers, |
| 55 | + ) |
| 56 | + |
| 57 | + assert response.status_code == 200 |
| 58 | + assert mock_workflow.ainvoke.call_count == 1 |
| 59 | + call_state = mock_workflow.ainvoke.call_args[0][0] |
| 60 | + assert call_state["api_key"] == "my-custom-anthropic-key" |
| 61 | + assert call_state["provider"] == "anthropic" |
| 62 | + |
| 63 | + # 3. Test when no headers are provided (should pass None) |
| 64 | + mock_workflow.reset_mock() |
| 65 | + response = client.post( |
| 66 | + "/api/v1/chat", |
| 67 | + json={"query": "test question"}, |
| 68 | + ) |
| 69 | + |
| 70 | + assert response.status_code == 200 |
| 71 | + assert mock_workflow.ainvoke.call_count == 1 |
| 72 | + call_state = mock_workflow.ainvoke.call_args[0][0] |
| 73 | + assert call_state["api_key"] is None |
| 74 | + assert call_state["provider"] is None |
| 75 | + |
| 76 | + app.dependency_overrides.clear() |
| 77 | + |
| 78 | + |
| 79 | +def test_chat_invalid_provider(): |
| 80 | + """Test that an unsupported provider returns a 400 Bad Request error.""" |
| 81 | + mock_workflow = AsyncMock() |
| 82 | + app.dependency_overrides[get_agent_workflow] = lambda: mock_workflow |
| 83 | + |
| 84 | + headers = { |
| 85 | + "X-Provider": "unsupported-llm-brand", |
| 86 | + } |
| 87 | + response = client.post( |
| 88 | + "/api/v1/chat", |
| 89 | + json={"query": "test question"}, |
| 90 | + headers=headers, |
| 91 | + ) |
| 92 | + |
| 93 | + assert response.status_code == 400 |
| 94 | + assert "Unsupported LLM provider" in response.json()["detail"] |
| 95 | + assert mock_workflow.ainvoke.call_count == 0 |
| 96 | + |
| 97 | + app.dependency_overrides.clear() |
0 commit comments