1212# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1313# See the License for the specific language governing permissions and
1414# limitations under the License.
15-
15+ from typing import Any , Awaitable , Callable , Dict
1616from unittest .mock import Mock
1717
18+ from twisted .test .proto_helpers import MemoryReactor
19+
1820import synapse .api .errors
1921import synapse .rest .admin
2022from synapse .api .constants import EventTypes
2123from synapse .rest .client import directory , login , room
22- from synapse .types import RoomAlias , create_requester
24+ from synapse .server import HomeServer
25+ from synapse .types import JsonDict , RoomAlias , create_requester
26+ from synapse .util import Clock
2327
2428from tests import unittest
2529from tests .test_utils import make_awaitable
2832class DirectoryTestCase (unittest .HomeserverTestCase ):
2933 """Tests the directory service."""
3034
31- def make_homeserver (self , reactor , clock ) :
35+ def make_homeserver (self , reactor : MemoryReactor , clock : Clock ) -> HomeServer :
3236 self .mock_federation = Mock ()
3337 self .mock_registry = Mock ()
3438
35- self .query_handlers = {}
39+ self .query_handlers : Dict [ str , Callable [[ dict ], Awaitable [ JsonDict ]]] = {}
3640
37- def register_query_handler (query_type , handler ):
41+ def register_query_handler (
42+ query_type : str , handler : Callable [[dict ], Awaitable [JsonDict ]]
43+ ) -> None :
3844 self .query_handlers [query_type ] = handler
3945
4046 self .mock_registry .register_query_handler = register_query_handler
@@ -54,7 +60,7 @@ def register_query_handler(query_type, handler):
5460
5561 return hs
5662
57- def test_get_local_association (self ):
63+ def test_get_local_association (self ) -> None :
5864 self .get_success (
5965 self .store .create_room_alias_association (
6066 self .my_room , "!8765qwer:test" , ["test" ]
@@ -65,7 +71,7 @@ def test_get_local_association(self):
6571
6672 self .assertEqual ({"room_id" : "!8765qwer:test" , "servers" : ["test" ]}, result )
6773
68- def test_get_remote_association (self ):
74+ def test_get_remote_association (self ) -> None :
6975 self .mock_federation .make_query .return_value = make_awaitable (
7076 {"room_id" : "!8765qwer:test" , "servers" : ["test" , "remote" ]}
7177 )
@@ -83,7 +89,7 @@ def test_get_remote_association(self):
8389 ignore_backoff = True ,
8490 )
8591
86- def test_incoming_fed_query (self ):
92+ def test_incoming_fed_query (self ) -> None :
8793 self .get_success (
8894 self .store .create_room_alias_association (
8995 self .your_room , "!8765asdf:test" , ["test" ]
@@ -105,7 +111,7 @@ class TestCreateAlias(unittest.HomeserverTestCase):
105111 directory .register_servlets ,
106112 ]
107113
108- def prepare (self , reactor , clock , hs ) :
114+ def prepare (self , reactor : MemoryReactor , clock : Clock , hs : HomeServer ) -> None :
109115 self .handler = hs .get_directory_handler ()
110116
111117 # Create user
@@ -125,7 +131,7 @@ def prepare(self, reactor, clock, hs):
125131 self .test_user_tok = self .login ("user" , "pass" )
126132 self .helper .join (room = self .room_id , user = self .test_user , tok = self .test_user_tok )
127133
128- def test_create_alias_joined_room (self ):
134+ def test_create_alias_joined_room (self ) -> None :
129135 """A user can create an alias for a room they're in."""
130136 self .get_success (
131137 self .handler .create_association (
@@ -135,7 +141,7 @@ def test_create_alias_joined_room(self):
135141 )
136142 )
137143
138- def test_create_alias_other_room (self ):
144+ def test_create_alias_other_room (self ) -> None :
139145 """A user cannot create an alias for a room they're NOT in."""
140146 other_room_id = self .helper .create_room_as (
141147 self .admin_user , tok = self .admin_user_tok
@@ -150,7 +156,7 @@ def test_create_alias_other_room(self):
150156 synapse .api .errors .SynapseError ,
151157 )
152158
153- def test_create_alias_admin (self ):
159+ def test_create_alias_admin (self ) -> None :
154160 """An admin can create an alias for a room they're NOT in."""
155161 other_room_id = self .helper .create_room_as (
156162 self .test_user , tok = self .test_user_tok
@@ -173,7 +179,7 @@ class TestDeleteAlias(unittest.HomeserverTestCase):
173179 directory .register_servlets ,
174180 ]
175181
176- def prepare (self , reactor , clock , hs ) :
182+ def prepare (self , reactor : MemoryReactor , clock : Clock , hs : HomeServer ) -> None :
177183 self .store = hs .get_datastores ().main
178184 self .handler = hs .get_directory_handler ()
179185 self .state_handler = hs .get_state_handler ()
@@ -195,15 +201,15 @@ def prepare(self, reactor, clock, hs):
195201 self .test_user_tok = self .login ("user" , "pass" )
196202 self .helper .join (room = self .room_id , user = self .test_user , tok = self .test_user_tok )
197203
198- def _create_alias (self , user ):
204+ def _create_alias (self , user ) -> None :
199205 # Create a new alias to this room.
200206 self .get_success (
201207 self .store .create_room_alias_association (
202208 self .room_alias , self .room_id , ["test" ], user
203209 )
204210 )
205211
206- def test_delete_alias_not_allowed (self ):
212+ def test_delete_alias_not_allowed (self ) -> None :
207213 """A user that doesn't meet the expected guidelines cannot delete an alias."""
208214 self ._create_alias (self .admin_user )
209215 self .get_failure (
@@ -213,7 +219,7 @@ def test_delete_alias_not_allowed(self):
213219 synapse .api .errors .AuthError ,
214220 )
215221
216- def test_delete_alias_creator (self ):
222+ def test_delete_alias_creator (self ) -> None :
217223 """An alias creator can delete their own alias."""
218224 # Create an alias from a different user.
219225 self ._create_alias (self .test_user )
@@ -232,7 +238,7 @@ def test_delete_alias_creator(self):
232238 synapse .api .errors .SynapseError ,
233239 )
234240
235- def test_delete_alias_admin (self ):
241+ def test_delete_alias_admin (self ) -> None :
236242 """A server admin can delete an alias created by another user."""
237243 # Create an alias from a different user.
238244 self ._create_alias (self .test_user )
@@ -251,7 +257,7 @@ def test_delete_alias_admin(self):
251257 synapse .api .errors .SynapseError ,
252258 )
253259
254- def test_delete_alias_sufficient_power (self ):
260+ def test_delete_alias_sufficient_power (self ) -> None :
255261 """A user with a sufficient power level should be able to delete an alias."""
256262 self ._create_alias (self .admin_user )
257263
@@ -288,7 +294,7 @@ class CanonicalAliasTestCase(unittest.HomeserverTestCase):
288294 directory .register_servlets ,
289295 ]
290296
291- def prepare (self , reactor , clock , hs ) :
297+ def prepare (self , reactor : MemoryReactor , clock : Clock , hs : HomeServer ) -> None :
292298 self .store = hs .get_datastores ().main
293299 self .handler = hs .get_directory_handler ()
294300 self .state_handler = hs .get_state_handler ()
@@ -317,7 +323,7 @@ def _add_alias(self, alias: str) -> RoomAlias:
317323 )
318324 return room_alias
319325
320- def _set_canonical_alias (self , content ):
326+ def _set_canonical_alias (self , content ) -> None :
321327 """Configure the canonical alias state on the room."""
322328 self .helper .send_state (
323329 self .room_id ,
@@ -334,7 +340,7 @@ def _get_canonical_alias(self):
334340 )
335341 )
336342
337- def test_remove_alias (self ):
343+ def test_remove_alias (self ) -> None :
338344 """Removing an alias that is the canonical alias should remove it there too."""
339345 # Set this new alias as the canonical alias for this room
340346 self ._set_canonical_alias (
@@ -356,7 +362,7 @@ def test_remove_alias(self):
356362 self .assertNotIn ("alias" , data ["content" ])
357363 self .assertNotIn ("alt_aliases" , data ["content" ])
358364
359- def test_remove_other_alias (self ):
365+ def test_remove_other_alias (self ) -> None :
360366 """Removing an alias listed as in alt_aliases should remove it there too."""
361367 # Create a second alias.
362368 other_test_alias = "#test2:test"
@@ -393,7 +399,7 @@ class TestCreateAliasACL(unittest.HomeserverTestCase):
393399
394400 servlets = [directory .register_servlets , room .register_servlets ]
395401
396- def default_config (self ):
402+ def default_config (self ) -> Dict [ str , Any ] :
397403 config = super ().default_config ()
398404
399405 # Add custom alias creation rules to the config.
@@ -403,7 +409,7 @@ def default_config(self):
403409
404410 return config
405411
406- def test_denied (self ):
412+ def test_denied (self ) -> None :
407413 room_id = self .helper .create_room_as (self .user_id )
408414
409415 channel = self .make_request (
@@ -413,7 +419,7 @@ def test_denied(self):
413419 )
414420 self .assertEqual (403 , channel .code , channel .result )
415421
416- def test_allowed (self ):
422+ def test_allowed (self ) -> None :
417423 room_id = self .helper .create_room_as (self .user_id )
418424
419425 channel = self .make_request (
@@ -423,7 +429,7 @@ def test_allowed(self):
423429 )
424430 self .assertEqual (200 , channel .code , channel .result )
425431
426- def test_denied_during_creation (self ):
432+ def test_denied_during_creation (self ) -> None :
427433 """A room alias that is not allowed should be rejected during creation."""
428434 # Invalid room alias.
429435 self .helper .create_room_as (
@@ -432,7 +438,7 @@ def test_denied_during_creation(self):
432438 extra_content = {"room_alias_name" : "foo" },
433439 )
434440
435- def test_allowed_during_creation (self ):
441+ def test_allowed_during_creation (self ) -> None :
436442 """A valid room alias should be allowed during creation."""
437443 room_id = self .helper .create_room_as (
438444 self .user_id ,
@@ -459,7 +465,7 @@ class TestCreatePublishedRoomACL(unittest.HomeserverTestCase):
459465 data = {"room_alias_name" : "unofficial_test" }
460466 allowed_localpart = "allowed"
461467
462- def default_config (self ):
468+ def default_config (self ) -> Dict [ str , Any ] :
463469 config = super ().default_config ()
464470
465471 # Add custom room list publication rules to the config.
@@ -474,7 +480,9 @@ def default_config(self):
474480
475481 return config
476482
477- def prepare (self , reactor , clock , hs ):
483+ def prepare (
484+ self , reactor : MemoryReactor , clock : Clock , hs : HomeServer
485+ ) -> HomeServer :
478486 self .allowed_user_id = self .register_user (self .allowed_localpart , "pass" )
479487 self .allowed_access_token = self .login (self .allowed_localpart , "pass" )
480488
@@ -483,7 +491,7 @@ def prepare(self, reactor, clock, hs):
483491
484492 return hs
485493
486- def test_denied_without_publication_permission (self ):
494+ def test_denied_without_publication_permission (self ) -> None :
487495 """
488496 Try to create a room, register an alias for it, and publish it,
489497 as a user without permission to publish rooms.
@@ -497,7 +505,7 @@ def test_denied_without_publication_permission(self):
497505 expect_code = 403 ,
498506 )
499507
500- def test_allowed_when_creating_private_room (self ):
508+ def test_allowed_when_creating_private_room (self ) -> None :
501509 """
502510 Try to create a room, register an alias for it, and NOT publish it,
503511 as a user without permission to publish rooms.
@@ -511,7 +519,7 @@ def test_allowed_when_creating_private_room(self):
511519 expect_code = 200 ,
512520 )
513521
514- def test_allowed_with_publication_permission (self ):
522+ def test_allowed_with_publication_permission (self ) -> None :
515523 """
516524 Try to create a room, register an alias for it, and publish it,
517525 as a user WITH permission to publish rooms.
@@ -525,7 +533,7 @@ def test_allowed_with_publication_permission(self):
525533 expect_code = 200 ,
526534 )
527535
528- def test_denied_publication_with_invalid_alias (self ):
536+ def test_denied_publication_with_invalid_alias (self ) -> None :
529537 """
530538 Try to create a room, register an alias for it, and publish it,
531539 as a user WITH permission to publish rooms.
@@ -538,7 +546,7 @@ def test_denied_publication_with_invalid_alias(self):
538546 expect_code = 403 ,
539547 )
540548
541- def test_can_create_as_private_room_after_rejection (self ):
549+ def test_can_create_as_private_room_after_rejection (self ) -> None :
542550 """
543551 After failing to publish a room with an alias as a user without publish permission,
544552 retry as the same user, but without publishing the room.
@@ -549,7 +557,7 @@ def test_can_create_as_private_room_after_rejection(self):
549557 self .test_denied_without_publication_permission ()
550558 self .test_allowed_when_creating_private_room ()
551559
552- def test_can_create_with_permission_after_rejection (self ):
560+ def test_can_create_with_permission_after_rejection (self ) -> None :
553561 """
554562 After failing to publish a room with an alias as a user without publish permission,
555563 retry as someone with permission, using the same alias.
@@ -566,7 +574,9 @@ class TestRoomListSearchDisabled(unittest.HomeserverTestCase):
566574
567575 servlets = [directory .register_servlets , room .register_servlets ]
568576
569- def prepare (self , reactor , clock , hs ):
577+ def prepare (
578+ self , reactor : MemoryReactor , clock : Clock , hs : HomeServer
579+ ) -> HomeServer :
570580 room_id = self .helper .create_room_as (self .user_id )
571581
572582 channel = self .make_request (
@@ -579,7 +589,7 @@ def prepare(self, reactor, clock, hs):
579589
580590 return hs
581591
582- def test_disabling_room_list (self ):
592+ def test_disabling_room_list (self ) -> None :
583593 self .room_list_handler .enable_room_list_search = True
584594 self .directory_handler .enable_room_list_search = True
585595
0 commit comments