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_realtime.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_realtime.py · 9.2 KB · 266 lines
1"""Tests for realtime push: the hub, permission-filtered publishing, and the
2websocket transport end to end."""
3
4import asyncio
5import json
6
7import object_permissions
8import object_realtime
9import object_server
10
11from test_object_server import (
12 TEST_ADMIN_TOKEN,
13 asgi_request,
14 enable_admin_token,
15 save_permission_policy,
16 write_records,
17)
18
19
20def _subject(user_id, *roles):
21 return object_permissions.PermissionSubject(user_id=user_id, roles=tuple(roles))
22
23
24# --- Hub (transport-only) ----------------------------------------------------
25
26def test_hub_membership_and_wants():
27 async def body():
28 hub = object_realtime.RealtimeHub()
29 sub = object_realtime.Subscriber(subject=_subject("7"), queue=asyncio.Queue())
30 assert hub.count() == 0
31 hub.add(sub)
32 assert hub.count() == 1
33 sub.collections.add("notes")
34 assert sub.wants("notes") and not sub.wants("tasks")
35 assert hub.wanting("notes") == [sub] and hub.wanting("tasks") == []
36 sub.collections.add(object_realtime.ALL)
37 assert sub.wants("anything")
38 hub.remove(sub)
39 assert hub.count() == 0
40
41 asyncio.run(body())
42
43
44def test_deliver_and_queue_full_is_not_fatal():
45 async def body():
46 sub = object_realtime.Subscriber(subject=None, queue=asyncio.Queue(maxsize=1))
47 assert sub.deliver({"n": 1}) is True
48 assert sub.deliver({"n": 2}) is False # full -> dropped, not raised
49 assert sub.queue.qsize() == 1
50
51 asyncio.run(body())
52
53
54# --- Permission-filtered publishing (the security-critical path) -------------
55
56def test_realtime_publish_respects_row_filters(tmp_path, monkeypatch):
57 data_dir = tmp_path / "data"
58 save_permission_policy(
59 data_dir,
60 {
61 "access_mode": "role_based",
62 "rules": [
63 {
64 "effect": "allow",
65 "principal": "registered",
66 "actions": ["read"],
67 "collection": "notes",
68 "row_filter": {"owner_id": "$user_id"},
69 "reason": "own notes",
70 }
71 ],
72 },
73 )
74 monkeypatch.setenv("DBBASIC_DATA_DIR", str(data_dir))
75 monkeypatch.setenv(object_server.PERMISSION_ENFORCEMENT_ENV, "true")
76 monkeypatch.setenv(object_server.PERMISSION_TRUST_HEADERS_ENV, "true")
77 enable_admin_token(monkeypatch)
78 assert object_server._permission_enforcement_enabled()
79
80 async def body():
81 owner = object_realtime.Subscriber(
82 subject=_subject("7", "member"), queue=asyncio.Queue(), collections={"notes"}
83 )
84 other = object_realtime.Subscriber(
85 subject=_subject("8", "member"), queue=asyncio.Queue(), collections={"notes"}
86 )
87 object_server._realtime_hub.add(owner)
88 object_server._realtime_hub.add(other)
89 try:
90 object_server._realtime_publish(
91 "notes", "n1", "create", {"id": "n1", "owner_id": "7"}
92 )
93 # only the owner (user 7) may see a record owned by 7
94 assert owner.queue.qsize() == 1
95 assert other.queue.qsize() == 0
96 event = owner.queue.get_nowait()
97 assert event == {
98 "type": "record",
99 "collection": "notes",
100 "record_id": "n1",
101 "action": "create",
102 }
103 finally:
104 object_server._realtime_hub.remove(owner)
105 object_server._realtime_hub.remove(other)
106
107 asyncio.run(body())
108
109
110def test_realtime_publish_noop_without_subscribers_or_when_disabled(tmp_path, monkeypatch):
111 monkeypatch.setenv("DBBASIC_DATA_DIR", str(tmp_path))
112
113 async def body():
114 # no subscribers -> nothing raised
115 object_server._realtime_publish("notes", "n1", "create", {"id": "n1"})
116
117 sub = object_realtime.Subscriber(
118 subject=_subject("7"), queue=asyncio.Queue(), collections={"notes"}
119 )
120 object_server._realtime_hub.add(sub)
121 try:
122 monkeypatch.setenv(object_server.REALTIME_ENABLED_ENV, "false")
123 object_server._realtime_publish("notes", "n1", "create", {"id": "n1"})
124 assert sub.queue.qsize() == 0 # disabled -> not delivered
125 finally:
126 object_server._realtime_hub.remove(sub)
127
128 asyncio.run(body())
129
130
131# --- Websocket transport -----------------------------------------------------
132
133class _WS:
134 """Drive object_server.app as a websocket peer."""
135
136 def __init__(self, headers):
137 self.to_server: asyncio.Queue = asyncio.Queue()
138 self.sent: list = []
139 self.scope = {
140 "type": "websocket",
141 "path": "/ws",
142 "headers": [(k.encode("latin-1"), v.encode("latin-1")) for k, v in headers],
143 }
144
145 async def receive(self):
146 return await self.to_server.get()
147
148 async def send(self, message):
149 self.sent.append(message)
150
151 def texts(self):
152 return [json.loads(m["text"]) for m in self.sent if m["type"] == "websocket.send"]
153
154 def kinds(self):
155 return [m["type"] for m in self.sent]
156
157
158async def _settle():
159 for _ in range(3):
160 await asyncio.sleep(0.01)
161
162
163def test_websocket_subscribe_and_receive_push(tmp_path, monkeypatch):
164 data_dir = tmp_path / "data"
165 write_records(data_dir, "notifications", "id\tuser_id\tbody\tis_read\n")
166 monkeypatch.setenv("DBBASIC_DATA_DIR", str(data_dir))
167 enable_admin_token(monkeypatch)
168
169 async def body():
170 ws = _WS([("authorization", f"Token {TEST_ADMIN_TOKEN}")])
171 task = asyncio.ensure_future(object_server.app(ws.scope, ws.receive, ws.send))
172 await ws.to_server.put({"type": "websocket.connect"})
173 await _settle()
174 assert "websocket.accept" in ws.kinds()
175 assert any(t.get("type") == "welcome" for t in ws.texts())
176
177 await ws.to_server.put(
178 {"type": "websocket.receive",
179 "text": json.dumps({"action": "subscribe", "collections": ["notifications"]})}
180 )
181 await _settle()
182 assert any(t.get("type") == "subscribed" and "notifications" in t["collections"]
183 for t in ws.texts())
184
185 object_server._realtime_publish(
186 "notifications", "n1", "create", {"id": "n1", "user_id": "admin"}
187 )
188 await _settle()
189 record_events = [t for t in ws.texts() if t.get("type") == "record"]
190 assert record_events and record_events[-1]["record_id"] == "n1"
191 assert record_events[-1]["collection"] == "notifications"
192
193 await ws.to_server.put({"type": "websocket.disconnect"})
194 await asyncio.wait_for(task, timeout=1)
195 assert object_server._realtime_hub.count() == 0
196
197 asyncio.run(body())
198
199
200def test_websocket_rejects_anonymous(tmp_path, monkeypatch):
201 monkeypatch.setenv("DBBASIC_DATA_DIR", str(tmp_path))
202 enable_admin_token(monkeypatch)
203
204 async def body():
205 ws = _WS([])
206 task = asyncio.ensure_future(object_server.app(ws.scope, ws.receive, ws.send))
207 await ws.to_server.put({"type": "websocket.connect"})
208 await asyncio.wait_for(task, timeout=1)
209 assert "websocket.accept" not in ws.kinds()
210 assert ws.sent[-1]["type"] == "websocket.close"
211
212 asyncio.run(body())
213
214
215def test_websocket_closed_when_realtime_disabled(tmp_path, monkeypatch):
216 monkeypatch.setenv("DBBASIC_DATA_DIR", str(tmp_path))
217 enable_admin_token(monkeypatch)
218 monkeypatch.setenv(object_server.REALTIME_ENABLED_ENV, "false")
219
220 async def body():
221 ws = _WS([("authorization", f"Token {TEST_ADMIN_TOKEN}")])
222 task = asyncio.ensure_future(object_server.app(ws.scope, ws.receive, ws.send))
223 await ws.to_server.put({"type": "websocket.connect"})
224 await asyncio.wait_for(task, timeout=1)
225 assert "websocket.accept" not in ws.kinds()
226 assert ws.sent[-1]["type"] == "websocket.close"
227
228 asyncio.run(body())
229
230
231def test_http_record_write_pushes_to_websocket(tmp_path, monkeypatch):
232 """The capstone: a create through the HTTP API reaches a live subscriber."""
233 data_dir = tmp_path / "data"
234 write_records(data_dir, "notes", "id\tcontent\n")
235 monkeypatch.setenv("DBBASIC_DATA_DIR", str(data_dir))
236 enable_admin_token(monkeypatch)
237
238 async def body():
239 ws = _WS([("authorization", f"Token {TEST_ADMIN_TOKEN}")])
240 task = asyncio.ensure_future(object_server.app(ws.scope, ws.receive, ws.send))
241 await ws.to_server.put({"type": "websocket.connect"})
242 await _settle()
243 await ws.to_server.put(
244 {"type": "websocket.receive",
245 "text": json.dumps({"action": "subscribe", "collections": ["notes"]})}
246 )
247 await _settle()
248
249 status, _, raw = await asgi_request(
250 "/collections/notes/records",
251 method="POST",
252 body=json.dumps({"id": "n-live", "content": "pushed"}).encode(),
253 headers=[("authorization", f"Token {TEST_ADMIN_TOKEN}"),
254 ("content-type", "application/json")],
255 )
256 assert status == 201
257 await _settle()
258
259 record_events = [t for t in ws.texts() if t.get("type") == "record"]
260 assert any(e["record_id"] == "n-live" and e["action"] == "create"
261 for e in record_events)
262
263 await ws.to_server.put({"type": "websocket.disconnect"})
264 await asyncio.wait_for(task, timeout=1)
265
266 asyncio.run(body())