test_user_directory.py 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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 defer.ensureDeferred(
  29. self.store.update_profile_in_user_dir(ALICE, "alice", None)
  30. )
  31. yield defer.ensureDeferred(
  32. self.store.update_profile_in_user_dir(BOB, "bob", None)
  33. )
  34. yield defer.ensureDeferred(
  35. self.store.update_profile_in_user_dir(BOBBY, "bobby", None)
  36. )
  37. yield defer.ensureDeferred(
  38. self.store.add_users_in_public_rooms("!room:id", (ALICE, BOB))
  39. )
  40. @defer.inlineCallbacks
  41. def test_search_user_dir(self):
  42. # normally when alice searches the directory she should just find
  43. # bob because bobby doesn't share a room with her.
  44. r = yield defer.ensureDeferred(self.store.search_user_dir(ALICE, "bob", 10))
  45. self.assertFalse(r["limited"])
  46. self.assertEqual(1, len(r["results"]))
  47. self.assertDictEqual(
  48. r["results"][0], {"user_id": BOB, "display_name": "bob", "avatar_url": None}
  49. )
  50. @defer.inlineCallbacks
  51. def test_search_user_dir_all_users(self):
  52. self.hs.config.user_directory_search_all_users = True
  53. try:
  54. r = yield defer.ensureDeferred(self.store.search_user_dir(ALICE, "bob", 10))
  55. self.assertFalse(r["limited"])
  56. self.assertEqual(2, len(r["results"]))
  57. self.assertDictEqual(
  58. r["results"][0],
  59. {"user_id": BOB, "display_name": "bob", "avatar_url": None},
  60. )
  61. self.assertDictEqual(
  62. r["results"][1],
  63. {"user_id": BOBBY, "display_name": "bobby", "avatar_url": None},
  64. )
  65. finally:
  66. self.hs.config.user_directory_search_all_users = False