Skip to content
Snippets Groups Projects
Verified Commit f641de06 authored by Jonathan Weth's avatar Jonathan Weth :keyboard:
Browse files

Write more tests and fix child space sync

parent 625b82d8
No related branches found
No related tags found
No related merge requests found
Pipeline #50607 failed
...@@ -315,11 +315,11 @@ class MatrixSpace(MatrixRoom): ...@@ -315,11 +315,11 @@ class MatrixSpace(MatrixRoom):
def ensure_children(self): def ensure_children(self):
"""Ensure that all child rooms/spaces exist.""" """Ensure that all child rooms/spaces exist."""
for group in self.group.child_groups.all().prefetch_related("child_groups"): for group in self.group.child_groups.all().prefetch_related("child_groups"):
group.use_in_matrix(sync=True)
if group.child_groups.all(): if group.child_groups.all():
space = MatrixSpace.from_group(group) space = MatrixSpace.from_group(group)
space.ensure_children() space.ensure_children()
else: space.sync_children()
group.use_in_matrix(sync=True)
def sync(self): def sync(self):
"""Sync this space.""" """Sync this space."""
......
...@@ -326,3 +326,80 @@ def test_space_creation(matrix_bot_user): ...@@ -326,3 +326,80 @@ def test_space_creation(matrix_bot_user):
assert len(rooms) == 4 assert len(rooms) == 4
assert set(interesting_events) == set(rooms) assert set(interesting_events) == set(rooms)
def test_space_creation_with_child_spaces(matrix_bot_user):
parent_group = Group.objects.create(name="Test Group")
child_1 = Group.objects.create(name="Test Group 1")
child_1_child_1 = Group.objects.create(name="Test Group 1 1")
child_1_child_2 = Group.objects.create(name="Test Group 1 2")
child_1.child_groups.set([child_1_child_1, child_1_child_2])
child_2 = Group.objects.create(name="Test Group 2")
child_3 = Group.objects.create(name="Test Group 3")
parent_group.child_groups.set([child_1, child_2, child_3])
parent_group.use_in_matrix(sync=True)
get_site_preferences()["matrix__use_spaces"] = True
space = MatrixSpace.from_group(parent_group)
space.ensure_children()
rooms = MatrixRoom.get_queryset().values_list("group_id", flat=True)
assert child_1.pk in rooms
assert child_2.pk in rooms
assert child_3.pk in rooms
assert child_1_child_1.pk in rooms
assert child_1_child_2.pk in rooms
spaces = MatrixSpace.get_queryset().values_list("group_id", flat=True)
assert parent_group.pk in spaces
assert child_1.pk in spaces
space.sync_children()
r = do_matrix_request("GET", f"rooms/{space.room_id}/state")
interesting_events = [x["state_key"] for x in r if x["type"] == "m.space.child"]
assert len(interesting_events) == 4
rooms = list(
MatrixRoom.get_queryset()
.filter(group__in=[parent_group, child_2, child_3])
.values_list("room_id", flat=True)
) + list(MatrixSpace.objects.filter(group=child_1).values_list("room_id", flat=True))
assert len(rooms) == 4
assert set(interesting_events) == set(rooms)
space = MatrixSpace.objects.get(group=child_1)
r = do_matrix_request("GET", f"rooms/{space.room_id}/state")
interesting_events = [x["state_key"] for x in r if x["type"] == "m.space.child"]
assert len(interesting_events) == 3
rooms = list(
MatrixRoom.get_queryset()
.filter(group__in=[child_1, child_1_child_1, child_1_child_2])
.values_list("room_id", flat=True)
)
assert len(rooms) == 3
assert set(interesting_events) == set(rooms)
def test_alias_room_id_using_group(matrix_bot_user):
g = Group.objects.create(name="Test Room")
room = MatrixRoom.from_group(g)
child_1 = Group.objects.create(name="Test Group 1")
g.child_groups.set([child_1])
room.sync()
assert MatrixSpace.objects.get_queryset().count() == 1
assert g.matrix_room_id == room.room_id
assert g.matrix_alias == room.alias
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment