File size: 2,497 Bytes
584b3d1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Return honest, non-disclosing HTTP errors for backend write and reset failures."""
from __future__ import annotations

import logging
import sys
from types import SimpleNamespace

import pytest
from fastapi.testclient import TestClient

from app import main


@pytest.fixture
def client(monkeypatch):
    async def allow(_authorization):
        return SimpleNamespace(can_write=True, namespace="tenant", allow_demo_write=False)

    monkeypatch.setattr(main, "resolve_identity", allow)
    monkeypatch.setattr(main, "log_activity", lambda *_args, **_kwargs: None)
    return TestClient(main.app)


@pytest.mark.parametrize(
    ("endpoint", "body", "function"),
    [
        ("/remember", {"text": "safe placeholder"}, "remember_fact"),
        ("/ingest", {"session": {"session_id": "s1", "task": "test"}, "cognify": False},
         "ingest_session"),
    ],
)
def test_backend_write_exception_is_500_without_private_details(
    client, monkeypatch, caplog, endpoint, body, function
):
    async def fail(*_args, **_kwargs):
        raise RuntimeError("private-connection-string")

    monkeypatch.setattr(main, function, fail)
    with caplog.at_level(logging.ERROR, logger=main.__name__):
        response = client.post(endpoint, json=body)

    assert response.status_code == 500
    assert "private-connection-string" not in response.text
    assert "private-connection-string" not in caplog.text
    assert "RuntimeError" in caplog.text


def test_admin_reset_prune_failure_is_not_reported_as_success(
    client, monkeypatch, caplog
):
    async def fail_prune():
        raise RuntimeError("private-db-credential")

    async def prune_system(**_kwargs):
        raise AssertionError("Should not prune system after data prune fails")

    monkeypatch.setenv("CF_ADMIN_TOKEN", "synthetic-admin-token")
    monkeypatch.setattr(main, "configure_cognee", lambda: None)
    monkeypatch.setitem(
        sys.modules, "cognee",
        SimpleNamespace(prune=SimpleNamespace(
            prune_data=fail_prune, prune_system=prune_system
        )),
    )
    with caplog.at_level(logging.ERROR, logger=main.__name__):
        response = client.post(
            "/reset", headers={"X-Admin-Token": "synthetic-admin-token"}
        )

    assert response.status_code == 500
    assert response.json()["detail"] == "Unable to reset memory."
    assert "private-db-credential" not in response.text
    assert "private-db-credential" not in caplog.text
    assert "RuntimeError" in caplog.text