From 0f933267a633d9d1a3fb43cad5528307e4e11ed7 Mon Sep 17 00:00:00 2001 From: Stephen Nwankwo Date: Tue, 7 Apr 2026 11:48:39 +0100 Subject: [PATCH] test: mock GOOGLE_API_KEY in API and integration tests to satisfy configuration requirements --- backend/tests/api/test_documents.py | 6 ++++-- backend/tests/api/test_widget_chat.py | 11 ++++++++--- backend/tests/integration/test_rag_scoped.py | 16 ++++++++++------ 3 files changed, 22 insertions(+), 11 deletions(-) diff --git a/backend/tests/api/test_documents.py b/backend/tests/api/test_documents.py index b6819eb..f862795 100644 --- a/backend/tests/api/test_documents.py +++ b/backend/tests/api/test_documents.py @@ -71,8 +71,10 @@ def test_api_chat_scoped(client, db_session): db_session.add(Business(user_id="u1", business_name="Test Biz")) db_session.commit() - # 3. Chat - with patch("app.api.routes.run_conversation", return_value="Bot says hi") as mock_chat: + # 3. Chat — patch API key so it reaches the mocked run_conversation + with patch("app.api.routes.settings") as mock_settings, \ + patch("app.api.routes.run_conversation", return_value="Bot says hi") as mock_chat: + mock_settings.GOOGLE_API_KEY = "test-key" response = client.post("/chat", json={"message": "hello"}, headers=headers) assert response.status_code == 200 diff --git a/backend/tests/api/test_widget_chat.py b/backend/tests/api/test_widget_chat.py index 77a877d..b307441 100644 --- a/backend/tests/api/test_widget_chat.py +++ b/backend/tests/api/test_widget_chat.py @@ -16,8 +16,9 @@ pytestmark = pytest.mark.api -# Shared mock target +# Shared mock targets RUN_CONVERSATION = "app.api.widget.run_conversation" +SETTINGS = "app.api.widget.settings" @pytest.fixture @@ -89,8 +90,10 @@ def test_404_for_unknown_widget(self, client): class TestInitGuestSession: """Tests for POST /widgets/guest/session/init/{public_widget_id}.""" + @patch(SETTINGS) @patch(RUN_CONVERSATION, new_callable=AsyncMock, return_value="Hello from AI") - def test_init_session_with_first_message(self, mock_ai, client, db_session, widget_env): + def test_init_session_with_first_message(self, mock_ai, mock_settings, client, db_session, widget_env): + mock_settings.GOOGLE_API_KEY = "test-key" widget = widget_env["widget"] guest = GuestUserFactory(widget=widget, widget_id=widget.id) db_session.commit() @@ -150,8 +153,10 @@ def test_init_404_unknown_guest(self, client, widget_env): class TestChatInSession: """Tests for POST /widgets/chat/{public_widget_id}/session/{session_id}.""" + @patch(SETTINGS) @patch(RUN_CONVERSATION, new_callable=AsyncMock, return_value="AI reply") - def test_send_message(self, mock_ai, client, db_session, widget_env): + def test_send_message(self, mock_ai, mock_settings, client, db_session, widget_env): + mock_settings.GOOGLE_API_KEY = "test-key" widget = widget_env["widget"] guest = GuestUserFactory(widget=widget, widget_id=widget.id) session = ChatSessionFactory(guest=guest, guest_id=guest.id) diff --git a/backend/tests/integration/test_rag_scoped.py b/backend/tests/integration/test_rag_scoped.py index 7bae7c7..099c532 100644 --- a/backend/tests/integration/test_rag_scoped.py +++ b/backend/tests/integration/test_rag_scoped.py @@ -41,15 +41,17 @@ def test_upload_document_scoped(db_session, mock_file_storage): def test_process_documents_scoped(db_session, mock_file_storage, mock_vector_db_service): user1 = "u1" user2 = "u2" - + # Setup DB with pending docs for both users doc1 = Document(user_id=user1, filename="doc1.txt", file_path="p1", status="pending") doc2 = Document(user_id=user2, filename="doc2.txt", file_path="p2", status="pending") db_session.add_all([doc1, doc2]) db_session.commit() - - # Mock file content - with patch("builtins.open", new_callable=MagicMock) as mock_open: + + # Mock file content and API key + with patch("builtins.open", new_callable=MagicMock) as mock_open, \ + patch("app.services.rag_service.settings") as mock_settings: + mock_settings.GOOGLE_API_KEY = "test-key" mock_open.return_value.__enter__.return_value.read.return_value = "content" with patch("os.path.exists", return_value=True): # Process for User 1 @@ -83,8 +85,10 @@ def test_list_documents_scoped(db_session): assert docs2[0]["filename"] == "b.txt" def test_query_scoped(mock_vector_db_service): - rag_service.query("check", "u1") - + with patch("app.services.rag_service.settings") as mock_settings: + mock_settings.GOOGLE_API_KEY = "test-key" + rag_service.query("check", "u1") + mock_vector_db_service.query.assert_called_once() args, kwargs = mock_vector_db_service.query.call_args assert kwargs['where'] == {"user_id": "u1"}