test_user_directory.py 2.5 KB

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