test_user_directory.py 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2018 New Vector Ltd
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. from twisted.internet import defer
  16. from synapse.storage import UserDirectoryStore
  17. from synapse.storage.roommember import ProfileInfo
  18. from tests import unittest
  19. from tests.utils import setup_test_homeserver
  20. ALICE = "@alice:a"
  21. BOB = "@bob:b"
  22. BOBBY = "@bobby:a"
  23. class UserDirectoryStoreTestCase(unittest.TestCase):
  24. @defer.inlineCallbacks
  25. def setUp(self):
  26. self.hs = yield setup_test_homeserver(self.addCleanup)
  27. self.store = UserDirectoryStore(self.hs.get_db_conn(), self.hs)
  28. # alice and bob are both in !room_id. bobby is not but shares
  29. # a homeserver with alice.
  30. yield self.store.add_profiles_to_user_dir(
  31. "!room:id",
  32. {
  33. ALICE: ProfileInfo(None, "alice"),
  34. BOB: ProfileInfo(None, "bob"),
  35. BOBBY: ProfileInfo(None, "bobby"),
  36. },
  37. )
  38. yield self.store.add_users_to_public_room("!room:id", [ALICE, BOB])
  39. yield self.store.add_users_who_share_room(
  40. "!room:id", False, ((ALICE, BOB), (BOB, ALICE))
  41. )
  42. @defer.inlineCallbacks
  43. def test_search_user_dir(self):
  44. # normally when alice searches the directory she should just find
  45. # bob because bobby doesn't share a room with her.
  46. r = yield self.store.search_user_dir(ALICE, "bob", 10)
  47. self.assertFalse(r["limited"])
  48. self.assertEqual(1, len(r["results"]))
  49. self.assertDictEqual(
  50. r["results"][0], {"user_id": BOB, "display_name": "bob", "avatar_url": None}
  51. )
  52. @defer.inlineCallbacks
  53. def test_search_user_dir_all_users(self):
  54. self.hs.config.user_directory_search_all_users = True
  55. try:
  56. r = yield self.store.search_user_dir(ALICE, "bob", 10)
  57. self.assertFalse(r["limited"])
  58. self.assertEqual(2, len(r["results"]))
  59. self.assertDictEqual(
  60. r["results"][0],
  61. {"user_id": BOB, "display_name": "bob", "avatar_url": None},
  62. )
  63. self.assertDictEqual(
  64. r["results"][1],
  65. {"user_id": BOBBY, "display_name": "bobby", "avatar_url": None},
  66. )
  67. finally:
  68. self.hs.config.user_directory_search_all_users = False