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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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
"""Comprehensive E2E tests for REST API server authentication.

Tests the actual server/main.py app through FastAPI's TestClient (full ASGI
round-trip) covering:
  - Auth disabled mode (ADMIN_API_KEY unset)
  - Auth enabled mode (ADMIN_API_KEY set)
  - Edge cases: empty keys, near-miss keys, timing-safe comparison, header
    casing, response headers, startup logging, and full CRUD flows through auth.
"""

import importlib
import logging
import os
from unittest.mock import MagicMock, patch

import pytest

pytest.importorskip("fastapi", reason="fastapi not installed")

from fastapi.testclient import TestClient


# ---------------------------------------------------------------------------
# Fixtures
# ---------------------------------------------------------------------------

@pytest.fixture
def _mock_memory():
    """Patch Memory.from_config so the server imports without a real backend."""
    mock_instance = MagicMock()
    # Set up return values so CRUD endpoints return realistic responses
    mock_instance.get.return_value = {"id": "mem-1", "memory": "test memory", "user_id": "alice"}
    mock_instance.get_all.return_value = [
        {"id": "mem-1", "memory": "test memory", "user_id": "alice"},
    ]
    mock_instance.add.return_value = {"results": [{"id": "mem-1", "event": "ADD", "memory": "test"}]}
    mock_instance.search.return_value = [{"id": "mem-1", "memory": "test", "score": 0.9}]
    mock_instance.update.return_value = {"message": "Memory updated"}
    mock_instance.history.return_value = [{"id": "mem-1", "old_memory": "a", "new_memory": "b"}]
    mock_instance.delete.return_value = None
    mock_instance.delete_all.return_value = {"message": "Memories deleted successfully!"}
    mock_instance.reset.return_value = None

    with patch.dict(os.environ, {"OPENAI_API_KEY": "fake-key"}):
        with patch("mem0.Memory.from_config", return_value=mock_instance):
            yield mock_instance


def _load_app(env_overrides: dict):
    """Reload server/main.py with the given environment and return the FastAPI app."""
    import server.main as server_main

    with patch.dict(os.environ, env_overrides, clear=False):
        importlib.reload(server_main)
    return server_main.app


# ---------------------------------------------------------------------------
# Auth disabled (ADMIN_API_KEY not set)
# ---------------------------------------------------------------------------

class TestAuthDisabled:
    """All endpoints should be freely accessible when ADMIN_API_KEY is empty."""

    @pytest.fixture(autouse=True)
    def _setup(self, _mock_memory):
        self.app = _load_app({"ADMIN_API_KEY": ""})
        self.client = TestClient(self.app)
        self.mock = _mock_memory

    def test_root_redirects_to_docs(self):
        resp = self.client.get("/", follow_redirects=False)
        assert resp.status_code == 307
        assert "/docs" in resp.headers["location"]

    def test_get_memory_without_key(self):
        resp = self.client.get("/memories/mem-1")
        assert resp.status_code == 200
        assert resp.json()["id"] == "mem-1"

    def test_get_all_memories_without_key(self):
        resp = self.client.get("/memories", params={"user_id": "alice"})
        assert resp.status_code == 200

    def test_create_memory_without_key(self):
        resp = self.client.post("/memories", json={
            "messages": [{"role": "user", "content": "I like pizza"}],
            "user_id": "alice",
        })
        assert resp.status_code == 200

    def test_search_without_key(self):
        resp = self.client.post("/search", json={"query": "pizza", "user_id": "alice"})
        assert resp.status_code == 200

    def test_update_memory_without_key(self):
        resp = self.client.put("/memories/mem-1", json={"text": "updated"})
        assert resp.status_code == 200

    def test_history_without_key(self):
        resp = self.client.get("/memories/mem-1/history")
        assert resp.status_code == 200

    def test_delete_memory_without_key(self):
        resp = self.client.delete("/memories/mem-1")
        assert resp.status_code == 200

    def test_delete_all_without_key(self):
        resp = self.client.delete("/memories", params={"user_id": "alice"})
        assert resp.status_code == 200

    def test_reset_without_key(self):
        resp = self.client.post("/reset")
        assert resp.status_code == 200

    def test_configure_without_key(self):
        self.mock.from_config = MagicMock()
        resp = self.client.post("/configure", json={"version": "v1.1"})
        assert resp.status_code == 200

    def test_supplying_key_still_works_when_auth_disabled(self):
        """A client that sends X-API-Key should not be penalized when auth is off."""
        resp = self.client.get(
            "/memories/mem-1", headers={"X-API-Key": "some-random-key"}
        )
        assert resp.status_code == 200

    @pytest.mark.parametrize(
        "method,path",
        [
            ("POST", "/configure"),
            ("POST", "/memories"),
            ("GET", "/memories"),
            ("GET", "/memories/test-id"),
            ("POST", "/search"),
            ("PUT", "/memories/test-id"),
            ("GET", "/memories/test-id/history"),
            ("DELETE", "/memories/test-id"),
            ("DELETE", "/memories"),
            ("POST", "/reset"),
        ],
    )
    def test_no_endpoint_returns_401_when_auth_disabled(self, method, path):
        resp = self.client.request(method, path)
        assert resp.status_code != 401, f"{method} {path} should not require auth"


