Additional unit tests for spaces summary. (#10305)

This commit is contained in:
Patrick Cloke 2021-07-12 11:21:04 -04:00 committed by GitHub
parent 8eddbde0e2
commit 19d0401c56
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 204 additions and 1 deletions

1
changelog.d/10305.misc Normal file
View file

@ -0,0 +1 @@
Additional unit tests for the spaces summary API.

View file

@ -14,7 +14,7 @@
from typing import Any, Iterable, Optional, Tuple from typing import Any, Iterable, Optional, Tuple
from unittest import mock from unittest import mock
from synapse.api.constants import EventContentFields, RoomTypes from synapse.api.constants import EventContentFields, JoinRules, RoomTypes
from synapse.api.errors import AuthError from synapse.api.errors import AuthError
from synapse.handlers.space_summary import _child_events_comparison_key from synapse.handlers.space_summary import _child_events_comparison_key
from synapse.rest import admin from synapse.rest import admin
@ -178,3 +178,205 @@ class SpaceSummaryTestCase(unittest.HomeserverTestCase):
result = self.get_success(self.handler.get_space_summary(user2, self.space)) result = self.get_success(self.handler.get_space_summary(user2, self.space))
self._assert_rooms(result, [self.space]) self._assert_rooms(result, [self.space])
self._assert_events(result, [(self.space, self.room)]) self._assert_events(result, [(self.space, self.room)])
def test_complex_space(self):
"""
Create a "complex" space to see how it handles things like loops and subspaces.
"""
# Create an inaccessible room.
user2 = self.register_user("user2", "pass")
token2 = self.login("user2", "pass")
room2 = self.helper.create_room_as(user2, tok=token2)
# This is a bit odd as "user" is adding a room they don't know about, but
# it works for the tests.
self._add_child(self.space, room2, self.token)
# Create a subspace under the space with an additional room in it.
subspace = self.helper.create_room_as(
self.user,
tok=self.token,
extra_content={
"creation_content": {EventContentFields.ROOM_TYPE: RoomTypes.SPACE}
},
)
subroom = self.helper.create_room_as(self.user, tok=self.token)
self._add_child(self.space, subspace, token=self.token)
self._add_child(subspace, subroom, token=self.token)
# Also add the two rooms from the space into this subspace (causing loops).
self._add_child(subspace, self.room, token=self.token)
self._add_child(subspace, room2, self.token)
result = self.get_success(self.handler.get_space_summary(self.user, self.space))
# The result should include each room a single time and each link.
self._assert_rooms(result, [self.space, self.room, subspace, subroom])
self._assert_events(
result,
[
(self.space, self.room),
(self.space, room2),
(self.space, subspace),
(subspace, subroom),
(subspace, self.room),
(subspace, room2),
],
)
def test_fed_complex(self):
"""
Return data over federation and ensure that it is handled properly.
"""
fed_hostname = self.hs.hostname + "2"
subspace = "#subspace:" + fed_hostname
subroom = "#subroom:" + fed_hostname
async def summarize_remote_room(
_self, room, suggested_only, max_children, exclude_rooms
):
# Return some good data, and some bad data:
#
# * Event *back* to the root room.
# * Unrelated events / rooms
# * Multiple levels of events (in a not-useful order, e.g. grandchild
# events before child events).
# Note that these entries are brief, but should contain enough info.
rooms = [
{
"room_id": subspace,
"world_readable": True,
"room_type": RoomTypes.SPACE,
},
{
"room_id": subroom,
"world_readable": True,
},
]
event_content = {"via": [fed_hostname]}
events = [
{
"room_id": subspace,
"state_key": subroom,
"content": event_content,
},
]
return rooms, events
# Add a room to the space which is on another server.
self._add_child(self.space, subspace, self.token)
with mock.patch(
"synapse.handlers.space_summary.SpaceSummaryHandler._summarize_remote_room",
new=summarize_remote_room,
):
result = self.get_success(
self.handler.get_space_summary(self.user, self.space)
)
self._assert_rooms(result, [self.space, self.room, subspace, subroom])
self._assert_events(
result,
[
(self.space, self.room),
(self.space, subspace),
(subspace, subroom),
],
)
def test_fed_filtering(self):
"""
Rooms returned over federation should be properly filtered to only include
rooms the user has access to.
"""
fed_hostname = self.hs.hostname + "2"
subspace = "#subspace:" + fed_hostname
# Create a few rooms which will have different properties.
restricted_room = "#restricted:" + fed_hostname
restricted_accessible_room = "#restricted_accessible:" + fed_hostname
world_readable_room = "#world_readable:" + fed_hostname
joined_room = self.helper.create_room_as(self.user, tok=self.token)
async def summarize_remote_room(
_self, room, suggested_only, max_children, exclude_rooms
):
# Note that these entries are brief, but should contain enough info.
rooms = [
{
"room_id": restricted_room,
"world_readable": False,
"join_rules": JoinRules.MSC3083_RESTRICTED,
"allowed_spaces": [],
},
{
"room_id": restricted_accessible_room,
"world_readable": False,
"join_rules": JoinRules.MSC3083_RESTRICTED,
"allowed_spaces": [self.room],
},
{
"room_id": world_readable_room,
"world_readable": True,
"join_rules": JoinRules.INVITE,
},
{
"room_id": joined_room,
"world_readable": False,
"join_rules": JoinRules.INVITE,
},
]
# Place each room in the sub-space.
event_content = {"via": [fed_hostname]}
events = [
{
"room_id": subspace,
"state_key": room["room_id"],
"content": event_content,
}
for room in rooms
]
# Also include the subspace.
rooms.insert(
0,
{
"room_id": subspace,
"world_readable": True,
},
)
return rooms, events
# Add a room to the space which is on another server.
self._add_child(self.space, subspace, self.token)
with mock.patch(
"synapse.handlers.space_summary.SpaceSummaryHandler._summarize_remote_room",
new=summarize_remote_room,
):
result = self.get_success(
self.handler.get_space_summary(self.user, self.space)
)
self._assert_rooms(
result,
[
self.space,
self.room,
subspace,
restricted_accessible_room,
world_readable_room,
joined_room,
],
)
self._assert_events(
result,
[
(self.space, self.room),
(self.space, subspace),
(subspace, restricted_room),
(subspace, restricted_accessible_room),
(subspace, world_readable_room),
(subspace, joined_room),
],
)