2018-01-25 22:25:03 +01:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# Copyright 2018 New Vector Ltd
|
|
|
|
#
|
|
|
|
# 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 twisted.internet import defer
|
|
|
|
|
|
|
|
from tests import unittest
|
|
|
|
from tests.utils import setup_test_homeserver
|
|
|
|
|
|
|
|
ALICE = "@alice:a"
|
|
|
|
BOB = "@bob:b"
|
|
|
|
BOBBY = "@bobby:a"
|
2020-12-17 14:42:30 +01:00
|
|
|
# The localpart isn't 'Bela' on purpose so we can test looking up display names.
|
|
|
|
BELA = "@somenickname:a"
|
2018-01-25 22:25:03 +01:00
|
|
|
|
|
|
|
|
|
|
|
class UserDirectoryStoreTestCase(unittest.TestCase):
|
|
|
|
@defer.inlineCallbacks
|
|
|
|
def setUp(self):
|
2018-08-13 08:47:46 +02:00
|
|
|
self.hs = yield setup_test_homeserver(self.addCleanup)
|
2019-12-06 17:02:50 +01:00
|
|
|
self.store = self.hs.get_datastore()
|
2018-01-25 22:25:03 +01:00
|
|
|
|
|
|
|
# alice and bob are both in !room_id. bobby is not but shares
|
|
|
|
# a homeserver with alice.
|
2020-08-27 23:24:46 +02:00
|
|
|
yield defer.ensureDeferred(
|
|
|
|
self.store.update_profile_in_user_dir(ALICE, "alice", None)
|
|
|
|
)
|
|
|
|
yield defer.ensureDeferred(
|
|
|
|
self.store.update_profile_in_user_dir(BOB, "bob", None)
|
|
|
|
)
|
|
|
|
yield defer.ensureDeferred(
|
|
|
|
self.store.update_profile_in_user_dir(BOBBY, "bobby", None)
|
|
|
|
)
|
2020-12-17 14:42:30 +01:00
|
|
|
yield defer.ensureDeferred(
|
|
|
|
self.store.update_profile_in_user_dir(BELA, "Bela", None)
|
|
|
|
)
|
2020-08-27 23:24:46 +02:00
|
|
|
yield defer.ensureDeferred(
|
|
|
|
self.store.add_users_in_public_rooms("!room:id", (ALICE, BOB))
|
|
|
|
)
|
2018-01-25 22:25:03 +01:00
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
|
|
|
def test_search_user_dir(self):
|
|
|
|
# normally when alice searches the directory she should just find
|
|
|
|
# bob because bobby doesn't share a room with her.
|
2020-08-07 18:17:17 +02:00
|
|
|
r = yield defer.ensureDeferred(self.store.search_user_dir(ALICE, "bob", 10))
|
2018-01-25 22:25:03 +01:00
|
|
|
self.assertFalse(r["limited"])
|
|
|
|
self.assertEqual(1, len(r["results"]))
|
2018-08-10 15:54:09 +02:00
|
|
|
self.assertDictEqual(
|
|
|
|
r["results"][0], {"user_id": BOB, "display_name": "bob", "avatar_url": None}
|
|
|
|
)
|
2018-01-25 22:25:03 +01:00
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
|
|
|
def test_search_user_dir_all_users(self):
|
|
|
|
self.hs.config.user_directory_search_all_users = True
|
|
|
|
try:
|
2020-08-07 18:17:17 +02:00
|
|
|
r = yield defer.ensureDeferred(self.store.search_user_dir(ALICE, "bob", 10))
|
2018-01-25 22:25:03 +01:00
|
|
|
self.assertFalse(r["limited"])
|
|
|
|
self.assertEqual(2, len(r["results"]))
|
2018-08-10 15:54:09 +02:00
|
|
|
self.assertDictEqual(
|
|
|
|
r["results"][0],
|
|
|
|
{"user_id": BOB, "display_name": "bob", "avatar_url": None},
|
|
|
|
)
|
|
|
|
self.assertDictEqual(
|
|
|
|
r["results"][1],
|
|
|
|
{"user_id": BOBBY, "display_name": "bobby", "avatar_url": None},
|
|
|
|
)
|
2018-01-25 22:25:03 +01:00
|
|
|
finally:
|
|
|
|
self.hs.config.user_directory_search_all_users = False
|
2020-12-17 14:42:30 +01:00
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
|
|
|
def test_search_user_dir_stop_words(self):
|
|
|
|
"""Tests that a user can look up another user by searching for the start if its
|
|
|
|
display name even if that name happens to be a common English word that would
|
|
|
|
usually be ignored in full text searches.
|
|
|
|
"""
|
|
|
|
self.hs.config.user_directory_search_all_users = True
|
|
|
|
try:
|
|
|
|
r = yield defer.ensureDeferred(self.store.search_user_dir(ALICE, "be", 10))
|
|
|
|
self.assertFalse(r["limited"])
|
|
|
|
self.assertEqual(1, len(r["results"]))
|
|
|
|
self.assertDictEqual(
|
|
|
|
r["results"][0],
|
|
|
|
{"user_id": BELA, "display_name": "Bela", "avatar_url": None},
|
|
|
|
)
|
|
|
|
finally:
|
|
|
|
self.hs.config.user_directory_search_all_users = False
|