# ---------------------------------------------------------------------------
# Auth enabled (ADMIN_API_KEY set)
# ---------------------------------------------------------------------------

class TestAuthEnabled:
    """All protected endpoints must enforce the API key."""

    API_KEY = "test-secret-key-12345"

    @pytest.fixture(autouse=True)
    def _setup(self, _mock_memory):
        self.app = _load_app({"ADMIN_API_KEY": self.API_KEY})
        self.client = TestClient(self.app)
        self.mock = _mock_memory

    # --- Rejection cases ---

    def test_missing_key_returns_401(self):
        resp = self.client.get("/memories/mem-1")
        assert resp.status_code == 401

    def test_missing_key_detail_mentions_header(self):
        resp = self.client.get("/memories/mem-1")
        assert "X-API-Key" in resp.json()["detail"]

    def test_wrong_key_returns_401(self):
        resp = self.client.get("/memories/mem-1", headers={"X-API-Key": "wrong"})
        assert resp.status_code == 401

    def test_wrong_key_detail_says_invalid(self):
        resp = self.client.get("/memories/mem-1", headers={"X-API-Key": "wrong"})
        assert "Invalid" in resp.json()["detail"]

    def test_empty_string_key_returns_401(self):
        resp = self.client.get("/memories/mem-1", headers={"X-API-Key": ""})
        assert resp.status_code == 401

    def test_401_includes_www_authenticate_header(self):
        resp = self.client.get("/memories/mem-1")
        assert resp.headers.get("www-authenticate") == "ApiKey"

    def test_near_miss_key_rejected(self):
        """Key that differs by one character should be rejected."""
        near_miss = self.API_KEY[:-1] + ("6" if self.API_KEY[-1] != "6" else "7")
        resp = self.client.get("/memories/mem-1", headers={"X-API-Key": near_miss})
        assert resp.status_code == 401

    def test_key_with_extra_whitespace_rejected(self):
        resp = self.client.get("/memories/mem-1", headers={"X-API-Key": f" {self.API_KEY} "})
        assert resp.status_code == 401

    def test_key_prefix_rejected(self):
        resp = self.client.get("/memories/mem-1", headers={"X-API-Key": self.API_KEY[:5]})
        assert resp.status_code == 401

    def test_key_with_different_case_rejected(self):
        resp = self.client.get("/memories/mem-1", headers={"X-API-Key": self.API_KEY.upper()})
        assert resp.status_code == 401

    @pytest.mark.parametrize(
        "method,path",
        [
            ("POST", "/configure"),
            ("POST", "/memories"),
            ("GET", "/memories"),
            ("GET", "/memories/test-id"),
            ("POST", "/search"),
            ("PUT", "/memories/test-id"),
            ("GET", "/memories/test-id/history"),
            ("DELETE", "/memories/test-id"),
            ("DELETE", "/memories"),
            ("POST", "/reset"),
        ],
    )
    def test_all_endpoints_reject_without_key(self, method, path):
        resp = self.client.request(method, path)
        assert resp.status_code == 401, f"{method} {path} should require auth"

    @pytest.mark.parametrize(
        "method,path",
        [
            ("POST", "/configure"),
            ("POST", "/memories"),
            ("GET", "/memories"),
            ("GET", "/memories/test-id"),
            ("POST", "/search"),
            ("PUT", "/memories/test-id"),
            ("GET", "/memories/test-id/history"),
            ("DELETE", "/memories/test-id"),
            ("DELETE", "/memories"),
            ("POST", "/reset"),
        ],
    )
    def test_all_endpoints_reject_wrong_key(self, method, path):
        resp = self.client.request(method, path, headers={"X-API-Key": "wrong-key"})
        assert resp.status_code == 401, f"{method} {path} should reject wrong key"

    # --- Acceptance cases ---

    def test_root_does_not_require_key(self):
        resp = self.client.get("/", follow_redirects=False)
        assert resp.status_code == 307

    def _authed(self, method, path, **kwargs):
        headers = kwargs.pop("headers", {})
        headers["X-API-Key"] = self.API_KEY
        return self.client.request(method, path, headers=headers, **kwargs)

    def test_get_memory_with_key(self):
        resp = self._authed("GET", "/memories/mem-1")
        assert resp.status_code == 200
        assert resp.json()["id"] == "mem-1"

    def test_get_all_memories_with_key(self):
        resp = self._authed("GET", "/memories", params={"user_id": "alice"})
        assert resp.status_code == 200

    def test_create_memory_with_key(self):
        resp = self._authed("POST", "/memories", json={
            "messages": [{"role": "user", "content": "I like pizza"}],
            "user_id": "alice",
        })
        assert resp.status_code == 200
        data = resp.json()
        assert "results" in data

    def test_search_with_key(self):
        resp = self._authed("POST", "/search", json={"query": "pizza", "user_id": "alice"})
        assert resp.status_code == 200

    def test_update_memory_with_key(self):
        resp = self._authed("PUT", "/memories/mem-1", json={"text": "updated"})
        assert resp.status_code == 200

    def test_history_with_key(self):
        resp = self._authed("GET", "/memories/mem-1/history")
        assert resp.status_code == 200

    def test_delete_memory_with_key(self):
        resp = self._authed("DELETE", "/memories/mem-1")
        assert resp.status_code == 200

    def test_delete_all_with_key(self):
        resp = self._authed("DELETE", "/memories", params={"user_id": "alice"})
        assert resp.status_code == 200

    def test_reset_with_key(self):
        resp = self._authed("POST", "/reset")
        assert resp.status_code == 200

    def test_configure_with_key(self):
        resp = self._authed("POST", "/configure", json={"version": "v1.1"})
        assert resp.status_code == 200


