2018-12-14 21:20:59 +03:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# 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.
|
|
|
|
from mock import Mock
|
|
|
|
|
2020-06-05 21:42:55 +03:00
|
|
|
from twisted.internet import defer
|
|
|
|
|
2019-05-01 17:32:38 +03:00
|
|
|
import synapse.rest.admin
|
2020-06-10 19:44:34 +03:00
|
|
|
from synapse.api.constants import EventTypes, RoomEncryptionAlgorithms, UserTypes
|
2019-05-01 17:32:38 +03:00
|
|
|
from synapse.rest.client.v1 import login, room
|
2019-03-20 18:16:36 +03:00
|
|
|
from synapse.rest.client.v2_alpha import user_directory
|
2018-12-14 21:20:59 +03:00
|
|
|
from synapse.storage.roommember import ProfileInfo
|
|
|
|
|
|
|
|
from tests import unittest
|
2020-06-10 19:44:34 +03:00
|
|
|
from tests.unittest import override_config
|
2018-12-14 21:20:59 +03:00
|
|
|
|
|
|
|
|
2019-03-07 12:22:53 +03:00
|
|
|
class UserDirectoryTestCase(unittest.HomeserverTestCase):
|
|
|
|
"""
|
|
|
|
Tests the UserDirectoryHandler.
|
|
|
|
"""
|
2018-12-14 21:20:59 +03:00
|
|
|
|
2019-03-07 12:22:53 +03:00
|
|
|
servlets = [
|
|
|
|
login.register_servlets,
|
2019-05-02 13:59:16 +03:00
|
|
|
synapse.rest.admin.register_servlets_for_client_rest_resource,
|
2019-03-07 12:22:53 +03:00
|
|
|
room.register_servlets,
|
|
|
|
]
|
2018-12-14 21:20:59 +03:00
|
|
|
|
2019-03-07 12:22:53 +03:00
|
|
|
def make_homeserver(self, reactor, clock):
|
2018-12-14 21:20:59 +03:00
|
|
|
|
2019-03-07 12:22:53 +03:00
|
|
|
config = self.default_config()
|
2019-05-13 23:01:14 +03:00
|
|
|
config["update_user_directory"] = True
|
2019-03-07 12:22:53 +03:00
|
|
|
return self.setup_test_homeserver(config=config)
|
2018-12-14 21:20:59 +03:00
|
|
|
|
2019-03-07 12:22:53 +03:00
|
|
|
def prepare(self, reactor, clock, hs):
|
|
|
|
self.store = hs.get_datastore()
|
|
|
|
self.handler = hs.get_user_directory_handler()
|
2018-12-14 21:20:59 +03:00
|
|
|
|
|
|
|
def test_handle_local_profile_change_with_support_user(self):
|
|
|
|
support_user_id = "@support:test"
|
2019-03-07 12:22:53 +03:00
|
|
|
self.get_success(
|
2019-07-10 18:26:49 +03:00
|
|
|
self.store.register_user(
|
|
|
|
user_id=support_user_id, password_hash=None, user_type=UserTypes.SUPPORT
|
2019-03-07 12:22:53 +03:00
|
|
|
)
|
2018-12-14 21:20:59 +03:00
|
|
|
)
|
|
|
|
|
2019-03-07 12:22:53 +03:00
|
|
|
self.get_success(
|
|
|
|
self.handler.handle_local_profile_change(support_user_id, None)
|
|
|
|
)
|
|
|
|
profile = self.get_success(self.store.get_user_in_directory(support_user_id))
|
2018-12-14 21:20:59 +03:00
|
|
|
self.assertTrue(profile is None)
|
2019-06-20 12:32:02 +03:00
|
|
|
display_name = "display_name"
|
2018-12-14 21:20:59 +03:00
|
|
|
|
2019-06-20 12:32:02 +03:00
|
|
|
profile_info = ProfileInfo(avatar_url="avatar_url", display_name=display_name)
|
|
|
|
regular_user_id = "@regular:test"
|
2019-03-07 12:22:53 +03: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 12:32:02 +03:00
|
|
|
self.assertTrue(profile["display_name"] == display_name)
|
2018-12-14 21:20:59 +03:00
|
|
|
|
|
|
|
def test_handle_user_deactivated_support_user(self):
|
|
|
|
s_user_id = "@support:test"
|
2019-03-07 12:22:53 +03:00
|
|
|
self.get_success(
|
2019-07-10 18:26:49 +03:00
|
|
|
self.store.register_user(
|
|
|
|
user_id=s_user_id, password_hash=None, user_type=UserTypes.SUPPORT
|
2019-03-07 12:22:53 +03:00
|
|
|
)
|
2018-12-14 21:20:59 +03:00
|
|
|
)
|
|
|
|
|
2020-06-05 21:42:55 +03:00
|
|
|
self.store.remove_from_user_dir = Mock(return_value=defer.succeed(None))
|
2019-03-07 12:22:53 +03:00
|
|
|
self.get_success(self.handler.handle_user_deactivated(s_user_id))
|
2018-12-14 21:20:59 +03:00
|
|
|
self.store.remove_from_user_dir.not_called()
|
|
|
|
|
|
|
|
def test_handle_user_deactivated_regular_user(self):
|
|
|
|
r_user_id = "@regular:test"
|
2019-03-07 12:22:53 +03:00
|
|
|
self.get_success(
|
2019-07-10 18:26:49 +03:00
|
|
|
self.store.register_user(user_id=r_user_id, password_hash=None)
|
2019-03-07 12:22:53 +03:00
|
|
|
)
|
2020-06-05 21:42:55 +03:00
|
|
|
self.store.remove_from_user_dir = Mock(return_value=defer.succeed(None))
|
2019-03-07 12:22:53 +03:00
|
|
|
self.get_success(self.handler.handle_user_deactivated(r_user_id))
|
2018-12-14 21:20:59 +03:00
|
|
|
self.store.remove_from_user_dir.called_once_with(r_user_id)
|
2019-03-07 12:22:53 +03:00
|
|
|
|
|
|
|
def test_private_room(self):
|
|
|
|
"""
|
|
|
|
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.
|
|
|
|
shares_private = self.get_users_who_share_private_rooms()
|
2019-03-11 16:35:31 +03:00
|
|
|
public_users = self.get_users_in_public_rooms()
|
2019-03-07 12:22:53 +03:00
|
|
|
|
|
|
|
self.assertEqual(
|
2020-02-21 15:15:07 +03:00
|
|
|
self._compress_shared(shares_private), {(u1, u2, room), (u2, u1, room)}
|
2019-03-07 12:22:53 +03:00
|
|
|
)
|
2019-03-11 16:35:31 +03:00
|
|
|
self.assertEqual(public_users, [])
|
2019-03-07 12:22:53 +03: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.
|
|
|
|
shares_private = self.get_users_who_share_private_rooms()
|
2019-03-11 13:11:36 +03:00
|
|
|
public_users = self.get_users_in_public_rooms()
|
2019-03-07 12:22:53 +03:00
|
|
|
|
|
|
|
self.assertEqual(self._compress_shared(shares_private), set())
|
2019-03-11 16:35:31 +03:00
|
|
|
self.assertEqual(public_users, [])
|
2019-03-07 12:22:53 +03: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)
|
|
|
|
|
2020-06-10 19:44:34 +03:00
|
|
|
@override_config({"encryption_enabled_by_default_for_room_type": "all"})
|
|
|
|
def test_encrypted_by_default_config_option_all(self):
|
|
|
|
"""Tests that invite-only and non-invite-only rooms have encryption enabled by
|
|
|
|
default when the config option encryption_enabled_by_default_for_room_type is "all".
|
|
|
|
"""
|
|
|
|
# Create a user
|
|
|
|
user = self.register_user("user", "pass")
|
|
|
|
user_token = self.login(user, "pass")
|
|
|
|
|
|
|
|
# Create an invite-only room as that user
|
|
|
|
room_id = self.helper.create_room_as(user, is_public=False, tok=user_token)
|
|
|
|
|
|
|
|
# Check that the room has an encryption state event
|
|
|
|
event_content = self.helper.get_state(
|
|
|
|
room_id=room_id, event_type=EventTypes.RoomEncryption, tok=user_token,
|
|
|
|
)
|
|
|
|
self.assertEqual(event_content, {"algorithm": RoomEncryptionAlgorithms.DEFAULT})
|
|
|
|
|
|
|
|
# Create a non invite-only room as that user
|
|
|
|
room_id = self.helper.create_room_as(user, is_public=True, tok=user_token)
|
|
|
|
|
|
|
|
# Check that the room has an encryption state event
|
|
|
|
event_content = self.helper.get_state(
|
|
|
|
room_id=room_id, event_type=EventTypes.RoomEncryption, tok=user_token,
|
|
|
|
)
|
|
|
|
self.assertEqual(event_content, {"algorithm": RoomEncryptionAlgorithms.DEFAULT})
|
|
|
|
|
|
|
|
@override_config({"encryption_enabled_by_default_for_room_type": "invite"})
|
|
|
|
def test_encrypted_by_default_config_option_invite(self):
|
|
|
|
"""Tests that only new, invite-only rooms have encryption enabled by default when
|
|
|
|
the config option encryption_enabled_by_default_for_room_type is "invite".
|
|
|
|
"""
|
|
|
|
# Create a user
|
|
|
|
user = self.register_user("user", "pass")
|
|
|
|
user_token = self.login(user, "pass")
|
|
|
|
|
|
|
|
# Create an invite-only room as that user
|
|
|
|
room_id = self.helper.create_room_as(user, is_public=False, tok=user_token)
|
|
|
|
|
|
|
|
# Check that the room has an encryption state event
|
|
|
|
event_content = self.helper.get_state(
|
|
|
|
room_id=room_id, event_type=EventTypes.RoomEncryption, tok=user_token,
|
|
|
|
)
|
|
|
|
self.assertEqual(event_content, {"algorithm": RoomEncryptionAlgorithms.DEFAULT})
|
|
|
|
|
|
|
|
# Create a non invite-only room as that user
|
|
|
|
room_id = self.helper.create_room_as(user, is_public=True, tok=user_token)
|
|
|
|
|
|
|
|
# Check that the room does not have an encryption state event
|
|
|
|
self.helper.get_state(
|
|
|
|
room_id=room_id,
|
|
|
|
event_type=EventTypes.RoomEncryption,
|
|
|
|
tok=user_token,
|
|
|
|
expect_code=404,
|
|
|
|
)
|
|
|
|
|
|
|
|
@override_config({"encryption_enabled_by_default_for_room_type": "off"})
|
|
|
|
def test_encrypted_by_default_config_option_off(self):
|
|
|
|
"""Tests that neither new invite-only nor non-invite-only rooms have encryption
|
|
|
|
enabled by default when the config option
|
|
|
|
encryption_enabled_by_default_for_room_type is "off".
|
|
|
|
"""
|
|
|
|
# Create a user
|
|
|
|
user = self.register_user("user", "pass")
|
|
|
|
user_token = self.login(user, "pass")
|
|
|
|
|
|
|
|
# Create an invite-only room as that user
|
|
|
|
room_id = self.helper.create_room_as(user, is_public=False, tok=user_token)
|
|
|
|
|
|
|
|
# Check that the room does not have an encryption state event
|
|
|
|
self.helper.get_state(
|
|
|
|
room_id=room_id,
|
|
|
|
event_type=EventTypes.RoomEncryption,
|
|
|
|
tok=user_token,
|
|
|
|
expect_code=404,
|
|
|
|
)
|
|
|
|
|
|
|
|
# Create a non invite-only room as that user
|
|
|
|
room_id = self.helper.create_room_as(user, is_public=True, tok=user_token)
|
|
|
|
|
|
|
|
# Check that the room does not have an encryption state event
|
|
|
|
self.helper.get_state(
|
|
|
|
room_id=room_id,
|
|
|
|
event_type=EventTypes.RoomEncryption,
|
|
|
|
tok=user_token,
|
|
|
|
expect_code=404,
|
|
|
|
)
|
|
|
|
|
2020-02-14 15:17:54 +03:00
|
|
|
def test_spam_checker(self):
|
|
|
|
"""
|
2020-08-20 22:42:58 +03:00
|
|
|
A user which fails the spam checks will not appear in search results.
|
2020-02-14 15:17:54 +03: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.
|
|
|
|
shares_private = self.get_users_who_share_private_rooms()
|
|
|
|
public_users = self.get_users_in_public_rooms()
|
|
|
|
|
|
|
|
self.assertEqual(
|
2020-02-21 15:15:07 +03:00
|
|
|
self._compress_shared(shares_private), {(u1, u2, room), (u2, u1, room)}
|
2020-02-14 15:17:54 +03: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)
|
|
|
|
|
|
|
|
# Configure a spam checker that does not filter any users.
|
|
|
|
spam_checker = self.hs.get_spam_checker()
|
|
|
|
|
2020-08-20 22:42:58 +03:00
|
|
|
class AllowAll:
|
2020-12-11 22:05:15 +03:00
|
|
|
async def check_username_for_spam(self, user_profile):
|
2020-02-14 15:17:54 +03:00
|
|
|
# Allow all users.
|
|
|
|
return False
|
|
|
|
|
2020-05-08 21:25:48 +03:00
|
|
|
spam_checker.spam_checkers = [AllowAll()]
|
2020-02-14 15:17:54 +03: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.
|
2020-08-20 22:42:58 +03:00
|
|
|
class BlockAll:
|
2020-12-11 22:05:15 +03:00
|
|
|
async def check_username_for_spam(self, user_profile):
|
2020-02-14 15:17:54 +03:00
|
|
|
# All users are spammy.
|
|
|
|
return True
|
|
|
|
|
2020-05-08 21:25:48 +03:00
|
|
|
spam_checker.spam_checkers = [BlockAll()]
|
2020-02-14 15:17:54 +03: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)
|
|
|
|
|
|
|
|
def test_legacy_spam_checker(self):
|
|
|
|
"""
|
|
|
|
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.
|
|
|
|
shares_private = self.get_users_who_share_private_rooms()
|
|
|
|
public_users = self.get_users_in_public_rooms()
|
|
|
|
|
|
|
|
self.assertEqual(
|
2020-02-21 15:15:07 +03:00
|
|
|
self._compress_shared(shares_private), {(u1, u2, room), (u2, u1, room)}
|
2020-02-14 15:17:54 +03: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)
|
|
|
|
|
2019-03-07 12:22:53 +03:00
|
|
|
def _compress_shared(self, shared):
|
|
|
|
"""
|
|
|
|
Compress a list of users who share rooms dicts to a list of tuples.
|
|
|
|
"""
|
|
|
|
r = set()
|
|
|
|
for i in shared:
|
|
|
|
r.add((i["user_id"], i["other_user_id"], i["room_id"]))
|
|
|
|
return r
|
|
|
|
|
2019-03-11 13:11:36 +03:00
|
|
|
def get_users_in_public_rooms(self):
|
2019-03-12 13:47:14 +03:00
|
|
|
r = self.get_success(
|
2020-08-05 23:38:57 +03:00
|
|
|
self.store.db_pool.simple_select_list(
|
2019-03-18 20:50:24 +03:00
|
|
|
"users_in_public_rooms", None, ("user_id", "room_id")
|
2019-03-11 13:11:36 +03:00
|
|
|
)
|
|
|
|
)
|
2019-03-12 13:47:14 +03:00
|
|
|
retval = []
|
|
|
|
for i in r:
|
|
|
|
retval.append((i["user_id"], i["room_id"]))
|
|
|
|
return retval
|
2019-03-11 13:11:36 +03:00
|
|
|
|
2019-03-07 12:22:53 +03:00
|
|
|
def get_users_who_share_private_rooms(self):
|
|
|
|
return self.get_success(
|
2020-08-05 23:38:57 +03:00
|
|
|
self.store.db_pool.simple_select_list(
|
2019-03-07 12:22:53 +03:00
|
|
|
"users_who_share_private_rooms",
|
|
|
|
None,
|
|
|
|
["user_id", "other_user_id", "room_id"],
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2019-03-18 20:50:24 +03:00
|
|
|
def _add_background_updates(self):
|
|
|
|
"""
|
|
|
|
Add the background updates we need to run.
|
|
|
|
"""
|
|
|
|
# Ugh, have to reset this flag
|
2020-08-05 23:38:57 +03:00
|
|
|
self.store.db_pool.updates._all_done = False
|
2019-03-18 20:50:24 +03:00
|
|
|
|
|
|
|
self.get_success(
|
2020-08-05 23:38:57 +03:00
|
|
|
self.store.db_pool.simple_insert(
|
2019-03-18 20:50:24 +03:00
|
|
|
"background_updates",
|
|
|
|
{
|
|
|
|
"update_name": "populate_user_directory_createtables",
|
|
|
|
"progress_json": "{}",
|
|
|
|
},
|
|
|
|
)
|
|
|
|
)
|
|
|
|
self.get_success(
|
2020-08-05 23:38:57 +03:00
|
|
|
self.store.db_pool.simple_insert(
|
2019-03-18 20:50:24 +03:00
|
|
|
"background_updates",
|
|
|
|
{
|
|
|
|
"update_name": "populate_user_directory_process_rooms",
|
|
|
|
"progress_json": "{}",
|
|
|
|
"depends_on": "populate_user_directory_createtables",
|
|
|
|
},
|
|
|
|
)
|
|
|
|
)
|
|
|
|
self.get_success(
|
2020-08-05 23:38:57 +03:00
|
|
|
self.store.db_pool.simple_insert(
|
2019-03-18 20:50:24 +03:00
|
|
|
"background_updates",
|
|
|
|
{
|
|
|
|
"update_name": "populate_user_directory_process_users",
|
|
|
|
"progress_json": "{}",
|
|
|
|
"depends_on": "populate_user_directory_process_rooms",
|
|
|
|
},
|
|
|
|
)
|
|
|
|
)
|
|
|
|
self.get_success(
|
2020-08-05 23:38:57 +03:00
|
|
|
self.store.db_pool.simple_insert(
|
2019-03-18 20:50:24 +03:00
|
|
|
"background_updates",
|
|
|
|
{
|
|
|
|
"update_name": "populate_user_directory_cleanup",
|
|
|
|
"progress_json": "{}",
|
|
|
|
"depends_on": "populate_user_directory_process_users",
|
|
|
|
},
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2019-03-07 12:22:53 +03:00
|
|
|
def test_initial(self):
|
|
|
|
"""
|
|
|
|
The user directory's initial handler correctly updates the search tables.
|
|
|
|
"""
|
|
|
|
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")
|
|
|
|
u3_token = self.login(u3, "pass")
|
|
|
|
|
|
|
|
room = self.helper.create_room_as(u1, is_public=True, tok=u1_token)
|
|
|
|
self.helper.invite(room, src=u1, targ=u2, tok=u1_token)
|
|
|
|
self.helper.join(room, user=u2, tok=u2_token)
|
|
|
|
|
|
|
|
private_room = self.helper.create_room_as(u1, is_public=False, tok=u1_token)
|
|
|
|
self.helper.invite(private_room, src=u1, targ=u3, tok=u1_token)
|
|
|
|
self.helper.join(private_room, user=u3, tok=u3_token)
|
|
|
|
|
|
|
|
self.get_success(self.store.update_user_directory_stream_pos(None))
|
|
|
|
self.get_success(self.store.delete_all_from_user_dir())
|
|
|
|
|
|
|
|
shares_private = self.get_users_who_share_private_rooms()
|
2019-03-11 16:35:31 +03:00
|
|
|
public_users = self.get_users_in_public_rooms()
|
2019-03-07 12:22:53 +03:00
|
|
|
|
2019-03-11 16:35:31 +03:00
|
|
|
# Nothing updated yet
|
2019-03-07 12:22:53 +03:00
|
|
|
self.assertEqual(shares_private, [])
|
2019-03-11 16:35:31 +03:00
|
|
|
self.assertEqual(public_users, [])
|
2019-03-07 12:22:53 +03:00
|
|
|
|
2019-03-18 20:50:24 +03:00
|
|
|
# Do the initial population of the user directory via the background update
|
|
|
|
self._add_background_updates()
|
2019-03-07 12:22:53 +03:00
|
|
|
|
2019-12-04 18:09:36 +03:00
|
|
|
while not self.get_success(
|
2020-08-05 23:38:57 +03:00
|
|
|
self.store.db_pool.updates.has_completed_background_updates()
|
2019-12-04 18:09:36 +03:00
|
|
|
):
|
|
|
|
self.get_success(
|
2020-08-05 23:38:57 +03:00
|
|
|
self.store.db_pool.updates.do_next_background_update(100), by=0.1
|
2019-12-04 18:09:36 +03:00
|
|
|
)
|
2019-03-07 12:22:53 +03:00
|
|
|
|
|
|
|
shares_private = self.get_users_who_share_private_rooms()
|
2019-03-11 16:35:31 +03:00
|
|
|
public_users = self.get_users_in_public_rooms()
|
2019-03-07 12:22:53 +03:00
|
|
|
|
2019-03-12 17:17:51 +03:00
|
|
|
# User 1 and User 2 are in the same public room
|
2020-02-21 15:15:07 +03:00
|
|
|
self.assertEqual(set(public_users), {(u1, room), (u2, room)})
|
2019-03-07 12:22:53 +03:00
|
|
|
|
|
|
|
# User 1 and User 3 share private rooms
|
|
|
|
self.assertEqual(
|
|
|
|
self._compress_shared(shares_private),
|
2020-02-21 15:15:07 +03:00
|
|
|
{(u1, u3, private_room), (u3, u1, private_room)},
|
2019-03-07 12:22:53 +03:00
|
|
|
)
|
|
|
|
|
2019-03-18 20:50:24 +03:00
|
|
|
def test_initial_share_all_users(self):
|
2019-03-07 12:22:53 +03: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
|
|
|
|
self.hs.config.user_directory_search_all_users = True
|
|
|
|
|
|
|
|
u1 = self.register_user("user1", "pass")
|
2019-03-18 20:50:24 +03:00
|
|
|
self.register_user("user2", "pass")
|
2019-03-07 12:22:53 +03:00
|
|
|
u3 = self.register_user("user3", "pass")
|
|
|
|
|
2019-03-18 20:50:24 +03:00
|
|
|
# Wipe the user dir
|
2019-03-07 12:22:53 +03:00
|
|
|
self.get_success(self.store.update_user_directory_stream_pos(None))
|
|
|
|
self.get_success(self.store.delete_all_from_user_dir())
|
|
|
|
|
2019-03-18 20:50:24 +03:00
|
|
|
# Do the initial population of the user directory via the background update
|
|
|
|
self._add_background_updates()
|
2019-03-07 12:22:53 +03:00
|
|
|
|
2019-12-04 18:09:36 +03:00
|
|
|
while not self.get_success(
|
2020-08-05 23:38:57 +03:00
|
|
|
self.store.db_pool.updates.has_completed_background_updates()
|
2019-12-04 18:09:36 +03:00
|
|
|
):
|
|
|
|
self.get_success(
|
2020-08-05 23:38:57 +03:00
|
|
|
self.store.db_pool.updates.do_next_background_update(100), by=0.1
|
2019-12-04 18:09:36 +03:00
|
|
|
)
|
2019-03-07 12:22:53 +03:00
|
|
|
|
2019-03-18 20:50:24 +03:00
|
|
|
shares_private = self.get_users_who_share_private_rooms()
|
|
|
|
public_users = self.get_users_in_public_rooms()
|
2019-03-07 12:22:53 +03:00
|
|
|
|
2019-03-18 20:50:24 +03:00
|
|
|
# No users share rooms
|
|
|
|
self.assertEqual(public_users, [])
|
2020-02-21 15:15:07 +03:00
|
|
|
self.assertEqual(self._compress_shared(shares_private), set())
|
2019-03-07 12:22:53 +03: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 20:50:24 +03: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 18:16:36 +03:00
|
|
|
|
|
|
|
|
|
|
|
class TestUserDirSearchDisabled(unittest.HomeserverTestCase):
|
|
|
|
user_id = "@test:test"
|
|
|
|
|
|
|
|
servlets = [
|
|
|
|
user_directory.register_servlets,
|
|
|
|
room.register_servlets,
|
|
|
|
login.register_servlets,
|
2019-05-02 13:59:16 +03:00
|
|
|
synapse.rest.admin.register_servlets_for_client_rest_resource,
|
2019-03-20 18:16:36 +03:00
|
|
|
]
|
|
|
|
|
|
|
|
def make_homeserver(self, reactor, clock):
|
|
|
|
config = self.default_config()
|
2019-05-13 23:01:14 +03:00
|
|
|
config["update_user_directory"] = True
|
2019-03-20 18:16:36 +03:00
|
|
|
hs = self.setup_test_homeserver(config=config)
|
|
|
|
|
|
|
|
self.config = hs.config
|
|
|
|
|
|
|
|
return hs
|
|
|
|
|
|
|
|
def test_disabling_room_list(self):
|
|
|
|
self.config.user_directory_search_enabled = True
|
|
|
|
|
|
|
|
# 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
|
|
|
|
request, channel = self.make_request(
|
2019-05-10 08:12:11 +03:00
|
|
|
"POST", b"user_directory/search", b'{"search_term":"user2"}'
|
2019-03-20 18:16:36 +03:00
|
|
|
)
|
|
|
|
self.assertEquals(200, channel.code, channel.result)
|
|
|
|
self.assertTrue(len(channel.json_body["results"]) > 0)
|
|
|
|
|
|
|
|
# Disable user directory and check search returns nothing
|
|
|
|
self.config.user_directory_search_enabled = False
|
|
|
|
request, channel = self.make_request(
|
2019-05-10 08:12:11 +03:00
|
|
|
"POST", b"user_directory/search", b'{"search_term":"user2"}'
|
2019-03-20 18:16:36 +03:00
|
|
|
)
|
|
|
|
self.assertEquals(200, channel.code, channel.result)
|
|
|
|
self.assertTrue(len(channel.json_body["results"]) == 0)
|