github-actions[bot]
Sync from GitHub himanshu748/ContextFirewall@772f53ebabbcc821ded77782f1692b49b5e8bb9b
8b026b2 Download tests/test_request_limits.py from HIMANSHUKUMARJHA/contextfirewall: direct link, hf CLI and curl.
- Browser
- Download file 2.69 kB
-
https://huggingface.co/spaces/HIMANSHUKUMARJHA/contextfirewall/resolve/main/tests/test_request_limits.py
- Command line
-
hf download hf://spaces/HIMANSHUKUMARJHA/contextfirewall/tests/test_request_limits.py
-
curl -L -o test_request_limits.py https://huggingface.co/spaces/HIMANSHUKUMARJHA/contextfirewall/resolve/main/tests/test_request_limits.py
2.69 kB
| """Reject oversized requests before invoking graph, vector, or LLM work.""" | |
| from __future__ import annotations | |
| import asyncio | |
| from unittest.mock import AsyncMock | |
| import pytest | |
| from fastapi.testclient import TestClient | |
| from pydantic import ValidationError | |
| from app import main, mcp_server | |
| from app.models import ( | |
| AuditRequest, | |
| PackRequest, | |
| RecordedSession, | |
| RememberRequest, | |
| ) | |
| def test_recall_request_rejects_out_of_bounds_values(request_model, payload): | |
| with pytest.raises(ValidationError): | |
| request_model(**payload) | |
| def test_remember_request_rejects_out_of_bounds_values(payload): | |
| with pytest.raises(ValidationError): | |
| RememberRequest(**payload) | |
| def test_session_lists_have_explicit_caps(field, max_items): | |
| data = {"session_id": "test", "task": "test"} | |
| data[field] = ([{"kind": "decision", "content": "test"}] if field == "events" else [{}]) * (max_items + 1) | |
| with pytest.raises(ValidationError): | |
| RecordedSession(**data) | |
| def test_rest_recall_rejects_invalid_top_k_before_core(path): | |
| client = TestClient(main.app) | |
| result = client.post(path, json={"query": "query", "top_k": 51}) | |
| assert result.status_code == 422 | |
| assert "top_k" in str(result.json()) | |
| def test_rest_list_limits_are_bounded(path, limit): | |
| client = TestClient(main.app) | |
| result = client.get(path, params={"limit": limit}) | |
| assert result.status_code == 422 | |
| assert "limit" in str(result.json()) | |
| def test_mcp_pack_rejects_large_top_k_without_core_call(monkeypatch): | |
| core = AsyncMock() | |
| monkeypatch.setattr(mcp_server, "_build_pack", core) | |
| with pytest.raises(ValidationError): | |
| asyncio.run(mcp_server.get_trusted_context("query", top_k=51)) | |
| core.assert_not_awaited() | |
| def test_mcp_remember_rejects_invalid_kind_without_core_call(monkeypatch): | |
| core = AsyncMock() | |
| monkeypatch.setattr(mcp_server, "_remember_fact", core) | |
| with pytest.raises(ValidationError): | |
| asyncio.run(mcp_server.remember("text", kind="unknown")) | |
| core.assert_not_awaited() | |