Skip to content

Commit 8b80b89

Browse files
authored
Merge pull request #65 from stenwire/whatsapp-intg-v1
test: mock GOOGLE_API_KEY in API and integration tests to satisfy con…
2 parents 0ad9370 + 0f93326 commit 8b80b89

3 files changed

Lines changed: 22 additions & 11 deletions

File tree

backend/tests/api/test_documents.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,10 @@ def test_api_chat_scoped(client, db_session):
7171
db_session.add(Business(user_id="u1", business_name="Test Biz"))
7272
db_session.commit()
7373

74-
# 3. Chat
75-
with patch("app.api.routes.run_conversation", return_value="Bot says hi") as mock_chat:
74+
# 3. Chat — patch API key so it reaches the mocked run_conversation
75+
with patch("app.api.routes.settings") as mock_settings, \
76+
patch("app.api.routes.run_conversation", return_value="Bot says hi") as mock_chat:
77+
mock_settings.GOOGLE_API_KEY = "test-key"
7678
response = client.post("/chat", json={"message": "hello"}, headers=headers)
7779

7880
assert response.status_code == 200

backend/tests/api/test_widget_chat.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@
1616

1717
pytestmark = pytest.mark.api
1818

19-
# Shared mock target
19+
# Shared mock targets
2020
RUN_CONVERSATION = "app.api.widget.run_conversation"
21+
SETTINGS = "app.api.widget.settings"
2122

2223

2324
@pytest.fixture
@@ -89,8 +90,10 @@ def test_404_for_unknown_widget(self, client):
8990
class TestInitGuestSession:
9091
"""Tests for POST /widgets/guest/session/init/{public_widget_id}."""
9192

93+
@patch(SETTINGS)
9294
@patch(RUN_CONVERSATION, new_callable=AsyncMock, return_value="Hello from AI")
93-
def test_init_session_with_first_message(self, mock_ai, client, db_session, widget_env):
95+
def test_init_session_with_first_message(self, mock_ai, mock_settings, client, db_session, widget_env):
96+
mock_settings.GOOGLE_API_KEY = "test-key"
9497
widget = widget_env["widget"]
9598
guest = GuestUserFactory(widget=widget, widget_id=widget.id)
9699
db_session.commit()
@@ -150,8 +153,10 @@ def test_init_404_unknown_guest(self, client, widget_env):
150153
class TestChatInSession:
151154
"""Tests for POST /widgets/chat/{public_widget_id}/session/{session_id}."""
152155

156+
@patch(SETTINGS)
153157
@patch(RUN_CONVERSATION, new_callable=AsyncMock, return_value="AI reply")
154-
def test_send_message(self, mock_ai, client, db_session, widget_env):
158+
def test_send_message(self, mock_ai, mock_settings, client, db_session, widget_env):
159+
mock_settings.GOOGLE_API_KEY = "test-key"
155160
widget = widget_env["widget"]
156161
guest = GuestUserFactory(widget=widget, widget_id=widget.id)
157162
session = ChatSessionFactory(guest=guest, guest_id=guest.id)

backend/tests/integration/test_rag_scoped.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,17 @@ def test_upload_document_scoped(db_session, mock_file_storage):
4141
def test_process_documents_scoped(db_session, mock_file_storage, mock_vector_db_service):
4242
user1 = "u1"
4343
user2 = "u2"
44-
44+
4545
# Setup DB with pending docs for both users
4646
doc1 = Document(user_id=user1, filename="doc1.txt", file_path="p1", status="pending")
4747
doc2 = Document(user_id=user2, filename="doc2.txt", file_path="p2", status="pending")
4848
db_session.add_all([doc1, doc2])
4949
db_session.commit()
50-
51-
# Mock file content
52-
with patch("builtins.open", new_callable=MagicMock) as mock_open:
50+
51+
# Mock file content and API key
52+
with patch("builtins.open", new_callable=MagicMock) as mock_open, \
53+
patch("app.services.rag_service.settings") as mock_settings:
54+
mock_settings.GOOGLE_API_KEY = "test-key"
5355
mock_open.return_value.__enter__.return_value.read.return_value = "content"
5456
with patch("os.path.exists", return_value=True):
5557
# Process for User 1
@@ -83,8 +85,10 @@ def test_list_documents_scoped(db_session):
8385
assert docs2[0]["filename"] == "b.txt"
8486

8587
def test_query_scoped(mock_vector_db_service):
86-
rag_service.query("check", "u1")
87-
88+
with patch("app.services.rag_service.settings") as mock_settings:
89+
mock_settings.GOOGLE_API_KEY = "test-key"
90+
rag_service.query("check", "u1")
91+
8892
mock_vector_db_service.query.assert_called_once()
8993
args, kwargs = mock_vector_db_service.query.call_args
9094
assert kwargs['where'] == {"user_id": "u1"}

0 commit comments

Comments
 (0)