Spaces:
Running
Running
File size: 6,452 Bytes
45b7965 | 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 | from __future__ import annotations
import sqlite3
import uuid
from pathlib import Path
from typing import Any
import httpx
from fastapi.testclient import TestClient
from app.config import Settings
from app.main import create_app
def memory_settings(database: Path, **overrides: Any) -> Settings:
values: dict[str, Any] = {
"openrouter_api_key": "test-key-not-a-secret",
"environment": "test",
"rate_limit_requests": 100,
"memory_enabled": True,
"memory_database_path": str(database),
"memory_session_pepper": "test-memory-pepper-with-more-than-32-characters",
}
values.update(overrides)
return Settings(**values)
def success_transport(captured: list[httpx.Request] | None = None) -> httpx.MockTransport:
async def handler(request: httpx.Request) -> httpx.Response:
if captured is not None:
captured.append(request)
body = httpx.Response(200, content=request.read()).json()
return httpx.Response(
200,
json={
"model": body["model"],
"choices": [{"message": {"content": "Memory-aware answer."}}],
},
)
return httpx.MockTransport(handler)
def test_explicit_memory_requires_consent_and_is_not_sent_upstream(tmp_path: Path) -> None:
captured: list[httpx.Request] = []
app = create_app(
memory_settings(tmp_path / "memory.sqlite3"),
transport=success_transport(captured),
)
session = str(uuid.uuid4())
headers = {"X-Fuugi-Session": session}
with TestClient(app) as client:
blocked = client.post(
"/api/fuugi", json={"message": "Merke dir: Ich mag kurze Antworten."}, headers=headers
)
consent = client.post(
"/api/fuugi/memory-consent", json={"enabled": True}, headers=headers
)
saved = client.post(
"/api/fuugi", json={"message": "Merke dir: Ich mag kurze Antworten."}, headers=headers
)
listed = client.get("/api/fuugi/memories", headers=headers)
assert blocked.json()["memory_event"]["status"] == "consent_required"
assert consent.json() == {"enabled": True, "memories": []}
assert saved.json()["memory_event"]["status"] == "saved"
assert listed.json()["memories"][0]["content"] == "Ich mag kurze Antworten."
assert captured == []
def test_memory_is_untrusted_context_and_raw_session_is_not_stored(tmp_path: Path) -> None:
database = tmp_path / "memory.sqlite3"
captured: list[httpx.Request] = []
app = create_app(memory_settings(database), transport=success_transport(captured))
session = str(uuid.uuid4())
headers = {"X-Fuugi-Session": session}
with TestClient(app) as client:
client.post("/api/fuugi/memory-consent", json={"enabled": True}, headers=headers)
client.post(
"/api/fuugi",
json={"message": "Merke dir: Mein Lieblingsformat ist WAV."},
headers=headers,
)
answer = client.post(
"/api/fuugi", json={"message": "Welches Format bevorzuge ich?"}, headers=headers
)
assert answer.status_code == 200
assert len(captured) == 1
upstream = httpx.Response(200, content=captured[0].read()).json()
serialized = str(upstream["messages"])
assert "Untrusted user-approved memory notes" in serialized
assert "Mein Lieblingsformat ist WAV." in serialized
assert session not in serialized
assert session.encode() not in database.read_bytes()
def test_memory_is_isolated_deletable_and_revocable(tmp_path: Path) -> None:
app = create_app(
memory_settings(tmp_path / "memory.sqlite3"),
transport=success_transport(),
)
first = {"X-Fuugi-Session": str(uuid.uuid4())}
second = {"X-Fuugi-Session": str(uuid.uuid4())}
with TestClient(app) as client:
client.post("/api/fuugi/memory-consent", json={"enabled": True}, headers=first)
client.post("/api/fuugi/memory-consent", json={"enabled": True}, headers=second)
saved = client.post(
"/api/fuugi", json={"message": "Merke dir: Nur Sitzung eins."}, headers=first
).json()
assert client.get("/api/fuugi/memories", headers=second).json()["memories"] == []
memory_id = saved["memory_event"]["id"]
assert client.delete(f"/api/fuugi/memories/{memory_id}", headers=second).status_code == 404
assert client.delete(f"/api/fuugi/memories/{memory_id}", headers=first).status_code == 204
client.post("/api/fuugi/memory-consent", json={"enabled": False}, headers=first)
revoked = client.get("/api/fuugi/memories", headers=first).json()
assert revoked == {"enabled": False, "memories": []}
def test_sensitive_memory_is_rejected_and_normal_chat_is_not_persisted(tmp_path: Path) -> None:
database = tmp_path / "memory.sqlite3"
app = create_app(memory_settings(database), transport=success_transport())
headers = {"X-Fuugi-Session": str(uuid.uuid4())}
with TestClient(app) as client:
client.post("/api/fuugi/memory-consent", json={"enabled": True}, headers=headers)
rejected = client.post(
"/api/fuugi", json={"message": "Merke dir: Mein Passwort ist geheim."}, headers=headers
)
normal = client.post("/api/fuugi", json={"message": "Ein normaler Chat."}, headers=headers)
assert rejected.json()["memory_event"]["status"] == "rejected"
assert normal.status_code == 200
with sqlite3.connect(database) as connection:
assert connection.execute("SELECT COUNT(*) FROM memory_nodes").fetchone()[0] == 0
def test_memory_cors_allows_session_header_and_delete(tmp_path: Path) -> None:
origin = "http://192.168.2.149:8080"
app = create_app(
memory_settings(
tmp_path / "memory.sqlite3",
environment="development",
localhost_origins=(origin,),
),
transport=success_transport(),
)
headers = {
"Origin": origin,
"Access-Control-Request-Method": "DELETE",
"Access-Control-Request-Headers": "content-type,x-fuugi-session",
}
with TestClient(app) as client:
preflight = client.options("/api/fuugi/memories", headers=headers)
assert preflight.status_code == 200
assert preflight.headers["access-control-allow-origin"] == origin
assert "X-Fuugi-Session" in preflight.headers["access-control-allow-headers"]
|