@@ -992,6 +992,67 @@ def test_prefer_local_users(self) -> None:
992
992
[self .assertIn (user , local_users ) for user in received_user_id_ordering [:3 ]]
993
993
[self .assertIn (user , remote_users ) for user in received_user_id_ordering [3 :]]
994
994
995
+ @override_config (
996
+ {
997
+ "user_directory" : {
998
+ "enabled" : True ,
999
+ "search_all_users" : True ,
1000
+ "exclude_remote_users" : True ,
1001
+ }
1002
+ }
1003
+ )
1004
+ def test_exclude_remote_users (self ) -> None :
1005
+ """Tests that only local users are returned when
1006
+ user_directory.exclude_remote_users is True.
1007
+ """
1008
+
1009
+ # Create a room and few users to test the directory with
1010
+ searching_user = self .register_user ("searcher" , "password" )
1011
+ searching_user_tok = self .login ("searcher" , "password" )
1012
+
1013
+ room_id = self .helper .create_room_as (
1014
+ searching_user ,
1015
+ room_version = RoomVersions .V1 .identifier ,
1016
+ tok = searching_user_tok ,
1017
+ )
1018
+
1019
+ # Create a few local users and join them to the room
1020
+ local_user_1 = self .register_user ("user_xxxxx" , "password" )
1021
+ local_user_2 = self .register_user ("user_bbbbb" , "password" )
1022
+ local_user_3 = self .register_user ("user_zzzzz" , "password" )
1023
+
1024
+ self ._add_user_to_room (room_id , RoomVersions .V1 , local_user_1 )
1025
+ self ._add_user_to_room (room_id , RoomVersions .V1 , local_user_2 )
1026
+ self ._add_user_to_room (room_id , RoomVersions .V1 , local_user_3 )
1027
+
1028
+ # Create a few "remote" users and join them to the room
1029
+ remote_user_1 = "@user_aaaaa:remote_server"
1030
+ remote_user_2 = "@user_yyyyy:remote_server"
1031
+ remote_user_3 = "@user_ccccc:remote_server"
1032
+ self ._add_user_to_room (room_id , RoomVersions .V1 , remote_user_1 )
1033
+ self ._add_user_to_room (room_id , RoomVersions .V1 , remote_user_2 )
1034
+ self ._add_user_to_room (room_id , RoomVersions .V1 , remote_user_3 )
1035
+
1036
+ local_users = [local_user_1 , local_user_2 , local_user_3 ]
1037
+ remote_users = [remote_user_1 , remote_user_2 , remote_user_3 ]
1038
+
1039
+ # The local searching user searches for the term "user", which other users have
1040
+ # in their user id
1041
+ results = self .get_success (
1042
+ self .handler .search_users (searching_user , "user" , 20 )
1043
+ )["results" ]
1044
+ received_user_ids = [result ["user_id" ] for result in results ]
1045
+
1046
+ for user in local_users :
1047
+ self .assertIn (
1048
+ user , received_user_ids , f"Local user { user } not found in results"
1049
+ )
1050
+
1051
+ for user in remote_users :
1052
+ self .assertNotIn (
1053
+ user , received_user_ids , f"Remote user { user } should not be in results"
1054
+ )
1055
+
995
1056
def _add_user_to_room (
996
1057
self ,
997
1058
room_id : str ,
0 commit comments