forked-synapse/tests/storage/test_user_directory.py

78 lines
2.8 KiB
Python
Raw Normal View History

2018-01-25 16:25:03 -05: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 synapse.storage import UserDirectoryStore
from synapse.storage.roommember import ProfileInfo
2018-07-09 02:09:20 -04:00
2018-01-25 16:25:03 -05:00
from tests import unittest
from tests.utils import setup_test_homeserver
ALICE = "@alice:a"
BOB = "@bob:b"
BOBBY = "@bobby:a"
class UserDirectoryStoreTestCase(unittest.TestCase):
@defer.inlineCallbacks
def setUp(self):
2018-08-13 02:47:46 -04:00
self.hs = yield setup_test_homeserver(self.addCleanup)
2018-09-03 12:21:48 -04:00
self.store = UserDirectoryStore(self.hs.get_db_conn(), self.hs)
2018-01-25 16:25:03 -05:00
# alice and bob are both in !room_id. bobby is not but shares
# a homeserver with alice.
yield self.store.add_profiles_to_user_dir(
"!room:id",
{
ALICE: ProfileInfo(None, "alice"),
BOB: ProfileInfo(None, "bob"),
2018-08-10 09:54:09 -04:00
BOBBY: ProfileInfo(None, "bobby"),
2018-01-25 16:25:03 -05:00
},
)
2018-08-10 09:54:09 -04:00
yield self.store.add_users_to_public_room("!room:id", [ALICE, BOB])
2018-01-25 16:25:03 -05:00
yield self.store.add_users_who_share_room(
2018-08-10 09:54:09 -04:00
"!room:id", False, ((ALICE, BOB), (BOB, ALICE))
2018-01-25 16:25:03 -05: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.
r = yield self.store.search_user_dir(ALICE, "bob", 10)
self.assertFalse(r["limited"])
self.assertEqual(1, len(r["results"]))
2018-08-10 09:54:09 -04:00
self.assertDictEqual(
r["results"][0], {"user_id": BOB, "display_name": "bob", "avatar_url": None}
)
2018-01-25 16:25:03 -05:00
@defer.inlineCallbacks
def test_search_user_dir_all_users(self):
self.hs.config.user_directory_search_all_users = True
try:
r = yield self.store.search_user_dir(ALICE, "bob", 10)
self.assertFalse(r["limited"])
self.assertEqual(2, len(r["results"]))
2018-08-10 09:54:09 -04: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 16:25:03 -05:00
finally:
self.hs.config.user_directory_search_all_users = False