2018-12-14 19:20:59 +01:00
|
|
|
# Copyright 2018 New Vector
|
|
|
|
#
|
|
|
|
# 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.
|
2021-09-30 12:04:40 +02:00
|
|
|
from unittest.mock import Mock, patch
|
2021-09-23 14:02:13 +02:00
|
|
|
from urllib.parse import quote
|
2018-12-14 19:20:59 +01:00
|
|
|
|
2020-06-05 20:42:55 +02:00
|
|
|
from twisted.internet import defer
|
2021-09-30 12:04:40 +02:00
|
|
|
from twisted.test.proto_helpers import MemoryReactor
|
2020-06-05 20:42:55 +02:00
|
|
|
|
2019-05-01 16:32:38 +02:00
|
|
|
import synapse.rest.admin
|
2021-09-06 12:37:54 +02:00
|
|
|
from synapse.api.constants import UserTypes
|
2021-02-19 12:02:03 +01:00
|
|
|
from synapse.api.room_versions import RoomVersion, RoomVersions
|
2021-08-17 13:57:58 +02:00
|
|
|
from synapse.rest.client import login, room, user_directory
|
2021-09-30 12:04:40 +02:00
|
|
|
from synapse.server import HomeServer
|
2018-12-14 19:20:59 +01:00
|
|
|
from synapse.storage.roommember import ProfileInfo
|
2021-09-23 14:02:13 +02:00
|
|
|
from synapse.types import create_requester
|
2021-09-30 12:04:40 +02:00
|
|
|
from synapse.util import Clock
|
2018-12-14 19:20:59 +01:00
|
|
|
|
|
|
|
from tests import unittest
|
2021-09-30 12:04:40 +02:00
|
|
|
from tests.storage.test_user_directory import GetUserDirectoryTables
|
2020-06-10 18:44:34 +02:00
|
|
|
from tests.unittest import override_config
|
2018-12-14 19:20:59 +01:00
|
|
|
|
|
|
|
|
2019-03-07 10:22:53 +01:00
|
|
|
class UserDirectoryTestCase(unittest.HomeserverTestCase):
|
2021-09-30 12:04:40 +02:00
|
|
|
"""Tests the UserDirectoryHandler.
|
|
|
|
|
|
|
|
We're broadly testing two kinds of things here.
|
|
|
|
|
|
|
|
1. Check that we correctly update the user directory in response
|
|
|
|
to events (e.g. join a room, leave a room, change name, make public)
|
|
|
|
2. Check that the search logic behaves as expected.
|
|
|
|
|
|
|
|
The background process that rebuilds the user directory is tested in
|
|
|
|
tests/storage/test_user_directory.py.
|
2019-03-07 10:22:53 +01:00
|
|
|
"""
|
2018-12-14 19:20:59 +01:00
|
|
|
|
2019-03-07 10:22:53 +01:00
|
|
|
servlets = [
|
|
|
|
login.register_servlets,
|
2021-09-23 14:02:13 +02:00
|
|
|
synapse.rest.admin.register_servlets,
|
2019-03-07 10:22:53 +01:00
|
|
|
room.register_servlets,
|
|
|
|
]
|
2018-12-14 19:20:59 +01:00
|
|
|
|
2021-09-30 12:04:40 +02:00
|
|
|
def make_homeserver(self, reactor: MemoryReactor, clock: Clock) -> HomeServer:
|
2019-03-07 10:22:53 +01:00
|
|
|
config = self.default_config()
|
2019-05-13 22:01:14 +02:00
|
|
|
config["update_user_directory"] = True
|
2019-03-07 10:22:53 +01:00
|
|
|
return self.setup_test_homeserver(config=config)
|
2018-12-14 19:20:59 +01:00
|
|
|
|
2021-09-30 12:04:40 +02:00
|
|
|
def prepare(self, reactor: MemoryReactor, clock: Clock, hs: HomeServer) -> None:
|
2019-03-07 10:22:53 +01:00
|
|
|
self.store = hs.get_datastore()
|
|
|
|
self.handler = hs.get_user_directory_handler()
|
2021-02-19 12:02:03 +01:00
|
|
|
self.event_builder_factory = self.hs.get_event_builder_factory()
|
|
|
|
self.event_creation_handler = self.hs.get_event_creation_handler()
|
2021-09-30 12:04:40 +02:00
|
|
|
self.user_dir_helper = GetUserDirectoryTables(self.store)
|
2018-12-14 19:20:59 +01:00
|
|
|
|
2021-09-30 12:04:40 +02:00
|
|
|
def test_handle_local_profile_change_with_support_user(self) -> None:
|
2018-12-14 19:20:59 +01:00
|
|
|
support_user_id = "@support:test"
|
2019-03-07 10:22:53 +01:00
|
|
|
self.get_success(
|
2019-07-10 17:26:49 +02:00
|
|
|
self.store.register_user(
|
|
|
|
user_id=support_user_id, password_hash=None, user_type=UserTypes.SUPPORT
|
2019-03-07 10:22:53 +01:00
|
|
|
)
|
2018-12-14 19:20:59 +01:00
|
|
|
)
|
2020-12-17 13:05:39 +01:00
|
|
|
regular_user_id = "@regular:test"
|
|
|
|
self.get_success(
|
|
|
|
self.store.register_user(user_id=regular_user_id, password_hash=None)
|
|
|
|
)
|
2018-12-14 19:20:59 +01:00
|
|
|
|
2019-03-07 10:22:53 +01:00
|
|
|
self.get_success(
|
2021-09-30 12:04:40 +02:00
|
|
|
self.handler.handle_local_profile_change(
|
|
|
|
support_user_id, ProfileInfo("I love support me", None)
|
|
|
|
)
|
2019-03-07 10:22:53 +01:00
|
|
|
)
|
|
|
|
profile = self.get_success(self.store.get_user_in_directory(support_user_id))
|
2018-12-14 19:20:59 +01:00
|
|
|
self.assertTrue(profile is None)
|
2019-06-20 11:32:02 +02:00
|
|
|
display_name = "display_name"
|
2018-12-14 19:20:59 +01:00
|
|
|
|
2019-06-20 11:32:02 +02:00
|
|
|
profile_info = ProfileInfo(avatar_url="avatar_url", display_name=display_name)
|
2019-03-07 10:22:53 +01:00
|
|
|
self.get_success(
|
|
|
|
self.handler.handle_local_profile_change(regular_user_id, profile_info)
|
|
|
|
)
|
|
|
|
profile = self.get_success(self.store.get_user_in_directory(regular_user_id))
|
2019-06-20 11:32:02 +02:00
|
|
|
self.assertTrue(profile["display_name"] == display_name)
|
2018-12-14 19:20:59 +01:00
|
|
|
|
2021-09-30 12:04:40 +02:00
|
|
|
def test_handle_local_profile_change_with_deactivated_user(self) -> None:
|
2020-12-17 13:05:39 +01:00
|
|
|
# create user
|
|
|
|
r_user_id = "@regular:test"
|
|
|
|
self.get_success(
|
|
|
|
self.store.register_user(user_id=r_user_id, password_hash=None)
|
|
|
|
)
|
|
|
|
|
|
|
|
# update profile
|
|
|
|
display_name = "Regular User"
|
|
|
|
profile_info = ProfileInfo(avatar_url="avatar_url", display_name=display_name)
|
|
|
|
self.get_success(
|
|
|
|
self.handler.handle_local_profile_change(r_user_id, profile_info)
|
|
|
|
)
|
|
|
|
|
|
|
|
# profile is in directory
|
|
|
|
profile = self.get_success(self.store.get_user_in_directory(r_user_id))
|
|
|
|
self.assertTrue(profile["display_name"] == display_name)
|
|
|
|
|
|
|
|
# deactivate user
|
|
|
|
self.get_success(self.store.set_user_deactivated_status(r_user_id, True))
|
2021-09-10 11:54:38 +02:00
|
|
|
self.get_success(self.handler.handle_local_user_deactivated(r_user_id))
|
2020-12-17 13:05:39 +01:00
|
|
|
|
|
|
|
# profile is not in directory
|
|
|
|
profile = self.get_success(self.store.get_user_in_directory(r_user_id))
|
|
|
|
self.assertTrue(profile is None)
|
|
|
|
|
|
|
|
# update profile after deactivation
|
|
|
|
self.get_success(
|
|
|
|
self.handler.handle_local_profile_change(r_user_id, profile_info)
|
|
|
|
)
|
|
|
|
|
|
|
|
# profile is furthermore not in directory
|
|
|
|
profile = self.get_success(self.store.get_user_in_directory(r_user_id))
|
|
|
|
self.assertTrue(profile is None)
|
|
|
|
|
2021-09-30 12:04:40 +02:00
|
|
|
def test_handle_user_deactivated_support_user(self) -> None:
|
2018-12-14 19:20:59 +01:00
|
|
|
s_user_id = "@support:test"
|
2019-03-07 10:22:53 +01:00
|
|
|
self.get_success(
|
2019-07-10 17:26:49 +02:00
|
|
|
self.store.register_user(
|
|
|
|
user_id=s_user_id, password_hash=None, user_type=UserTypes.SUPPORT
|
2019-03-07 10:22:53 +01:00
|
|
|
)
|
2018-12-14 19:20:59 +01:00
|
|
|
)
|
|
|
|
|
2021-09-30 12:04:40 +02:00
|
|
|
mock_remove_from_user_dir = Mock(return_value=defer.succeed(None))
|
|
|
|
with patch.object(
|
|
|
|
self.store, "remove_from_user_dir", mock_remove_from_user_dir
|
|
|
|
):
|
|
|
|
self.get_success(self.handler.handle_local_user_deactivated(s_user_id))
|
|
|
|
# BUG: the correct spelling is assert_not_called, but that makes the test fail
|
|
|
|
# and it's not clear that this is actually the behaviour we want.
|
|
|
|
mock_remove_from_user_dir.not_called()
|
2018-12-14 19:20:59 +01:00
|
|
|
|
2021-09-30 12:04:40 +02:00
|
|
|
def test_handle_user_deactivated_regular_user(self) -> None:
|
2018-12-14 19:20:59 +01:00
|
|
|
r_user_id = "@regular:test"
|
2019-03-07 10:22:53 +01:00
|
|
|
self.get_success(
|
2019-07-10 17:26:49 +02:00
|
|
|
self.store.register_user(user_id=r_user_id, password_hash=None)
|
2019-03-07 10:22:53 +01:00
|
|
|
)
|
|
|
|
|
2021-09-30 12:04:40 +02:00
|
|
|
mock_remove_from_user_dir = Mock(return_value=defer.succeed(None))
|
|
|
|
with patch.object(
|
|
|
|
self.store, "remove_from_user_dir", mock_remove_from_user_dir
|
|
|
|
):
|
|
|
|
self.get_success(self.handler.handle_local_user_deactivated(r_user_id))
|
|
|
|
mock_remove_from_user_dir.assert_called_once_with(r_user_id)
|
|
|
|
|
|
|
|
def test_reactivation_makes_regular_user_searchable(self) -> None:
|
2021-09-23 14:02:13 +02:00
|
|
|
user = self.register_user("regular", "pass")
|
|
|
|
user_token = self.login(user, "pass")
|
|
|
|
admin_user = self.register_user("admin", "pass", admin=True)
|
|
|
|
admin_token = self.login(admin_user, "pass")
|
|
|
|
|
|
|
|
# Ensure the regular user is publicly visible and searchable.
|
|
|
|
self.helper.create_room_as(user, is_public=True, tok=user_token)
|
|
|
|
s = self.get_success(self.handler.search_users(admin_user, user, 10))
|
|
|
|
self.assertEqual(len(s["results"]), 1)
|
|
|
|
self.assertEqual(s["results"][0]["user_id"], user)
|
|
|
|
|
|
|
|
# Deactivate the user and check they're not searchable.
|
|
|
|
deactivate_handler = self.hs.get_deactivate_account_handler()
|
|
|
|
self.get_success(
|
|
|
|
deactivate_handler.deactivate_account(
|
|
|
|
user, erase_data=False, requester=create_requester(admin_user)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
s = self.get_success(self.handler.search_users(admin_user, user, 10))
|
|
|
|
self.assertEqual(s["results"], [])
|
|
|
|
|
|
|
|
# Reactivate the user
|
|
|
|
channel = self.make_request(
|
|
|
|
"PUT",
|
|
|
|
f"/_synapse/admin/v2/users/{quote(user)}",
|
|
|
|
access_token=admin_token,
|
|
|
|
content={"deactivated": False, "password": "pass"},
|
|
|
|
)
|
|
|
|
self.assertEqual(channel.code, 200)
|
|
|
|
user_token = self.login(user, "pass")
|
|
|
|
self.helper.create_room_as(user, is_public=True, tok=user_token)
|
|
|
|
|
|
|
|
# Check they're searchable.
|
|
|
|
s = self.get_success(self.handler.search_users(admin_user, user, 10))
|
|
|
|
self.assertEqual(len(s["results"]), 1)
|
|
|
|
self.assertEqual(s["results"][0]["user_id"], user)
|
|
|
|
|
2021-09-30 12:04:40 +02:00
|
|
|
def test_private_room(self) -> None:
|
2019-03-07 10:22:53 +01:00
|
|
|
"""
|
|
|
|
A user can be searched for only by people that are either in a public
|
|
|
|
room, or that share a private chat.
|
|
|
|
"""
|
|
|
|
u1 = self.register_user("user1", "pass")
|
|
|
|
u1_token = self.login(u1, "pass")
|
|
|
|
u2 = self.register_user("user2", "pass")
|
|
|
|
u2_token = self.login(u2, "pass")
|
|
|
|
u3 = self.register_user("user3", "pass")
|
|
|
|
|
|
|
|
# We do not add users to the directory until they join a room.
|
|
|
|
s = self.get_success(self.handler.search_users(u1, "user2", 10))
|
|
|
|
self.assertEqual(len(s["results"]), 0)
|
|
|
|
|
|
|
|
room = self.helper.create_room_as(u1, is_public=False, tok=u1_token)
|
|
|
|
self.helper.invite(room, src=u1, targ=u2, tok=u1_token)
|
|
|
|
self.helper.join(room, user=u2, tok=u2_token)
|
|
|
|
|
|
|
|
# Check we have populated the database correctly.
|
2021-09-30 12:04:40 +02:00
|
|
|
shares_private = self.get_success(
|
|
|
|
self.user_dir_helper.get_users_who_share_private_rooms()
|
|
|
|
)
|
|
|
|
public_users = self.get_success(
|
|
|
|
self.user_dir_helper.get_users_in_public_rooms()
|
|
|
|
)
|
2019-03-07 10:22:53 +01:00
|
|
|
|
|
|
|
self.assertEqual(
|
2021-09-30 12:04:40 +02:00
|
|
|
self.user_dir_helper._compress_shared(shares_private),
|
|
|
|
{(u1, u2, room), (u2, u1, room)},
|
2019-03-07 10:22:53 +01:00
|
|
|
)
|
2019-03-11 14:35:31 +01:00
|
|
|
self.assertEqual(public_users, [])
|
2019-03-07 10:22:53 +01:00
|
|
|
|
|
|
|
# We get one search result when searching for user2 by user1.
|
|
|
|
s = self.get_success(self.handler.search_users(u1, "user2", 10))
|
|
|
|
self.assertEqual(len(s["results"]), 1)
|
|
|
|
|
|
|
|
# We get NO search results when searching for user2 by user3.
|
|
|
|
s = self.get_success(self.handler.search_users(u3, "user2", 10))
|
|
|
|
self.assertEqual(len(s["results"]), 0)
|
|
|
|
|
|
|
|
# We get NO search results when searching for user3 by user1.
|
|
|
|
s = self.get_success(self.handler.search_users(u1, "user3", 10))
|
|
|
|
self.assertEqual(len(s["results"]), 0)
|
|
|
|
|
|
|
|
# User 2 then leaves.
|
|
|
|
self.helper.leave(room, user=u2, tok=u2_token)
|
|
|
|
|
|
|
|
# Check we have removed the values.
|
2021-09-30 12:04:40 +02:00
|
|
|
shares_private = self.get_success(
|
|
|
|
self.user_dir_helper.get_users_who_share_private_rooms()
|
|
|
|
)
|
|
|
|
public_users = self.get_success(
|
|
|
|
self.user_dir_helper.get_users_in_public_rooms()
|
|
|
|
)
|
2019-03-07 10:22:53 +01:00
|
|
|
|
2021-09-30 12:04:40 +02:00
|
|
|
self.assertEqual(self.user_dir_helper._compress_shared(shares_private), set())
|
2019-03-11 14:35:31 +01:00
|
|
|
self.assertEqual(public_users, [])
|
2019-03-07 10:22:53 +01:00
|
|
|
|
|
|
|
# User1 now gets no search results for any of the other users.
|
|
|
|
s = self.get_success(self.handler.search_users(u1, "user2", 10))
|
|
|
|
self.assertEqual(len(s["results"]), 0)
|
|
|
|
|
|
|
|
s = self.get_success(self.handler.search_users(u1, "user3", 10))
|
|
|
|
self.assertEqual(len(s["results"]), 0)
|
|
|
|
|
2021-09-30 12:04:40 +02:00
|
|
|
def test_spam_checker(self) -> None:
|
2020-02-14 13:17:54 +01:00
|
|
|
"""
|
2020-08-20 21:42:58 +02:00
|
|
|
A user which fails the spam checks will not appear in search results.
|
2020-02-14 13:17:54 +01:00
|
|
|
"""
|
|
|
|
u1 = self.register_user("user1", "pass")
|
|
|
|
u1_token = self.login(u1, "pass")
|
|
|
|
u2 = self.register_user("user2", "pass")
|
|
|
|
u2_token = self.login(u2, "pass")
|
|
|
|
|
|
|
|
# We do not add users to the directory until they join a room.
|
|
|
|
s = self.get_success(self.handler.search_users(u1, "user2", 10))
|
|
|
|
self.assertEqual(len(s["results"]), 0)
|
|
|
|
|
|
|
|
room = self.helper.create_room_as(u1, is_public=False, tok=u1_token)
|
|
|
|
self.helper.invite(room, src=u1, targ=u2, tok=u1_token)
|
|
|
|
self.helper.join(room, user=u2, tok=u2_token)
|
|
|
|
|
|
|
|
# Check we have populated the database correctly.
|
2021-09-30 12:04:40 +02:00
|
|
|
shares_private = self.get_success(
|
|
|
|
self.user_dir_helper.get_users_who_share_private_rooms()
|
|
|
|
)
|
|
|
|
public_users = self.get_success(
|
|
|
|
self.user_dir_helper.get_users_in_public_rooms()
|
|
|
|
)
|
2020-02-14 13:17:54 +01:00
|
|
|
|
|
|
|
self.assertEqual(
|
2021-09-30 12:04:40 +02:00
|
|
|
self.user_dir_helper._compress_shared(shares_private),
|
|
|
|
{(u1, u2, room), (u2, u1, room)},
|
2020-02-14 13:17:54 +01:00
|
|
|
)
|
|
|
|
self.assertEqual(public_users, [])
|
|
|
|
|
|
|
|
# We get one search result when searching for user2 by user1.
|
|
|
|
s = self.get_success(self.handler.search_users(u1, "user2", 10))
|
|
|
|
self.assertEqual(len(s["results"]), 1)
|
|
|
|
|
2021-09-30 12:04:40 +02:00
|
|
|
async def allow_all(user_profile: ProfileInfo) -> bool:
|
2021-06-18 13:15:52 +02:00
|
|
|
# Allow all users.
|
|
|
|
return False
|
|
|
|
|
2020-02-14 13:17:54 +01:00
|
|
|
# Configure a spam checker that does not filter any users.
|
|
|
|
spam_checker = self.hs.get_spam_checker()
|
2021-06-18 13:15:52 +02:00
|
|
|
spam_checker._check_username_for_spam_callbacks = [allow_all]
|
2020-02-14 13:17:54 +01:00
|
|
|
|
|
|
|
# The results do not change:
|
|
|
|
# We get one search result when searching for user2 by user1.
|
|
|
|
s = self.get_success(self.handler.search_users(u1, "user2", 10))
|
|
|
|
self.assertEqual(len(s["results"]), 1)
|
|
|
|
|
|
|
|
# Configure a spam checker that filters all users.
|
2021-09-30 12:04:40 +02:00
|
|
|
async def block_all(user_profile: ProfileInfo) -> bool:
|
2021-06-18 13:15:52 +02:00
|
|
|
# All users are spammy.
|
|
|
|
return True
|
2020-02-14 13:17:54 +01:00
|
|
|
|
2021-06-18 13:15:52 +02:00
|
|
|
spam_checker._check_username_for_spam_callbacks = [block_all]
|
2020-02-14 13:17:54 +01:00
|
|
|
|
|
|
|
# User1 now gets no search results for any of the other users.
|
|
|
|
s = self.get_success(self.handler.search_users(u1, "user2", 10))
|
|
|
|
self.assertEqual(len(s["results"]), 0)
|
|
|
|
|
2021-09-30 12:04:40 +02:00
|
|
|
def test_legacy_spam_checker(self) -> None:
|
2020-02-14 13:17:54 +01:00
|
|
|
"""
|
|
|
|
A spam checker without the expected method should be ignored.
|
|
|
|
"""
|
|
|
|
u1 = self.register_user("user1", "pass")
|
|
|
|
u1_token = self.login(u1, "pass")
|
|
|
|
u2 = self.register_user("user2", "pass")
|
|
|
|
u2_token = self.login(u2, "pass")
|
|
|
|
|
|
|
|
# We do not add users to the directory until they join a room.
|
|
|
|
s = self.get_success(self.handler.search_users(u1, "user2", 10))
|
|
|
|
self.assertEqual(len(s["results"]), 0)
|
|
|
|
|
|
|
|
room = self.helper.create_room_as(u1, is_public=False, tok=u1_token)
|
|
|
|
self.helper.invite(room, src=u1, targ=u2, tok=u1_token)
|
|
|
|
self.helper.join(room, user=u2, tok=u2_token)
|
|
|
|
|
|
|
|
# Check we have populated the database correctly.
|
2021-09-30 12:04:40 +02:00
|
|
|
shares_private = self.get_success(
|
|
|
|
self.user_dir_helper.get_users_who_share_private_rooms()
|
|
|
|
)
|
|
|
|
public_users = self.get_success(
|
|
|
|
self.user_dir_helper.get_users_in_public_rooms()
|
|
|
|
)
|
2020-02-14 13:17:54 +01:00
|
|
|
|
|
|
|
self.assertEqual(
|
2021-09-30 12:04:40 +02:00
|
|
|
self.user_dir_helper._compress_shared(shares_private),
|
|
|
|
{(u1, u2, room), (u2, u1, room)},
|
2020-02-14 13:17:54 +01:00
|
|
|
)
|
|
|
|
self.assertEqual(public_users, [])
|
|
|
|
|
|
|
|
# Configure a spam checker.
|
|
|
|
spam_checker = self.hs.get_spam_checker()
|
|
|
|
# The spam checker doesn't need any methods, so create a bare object.
|
|
|
|
spam_checker.spam_checker = object()
|
|
|
|
|
|
|
|
# We get one search result when searching for user2 by user1.
|
|
|
|
s = self.get_success(self.handler.search_users(u1, "user2", 10))
|
|
|
|
self.assertEqual(len(s["results"]), 1)
|
|
|
|
|
2021-09-30 12:04:40 +02:00
|
|
|
def test_initial_share_all_users(self) -> None:
|
2019-03-07 10:22:53 +01:00
|
|
|
"""
|
|
|
|
Search all users = True means that a user does not have to share a
|
|
|
|
private room with the searching user or be in a public room to be search
|
|
|
|
visible.
|
|
|
|
"""
|
|
|
|
self.handler.search_all_users = True
|
2021-09-24 13:25:21 +02:00
|
|
|
self.hs.config.userdirectory.user_directory_search_all_users = True
|
2019-03-07 10:22:53 +01:00
|
|
|
|
|
|
|
u1 = self.register_user("user1", "pass")
|
2019-03-18 18:50:24 +01:00
|
|
|
self.register_user("user2", "pass")
|
2019-03-07 10:22:53 +01:00
|
|
|
u3 = self.register_user("user3", "pass")
|
|
|
|
|
2021-09-30 12:04:40 +02:00
|
|
|
shares_private = self.get_success(
|
|
|
|
self.user_dir_helper.get_users_who_share_private_rooms()
|
|
|
|
)
|
|
|
|
public_users = self.get_success(
|
|
|
|
self.user_dir_helper.get_users_in_public_rooms()
|
|
|
|
)
|
2019-03-07 10:22:53 +01:00
|
|
|
|
2019-03-18 18:50:24 +01:00
|
|
|
# No users share rooms
|
|
|
|
self.assertEqual(public_users, [])
|
2021-09-30 12:04:40 +02:00
|
|
|
self.assertEqual(self.user_dir_helper._compress_shared(shares_private), set())
|
2019-03-07 10:22:53 +01:00
|
|
|
|
|
|
|
# Despite not sharing a room, search_all_users means we get a search
|
|
|
|
# result.
|
|
|
|
s = self.get_success(self.handler.search_users(u1, u3, 10))
|
|
|
|
self.assertEqual(len(s["results"]), 1)
|
2019-03-18 18:50:24 +01:00
|
|
|
|
|
|
|
# We can find the other two users
|
|
|
|
s = self.get_success(self.handler.search_users(u1, "user", 10))
|
|
|
|
self.assertEqual(len(s["results"]), 2)
|
|
|
|
|
|
|
|
# Registering a user and then searching for them works.
|
|
|
|
u4 = self.register_user("user4", "pass")
|
|
|
|
s = self.get_success(self.handler.search_users(u1, u4, 10))
|
|
|
|
self.assertEqual(len(s["results"]), 1)
|
2019-03-20 16:16:36 +01:00
|
|
|
|
2021-02-19 12:02:03 +01:00
|
|
|
@override_config(
|
|
|
|
{
|
|
|
|
"user_directory": {
|
|
|
|
"enabled": True,
|
|
|
|
"search_all_users": True,
|
|
|
|
"prefer_local_users": True,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
2021-09-30 12:04:40 +02:00
|
|
|
def test_prefer_local_users(self) -> None:
|
2021-02-19 12:02:03 +01:00
|
|
|
"""Tests that local users are shown higher in search results when
|
|
|
|
user_directory.prefer_local_users is True.
|
|
|
|
"""
|
|
|
|
# Create a room and few users to test the directory with
|
|
|
|
searching_user = self.register_user("searcher", "password")
|
|
|
|
searching_user_tok = self.login("searcher", "password")
|
|
|
|
|
|
|
|
room_id = self.helper.create_room_as(
|
|
|
|
searching_user,
|
|
|
|
room_version=RoomVersions.V1.identifier,
|
|
|
|
tok=searching_user_tok,
|
|
|
|
)
|
|
|
|
|
|
|
|
# Create a few local users and join them to the room
|
|
|
|
local_user_1 = self.register_user("user_xxxxx", "password")
|
|
|
|
local_user_2 = self.register_user("user_bbbbb", "password")
|
|
|
|
local_user_3 = self.register_user("user_zzzzz", "password")
|
|
|
|
|
|
|
|
self._add_user_to_room(room_id, RoomVersions.V1, local_user_1)
|
|
|
|
self._add_user_to_room(room_id, RoomVersions.V1, local_user_2)
|
|
|
|
self._add_user_to_room(room_id, RoomVersions.V1, local_user_3)
|
|
|
|
|
|
|
|
# Create a few "remote" users and join them to the room
|
|
|
|
remote_user_1 = "@user_aaaaa:remote_server"
|
|
|
|
remote_user_2 = "@user_yyyyy:remote_server"
|
|
|
|
remote_user_3 = "@user_ccccc:remote_server"
|
|
|
|
self._add_user_to_room(room_id, RoomVersions.V1, remote_user_1)
|
|
|
|
self._add_user_to_room(room_id, RoomVersions.V1, remote_user_2)
|
|
|
|
self._add_user_to_room(room_id, RoomVersions.V1, remote_user_3)
|
|
|
|
|
|
|
|
local_users = [local_user_1, local_user_2, local_user_3]
|
|
|
|
remote_users = [remote_user_1, remote_user_2, remote_user_3]
|
|
|
|
|
|
|
|
# The local searching user searches for the term "user", which other users have
|
|
|
|
# in their user id
|
|
|
|
results = self.get_success(
|
|
|
|
self.handler.search_users(searching_user, "user", 20)
|
|
|
|
)["results"]
|
|
|
|
received_user_id_ordering = [result["user_id"] for result in results]
|
|
|
|
|
|
|
|
# Typically we'd expect Synapse to return users in lexicographical order,
|
|
|
|
# assuming they have similar User IDs/display names, and profile information.
|
|
|
|
|
|
|
|
# Check that the order of returned results using our module is as we expect,
|
|
|
|
# i.e our local users show up first, despite all users having lexographically mixed
|
|
|
|
# user IDs.
|
|
|
|
[self.assertIn(user, local_users) for user in received_user_id_ordering[:3]]
|
|
|
|
[self.assertIn(user, remote_users) for user in received_user_id_ordering[3:]]
|
|
|
|
|
|
|
|
def _add_user_to_room(
|
2021-02-19 14:19:54 +01:00
|
|
|
self,
|
|
|
|
room_id: str,
|
|
|
|
room_version: RoomVersion,
|
|
|
|
user_id: str,
|
2021-09-30 12:04:40 +02:00
|
|
|
) -> None:
|
2021-02-19 12:02:03 +01:00
|
|
|
# Add a user to the room.
|
|
|
|
builder = self.event_builder_factory.for_room_version(
|
|
|
|
room_version,
|
|
|
|
{
|
|
|
|
"type": "m.room.member",
|
|
|
|
"sender": user_id,
|
|
|
|
"state_key": user_id,
|
|
|
|
"room_id": room_id,
|
|
|
|
"content": {"membership": "join"},
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
event, context = self.get_success(
|
|
|
|
self.event_creation_handler.create_new_client_event(builder)
|
|
|
|
)
|
|
|
|
|
|
|
|
self.get_success(
|
|
|
|
self.hs.get_storage().persistence.persist_event(event, context)
|
|
|
|
)
|
|
|
|
|
2019-03-20 16:16:36 +01:00
|
|
|
|
|
|
|
class TestUserDirSearchDisabled(unittest.HomeserverTestCase):
|
|
|
|
user_id = "@test:test"
|
|
|
|
|
|
|
|
servlets = [
|
|
|
|
user_directory.register_servlets,
|
|
|
|
room.register_servlets,
|
|
|
|
login.register_servlets,
|
2019-05-02 12:59:16 +02:00
|
|
|
synapse.rest.admin.register_servlets_for_client_rest_resource,
|
2019-03-20 16:16:36 +01:00
|
|
|
]
|
|
|
|
|
2021-09-30 12:04:40 +02:00
|
|
|
def make_homeserver(self, reactor: MemoryReactor, clock: Clock) -> HomeServer:
|
2019-03-20 16:16:36 +01:00
|
|
|
config = self.default_config()
|
2019-05-13 22:01:14 +02:00
|
|
|
config["update_user_directory"] = True
|
2019-03-20 16:16:36 +01:00
|
|
|
hs = self.setup_test_homeserver(config=config)
|
|
|
|
|
|
|
|
self.config = hs.config
|
|
|
|
|
|
|
|
return hs
|
|
|
|
|
2021-09-30 12:04:40 +02:00
|
|
|
def test_disabling_room_list(self) -> None:
|
2021-09-24 13:25:21 +02:00
|
|
|
self.config.userdirectory.user_directory_search_enabled = True
|
2019-03-20 16:16:36 +01:00
|
|
|
|
|
|
|
# First we create a room with another user so that user dir is non-empty
|
|
|
|
# for our user
|
|
|
|
self.helper.create_room_as(self.user_id)
|
|
|
|
u2 = self.register_user("user2", "pass")
|
|
|
|
room = self.helper.create_room_as(self.user_id)
|
|
|
|
self.helper.join(room, user=u2)
|
|
|
|
|
|
|
|
# Assert user directory is not empty
|
2020-12-15 15:44:04 +01:00
|
|
|
channel = self.make_request(
|
2019-05-10 07:12:11 +02:00
|
|
|
"POST", b"user_directory/search", b'{"search_term":"user2"}'
|
2019-03-20 16:16:36 +01:00
|
|
|
)
|
|
|
|
self.assertEquals(200, channel.code, channel.result)
|
|
|
|
self.assertTrue(len(channel.json_body["results"]) > 0)
|
|
|
|
|
|
|
|
# Disable user directory and check search returns nothing
|
2021-09-24 13:25:21 +02:00
|
|
|
self.config.userdirectory.user_directory_search_enabled = False
|
2020-12-15 15:44:04 +01:00
|
|
|
channel = self.make_request(
|
2019-05-10 07:12:11 +02:00
|
|
|
"POST", b"user_directory/search", b'{"search_term":"user2"}'
|
2019-03-20 16:16:36 +01:00
|
|
|
)
|
|
|
|
self.assertEquals(200, channel.code, channel.result)
|
|
|
|
self.assertTrue(len(channel.json_body["results"]) == 0)
|