File size: 14,065 Bytes
7c61e48
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
97f65d7
7c61e48
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a9d7074
7c61e48
a9d7074
7c61e48
 
 
 
 
 
 
 
 
 
 
 
a9d7074
7c61e48
 
 
 
 
 
a9d7074
 
 
7c61e48
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a9d7074
7c61e48
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a9d7074
7c61e48
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
97f65d7
7c61e48
 
 
 
 
 
 
 
 
 
a9d7074
7c61e48
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a9d7074
7c61e48
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a9d7074
7c61e48
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a9d7074
7c61e48
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a9d7074
7c61e48
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
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()