Skip to content

Commit 28bb434

Browse files
bjesterclaude
andcommitted
List read-write partitions last in Scope filters
Deserialization streams partitions in the order of the sync filter (#392), so a record whose FK target is in a later partition fails. Scope listed the read-write partitions first, which put a single-user scope's logs before the user they reference. The read filter now lists the read template's partitions, then the read-write template's, and the write filter does the same with the write template. Each template keeps its own order. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
1 parent fa3edde commit 28bb434

2 files changed

Lines changed: 63 additions & 4 deletions

File tree

‎morango/models/certificates.py‎

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -483,12 +483,24 @@ def from_template(cls, template, params=None):
483483

484484

485485
class Scope(object):
486+
"""
487+
The read and write filters of a scope definition, with its params applied.
488+
489+
The read filter lists the partitions of the read template, then those of the read-write
490+
template, and the write filter likewise lists the write template's partitions first. Each
491+
template keeps its own order. Deserialization processes partitions in filter order, so a
492+
record's FK targets must be in the same or an earlier partition. Read-write partitions usually
493+
hold a device's own records, which reference the shared records in the other partitions. A
494+
read-only or write-only record that references a read-write record would fail to deserialize.
495+
"""
496+
486497
def __init__(self, definition, params):
487-
# turn the scope definition filter templates into Filter objects
498+
# turn the scope definition filter templates into Filter objects, with the read-write
499+
# partitions last, since their records usually reference records in the others
488500
rw_filter = Filter.from_template(definition.read_write_filter_template, params)
489-
self.read_filter = rw_filter + Filter.from_template(definition.read_filter_template, params)
490-
self.write_filter = rw_filter + Filter.from_template(
491-
definition.write_filter_template, params
501+
self.read_filter = Filter.from_template(definition.read_filter_template, params) + rw_filter
502+
self.write_filter = (
503+
Filter.from_template(definition.write_filter_template, params) + rw_filter
492504
)
493505

494506
def is_subset_of(self, other):

‎tests/testapp/tests/models/test_certificates.py‎

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from morango.models.certificates import Certificate
1212
from morango.models.certificates import Filter
1313
from morango.models.certificates import Key
14+
from morango.models.certificates import Scope
1415
from morango.models.certificates import ScopeDefinition
1516

1617

@@ -271,3 +272,49 @@ def test_from_template(self):
271272
def test_from_template__with_json_params(self):
272273
f = Filter.from_template("test:${param}", '{"param": "value"}')
273274
self.assertEqual(f._filter_tuple, ("test:value",))
275+
276+
277+
class ScopeTestCase(SimpleTestCase):
278+
params = {"user_id": "U"}
279+
280+
def _scope(self, read="", write="", read_write=""):
281+
definition = ScopeDefinition(
282+
read_filter_template=read,
283+
write_filter_template=write,
284+
read_write_filter_template=read_write,
285+
)
286+
return Scope(definition, self.params)
287+
288+
def test_read_filter__read_write_last(self):
289+
scope = self._scope(
290+
read="a:allusers-ro\na:user-ro:${user_id}",
291+
read_write="a:user-rw:${user_id}\na:rw2",
292+
)
293+
self.assertEqual(
294+
tuple(scope.read_filter),
295+
("a:allusers-ro", "a:user-ro:U", "a:user-rw:U", "a:rw2"),
296+
)
297+
298+
def test_write_filter__read_write_last(self):
299+
scope = self._scope(
300+
write="a:user-wo:${user_id}\na:other-wo",
301+
read_write="a:user-rw:${user_id}\na:rw2",
302+
)
303+
self.assertEqual(
304+
tuple(scope.write_filter),
305+
("a:user-wo:U", "a:other-wo", "a:user-rw:U", "a:rw2"),
306+
)
307+
308+
def test_filters__duplicate_partition(self):
309+
scope = self._scope(
310+
read="a:shared\na:user-ro:${user_id}",
311+
read_write="a:user-rw:${user_id}\na:shared",
312+
)
313+
partitions = tuple(scope.read_filter)
314+
self.assertEqual(len(partitions), len(set(partitions)))
315+
self.assertEqual(set(partitions), {"a:shared", "a:user-ro:U", "a:user-rw:U"})
316+
317+
def test_filters__empty_read_write_template(self):
318+
scope = self._scope(read="a:ro\nb:ro", write="a:wo")
319+
self.assertEqual(tuple(scope.read_filter), ("a:ro", "b:ro"))
320+
self.assertEqual(tuple(scope.write_filter), ("a:wo",))

0 commit comments

Comments
 (0)