askrobots / dbbasic-object-server
Public
Runs live, versioned Python application objects on one VM, with a suite of apps: projects, notes, tasks, files and more.
danq9
signup: the person chooses the password and name on the confirm page
83c7aa6
2 weeks, 5 days ago
History
test_object_reader_endpoint.py · 6.2 KB · 187 lines
| 1 | """Tests for the /api/read endpoint and the read_page MCP tool. |
| 2 | |
| 3 | Mirrors tests/test_object_tts.py's gating tests (flag off -> 403, no |
| 4 | session -> 401) and tests/test_object_mcp.py's session-token-through-MCP |
| 5 | pattern for the SESSION_ADMIN_GATES_ENV case. object_reader.read_page |
| 6 | itself is unit-tested in tests/test_object_reader.py -- these tests |
| 7 | monkeypatch it out so the endpoint/tool wiring is what's under test here. |
| 8 | """ |
| 9 | |
| 10 | import json |
| 11 | |
| 12 | import object_mcp |
| 13 | import object_reader |
| 14 | import object_server |
| 15 | |
| 16 | from test_object_mcp import call_tool |
| 17 | from test_object_server import ( |
| 18 | auth_headers, |
| 19 | create_identity_session, |
| 20 | enable_admin_token, |
| 21 | request, |
| 22 | ) |
| 23 | |
| 24 | |
| 25 | def reader_env(tmp_path, monkeypatch): |
| 26 | monkeypatch.setenv(object_server.DATA_DIR_ENV, str(tmp_path)) |
| 27 | monkeypatch.setenv(object_server.READER_ENABLED_ENV, "true") |
| 28 | enable_admin_token(monkeypatch) |
| 29 | |
| 30 | |
| 31 | def signed_in_bearer(): |
| 32 | token, _ = create_identity_session({"user_id": "dan"}) |
| 33 | return [("authorization", f"Bearer {token}")] |
| 34 | |
| 35 | |
| 36 | def test_read_endpoint_requires_flag_and_session(tmp_path, monkeypatch): |
| 37 | monkeypatch.setenv(object_server.DATA_DIR_ENV, str(tmp_path)) |
| 38 | enable_admin_token(monkeypatch) |
| 39 | |
| 40 | status, _, disabled = request( |
| 41 | "/api/read", method="POST", body=json.dumps({"url": "https://example.com"}).encode() |
| 42 | ) |
| 43 | assert status == 403 and "disabled" in disabled["error"] |
| 44 | |
| 45 | monkeypatch.setenv(object_server.READER_ENABLED_ENV, "true") |
| 46 | status, _, anonymous = request( |
| 47 | "/api/read", method="POST", body=json.dumps({"url": "https://example.com"}).encode() |
| 48 | ) |
| 49 | assert status == 401 |
| 50 | |
| 51 | |
| 52 | def test_read_endpoint_requires_url(tmp_path, monkeypatch): |
| 53 | reader_env(tmp_path, monkeypatch) |
| 54 | bearer = signed_in_bearer() |
| 55 | |
| 56 | status, _, body = request("/api/read", method="POST", body=json.dumps({}).encode(), headers=bearer) |
| 57 | assert status == 400 |
| 58 | assert "url" in body["error"] |
| 59 | |
| 60 | |
| 61 | def test_read_endpoint_maps_reader_error_to_502(tmp_path, monkeypatch): |
| 62 | reader_env(tmp_path, monkeypatch) |
| 63 | bearer = signed_in_bearer() |
| 64 | |
| 65 | def fake_read_page(url, *, timeout=10, max_bytes=2_000_000): |
| 66 | raise object_reader.ReaderError("Refusing to fetch 'internal.example': resolves to internal address 10.0.0.1") |
| 67 | |
| 68 | monkeypatch.setattr(object_server.object_reader, "read_page", fake_read_page) |
| 69 | |
| 70 | status, _, body = request( |
| 71 | "/api/read", |
| 72 | method="POST", |
| 73 | body=json.dumps({"url": "http://internal.example/"}).encode(), |
| 74 | headers=bearer, |
| 75 | ) |
| 76 | assert status == 502 |
| 77 | assert "internal address" in body["error"] |
| 78 | |
| 79 | |
| 80 | def test_read_endpoint_happy_path_returns_structured_result(tmp_path, monkeypatch): |
| 81 | reader_env(tmp_path, monkeypatch) |
| 82 | bearer = signed_in_bearer() |
| 83 | |
| 84 | fake_result = { |
| 85 | "title": "Example", |
| 86 | "text": "Hello there.", |
| 87 | "links": [{"n": 1, "label": "About", "href": "https://example.com/about"}], |
| 88 | "final_url": "https://example.com/", |
| 89 | "truncated": False, |
| 90 | } |
| 91 | |
| 92 | def fake_read_page(url, *, timeout=10, max_bytes=2_000_000): |
| 93 | assert url == "https://example.com/" |
| 94 | return dict(fake_result) |
| 95 | |
| 96 | monkeypatch.setattr(object_server.object_reader, "read_page", fake_read_page) |
| 97 | |
| 98 | status, _, body = request( |
| 99 | "/api/read", |
| 100 | method="POST", |
| 101 | body=json.dumps({"url": "https://example.com/"}).encode(), |
| 102 | headers=bearer, |
| 103 | ) |
| 104 | assert status == 200 |
| 105 | assert body["status"] == "ok" |
| 106 | assert body["title"] == "Example" |
| 107 | assert body["links"] == fake_result["links"] |
| 108 | |
| 109 | |
| 110 | def test_mcp_read_page_tool_is_in_the_catalog(): |
| 111 | names = [tool["name"] for tool in object_mcp.TOOLS] |
| 112 | assert "read_page" in names |
| 113 | |
| 114 | |
| 115 | def test_read_page_tool_route_maps_to_api_read(): |
| 116 | method, path, query_string, body = object_mcp.tool_route("read_page", {"url": "https://example.com"}) |
| 117 | assert (method, path, query_string) == ("POST", "/api/read", "") |
| 118 | assert json.loads(body) == {"url": "https://example.com"} |
| 119 | |
| 120 | |
| 121 | def test_read_page_tool_route_requires_url(): |
| 122 | import pytest |
| 123 | |
| 124 | with pytest.raises(ValueError, match="url"): |
| 125 | object_mcp.tool_route("read_page", {}) |
| 126 | |
| 127 | |
| 128 | def test_mcp_read_page_tool_is_gated_by_the_flag(tmp_path, monkeypatch): |
| 129 | """Flag off: the endpoint the tool routes to refuses, and that refusal |
| 130 | comes back through MCP as an isError result -- the tool isn't silently |
| 131 | dropped from the catalog, it just fails closed like every other gated |
| 132 | surface here.""" |
| 133 | monkeypatch.setenv(object_server.DATA_DIR_ENV, str(tmp_path)) |
| 134 | monkeypatch.setenv(object_server.SESSION_ADMIN_GATES_ENV, "true") |
| 135 | enable_admin_token(monkeypatch) |
| 136 | request( |
| 137 | "/identity/users", |
| 138 | method="POST", |
| 139 | body=json.dumps({"user_id": "agent-claude", "roles": ["admin"]}).encode(), |
| 140 | headers=auth_headers(), |
| 141 | ) |
| 142 | token, _ = create_identity_session({"user_id": "agent-claude", "label": "mcp claude-code"}) |
| 143 | |
| 144 | is_error, body = call_tool( |
| 145 | "read_page", |
| 146 | {"url": "https://example.com"}, |
| 147 | headers=[("authorization", f"Bearer {token}")], |
| 148 | ) |
| 149 | |
| 150 | assert is_error |
| 151 | assert body["http_status"] == 403 |
| 152 | assert "disabled" in body["response"]["error"] |
| 153 | |
| 154 | |
| 155 | def test_mcp_read_page_tool_happy_path_through_session_token(tmp_path, monkeypatch): |
| 156 | monkeypatch.setenv(object_server.DATA_DIR_ENV, str(tmp_path)) |
| 157 | monkeypatch.setenv(object_server.SESSION_ADMIN_GATES_ENV, "true") |
| 158 | monkeypatch.setenv(object_server.READER_ENABLED_ENV, "true") |
| 159 | enable_admin_token(monkeypatch) |
| 160 | request( |
| 161 | "/identity/users", |
| 162 | method="POST", |
| 163 | body=json.dumps({"user_id": "agent-claude", "roles": ["admin"]}).encode(), |
| 164 | headers=auth_headers(), |
| 165 | ) |
| 166 | token, _ = create_identity_session({"user_id": "agent-claude", "label": "mcp claude-code"}) |
| 167 | |
| 168 | def fake_read_page(url, *, timeout=10, max_bytes=2_000_000): |
| 169 | return { |
| 170 | "title": "Example", |
| 171 | "text": "Body text.", |
| 172 | "links": [], |
| 173 | "final_url": url, |
| 174 | "truncated": False, |
| 175 | } |
| 176 | |
| 177 | monkeypatch.setattr(object_server.object_reader, "read_page", fake_read_page) |
| 178 | |
| 179 | is_error, body = call_tool( |
| 180 | "read_page", |
| 181 | {"url": "https://example.com/"}, |
| 182 | headers=[("authorization", f"Bearer {token}")], |
| 183 | ) |
| 184 | |
| 185 | assert not is_error |
| 186 | assert body["http_status"] == 200 |
| 187 | assert body["response"]["title"] == "Example" |