test_user_directory.py 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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 tests import unittest
  18. from tests.utils import setup_test_homeserver
  19. ALICE = "@alice:a"
  20. BOB = "@bob:b"
  21. BOBBY = "@bobby:a"
  22. class UserDirectoryStoreTestCase(unittest.TestCase):
  23. @defer.inlineCallbacks
  24. def setUp(self):
  25. self.hs = yield setup_test_homeserver(self.addCleanup)
  26. self.store = UserDirectoryStore(self.hs.get_db_conn(), self.hs)
  27. # alice and bob are both in !room_id. bobby is not but shares
  28. # a homeserver with alice.
  29. yield self.store.update_profile_in_user_dir(ALICE, "alice", None)
  30. yield self.store.update_profile_in_user_dir(BOB, "bob", None)
  31. yield self.store.update_profile_in_user_dir(BOBBY, "bobby", None)
  32. yield self.store.add_users_in_public_rooms("!room:id", (ALICE, BOB))
  33. @defer.inlineCallbacks
  34. def test_search_user_dir(self):
  35. # normally when alice searches the directory she should just find
  36. # bob because bobby doesn't share a room with her.
  37. r = yield self.store.search_user_dir(ALICE, "bob", 10)
  38. self.assertFalse(r["limited"])
  39. self.assertEqual(1, len(r["results"]))
  40. self.assertDictEqual(
  41. r["results"][0], {"user_id": BOB, "display_name": "bob", "avatar_url": None}
  42. )
  43. @defer.inlineCallbacks
  44. def test_search_user_dir_all_users(self):
  45. self.hs.config.user_directory_search_all_users = True
  46. try:
  47. r = yield self.store.search_user_dir(ALICE, "bob", 10)
  48. self.assertFalse(r["limited"])
  49. self.assertEqual(2, len(r["results"]))
  50. self.assertDictEqual(
  51. r["results"][0],
  52. {"user_id": BOB, "display_name": "bob", "avatar_url": None},
  53. )
  54. self.assertDictEqual(
  55. r["results"][1],
  56. {"user_id": BOBBY, "display_name": "bobby", "avatar_url": None},
  57. )
  58. finally:
  59. self.hs.config.user_directory_search_all_users = False