# ---------------------------------------------------------------------------
# Full CRUD flow through auth
# ---------------------------------------------------------------------------

class TestAuthenticatedCRUDFlow:
    """Verify a complete create → read → search → update → history → delete
    cycle works end-to-end through the auth layer."""

    API_KEY = "flow-test-key-99"

    @pytest.fixture(autouse=True)
    def _setup(self, _mock_memory):
        self.app = _load_app({"ADMIN_API_KEY": self.API_KEY})
        self.client = TestClient(self.app)
        self.mock = _mock_memory

    def _authed(self, method, path, **kwargs):
        headers = kwargs.pop("headers", {})
        headers["X-API-Key"] = self.API_KEY
        return self.client.request(method, path, headers=headers, **kwargs)

    def test_full_crud_cycle(self):
        # 1. Create
        resp = self._authed("POST", "/memories", json={
            "messages": [{"role": "user", "content": "I love fresh vegetable pizza"}],
            "user_id": "alice",
        })
        assert resp.status_code == 200
        self.mock.add.assert_called_once()

        # 2. Read single
        resp = self._authed("GET", "/memories/mem-1")
        assert resp.status_code == 200
        self.mock.get.assert_called_once_with("mem-1")

        # 3. Read all
        resp = self._authed("GET", "/memories", params={"user_id": "alice"})
        assert resp.status_code == 200
        self.mock.get_all.assert_called_once_with(user_id="alice")

        # 4. Search
        resp = self._authed("POST", "/search", json={"query": "pizza", "user_id": "alice"})
        assert resp.status_code == 200
        self.mock.search.assert_called_once()

        # 5. Update
        resp = self._authed("PUT", "/memories/mem-1", json={"text": "updated content"})
        assert resp.status_code == 200
        self.mock.update.assert_called_once()

        # 6. History
        resp = self._authed("GET", "/memories/mem-1/history")
        assert resp.status_code == 200
        self.mock.history.assert_called_once_with(memory_id="mem-1")

        # 7. Delete single
        resp = self._authed("DELETE", "/memories/mem-1")
        assert resp.status_code == 200
        self.mock.delete.assert_called_once_with(memory_id="mem-1")

        # 8. Delete all
        resp = self._authed("DELETE", "/memories", params={"user_id": "alice"})
        assert resp.status_code == 200
        self.mock.delete_all.assert_called_once()

    def test_crud_flow_blocked_without_auth(self):
        """Same flow should fail at every step without the key."""
        endpoints = [
            ("POST", "/memories", {"json": {
                "messages": [{"role": "user", "content": "test"}], "user_id": "alice"
            }}),
            ("GET", "/memories/mem-1", {}),
            ("GET", "/memories", {"params": {"user_id": "alice"}}),
            ("POST", "/search", {"json": {"query": "pizza", "user_id": "alice"}}),
            ("PUT", "/memories/mem-1", {"json": {"data": "x"}}),
            ("GET", "/memories/mem-1/history", {}),
            ("DELETE", "/memories/mem-1", {}),
            ("DELETE", "/memories", {"params": {"user_id": "alice"}}),
            ("POST", "/reset", {}),
        ]
        for method, path, kwargs in endpoints:
            resp = self.client.request(method, path, **kwargs)
            assert resp.status_code == 401, f"Unauthenticated {method} {path} should be 401"
            # Verify the mock was NOT called (auth blocked before reaching handler)
        self.mock.add.assert_not_called()
        self.mock.get.assert_not_called()
        self.mock.search.assert_not_called()
        self.mock.update.assert_not_called()
        self.mock.history.assert_not_called()
        self.mock.delete.assert_not_called()
        self.mock.delete_all.assert_not_called()
        self.mock.reset.assert_not_called()


