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
6 changes: 4 additions & 2 deletions backend/tests/api/test_documents.py
Original file line number Diff line number Diff line change
Expand Up @@ -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, \
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟢 Looks good! Mocking the API key in tests is a good practice.

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
Expand Down
11 changes: 8 additions & 3 deletions backend/tests/api/test_widget_chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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)
Expand Down
16 changes: 10 additions & 6 deletions backend/tests/integration/test_rag_scoped.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"}
Loading