Runs live, versioned Python application objects on one VM, with a suite of apps: projects, notes, tasks, files and more.

0 downloads

dbbasic-object-server/tests/test_object_feed.py
Download zip
Clone
Clone with git
git clone https://gitstable.com/git/askrobots/dbbasic-object-server.git
HTTPS
https://gitstable.com/git/askrobots/dbbasic-object-server.git
SSH
ssh://git@gitstable.com:2222/askrobots/dbbasic-object-server.git

Cloning needs no sign-in. Pushing over HTTPS asks for your username and an access token. SSH needs your key under SSH keys.

danq9 signup: the person chooses the password and name on the confirm page 83c7aa6 2 weeks, 5 days ago History
test_object_feed.py · 9.9 KB · 287 lines
1"""Tests for 64 (Feed): GET /api/feed, its blocks.feed schema discovery,
2and the get_feed MCP verb.
3
4The point of this suite is the privacy invariant the spec (plan/
5vocabulary/64-feed-spec.md, Permissions Posture) states up front: the feed
6composes two EXISTING permission-gated /collections/{c}/records reads (58)
7through _internal_request, forwarding the viewer's own credentials --
8never a direct read of the record layer. So a followed account's PRIVATE
9content can never enter the feed; following only changes which already-
10PUBLIC rows get selected in. Tests use real minted sessions (not trusted
11headers) so the Authorization-header forwarding in object_server._handle_
12feed is actually exercised end-to-end, not just the outer request's own
13identity resolution.
14"""
15
16import json
17
18import object_server
19
20from test_object_server import (
21 create_identity_session,
22 enable_admin_token,
23 request,
24 save_permission_policy,
25 session_headers,
26 write_records,
27)
28
29ARTICLES_FIELDS = [
30 {"name": "id"},
31 {"name": "title", "type": "text"},
32 {"name": "owner_id", "type": "text"},
33 {"name": "is_public", "type": "boolean"},
34 {"name": "published_on", "type": "date"},
35]
36
37FOLLOWS_FIELDS = [
38 {"name": "id"},
39 {"name": "follower_id", "type": "text"},
40 {"name": "following_id", "type": "text"},
41 {"name": "owner_id", "type": "text"},
42]
43
44# Mirrors packages/app-articles/permissions/rules.json and
45# packages/app-worker/permissions/rules.json's real posture: owners manage
46# their own rows, everyone (including anonymous) may read public articles
47# and the follow graph.
48FEED_TEST_POLICY = {
49 "access_mode": "role_based",
50 "rules": [
51 {
52 "effect": "allow",
53 "principal": "public",
54 "actions": ["read"],
55 "collection": "follows",
56 },
57 {
58 "effect": "allow",
59 "principal": "registered",
60 "actions": ["create", "read", "update", "delete"],
61 "collection": "follows",
62 "row_filter": {"owner_id": "$user_id"},
63 },
64 {
65 "effect": "allow",
66 "principal": "registered",
67 "actions": ["create", "read", "update", "delete"],
68 "collection": "articles",
69 "row_filter": {"owner_id": "$user_id"},
70 },
71 {
72 "effect": "allow",
73 "principal": "public",
74 "actions": ["read"],
75 "collection": "articles",
76 "row_filter": {"is_public": "true"},
77 },
78 ],
79}
80
81
82def write_articles_schema(data_dir, *, with_feed_block=True):
83 path = data_dir / "schemas" / "articles.json"
84 path.parent.mkdir(parents=True, exist_ok=True)
85 payload = {"fields": ARTICLES_FIELDS}
86 if with_feed_block:
87 payload["blocks"] = {
88 "feed": {
89 "owner_field": "owner_id",
90 "visibility_field": "is_public",
91 "visibility_true_value": "true",
92 "time_field": "published_on",
93 "summary_fields": ["title"],
94 "link_field": "id",
95 }
96 }
97 path.write_text(json.dumps(payload))
98 return path
99
100
101def write_follows_schema(data_dir):
102 path = data_dir / "schemas" / "follows.json"
103 path.parent.mkdir(parents=True, exist_ok=True)
104 path.write_text(json.dumps({"fields": FOLLOWS_FIELDS}))
105 return path
106
107
108def setup_feed_env(tmp_path, monkeypatch, *, with_feed_block=True):
109 data_dir = tmp_path / "data"
110 write_articles_schema(data_dir, with_feed_block=with_feed_block)
111 write_follows_schema(data_dir)
112 save_permission_policy(data_dir, FEED_TEST_POLICY)
113 monkeypatch.setenv("DBBASIC_DATA_DIR", str(data_dir))
114 monkeypatch.setenv(object_server.PERMISSION_ENFORCEMENT_ENV, "true")
115 enable_admin_token(monkeypatch)
116 return data_dir
117
118
119def viewer_session_headers(user_id):
120 token, _ = create_identity_session({"user_id": user_id})
121 return session_headers(token)
122
123
124def write_base_fixture(data_dir):
125 """Viewer v1 follows a1 (not b1). a1 has one public + one private
126 article; b1 has one public article. A second public article by a1,
127 dated later, exists to exercise newest-first ordering."""
128 write_records(
129 data_dir,
130 "follows",
131 "id\tfollower_id\tfollowing_id\towner_id\n"
132 "f1\tv1\ta1\tv1\n",
133 )
134 write_records(
135 data_dir,
136 "articles",
137 "id\ttitle\towner_id\tis_public\tpublished_on\n"
138 "art1\tPublic A1\ta1\ttrue\t2026-01-01\n"
139 "art2\tPrivate A1\ta1\tfalse\t2026-01-02\n"
140 "art3\tPublic B1\tb1\ttrue\t2026-01-03\n"
141 "art4\tPublic A2\ta1\ttrue\t2026-02-01\n",
142 )
143
144
145def test_feed_includes_public_article_from_followed_account(tmp_path, monkeypatch):
146 data_dir = setup_feed_env(tmp_path, monkeypatch)
147 write_base_fixture(data_dir)
148
149 status, _, payload = request("/api/feed", headers=viewer_session_headers("v1"))
150
151 assert status == 200
152 assert payload["authenticated"] is True
153 ids = [item["source_id"] for item in payload["items"]]
154 assert "art1" in ids
155
156
157def test_feed_excludes_private_article_from_followed_account(tmp_path, monkeypatch):
158 """Core privacy test: a1 is followed, but art2 is a1's PRIVATE article.
159 Following someone must never widen what the viewer can read -- art2
160 must never appear, even though its author is in the followed set."""
161 data_dir = setup_feed_env(tmp_path, monkeypatch)
162 write_base_fixture(data_dir)
163
164 status, _, payload = request("/api/feed", headers=viewer_session_headers("v1"))
165
166 assert status == 200
167 ids = [item["source_id"] for item in payload["items"]]
168 assert "art2" not in ids
169
170
171def test_feed_excludes_public_article_from_unfollowed_account(tmp_path, monkeypatch):
172 data_dir = setup_feed_env(tmp_path, monkeypatch)
173 write_base_fixture(data_dir)
174
175 status, _, payload = request("/api/feed", headers=viewer_session_headers("v1"))
176
177 assert status == 200
178 ids = [item["source_id"] for item in payload["items"]]
179 assert "art3" not in ids # public, but authored by b1, whom v1 does not follow
180
181
182def test_feed_anonymous_returns_empty_not_error(tmp_path, monkeypatch):
183 data_dir = setup_feed_env(tmp_path, monkeypatch)
184 write_base_fixture(data_dir)
185
186 status, _, payload = request("/api/feed")
187
188 assert status == 200
189 assert payload == {"status": "ok", "items": [], "count": 0, "authenticated": False}
190
191
192def test_feed_disabled_returns_empty_with_flag(tmp_path, monkeypatch):
193 data_dir = setup_feed_env(tmp_path, monkeypatch)
194 write_base_fixture(data_dir)
195 monkeypatch.setenv(object_server.FEED_ENABLED_ENV, "false")
196
197 status, _, payload = request("/api/feed", headers=viewer_session_headers("v1"))
198
199 assert status == 200
200 assert payload == {"status": "ok", "items": [], "count": 0, "enabled": False}
201
202
203def test_feed_viewer_follows_nobody_returns_empty(tmp_path, monkeypatch):
204 data_dir = setup_feed_env(tmp_path, monkeypatch)
205 write_base_fixture(data_dir) # v1 follows a1; v2 follows no one
206
207 status, _, payload = request("/api/feed", headers=viewer_session_headers("v2"))
208
209 assert status == 200
210 assert payload["authenticated"] is True
211 assert payload["items"] == []
212 assert payload["count"] == 0
213
214
215def test_feed_items_sorted_newest_first(tmp_path, monkeypatch):
216 data_dir = setup_feed_env(tmp_path, monkeypatch)
217 write_base_fixture(data_dir)
218
219 status, _, payload = request("/api/feed", headers=viewer_session_headers("v1"))
220
221 assert status == 200
222 times = [item["time"] for item in payload["items"]]
223 assert times == sorted(times, reverse=True)
224 # art4 (2026-02-01) must sort ahead of art1 (2026-01-01).
225 ids_in_order = [item["source_id"] for item in payload["items"]]
226 assert ids_in_order.index("art4") < ids_in_order.index("art1")
227
228
229def test_feed_truncates_following_over_cap_and_flags(tmp_path, monkeypatch):
230 data_dir = setup_feed_env(tmp_path, monkeypatch)
231 write_base_fixture(data_dir)
232 rows = ["id\tfollower_id\tfollowing_id\towner_id"]
233 for i in range(object_server.FILTER_IN_MAX_VALUES + 1):
234 rows.append(f"f{i}\tv3\tacct{i}\tv3")
235 write_records(data_dir, "follows", "\n".join(rows) + "\n")
236
237 status, _, payload = request("/api/feed", headers=viewer_session_headers("v3"))
238
239 assert status == 200
240 assert payload["authenticated"] is True
241 assert payload["truncated_following"] is True
242
243
244def test_feed_sources_discovers_blocks_feed_metadata(tmp_path, monkeypatch):
245 """_feed_sources() discovers a collection's `blocks.feed` key through
246 the normal get_schema path (blocks is whitelisted in
247 object_schemas._normalize_schema, so it survives normalization and
248 install) -- the additive-opt-in metadata a collection declares to
249 become a feed source (spec's Parameterization section)."""
250 data_dir = setup_feed_env(tmp_path, monkeypatch)
251
252 sources = object_server._feed_sources()
253
254 assert len(sources) == 1
255 source = sources[0]
256 assert source["collection"] == "articles"
257 assert source["owner_field"] == "owner_id"
258 assert source["visibility_field"] == "is_public"
259 assert source["visibility_true_value"] == "true"
260 assert source["time_field"] == "published_on"
261 assert source["summary_fields"] == ["title"]
262 assert source["link_field"] == "id"
263
264
265def test_feed_sources_skips_collections_without_blocks_feed(tmp_path, monkeypatch):
266 """A collection with no blocks.feed key (here, follows) is simply not
267 included -- additive opt-in, nothing swept in by default."""
268 data_dir = setup_feed_env(tmp_path, monkeypatch, with_feed_block=False)
269
270 sources = object_server._feed_sources()
271
272 assert sources == []
273
274
275def test_mcp_get_feed_tool_route_maps_to_get_api_feed():
276 import urllib.parse
277
278 import object_mcp
279
280 method, path, query, body = object_mcp.tool_route("get_feed", {"limit": 5, "offset": 10})
281
282 assert method == "GET"
283 assert path == "/api/feed"
284 pairs = dict(urllib.parse.parse_qsl(query))
285 assert pairs["limit"] == "5"
286 assert pairs["offset"] == "10"
287 assert body == b""