import sys import types import unittest from unittest.mock import MagicMock import httpx from fastapi import FastAPI from requests.exceptions import Timeout class RateLimitExceeded(Exception): pass def _mock_module(name, **attrs): module = types.ModuleType(name) for attr_name, value in attrs.items(): setattr(module, attr_name, value) sys.modules[name] = module return module cache_service = _mock_module( "services.cache_service", clear_all_caches=MagicMock(), clear_product_result_cache_for_slug=MagicMock(), ) document_service = _mock_module( "services.document_service", delete_document=MagicMock(), ensure_workspace_exists=MagicMock(return_value="workspace-123"), get_document_status=MagicMock(), queue_document_ingest=MagicMock(), validate_supported_mime=MagicMock(), ) onboarding_service = _mock_module( "services.onboarding_service", onboard_shopify_workspace=MagicMock(), ) query_service = _mock_module( "services.query_service", RateLimitExceeded=RateLimitExceeded, answer_query_request=MagicMock(), ) shopify_ingest_service = _mock_module( "services.shopify_ingest_service", ingest_shopify_workspace=MagicMock(), ) from api.errors import add_exception_handlers # noqa: E402 from api.routes import admin, documents, onboarding, query # noqa: E402 from core.exceptions import DocumentNotFoundError, WorkspaceNotFoundError # noqa: E402 from core.security import verify_api_key # noqa: E402 SHOP_SLUG = "replyflow-test" async def _allow_api_key(): return "test-api-key" def _build_test_client(authenticated=True): app = FastAPI() add_exception_handlers(app) app.include_router(query.router) app.include_router(onboarding.router) app.include_router(documents.router) app.include_router(admin.router) if authenticated: app.dependency_overrides[verify_api_key] = _allow_api_key # raise_app_exceptions=False so unhandled exceptions surface as the real # HTTP response our handlers produce, instead of propagating to the test. transport = httpx.ASGITransport(app=app, raise_app_exceptions=False) return httpx.AsyncClient(transport=transport, base_url="http://testserver") class ApiEndpointTests(unittest.IsolatedAsyncioTestCase): async def asyncSetUp(self): self.client = _build_test_client() for service in ( cache_service, document_service, onboarding_service, query_service, shopify_ingest_service, ): for value in vars(service).values(): if isinstance(value, MagicMock): value.reset_mock(return_value=True, side_effect=True) document_service.ensure_workspace_exists.return_value = "workspace-123" async def asyncTearDown(self): await self.client.aclose() async def test_health_returns_ok(self): response = await self.client.get("/health") self.assertEqual(response.status_code, 200) self.assertEqual(response.json(), {"status": "ok"}) async def test_query_returns_generated_answer(self): query_service.answer_query_request.return_value = "Yes, this is in stock." response = await self.client.post( "/query", json={ "query": "Is the hoodie available?", "channel": "instagram", "channel_identifier": SHOP_SLUG, "external_id": "comment-1", "commenter_handle": "customer", "post_caption": "New hoodie drop", }, ) self.assertEqual(response.status_code, 200) self.assertEqual( response.json(), { "answer": "Yes, this is in stock.", "query": "Is the hoodie available?", "route": None, }, ) query_service.answer_query_request.assert_called_once_with( query="Is the hoodie available?", channel="instagram", channel_identifier=SHOP_SLUG, external_id="comment-1", commenter_handle="customer", post_caption="New hoodie drop", ) async def test_query_maps_empty_query_to_bad_request(self): query_service.answer_query_request.side_effect = ValueError("Query cannot be empty") response = await self.client.post( "/query", json={ "query": " ", "channel": "instagram", "channel_identifier": SHOP_SLUG, }, ) self.assertEqual(response.status_code, 400) self.assertEqual(response.json()["detail"], "Query cannot be empty") async def test_query_maps_rate_limit_to_too_many_requests(self): query_service.answer_query_request.side_effect = RateLimitExceeded( "Too many requests" ) response = await self.client.post( "/query", json={ "query": "Do you ship internationally?", "channel": "instagram", "channel_identifier": SHOP_SLUG, }, ) self.assertEqual(response.status_code, 429) self.assertEqual(response.json()["detail"], "Too many requests") async def test_query_maps_missing_workspace_to_not_found(self): query_service.answer_query_request.side_effect = WorkspaceNotFoundError( f"No workspace found for instagram: {SHOP_SLUG}" ) response = await self.client.post( "/query", json={ "query": "Do you ship internationally?", "channel": "instagram", "channel_identifier": SHOP_SLUG, }, ) self.assertEqual(response.status_code, 404) self.assertEqual( response.json()["detail"], f"No workspace found for instagram: {SHOP_SLUG}", ) async def test_query_maps_unexpected_error_to_server_error(self): query_service.answer_query_request.side_effect = RuntimeError("model failed") response = await self.client.post( "/query", json={ "query": "Do you ship internationally?", "channel": "instagram", "channel_identifier": SHOP_SLUG, }, ) self.assertEqual(response.status_code, 500) self.assertEqual(response.json()["detail"], "Internal server error") async def test_ingest_runs_for_existing_shop_slug(self): shopify_ingest_service.ingest_shopify_workspace.return_value = { "products": 4, "policies": 2, } response = await self.client.post("/ingest", json={"brand_slug": SHOP_SLUG}) self.assertEqual(response.status_code, 200) self.assertEqual( response.json(), { "status": "ok", "brand_slug": SHOP_SLUG, "indexed": {"products": 4, "policies": 2}, }, ) cache_service.clear_product_result_cache_for_slug.assert_called_once_with(SHOP_SLUG) shopify_ingest_service.ingest_shopify_workspace.assert_called_once_with(SHOP_SLUG) async def test_ingest_rejects_blank_shop_slug(self): response = await self.client.post("/ingest", json={"brand_slug": " "}) self.assertEqual(response.status_code, 400) self.assertEqual(response.json()["detail"], "brand_slug is required") shopify_ingest_service.ingest_shopify_workspace.assert_not_called() async def test_ingest_maps_missing_shop_slug_to_not_found(self): cache_service.clear_product_result_cache_for_slug.side_effect = WorkspaceNotFoundError( f"No workspace found for slug: {SHOP_SLUG}" ) response = await self.client.post("/ingest", json={"brand_slug": SHOP_SLUG}) self.assertEqual(response.status_code, 404) self.assertEqual( response.json()["detail"], f"No workspace found for slug: {SHOP_SLUG}", ) shopify_ingest_service.ingest_shopify_workspace.assert_not_called() async def test_ingest_maps_shopify_timeout_to_gateway_timeout(self): shopify_ingest_service.ingest_shopify_workspace.side_effect = Timeout() response = await self.client.post("/ingest", json={"brand_slug": SHOP_SLUG}) self.assertEqual(response.status_code, 504) self.assertEqual(response.json()["detail"], "External service timed out") async def test_document_ingest_queues_supported_document(self): response = await self.client.post( "/documents/ingest", json={ "workspace_slug": SHOP_SLUG, "document_id": "shipping-policy", "source_url": "https://example.com/shipping.txt", "mime": "text/plain", "title": "Shipping Policy", }, ) self.assertEqual(response.status_code, 202) self.assertEqual(response.json(), {"status": "queued"}) document_service.validate_supported_mime.assert_called_once_with("text/plain") document_service.ensure_workspace_exists.assert_called_once_with(SHOP_SLUG) document_service.queue_document_ingest.assert_called_once() async def test_document_ingest_rejects_unsupported_mime(self): document_service.validate_supported_mime.side_effect = ValueError( "Unsupported mime type" ) response = await self.client.post( "/documents/ingest", json={ "workspace_slug": SHOP_SLUG, "document_id": "shipping-policy", "source_url": "https://example.com/shipping.docx", "mime": "application/vnd.openxmlformats-officedocument.wordprocessingml.document", "title": "Shipping Policy", }, ) self.assertEqual(response.status_code, 400) self.assertEqual(response.json()["detail"], "Unsupported mime type") document_service.ensure_workspace_exists.assert_not_called() document_service.queue_document_ingest.assert_not_called() async def test_document_ingest_maps_missing_workspace_to_not_found(self): document_service.ensure_workspace_exists.side_effect = WorkspaceNotFoundError( f"No workspace found for slug: {SHOP_SLUG}" ) response = await self.client.post( "/documents/ingest", json={ "workspace_slug": SHOP_SLUG, "document_id": "shipping-policy", "source_url": "https://example.com/shipping.txt", "mime": "text/plain", "title": "Shipping Policy", }, ) self.assertEqual(response.status_code, 404) self.assertEqual( response.json()["detail"], f"No workspace found for slug: {SHOP_SLUG}", ) document_service.queue_document_ingest.assert_not_called() async def test_document_status_returns_status_for_shop_slug(self): document_service.get_document_status.return_value = { "status": "ready", "error": None, "chunks": 3, } response = await self.client.get( "/documents/shipping-policy/status", params={"workspace_slug": SHOP_SLUG}, ) self.assertEqual(response.status_code, 200) self.assertEqual( response.json(), {"status": "ready", "error": None, "chunks": 3}, ) document_service.get_document_status.assert_called_once_with( "shipping-policy", SHOP_SLUG, ) async def test_document_status_maps_missing_document_to_not_found(self): document_service.get_document_status.side_effect = DocumentNotFoundError("Document not found") response = await self.client.get( "/documents/shipping-policy/status", params={"workspace_slug": SHOP_SLUG}, ) self.assertEqual(response.status_code, 404) self.assertEqual(response.json()["detail"], "Document not found") async def test_delete_document_returns_ok_for_shop_slug(self): response = await self.client.delete( "/documents/shipping-policy", params={"workspace_slug": SHOP_SLUG}, ) self.assertEqual(response.status_code, 200) self.assertEqual( response.json(), { "status": "ok", "message": "Document 'shipping-policy' deleted", }, ) document_service.delete_document.assert_called_once_with( "shipping-policy", SHOP_SLUG, ) async def test_delete_document_maps_missing_document_to_not_found(self): document_service.delete_document.side_effect = DocumentNotFoundError("Document not found") response = await self.client.delete( "/documents/shipping-policy", params={"workspace_slug": SHOP_SLUG}, ) self.assertEqual(response.status_code, 404) self.assertEqual(response.json()["detail"], "Document not found") async def test_admin_cache_clear_clears_caches(self): response = await self.client.post("/admin/cache-clear") self.assertEqual(response.status_code, 200) self.assertEqual( response.json(), {"status": "ok", "message": "Cache cleared"}, ) cache_service.clear_all_caches.assert_called_once_with() async def test_admin_cache_clear_requires_api_key(self): await self.client.aclose() self.client = _build_test_client(authenticated=False) response = await self.client.post("/admin/cache-clear") self.assertEqual(response.status_code, 401) self.assertEqual(response.json()["detail"], "Invalid or missing API key") if __name__ == "__main__": unittest.main()