@@ -914,6 +914,56 @@ def _add_user_to_room(
914914 self .hs .get_storage ().persistence .persist_event (event , context )
915915 )
916916
917+ def test_local_user_leaving_room_remains_in_user_directory (self ) -> None :
918+ """We've chosen to simplify the user directory's implementation by
919+ always including local users. Ensure this invariant is maintained when
920+ a local user
921+ - leaves a room, and
922+ - leaves the last room they're in which is visible to this server.
923+
924+ This is user-visible if the "search_all_users" config option is on: the
925+ local user who left a room would no longer be searchable if this test fails!
926+ """
927+ alice = self .register_user ("alice" , "pass" )
928+ alice_token = self .login (alice , "pass" )
929+ bob = self .register_user ("bob" , "pass" )
930+ bob_token = self .login (bob , "pass" )
931+
932+ # Alice makes two public rooms, which Bob joins.
933+ room1 = self .helper .create_room_as (alice , is_public = True , tok = alice_token )
934+ room2 = self .helper .create_room_as (alice , is_public = True , tok = alice_token )
935+ self .helper .join (room1 , bob , tok = bob_token )
936+ self .helper .join (room2 , bob , tok = bob_token )
937+
938+ # The user directory tables are updated.
939+ users , in_public , in_private = self .get_success (
940+ self .user_dir_helper .get_tables ()
941+ )
942+ self .assertEqual (users , {alice , bob })
943+ self .assertEqual (
944+ in_public , {(alice , room1 ), (alice , room2 ), (bob , room1 ), (bob , room2 )}
945+ )
946+ self .assertEqual (in_private , set ())
947+
948+ # Alice leaves one room. She should still be in the directory.
949+ self .helper .leave (room1 , alice , tok = alice_token )
950+ users , in_public , in_private = self .get_success (
951+ self .user_dir_helper .get_tables ()
952+ )
953+ self .assertEqual (users , {alice , bob })
954+ self .assertEqual (in_public , {(alice , room2 ), (bob , room1 ), (bob , room2 )})
955+ self .assertEqual (in_private , set ())
956+
957+ # Alice leaves the other. She should still be in the directory.
958+ self .helper .leave (room2 , alice , tok = alice_token )
959+ self .wait_for_background_updates ()
960+ users , in_public , in_private = self .get_success (
961+ self .user_dir_helper .get_tables ()
962+ )
963+ self .assertEqual (users , {alice , bob })
964+ self .assertEqual (in_public , {(bob , room1 ), (bob , room2 )})
965+ self .assertEqual (in_private , set ())
966+
917967
918968class TestUserDirSearchDisabled (unittest .HomeserverTestCase ):
919969 servlets = [
0 commit comments