@ -18,11 +18,12 @@
""" Tests REST events for /rooms paths. """
import json
from typing import Iterable , List
from typing import Any , Dict , Iterable , List , Optional
from unittest . mock import Mock , call
from urllib import parse as urlparse
from twisted . internet import defer
from twisted . test . proto_helpers import MemoryReactor
import synapse . rest . admin
from synapse . api . constants import (
@ -35,7 +36,9 @@ from synapse.api.errors import Codes, HttpResponseException
from synapse . handlers . pagination import PurgeStatus
from synapse . rest import admin
from synapse . rest . client import account , directory , login , profile , room , sync
from synapse . server import HomeServer
from synapse . types import JsonDict , RoomAlias , UserID , create_requester
from synapse . util import Clock
from synapse . util . stringutils import random_string
from tests import unittest
@ -45,11 +48,11 @@ PATH_PREFIX = b"/_matrix/client/api/v1"
class RoomBase ( unittest . HomeserverTestCase ) :
rmcreator_id = None
rmcreator_id : Optional [ str ] = None
servlets = [ room . register_servlets , room . register_deprecated_servlets ]
def make_homeserver ( self , reactor , clock ) :
def make_homeserver ( self , reactor : MemoryReactor , clock : Clock ) - > HomeServer :
self . hs = self . setup_test_homeserver (
" red " ,
@ -57,15 +60,15 @@ class RoomBase(unittest.HomeserverTestCase):
federation_client = Mock ( ) ,
)
self . hs . get_federation_handler = Mock ( )
self . hs . get_federation_handler = Mock ( ) # type: ignore[assignment]
self . hs . get_federation_handler . return_value . maybe_backfill = Mock (
return_value = make_awaitable ( None )
)
async def _insert_client_ip ( * args , * * kwargs ) :
async def _insert_client_ip ( * args : Any , * * kwargs : Any ) - > None :
return None
self . hs . get_datastores ( ) . main . insert_client_ip = _insert_client_ip
self . hs . get_datastores ( ) . main . insert_client_ip = _insert_client_ip # type: ignore[assignment]
return self . hs
@ -76,7 +79,7 @@ class RoomPermissionsTestCase(RoomBase):
user_id = " @sid1:red "
rmcreator_id = " @notme:red "
def prepare ( self , reactor , clock , hs ) :
def prepare ( self , reactor : MemoryReactor , clock : Clock , hs : HomeServer ) - > None :
self . helper . auth_user_id = self . rmcreator_id
# create some rooms under the name rmcreator_id
@ -108,12 +111,12 @@ class RoomPermissionsTestCase(RoomBase):
# auth as user_id now
self . helper . auth_user_id = self . user_id
def test_can_do_action ( self ) :
def test_can_do_action ( self ) - > None :
msg_content = b ' { " msgtype " : " m.text " , " body " : " hello " } '
seq = iter ( range ( 100 ) )
def send_msg_path ( ) :
def send_msg_path ( ) - > str :
return " /rooms/ %s /send/m.room.message/mid %s " % (
self . created_rmid ,
str ( next ( seq ) ) ,
@ -148,7 +151,7 @@ class RoomPermissionsTestCase(RoomBase):
channel = self . make_request ( " PUT " , send_msg_path ( ) , msg_content )
self . assertEqual ( 403 , channel . code , msg = channel . result [ " body " ] )
def test_topic_perms ( self ) :
def test_topic_perms ( self ) - > None :
topic_content = b ' { " topic " : " My Topic Name " } '
topic_path = " /rooms/ %s /state/m.room.topic " % self . created_rmid
@ -214,14 +217,14 @@ class RoomPermissionsTestCase(RoomBase):
self . assertEqual ( 403 , channel . code , msg = channel . result [ " body " ] )
def _test_get_membership (
self , room = None , members : Iterable = frozenset ( ) , expect_code = None
) :
self , room : str , members : Iterable = frozenset ( ) , expect_code : int = 200
) - > None :
for member in members :
path = " /rooms/ %s /state/m.room.member/ %s " % ( room , member )
channel = self . make_request ( " GET " , path )
self . assertEqual ( expect_code , channel . code )
def test_membership_basic_room_perms ( self ) :
def test_membership_basic_room_perms ( self ) - > None :
# === room does not exist ===
room = self . uncreated_rmid
# get membership of self, get membership of other, uncreated room
@ -241,7 +244,7 @@ class RoomPermissionsTestCase(RoomBase):
self . helper . join ( room = room , user = usr , expect_code = 404 )
self . helper . leave ( room = room , user = usr , expect_code = 404 )
def test_membership_private_room_perms ( self ) :
def test_membership_private_room_perms ( self ) - > None :
room = self . created_rmid
# get membership of self, get membership of other, private room + invite
# expect all 403s
@ -264,7 +267,7 @@ class RoomPermissionsTestCase(RoomBase):
members = [ self . user_id , self . rmcreator_id ] , room = room , expect_code = 200
)
def test_membership_public_room_perms ( self ) :
def test_membership_public_room_perms ( self ) - > None :
room = self . created_public_rmid
# get membership of self, get membership of other, public room + invite
# expect 403
@ -287,7 +290,7 @@ class RoomPermissionsTestCase(RoomBase):
members = [ self . user_id , self . rmcreator_id ] , room = room , expect_code = 200
)
def test_invited_permissions ( self ) :
def test_invited_permissions ( self ) - > None :
room = self . created_rmid
self . helper . invite ( room = room , src = self . rmcreator_id , targ = self . user_id )
@ -310,7 +313,7 @@ class RoomPermissionsTestCase(RoomBase):
expect_code = 403 ,
)
def test_joined_permissions ( self ) :
def test_joined_permissions ( self ) - > None :
room = self . created_rmid
self . helper . invite ( room = room , src = self . rmcreator_id , targ = self . user_id )
self . helper . join ( room = room , user = self . user_id )
@ -348,7 +351,7 @@ class RoomPermissionsTestCase(RoomBase):
# set left of self, expect 200
self . helper . leave ( room = room , user = self . user_id )
def test_leave_permissions ( self ) :
def test_leave_permissions ( self ) - > None :
room = self . created_rmid
self . helper . invite ( room = room , src = self . rmcreator_id , targ = self . user_id )
self . helper . join ( room = room , user = self . user_id )
@ -383,7 +386,7 @@ class RoomPermissionsTestCase(RoomBase):
)
# tests the "from banned" line from the table in https://spec.matrix.org/unstable/client-server-api/#mroommember
def test_member_event_from_ban ( self ) :
def test_member_event_from_ban ( self ) - > None :
room = self . created_rmid
self . helper . invite ( room = room , src = self . rmcreator_id , targ = self . user_id )
self . helper . join ( room = room , user = self . user_id )
@ -475,21 +478,21 @@ class RoomsMemberListTestCase(RoomBase):
user_id = " @sid1:red "
def test_get_member_list ( self ) :
def test_get_member_list ( self ) - > None :
room_id = self . helper . create_room_as ( self . user_id )
channel = self . make_request ( " GET " , " /rooms/ %s /members " % room_id )
self . assertEqual ( 200 , channel . code , msg = channel . result [ " body " ] )
def test_get_member_list_no_room ( self ) :
def test_get_member_list_no_room ( self ) - > None :
channel = self . make_request ( " GET " , " /rooms/roomdoesnotexist/members " )
self . assertEqual ( 403 , channel . code , msg = channel . result [ " body " ] )
def test_get_member_list_no_permission ( self ) :
def test_get_member_list_no_permission ( self ) - > None :
room_id = self . helper . create_room_as ( " @some_other_guy:red " )
channel = self . make_request ( " GET " , " /rooms/ %s /members " % room_id )
self . assertEqual ( 403 , channel . code , msg = channel . result [ " body " ] )
def test_get_member_list_no_permission_with_at_token ( self ) :
def test_get_member_list_no_permission_with_at_token ( self ) - > None :
"""
Tests that a stranger to the room cannot get the member list
( in the case that they use an at token ) .
@ -509,7 +512,7 @@ class RoomsMemberListTestCase(RoomBase):
)
self . assertEqual ( 403 , channel . code , msg = channel . result [ " body " ] )
def test_get_member_list_no_permission_former_member ( self ) :
def test_get_member_list_no_permission_former_member ( self ) - > None :
"""
Tests that a former member of the room can not get the member list .
"""
@ -529,7 +532,7 @@ class RoomsMemberListTestCase(RoomBase):
channel = self . make_request ( " GET " , " /rooms/ %s /members " % room_id )
self . assertEqual ( 403 , channel . code , msg = channel . result [ " body " ] )
def test_get_member_list_no_permission_former_member_with_at_token ( self ) :
def test_get_member_list_no_permission_former_member_with_at_token ( self ) - > None :
"""
Tests that a former member of the room can not get the member list
( in the case that they use an at token ) .
@ -569,7 +572,7 @@ class RoomsMemberListTestCase(RoomBase):
)
self . assertEqual ( 403 , channel . code , msg = channel . result [ " body " ] )
def test_get_member_list_mixed_memberships ( self ) :
def test_get_member_list_mixed_memberships ( self ) - > None :
room_creator = " @some_other_guy:red "
room_id = self . helper . create_room_as ( room_creator )
room_path = " /rooms/ %s /members " % room_id
@ -594,26 +597,26 @@ class RoomsCreateTestCase(RoomBase):
user_id = " @sid1:red "
def test_post_room_no_keys ( self ) :
def test_post_room_no_keys ( self ) - > None :
# POST with no config keys, expect new room id
channel = self . make_request ( " POST " , " /createRoom " , " {} " )
self . assertEqual ( 200 , channel . code , channel . result )
self . assertTrue ( " room_id " in channel . json_body )
def test_post_room_visibility_key ( self ) :
def test_post_room_visibility_key ( self ) - > None :
# POST with visibility config key, expect new room id
channel = self . make_request ( " POST " , " /createRoom " , b ' { " visibility " : " private " } ' )
self . assertEqual ( 200 , channel . code )
self . assertTrue ( " room_id " in channel . json_body )
def test_post_room_custom_key ( self ) :
def test_post_room_custom_key ( self ) - > None :
# POST with custom config keys, expect new room id
channel = self . make_request ( " POST " , " /createRoom " , b ' { " custom " : " stuff " } ' )
self . assertEqual ( 200 , channel . code )
self . assertTrue ( " room_id " in channel . json_body )
def test_post_room_known_and_unknown_keys ( self ) :
def test_post_room_known_and_unknown_keys ( self ) - > None :
# POST with custom + known config keys, expect new room id
channel = self . make_request (
" POST " , " /createRoom " , b ' { " visibility " : " private " , " custom " : " things " } '
@ -621,7 +624,7 @@ class RoomsCreateTestCase(RoomBase):
self . assertEqual ( 200 , channel . code )
self . assertTrue ( " room_id " in channel . json_body )
def test_post_room_invalid_content ( self ) :
def test_post_room_invalid_content ( self ) - > None :
# POST with invalid content / paths, expect 400
channel = self . make_request ( " POST " , " /createRoom " , b ' { " visibili ' )
self . assertEqual ( 400 , channel . code )
@ -629,7 +632,7 @@ class RoomsCreateTestCase(RoomBase):
channel = self . make_request ( " POST " , " /createRoom " , b ' [ " hello " ] ' )
self . assertEqual ( 400 , channel . code )
def test_post_room_invitees_invalid_mxid ( self ) :
def test_post_room_invitees_invalid_mxid ( self ) - > None :
# POST with invalid invitee, see https://github.com/matrix-org/synapse/issues/4088
# Note the trailing space in the MXID here!
channel = self . make_request (
@ -638,7 +641,7 @@ class RoomsCreateTestCase(RoomBase):
self . assertEqual ( 400 , channel . code )
@unittest . override_config ( { " rc_invites " : { " per_room " : { " burst_count " : 3 } } } )
def test_post_room_invitees_ratelimit ( self ) :
def test_post_room_invitees_ratelimit ( self ) - > None :
""" Test that invites sent when creating a room are ratelimited by a RateLimiter,
which ratelimits them correctly , including by not limiting when the requester is
exempt from ratelimiting .
@ -674,7 +677,7 @@ class RoomsCreateTestCase(RoomBase):
channel = self . make_request ( " POST " , " /createRoom " , content )
self . assertEqual ( 200 , channel . code )
def test_spam_checker_may_join_room ( self ) :
def test_spam_checker_may_join_room ( self ) - > None :
""" Tests that the user_may_join_room spam checker callback is correctly bypassed
when creating a new room .
"""
@ -704,12 +707,12 @@ class RoomTopicTestCase(RoomBase):
user_id = " @sid1:red "
def prepare ( self , reactor , clock , hs ) :
def prepare ( self , reactor : MemoryReactor , clock : Clock , hs : HomeServer ) - > None :
# create the room
self . room_id = self . helper . create_room_as ( self . user_id )
self . path = " /rooms/ %s /state/m.room.topic " % ( self . room_id , )
def test_invalid_puts ( self ) :
def test_invalid_puts ( self ) - > None :
# missing keys or invalid json
channel = self . make_request ( " PUT " , self . path , " {} " )
self . assertEqual ( 400 , channel . code , msg = channel . result [ " body " ] )
@ -736,7 +739,7 @@ class RoomTopicTestCase(RoomBase):
channel = self . make_request ( " PUT " , self . path , content )
self . assertEqual ( 400 , channel . code , msg = channel . result [ " body " ] )
def test_rooms_topic ( self ) :
def test_rooms_topic ( self ) - > None :
# nothing should be there
channel = self . make_request ( " GET " , self . path )
self . assertEqual ( 404 , channel . code , msg = channel . result [ " body " ] )
@ -751,7 +754,7 @@ class RoomTopicTestCase(RoomBase):
self . assertEqual ( 200 , channel . code , msg = channel . result [ " body " ] )
self . assert_dict ( json . loads ( content ) , channel . json_body )
def test_rooms_topic_with_extra_keys ( self ) :
def test_rooms_topic_with_extra_keys ( self ) - > None :
# valid put with extra keys
content = ' { " topic " : " Seasons " , " subtopic " : " Summer " } '
channel = self . make_request ( " PUT " , self . path , content )
@ -768,10 +771,10 @@ class RoomMemberStateTestCase(RoomBase):
user_id = " @sid1:red "
def prepare ( self , reactor , clock , hs ) :
def prepare ( self , reactor : MemoryReactor , clock : Clock , hs : HomeServer ) - > None :
self . room_id = self . helper . create_room_as ( self . user_id )
def test_invalid_puts ( self ) :
def test_invalid_puts ( self ) - > None :
path = " /rooms/ %s /state/m.room.member/ %s " % ( self . room_id , self . user_id )
# missing keys or invalid json
channel = self . make_request ( " PUT " , path , " {} " )
@ -801,7 +804,7 @@ class RoomMemberStateTestCase(RoomBase):
channel = self . make_request ( " PUT " , path , content . encode ( " ascii " ) )
self . assertEqual ( 400 , channel . code , msg = channel . result [ " body " ] )
def test_rooms_members_self ( self ) :
def test_rooms_members_self ( self ) - > None :
path = " /rooms/ %s /state/m.room.member/ %s " % (
urlparse . quote ( self . room_id ) ,
self . user_id ,
@ -812,13 +815,13 @@ class RoomMemberStateTestCase(RoomBase):
channel = self . make_request ( " PUT " , path , content . encode ( " ascii " ) )
self . assertEqual ( 200 , channel . code , msg = channel . result [ " body " ] )
channel = self . make_request ( " GET " , path , None )
channel = self . make_request ( " GET " , path , content = b " " )
self . assertEqual ( 200 , channel . code , msg = channel . result [ " body " ] )
expected_response = { " membership " : Membership . JOIN }
self . assertEqual ( expected_response , channel . json_body )
def test_rooms_members_other ( self ) :
def test_rooms_members_other ( self ) - > None :
self . other_id = " @zzsid1:red "
path = " /rooms/ %s /state/m.room.member/ %s " % (
urlparse . quote ( self . room_id ) ,
@ -830,11 +833,11 @@ class RoomMemberStateTestCase(RoomBase):
channel = self . make_request ( " PUT " , path , content )
self . assertEqual ( 200 , channel . code , msg = channel . result [ " body " ] )
channel = self . make_request ( " GET " , path , None )
channel = self . make_request ( " GET " , path , content = b " " )
self . assertEqual ( 200 , channel . code , msg = channel . result [ " body " ] )
self . assertEqual ( json . loads ( content ) , channel . json_body )
def test_rooms_members_other_custom_keys ( self ) :
def test_rooms_members_other_custom_keys ( self ) - > None :
self . other_id = " @zzsid1:red "
path = " /rooms/ %s /state/m.room.member/ %s " % (
urlparse . quote ( self . room_id ) ,
@ -849,7 +852,7 @@ class RoomMemberStateTestCase(RoomBase):
channel = self . make_request ( " PUT " , path , content )
self . assertEqual ( 200 , channel . code , msg = channel . result [ " body " ] )
channel = self . make_request ( " GET " , path , None )
channel = self . make_request ( " GET " , path , content = b " " )
self . assertEqual ( 200 , channel . code , msg = channel . result [ " body " ] )
self . assertEqual ( json . loads ( content ) , channel . json_body )
@ -866,7 +869,7 @@ class RoomInviteRatelimitTestCase(RoomBase):
@unittest . override_config (
{ " rc_invites " : { " per_room " : { " per_second " : 0.5 , " burst_count " : 3 } } }
)
def test_invites_by_rooms_ratelimit ( self ) :
def test_invites_by_rooms_ratelimit ( self ) - > None :
""" Tests that invites in a room are actually rate-limited. """
room_id = self . helper . create_room_as ( self . user_id )
@ -878,7 +881,7 @@ class RoomInviteRatelimitTestCase(RoomBase):
@unittest . override_config (
{ " rc_invites " : { " per_user " : { " per_second " : 0.5 , " burst_count " : 3 } } }
)
def test_invites_by_users_ratelimit ( self ) :
def test_invites_by_users_ratelimit ( self ) - > None :
""" Tests that invites to a specific user are actually rate-limited. """
for _ in range ( 3 ) :
@ -897,7 +900,7 @@ class RoomJoinTestCase(RoomBase):
room . register_servlets ,
]
def prepare ( self , reactor , clock , homeserver ) :
def prepare ( self , reactor : MemoryReactor , clock : Clock , hs : HomeServer ) - > None :
self . user1 = self . register_user ( " thomas " , " hackme " )
self . tok1 = self . login ( " thomas " , " hackme " )
@ -908,7 +911,7 @@ class RoomJoinTestCase(RoomBase):
self . room2 = self . helper . create_room_as ( room_creator = self . user1 , tok = self . tok1 )
self . room3 = self . helper . create_room_as ( room_creator = self . user1 , tok = self . tok1 )
def test_spam_checker_may_join_room ( self ) :
def test_spam_checker_may_join_room ( self ) - > None :
""" Tests that the user_may_join_room spam checker callback is correctly called
and blocks room joins when needed .
"""
@ -975,8 +978,8 @@ class RoomJoinRatelimitTestCase(RoomBase):
room . register_servlets ,
]
def prepare ( self , reactor , clock , homeserver ) :
super ( ) . prepare ( reactor , clock , home server )
def prepare ( self , reactor : MemoryReactor , clock : Clock , hs : HomeServer ) - > None :
super ( ) . prepare ( reactor , clock , hs )
# profile changes expect that the user is actually registered
user = UserID . from_string ( self . user_id )
self . get_success ( self . register_user ( user . localpart , " supersecretpassword " ) )
@ -984,7 +987,7 @@ class RoomJoinRatelimitTestCase(RoomBase):
@unittest . override_config (
{ " rc_joins " : { " local " : { " per_second " : 0.5 , " burst_count " : 3 } } }
)
def test_join_local_ratelimit ( self ) :
def test_join_local_ratelimit ( self ) - > None :
""" Tests that local joins are actually rate-limited. """
for _ in range ( 3 ) :
self . helper . create_room_as ( self . user_id )
@ -994,7 +997,7 @@ class RoomJoinRatelimitTestCase(RoomBase):
@unittest . override_config (
{ " rc_joins " : { " local " : { " per_second " : 0.5 , " burst_count " : 3 } } }
)
def test_join_local_ratelimit_profile_change ( self ) :
def test_join_local_ratelimit_profile_change ( self ) - > None :
""" Tests that sending a profile update into all of the user ' s joined rooms isn ' t
rate - limited by the rate - limiter on joins . """
@ -1031,7 +1034,7 @@ class RoomJoinRatelimitTestCase(RoomBase):
@unittest . override_config (
{ " rc_joins " : { " local " : { " per_second " : 0.5 , " burst_count " : 3 } } }
)
def test_join_local_ratelimit_idempotent ( self ) :
def test_join_local_ratelimit_idempotent ( self ) - > None :
""" Tests that the room join endpoints remain idempotent despite rate-limiting
on room joins . """
room_id = self . helper . create_room_as ( self . user_id )
@ -1056,7 +1059,7 @@ class RoomJoinRatelimitTestCase(RoomBase):
" autocreate_auto_join_rooms " : True ,
} ,
)
def test_autojoin_rooms ( self ) :
def test_autojoin_rooms ( self ) - > None :
user_id = self . register_user ( " testuser " , " password " )
# Check that the new user successfully joined the four rooms
@ -1071,10 +1074,10 @@ class RoomMessagesTestCase(RoomBase):
user_id = " @sid1:red "
def prepare ( self , reactor , clock , hs ) :
def prepare ( self , reactor : MemoryReactor , clock : Clock , hs : HomeServer ) - > None :
self . room_id = self . helper . create_room_as ( self . user_id )
def test_invalid_puts ( self ) :
def test_invalid_puts ( self ) - > None :
path = " /rooms/ %s /send/m.room.message/mid1 " % ( urlparse . quote ( self . room_id ) )
# missing keys or invalid json
channel = self . make_request ( " PUT " , path , b " {} " )
@ -1095,7 +1098,7 @@ class RoomMessagesTestCase(RoomBase):
channel = self . make_request ( " PUT " , path , b " " )
self . assertEqual ( 400 , channel . code , msg = channel . result [ " body " ] )
def test_rooms_messages_sent ( self ) :
def test_rooms_messages_sent ( self ) - > None :
path = " /rooms/ %s /send/m.room.message/mid1 " % ( urlparse . quote ( self . room_id ) )
content = b ' { " body " : " test " , " msgtype " : { " type " : " a " }} '
@ -1119,11 +1122,11 @@ class RoomInitialSyncTestCase(RoomBase):
user_id = " @sid1:red "
def prepare ( self , reactor , clock , hs ) :
def prepare ( self , reactor : MemoryReactor , clock : Clock , hs : HomeServer ) - > None :
# create the room
self . room_id = self . helper . create_room_as ( self . user_id )
def test_initial_sync ( self ) :
def test_initial_sync ( self ) - > None :
channel = self . make_request ( " GET " , " /rooms/ %s /initialSync " % self . room_id )
self . assertEqual ( 200 , channel . code )
@ -1131,7 +1134,7 @@ class RoomInitialSyncTestCase(RoomBase):
self . assertEqual ( " join " , channel . json_body [ " membership " ] )
# Room state is easier to assert on if we unpack it into a dict
state = { }
state : JsonDict = { }
for event in channel . json_body [ " state " ] :
if " state_key " not in event :
continue
@ -1160,10 +1163,10 @@ class RoomMessageListTestCase(RoomBase):
user_id = " @sid1:red "
def prepare ( self , reactor , clock , hs ) :
def prepare ( self , reactor : MemoryReactor , clock : Clock , hs : HomeServer ) - > None :
self . room_id = self . helper . create_room_as ( self . user_id )
def test_topo_token_is_accepted ( self ) :
def test_topo_token_is_accepted ( self ) - > None :
token = " t1-0_0_0_0_0_0_0_0_0 "
channel = self . make_request (
" GET " , " /rooms/ %s /messages?access_token=x&from= %s " % ( self . room_id , token )
@ -1174,7 +1177,7 @@ class RoomMessageListTestCase(RoomBase):
self . assertTrue ( " chunk " in channel . json_body )
self . assertTrue ( " end " in channel . json_body )
def test_stream_token_is_accepted_for_fwd_pagianation ( self ) :
def test_stream_token_is_accepted_for_fwd_pagianation ( self ) - > None :
token = " s0_0_0_0_0_0_0_0_0 "
channel = self . make_request (
" GET " , " /rooms/ %s /messages?access_token=x&from= %s " % ( self . room_id , token )
@ -1185,7 +1188,7 @@ class RoomMessageListTestCase(RoomBase):
self . assertTrue ( " chunk " in channel . json_body )
self . assertTrue ( " end " in channel . json_body )
def test_room_messages_purge ( self ) :
def test_room_messages_purge ( self ) - > None :
store = self . hs . get_datastores ( ) . main
pagination_handler = self . hs . get_pagination_handler ( )
@ -1278,10 +1281,10 @@ class RoomSearchTestCase(unittest.HomeserverTestCase):
user_id = True
hijack_auth = False
def prepare ( self , reactor , clock , hs ) :
def prepare ( self , reactor : MemoryReactor , clock : Clock , hs : HomeServer ) - > None :
# Register the user who does the searching
self . user_id = self . register_user ( " user " , " pass " )
self . user_id2 = self . register_user ( " user " , " pass " )
self . access_token = self . login ( " user " , " pass " )
# Register the user who sends the message
@ -1289,12 +1292,12 @@ class RoomSearchTestCase(unittest.HomeserverTestCase):
self . other_access_token = self . login ( " otheruser " , " pass " )
# Create a room
self . room = self . helper . create_room_as ( self . user_id , tok = self . access_token )
self . room = self . helper . create_room_as ( self . user_id2 , tok = self . access_token )
# Invite the other person
self . helper . invite (
room = self . room ,
src = self . user_id ,
src = self . user_id2 ,
tok = self . access_token ,
targ = self . other_user_id ,
)
@ -1304,7 +1307,7 @@ class RoomSearchTestCase(unittest.HomeserverTestCase):
room = self . room , user = self . other_user_id , tok = self . other_access_token
)
def test_finds_message ( self ) :
def test_finds_message ( self ) - > None :
"""
The search functionality will search for content in messages if asked to
do so .
@ -1333,7 +1336,7 @@ class RoomSearchTestCase(unittest.HomeserverTestCase):
# No context was requested, so we should get none.
self . assertEqual ( results [ " results " ] [ 0 ] [ " context " ] , { } )
def test_include_context ( self ) :
def test_include_context ( self ) - > None :
"""
When event_context includes include_profile , profile information will be
included in the search response .
@ -1379,7 +1382,7 @@ class PublicRoomsRestrictedTestCase(unittest.HomeserverTestCase):
login . register_servlets ,
]
def make_homeserver ( self , reactor , clock ) :
def make_homeserver ( self , reactor : MemoryReactor , clock : Clock ) - > HomeServer :
self . url = b " /_matrix/client/r0/publicRooms "
@ -1389,11 +1392,11 @@ class PublicRoomsRestrictedTestCase(unittest.HomeserverTestCase):
return self . hs
def test_restricted_no_auth ( self ) :
def test_restricted_no_auth ( self ) - > None :
channel = self . make_request ( " GET " , self . url )
self . assertEqual ( channel . code , 401 , channel . result )
def test_restricted_auth ( self ) :
def test_restricted_auth ( self ) - > None :
self . register_user ( " user " , " pass " )
tok = self . login ( " user " , " pass " )
@ -1412,19 +1415,19 @@ class PublicRoomsTestRemoteSearchFallbackTestCase(unittest.HomeserverTestCase):
login . register_servlets ,
]
def make_homeserver ( self , reactor , clock ) :
def make_homeserver ( self , reactor : MemoryReactor , clock : Clock ) - > HomeServer :
return self . setup_test_homeserver ( federation_client = Mock ( ) )
def prepare ( self , reactor , clock , hs ) :
def prepare ( self , reactor : MemoryReactor , clock : Clock , hs : HomeServer ) - > None :
self . register_user ( " user " , " pass " )
self . token = self . login ( " user " , " pass " )
self . federation_client = hs . get_federation_client ( )
def test_simple ( self ) :
def test_simple ( self ) - > None :
" Simple test for searching rooms over federation "
self . federation_client . get_public_rooms . side_effect = (
lambda * a , * * k : defer . succeed ( { } )
self . federation_client . get_public_rooms . side_effect = lambda * a , * * k : defer . succeed ( # type: ignore[attr-defined]
{ }
)
search_filter = { " generic_search_term " : " foobar " }
@ -1437,7 +1440,7 @@ class PublicRoomsTestRemoteSearchFallbackTestCase(unittest.HomeserverTestCase):
)
self . assertEqual ( channel . code , 200 , channel . result )
self . federation_client . get_public_rooms . assert_called_once_with (
self . federation_client . get_public_rooms . assert_called_once_with ( # type: ignore[attr-defined]
" testserv " ,
limit = 100 ,
since_token = None ,
@ -1446,12 +1449,12 @@ class PublicRoomsTestRemoteSearchFallbackTestCase(unittest.HomeserverTestCase):
third_party_instance_id = None ,
)
def test_fallback ( self ) :
def test_fallback ( self ) - > None :
" Test that searching public rooms over federation falls back if it gets a 404 "
# The `get_public_rooms` should be called again if the first call fails
# with a 404, when using search filters.
self . federation_client . get_public_rooms . side_effect = (
self . federation_client . get_public_rooms . side_effect = ( # type: ignore[attr-defined]
HttpResponseException ( 404 , " Not Found " , b " " ) ,
defer . succeed ( { } ) ,
)
@ -1466,7 +1469,7 @@ class PublicRoomsTestRemoteSearchFallbackTestCase(unittest.HomeserverTestCase):
)
self . assertEqual ( channel . code , 200 , channel . result )
self . federation_client . get_public_rooms . assert_has_calls (
self . federation_client . get_public_rooms . assert_has_calls ( # type: ignore[attr-defined]
[
call (
" testserv " ,
@ -1497,14 +1500,14 @@ class PerRoomProfilesForbiddenTestCase(unittest.HomeserverTestCase):
profile . register_servlets ,
]
def make_homeserver ( self , reactor , clock ) :
def make_homeserver ( self , reactor : MemoryReactor , clock : Clock ) - > HomeServer :
config = self . default_config ( )
config [ " allow_per_room_profiles " ] = False
self . hs = self . setup_test_homeserver ( config = config )
return self . hs
def prepare ( self , reactor , clock , homeserver ) :
def prepare ( self , reactor : MemoryReactor , clock : Clock , hs : HomeServer ) - > None :
self . user_id = self . register_user ( " test " , " test " )
self . tok = self . login ( " test " , " test " )
@ -1522,7 +1525,7 @@ class PerRoomProfilesForbiddenTestCase(unittest.HomeserverTestCase):
self . room_id = self . helper . create_room_as ( self . user_id , tok = self . tok )
def test_per_room_profile_forbidden ( self ) :
def test_per_room_profile_forbidden ( self ) - > None :
data = { " membership " : " join " , " displayname " : " other test user " }
request_data = json . dumps ( data )
channel = self . make_request (
@ -1557,7 +1560,7 @@ class RoomMembershipReasonTestCase(unittest.HomeserverTestCase):
login . register_servlets ,
]
def prepare ( self , reactor , clock , homeserver ) :
def prepare ( self , reactor : MemoryReactor , clock : Clock , hs : HomeServer ) - > None :
self . creator = self . register_user ( " creator " , " test " )
self . creator_tok = self . login ( " creator " , " test " )
@ -1566,7 +1569,7 @@ class RoomMembershipReasonTestCase(unittest.HomeserverTestCase):
self . room_id = self . helper . create_room_as ( self . creator , tok = self . creator_tok )
def test_join_reason ( self ) :
def test_join_reason ( self ) - > None :
reason = " hello "
channel = self . make_request (
" POST " ,
@ -1578,7 +1581,7 @@ class RoomMembershipReasonTestCase(unittest.HomeserverTestCase):
self . _check_for_reason ( reason )
def test_leave_reason ( self ) :
def test_leave_reason ( self ) - > None :
self . helper . join ( self . room_id , user = self . second_user_id , tok = self . second_tok )
reason = " hello "
@ -1592,7 +1595,7 @@ class RoomMembershipReasonTestCase(unittest.HomeserverTestCase):
self . _check_for_reason ( reason )
def test_kick_reason ( self ) :
def test_kick_reason ( self ) - > None :
self . helper . join ( self . room_id , user = self . second_user_id , tok = self . second_tok )
reason = " hello "
@ -1606,7 +1609,7 @@ class RoomMembershipReasonTestCase(unittest.HomeserverTestCase):
self . _check_for_reason ( reason )
def test_ban_reason ( self ) :
def test_ban_reason ( self ) - > None :
self . helper . join ( self . room_id , user = self . second_user_id , tok = self . second_tok )
reason = " hello "
@ -1620,7 +1623,7 @@ class RoomMembershipReasonTestCase(unittest.HomeserverTestCase):
self . _check_for_reason ( reason )
def test_unban_reason ( self ) :
def test_unban_reason ( self ) - > None :
reason = " hello "
channel = self . make_request (
" POST " ,
@ -1632,7 +1635,7 @@ class RoomMembershipReasonTestCase(unittest.HomeserverTestCase):
self . _check_for_reason ( reason )
def test_invite_reason ( self ) :
def test_invite_reason ( self ) - > None :
reason = " hello "
channel = self . make_request (
" POST " ,
@ -1644,7 +1647,7 @@ class RoomMembershipReasonTestCase(unittest.HomeserverTestCase):
self . _check_for_reason ( reason )
def test_reject_invite_reason ( self ) :
def test_reject_invite_reason ( self ) - > None :
self . helper . invite (
self . room_id ,
src = self . creator ,
@ -1663,7 +1666,7 @@ class RoomMembershipReasonTestCase(unittest.HomeserverTestCase):
self . _check_for_reason ( reason )
def _check_for_reason ( self , reason ) :
def _check_for_reason ( self , reason : str ) - > None :
channel = self . make_request (
" GET " ,
" /_matrix/client/r0/rooms/ {} /state/m.room.member/ {} " . format (
@ -1704,12 +1707,12 @@ class LabelsTestCase(unittest.HomeserverTestCase):
" org.matrix.not_labels " : [ " #notfun " ] ,
}
def prepare ( self , reactor , clock , homeserver ) :
def prepare ( self , reactor : MemoryReactor , clock : Clock , hs : HomeServer ) - > None :
self . user_id = self . register_user ( " test " , " test " )
self . tok = self . login ( " test " , " test " )
self . room_id = self . helper . create_room_as ( self . user_id , tok = self . tok )
def test_context_filter_labels ( self ) :
def test_context_filter_labels ( self ) - > None :
""" Test that we can filter by a label on a /context request. """
event_id = self . _send_labelled_messages_in_room ( )
@ -1739,7 +1742,7 @@ class LabelsTestCase(unittest.HomeserverTestCase):
events_after [ 0 ] [ " content " ] [ " body " ] , " with right label " , events_after [ 0 ]
)
def test_context_filter_not_labels ( self ) :
def test_context_filter_not_labels ( self ) - > None :
""" Test that we can filter by the absence of a label on a /context request. """
event_id = self . _send_labelled_messages_in_room ( )
@ -1772,7 +1775,7 @@ class LabelsTestCase(unittest.HomeserverTestCase):
events_after [ 1 ] [ " content " ] [ " body " ] , " with two wrong labels " , events_after [ 1 ]
)
def test_context_filter_labels_not_labels ( self ) :
def test_context_filter_labels_not_labels ( self ) - > None :
""" Test that we can filter by both a label and the absence of another label on a
/ context request .
"""
@ -1801,7 +1804,7 @@ class LabelsTestCase(unittest.HomeserverTestCase):
events_after [ 0 ] [ " content " ] [ " body " ] , " with wrong label " , events_after [ 0 ]
)
def test_messages_filter_labels ( self ) :
def test_messages_filter_labels ( self ) - > None :
""" Test that we can filter by a label on a /messages request. """
self . _send_labelled_messages_in_room ( )
@ -1818,7 +1821,7 @@ class LabelsTestCase(unittest.HomeserverTestCase):
self . assertEqual ( events [ 0 ] [ " content " ] [ " body " ] , " with right label " , events [ 0 ] )
self . assertEqual ( events [ 1 ] [ " content " ] [ " body " ] , " with right label " , events [ 1 ] )
def test_messages_filter_not_labels ( self ) :
def test_messages_filter_not_labels ( self ) - > None :
""" Test that we can filter by the absence of a label on a /messages request. """
self . _send_labelled_messages_in_room ( )
@ -1839,7 +1842,7 @@ class LabelsTestCase(unittest.HomeserverTestCase):
events [ 3 ] [ " content " ] [ " body " ] , " with two wrong labels " , events [ 3 ]
)
def test_messages_filter_labels_not_labels ( self ) :
def test_messages_filter_labels_not_labels ( self ) - > None :
""" Test that we can filter by both a label and the absence of another label on a
/ messages request .
"""
@ -1862,7 +1865,7 @@ class LabelsTestCase(unittest.HomeserverTestCase):
self . assertEqual ( len ( events ) , 1 , [ event [ " content " ] for event in events ] )
self . assertEqual ( events [ 0 ] [ " content " ] [ " body " ] , " with wrong label " , events [ 0 ] )
def test_search_filter_labels ( self ) :
def test_search_filter_labels ( self ) - > None :
""" Test that we can filter by a label on a /search request. """
request_data = json . dumps (
{
@ -1899,7 +1902,7 @@ class LabelsTestCase(unittest.HomeserverTestCase):
results [ 1 ] [ " result " ] [ " content " ] [ " body " ] ,
)
def test_search_filter_not_labels ( self ) :
def test_search_filter_not_labels ( self ) - > None :
""" Test that we can filter by the absence of a label on a /search request. """
request_data = json . dumps (
{
@ -1946,7 +1949,7 @@ class LabelsTestCase(unittest.HomeserverTestCase):
results [ 3 ] [ " result " ] [ " content " ] [ " body " ] ,
)
def test_search_filter_labels_not_labels ( self ) :
def test_search_filter_labels_not_labels ( self ) - > None :
""" Test that we can filter by both a label and the absence of another label on a
/ search request .
"""
@ -1980,7 +1983,7 @@ class LabelsTestCase(unittest.HomeserverTestCase):
results [ 0 ] [ " result " ] [ " content " ] [ " body " ] ,
)
def _send_labelled_messages_in_room ( self ) :
def _send_labelled_messages_in_room ( self ) - > str :
""" Sends several messages to a room with different labels (or without any) to test
filtering by label .
Returns :
@ -2056,12 +2059,12 @@ class RelationsTestCase(unittest.HomeserverTestCase):
login . register_servlets ,
]
def default_config ( self ) :
def default_config ( self ) - > Dict [ str , Any ] :
config = super ( ) . default_config ( )
config [ " experimental_features " ] = { " msc3440_enabled " : True }
return config
def prepare ( self , reactor , clock , homeserver ) :
def prepare ( self , reactor : MemoryReactor , clock : Clock , hs : HomeServer ) - > None :
self . user_id = self . register_user ( " test " , " test " )
self . tok = self . login ( " test " , " test " )
self . room_id = self . helper . create_room_as ( self . user_id , tok = self . tok )
@ -2136,7 +2139,7 @@ class RelationsTestCase(unittest.HomeserverTestCase):
return channel . json_body [ " chunk " ]
def test_filter_relation_senders ( self ) :
def test_filter_relation_senders ( self ) - > None :
# Messages which second user reacted to.
filter = { " io.element.relation_senders " : [ self . second_user_id ] }
chunk = self . _filter_messages ( filter )
@ -2159,7 +2162,7 @@ class RelationsTestCase(unittest.HomeserverTestCase):
[ c [ " event_id " ] for c in chunk ] , [ self . event_id_1 , self . event_id_2 ]
)
def test_filter_relation_type ( self ) :
def test_filter_relation_type ( self ) - > None :
# Messages which have annotations.
filter = { " io.element.relation_types " : [ RelationTypes . ANNOTATION ] }
chunk = self . _filter_messages ( filter )
@ -2185,7 +2188,7 @@ class RelationsTestCase(unittest.HomeserverTestCase):
[ c [ " event_id " ] for c in chunk ] , [ self . event_id_1 , self . event_id_2 ]
)
def test_filter_relation_senders_and_type ( self ) :
def test_filter_relation_senders_and_type ( self ) - > None :
# Messages which second user reacted to.
filter = {
" io.element.relation_senders " : [ self . second_user_id ] ,
@ -2205,7 +2208,7 @@ class ContextTestCase(unittest.HomeserverTestCase):
account . register_servlets ,
]
def prepare ( self , reactor , clock , homeserver ) :
def prepare ( self , reactor : MemoryReactor , clock : Clock , hs : HomeServer ) - > None :
self . user_id = self . register_user ( " user " , " password " )
self . tok = self . login ( " user " , " password " )
self . room_id = self . helper . create_room_as (
@ -2218,7 +2221,7 @@ class ContextTestCase(unittest.HomeserverTestCase):
self . helper . invite ( self . room_id , self . user_id , self . other_user_id , tok = self . tok )
self . helper . join ( self . room_id , self . other_user_id , tok = self . other_tok )
def test_erased_sender ( self ) :
def test_erased_sender ( self ) - > None :
""" Test that an erasure request results in the requester ' s events being hidden
from any new member of the room .
"""
@ -2332,7 +2335,7 @@ class RoomAliasListTestCase(unittest.HomeserverTestCase):
room . register_servlets ,
]
def prepare ( self , reactor , clock , homeserver ) :
def prepare ( self , reactor : MemoryReactor , clock : Clock , hs : HomeServer ) - > None :
self . room_owner = self . register_user ( " room_owner " , " test " )
self . room_owner_tok = self . login ( " room_owner " , " test " )
@ -2340,17 +2343,17 @@ class RoomAliasListTestCase(unittest.HomeserverTestCase):
self . room_owner , tok = self . room_owner_tok
)
def test_no_aliases ( self ) :
def test_no_aliases ( self ) - > None :
res = self . _get_aliases ( self . room_owner_tok )
self . assertEqual ( res [ " aliases " ] , [ ] )
def test_not_in_room ( self ) :
def test_not_in_room ( self ) - > None :
self . register_user ( " user " , " test " )
user_tok = self . login ( " user " , " test " )
res = self . _get_aliases ( user_tok , expected_code = 403 )
self . assertEqual ( res [ " errcode " ] , " M_FORBIDDEN " )
def test_admin_user ( self ) :
def test_admin_user ( self ) - > None :
alias1 = self . _random_alias ( )
self . _set_alias_via_directory ( alias1 )
@ -2360,7 +2363,7 @@ class RoomAliasListTestCase(unittest.HomeserverTestCase):
res = self . _get_aliases ( user_tok )
self . assertEqual ( res [ " aliases " ] , [ alias1 ] )
def test_with_aliases ( self ) :
def test_with_aliases ( self ) - > None :
alias1 = self . _random_alias ( )
alias2 = self . _random_alias ( )
@ -2370,7 +2373,7 @@ class RoomAliasListTestCase(unittest.HomeserverTestCase):
res = self . _get_aliases ( self . room_owner_tok )
self . assertEqual ( set ( res [ " aliases " ] ) , { alias1 , alias2 } )
def test_peekable_room ( self ) :
def test_peekable_room ( self ) - > None :
alias1 = self . _random_alias ( )
self . _set_alias_via_directory ( alias1 )
@ -2404,7 +2407,7 @@ class RoomAliasListTestCase(unittest.HomeserverTestCase):
def _random_alias ( self ) - > str :
return RoomAlias ( random_string ( 5 ) , self . hs . hostname ) . to_string ( )
def _set_alias_via_directory ( self , alias : str , expected_code : int = 200 ) :
def _set_alias_via_directory ( self , alias : str , expected_code : int = 200 ) - > None :
url = " /_matrix/client/r0/directory/room/ " + alias
data = { " room_id " : self . room_id }
request_data = json . dumps ( data )
@ -2423,7 +2426,7 @@ class RoomCanonicalAliasTestCase(unittest.HomeserverTestCase):
room . register_servlets ,
]
def prepare ( self , reactor , clock , homeserver ) :
def prepare ( self , reactor : MemoryReactor , clock : Clock , hs : HomeServer ) - > None :
self . room_owner = self . register_user ( " room_owner " , " test " )
self . room_owner_tok = self . login ( " room_owner " , " test " )
@ -2434,7 +2437,7 @@ class RoomCanonicalAliasTestCase(unittest.HomeserverTestCase):
self . alias = " #alias:test "
self . _set_alias_via_directory ( self . alias )
def _set_alias_via_directory ( self , alias : str , expected_code : int = 200 ) :
def _set_alias_via_directory ( self , alias : str , expected_code : int = 200 ) - > None :
url = " /_matrix/client/r0/directory/room/ " + alias
data = { " room_id " : self . room_id }
request_data = json . dumps ( data )
@ -2456,7 +2459,9 @@ class RoomCanonicalAliasTestCase(unittest.HomeserverTestCase):
self . assertIsInstance ( res , dict )
return res
def _set_canonical_alias ( self , content : str , expected_code : int = 200 ) - > JsonDict :
def _set_canonical_alias (
self , content : JsonDict , expected_code : int = 200
) - > JsonDict :
""" Calls the endpoint under test. returns the json response object. """
channel = self . make_request (
" PUT " ,
@ -2469,7 +2474,7 @@ class RoomCanonicalAliasTestCase(unittest.HomeserverTestCase):
self . assertIsInstance ( res , dict )
return res
def test_canonical_alias ( self ) :
def test_canonical_alias ( self ) - > None :
""" Test a basic alias message. """
# There is no canonical alias to start with.
self . _get_canonical_alias ( expected_code = 404 )
@ -2488,7 +2493,7 @@ class RoomCanonicalAliasTestCase(unittest.HomeserverTestCase):
res = self . _get_canonical_alias ( )
self . assertEqual ( res , { } )
def test_alt_aliases ( self ) :
def test_alt_aliases ( self ) - > None :
""" Test a canonical alias message with alt_aliases. """
# Create an alias.
self . _set_canonical_alias ( { " alt_aliases " : [ self . alias ] } )
@ -2504,7 +2509,7 @@ class RoomCanonicalAliasTestCase(unittest.HomeserverTestCase):
res = self . _get_canonical_alias ( )
self . assertEqual ( res , { } )
def test_alias_alt_aliases ( self ) :
def test_alias_alt_aliases ( self ) - > None :
""" Test a canonical alias message with an alias and alt_aliases. """
# Create an alias.
self . _set_canonical_alias ( { " alias " : self . alias , " alt_aliases " : [ self . alias ] } )
@ -2520,7 +2525,7 @@ class RoomCanonicalAliasTestCase(unittest.HomeserverTestCase):
res = self . _get_canonical_alias ( )
self . assertEqual ( res , { } )
def test_partial_modify ( self ) :
def test_partial_modify ( self ) - > None :
""" Test removing only the alt_aliases. """
# Create an alias.
self . _set_canonical_alias ( { " alias " : self . alias , " alt_aliases " : [ self . alias ] } )
@ -2536,7 +2541,7 @@ class RoomCanonicalAliasTestCase(unittest.HomeserverTestCase):
res = self . _get_canonical_alias ( )
self . assertEqual ( res , { " alias " : self . alias } )
def test_add_alias ( self ) :
def test_add_alias ( self ) - > None :
""" Test removing only the alt_aliases. """
# Create an additional alias.
second_alias = " #second:test "
@ -2556,7 +2561,7 @@ class RoomCanonicalAliasTestCase(unittest.HomeserverTestCase):
res , { " alias " : self . alias , " alt_aliases " : [ self . alias , second_alias ] }
)
def test_bad_data ( self ) :
def test_bad_data ( self ) - > None :
""" Invalid data for alt_aliases should cause errors. """
self . _set_canonical_alias ( { " alt_aliases " : " @bad:test " } , expected_code = 400 )
self . _set_canonical_alias ( { " alt_aliases " : None } , expected_code = 400 )
@ -2566,7 +2571,7 @@ class RoomCanonicalAliasTestCase(unittest.HomeserverTestCase):
self . _set_canonical_alias ( { " alt_aliases " : True } , expected_code = 400 )
self . _set_canonical_alias ( { " alt_aliases " : { } } , expected_code = 400 )
def test_bad_alias ( self ) :
def test_bad_alias ( self ) - > None :
""" An alias which does not point to the room raises a SynapseError. """
self . _set_canonical_alias ( { " alias " : " @unknown:test " } , expected_code = 400 )
self . _set_canonical_alias ( { " alt_aliases " : [ " @unknown:test " ] } , expected_code = 400 )
@ -2580,13 +2585,13 @@ class ThreepidInviteTestCase(unittest.HomeserverTestCase):
room . register_servlets ,
]
def prepare ( self , reactor , clock , homeserver ) :
def prepare ( self , reactor : MemoryReactor , clock : Clock , hs : HomeServer ) - > None :
self . user_id = self . register_user ( " thomas " , " hackme " )
self . tok = self . login ( " thomas " , " hackme " )
self . room_id = self . helper . create_room_as ( self . user_id , tok = self . tok )
def test_threepid_invite_spamcheck ( self ) :
def test_threepid_invite_spamcheck ( self ) - > None :
# Mock a few functions to prevent the test from failing due to failing to talk to
# a remote IS. We keep the mock for _mock_make_and_store_3pid_invite around so we
# can check its call_count later on during the test.