# ---------------------------------------------------------------------------
# Edge cases
# ---------------------------------------------------------------------------

class TestAuthEdgeCases:
    """Boundary conditions and unusual inputs."""

    @pytest.fixture(autouse=True)
    def _setup(self, _mock_memory):
        self.mock = _mock_memory

    def test_very_long_api_key(self):
        """Server should handle a very long key without crashing."""
        long_key = "k" * 4096
        app = _load_app({"ADMIN_API_KEY": long_key})
        client = TestClient(app)
        resp = client.get("/memories/mem-1", headers={"X-API-Key": long_key})
        assert resp.status_code == 200

    def test_special_characters_in_api_key(self):
        """Keys with special ASCII characters should work."""
        special_key = "sk-!@#$%^&*()_+-=[]{}|;:',.<>?/~`"
        app = _load_app({"ADMIN_API_KEY": special_key})
        client = TestClient(app)

        resp = client.get("/memories/mem-1", headers={"X-API-Key": special_key})
        assert resp.status_code == 200

        resp = client.get("/memories/mem-1", headers={"X-API-Key": "wrong"})
        assert resp.status_code == 401

    def test_key_env_var_not_present_at_all(self):
        """When the env var is completely absent, auth should be disabled."""
        import server.main as server_main
        env = os.environ.copy()
        env.pop("ADMIN_API_KEY", None)
        with patch.dict(os.environ, env, clear=True):
            importlib.reload(server_main)
        client = TestClient(server_main.app)
        resp = client.get("/memories/mem-1")
        assert resp.status_code != 401

    def test_switching_from_enabled_to_disabled(self):
        """Simulates a server restart with auth toggled off."""
        # First: auth enabled
        app1 = _load_app({"ADMIN_API_KEY": "secret"})
        c1 = TestClient(app1)
        assert c1.get("/memories/mem-1").status_code == 401

        # Then: auth disabled
        app2 = _load_app({"ADMIN_API_KEY": ""})
        c2 = TestClient(app2)
        assert c2.get("/memories/mem-1").status_code != 401

    def test_openapi_schema_accessible_without_key(self):
        """The /docs and /openapi.json endpoints should always be reachable."""
        app = _load_app({"ADMIN_API_KEY": "secret"})
        client = TestClient(app)

        resp = client.get("/openapi.json")
        assert resp.status_code == 200
        schema = resp.json()
        assert "paths" in schema

        resp = client.get("/docs")
        assert resp.status_code == 200

    def test_openapi_schema_documents_auth(self):
        """The OpenAPI schema should mention authentication."""
        app = _load_app({"ADMIN_API_KEY": "secret"})
        client = TestClient(app)
        schema = client.get("/openapi.json").json()
        assert "Authentication" in schema.get("info", {}).get("description", "")


# ---------------------------------------------------------------------------
# Startup logging
# ---------------------------------------------------------------------------

class TestStartupLogging:
    """Verify the server emits the correct log messages at import time."""

    @pytest.fixture(autouse=True)
    def _setup(self, _mock_memory):
        pass

    def test_warning_when_auth_disabled(self, caplog):
        with caplog.at_level(logging.WARNING):
            _load_app({"ADMIN_API_KEY": ""})
        assert any("UNSECURED" in r.message for r in caplog.records)

    def test_info_when_auth_enabled(self, caplog):
        with caplog.at_level(logging.INFO):
            _load_app({"ADMIN_API_KEY": "a-long-enough-secret-key"})
        assert any("authentication enabled" in r.message for r in caplog.records)

    def test_warning_when_key_too_short(self, caplog):
        with caplog.at_level(logging.WARNING):
            _load_app({"ADMIN_API_KEY": "short"})
        assert any("shorter than" in r.message for r in caplog.records)