1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
|
# Copyright 2021 The Matrix.org Foundation C.I.C.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from typing import Dict, Iterable, Mapping, NoReturn, Optional, Sequence, Tuple
from unittest import mock
from twisted.test.proto_helpers import MemoryReactor
from synapse.api.constants import EventContentFields, EventTypes, RoomTypes
from synapse.handlers.space_hierarchy import SpaceHierarchyHandler
from synapse.rest import admin
from synapse.rest.client import login, room
from synapse.server import HomeServer
from synapse.types import JsonDict
from synapse.util import Clock
from tests import unittest
class SpaceDescendantsTestCase(unittest.HomeserverTestCase):
"""Tests iteration over the descendants of a space."""
servlets = [
admin.register_servlets_for_client_rest_resource,
login.register_servlets,
room.register_servlets,
]
def prepare(self, reactor: MemoryReactor, clock: Clock, hs: HomeServer):
self.hs = hs
self.handler = self.hs.get_space_hierarchy_handler()
# Create a user.
self.user = self.register_user("user", "pass")
self.token = self.login("user", "pass")
# Create a space and a child room.
self.space = self.helper.create_room_as(
self.user,
tok=self.token,
extra_content={
"creation_content": {EventContentFields.ROOM_TYPE: RoomTypes.SPACE}
},
)
self.room = self.helper.create_room_as(self.user, tok=self.token)
self._add_child(self.space, self.room)
def _add_child(
self, space_id: str, room_id: str, order: Optional[str] = None
) -> None:
"""Adds a room to a space."""
content: JsonDict = {"via": [self.hs.hostname]}
if order is not None:
content["order"] = order
self.helper.send_state(
space_id,
event_type=EventTypes.SpaceChild,
body=content,
tok=self.token,
state_key=room_id,
)
def _create_space(self) -> str:
"""Creates a space."""
return self._create_room(
extra_content={
"creation_content": {EventContentFields.ROOM_TYPE: RoomTypes.SPACE}
},
)
def _create_room(self, extra_content: Optional[Dict] = None) -> str:
"""Creates a room."""
return self.helper.create_room_as(
self.user,
tok=self.token,
extra_content=extra_content,
)
def test_empty_space(self):
"""Tests iteration over an empty space."""
space_id = self._create_space()
descendants, inaccessible_room_ids = self.get_success(
self.handler.get_space_descendants(space_id)
)
self.assertEqual(descendants, [(space_id, [])])
self.assertEqual(inaccessible_room_ids, [])
def test_invalid_space(self):
"""Tests iteration over an inaccessible space."""
space_id = f"!invalid:{self.hs.hostname}"
descendants, inaccessible_room_ids = self.get_success(
self.handler.get_space_descendants(space_id)
)
self.assertEqual(descendants, [(space_id, [])])
self.assertEqual(inaccessible_room_ids, [space_id])
def test_invalid_room(self):
"""Tests iteration over a space containing an inaccessible room."""
space_id = self._create_space()
room_id = f"!invalid:{self.hs.hostname}"
self._add_child(space_id, room_id)
descendants, inaccessible_room_ids = self.get_success(
self.handler.get_space_descendants(space_id)
)
self.assertEqual(descendants, [(space_id, []), (room_id, [self.hs.hostname])])
self.assertEqual(inaccessible_room_ids, [room_id])
def test_remote_space_with_federation_enabled(self):
"""Tests iteration over a remote space with federation enabled."""
space_id = "!space:remote"
room_id = "!room:remote"
async def _get_space_children_remote(
_self: SpaceHierarchyHandler, space_id: str, via: Iterable[str]
) -> Tuple[
Sequence[Tuple[str, Iterable[str]]], Mapping[str, Optional[JsonDict]]
]:
if space_id == "!space:remote":
self.assertEqual(via, ["remote"])
return [("!room:remote", ["remote"])], {}
elif space_id == "!room:remote":
self.assertEqual(via, ["remote"])
return [], {}
else:
self.fail(
f"Unexpected _get_space_children_remote({space_id!r}, {via!r}) call"
)
raise # `fail` is missing type hints
with mock.patch(
"synapse.handlers.space_hierarchy.SpaceHierarchyHandler._get_space_children_remote",
new=_get_space_children_remote,
):
descendants, inaccessible_room_ids = self.get_success(
self.handler.get_space_descendants(
space_id, via=["remote"], enable_federation=True
)
)
self.assertEqual(descendants, [(space_id, ["remote"]), (room_id, ["remote"])])
self.assertEqual(inaccessible_room_ids, [space_id, room_id])
def test_remote_space_with_federation_disabled(self):
"""Tests iteration over a remote space with federation disabled."""
space_id = "!space:remote"
async def _get_space_children_remote(
_self: SpaceHierarchyHandler, space_id: str, via: Iterable[str]
) -> NoReturn:
self.fail(
f"Unexpected _get_space_children_remote({space_id!r}, {via!r}) call"
)
raise # `fail` is missing type hints
with mock.patch(
"synapse.handlers.space_hierarchy.SpaceHierarchyHandler._get_space_children_remote",
new=_get_space_children_remote,
):
descendants, inaccessible_room_ids = self.get_success(
self.handler.get_space_descendants(
space_id, via=["remote"], enable_federation=False
)
)
self.assertEqual(descendants, [(space_id, ["remote"])])
self.assertEqual(inaccessible_room_ids, [space_id])
def test_cycle(self):
"""Tests iteration over a cyclic space."""
# space_id
# - subspace_id
# - space_id
space_id = self._create_space()
subspace_id = self._create_space()
self._add_child(space_id, subspace_id)
self._add_child(subspace_id, space_id)
descendants, inaccessible_room_ids = self.get_success(
self.handler.get_space_descendants(space_id)
)
self.assertEqual(
descendants, [(space_id, []), (subspace_id, [self.hs.hostname])]
)
self.assertEqual(inaccessible_room_ids, [])
def test_duplicates(self):
"""Tests iteration over a space with repeated rooms."""
# space_id
# - subspace_id
# - duplicate_room_1_id
# - duplicate_room_2_id
# - room_id
# - duplicate_room_1_id
# - duplicate_room_2_id
space_id = self._create_space()
subspace_id = self._create_space()
room_id = self._create_room()
duplicate_room_1_id = self._create_room()
duplicate_room_2_id = self._create_room()
self._add_child(space_id, subspace_id, order="1")
self._add_child(space_id, duplicate_room_1_id, order="2")
self._add_child(space_id, duplicate_room_2_id, order="3")
self._add_child(subspace_id, duplicate_room_1_id, order="1")
self._add_child(subspace_id, duplicate_room_2_id, order="2")
self._add_child(subspace_id, room_id, order="3")
descendants, inaccessible_room_ids = self.get_success(
self.handler.get_space_descendants(space_id)
)
self.assertEqual(
descendants,
[
(space_id, []),
(subspace_id, [self.hs.hostname]),
(room_id, [self.hs.hostname]),
(duplicate_room_1_id, [self.hs.hostname]),
(duplicate_room_2_id, [self.hs.hostname]),
],
)
self.assertEqual(inaccessible_room_ids, [])
|