test_user_directory.py 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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()
  27. self.store = UserDirectoryStore(None, 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(
  39. "!room:id",
  40. [ALICE, BOB],
  41. )
  42. yield self.store.add_users_who_share_room(
  43. "!room:id",
  44. False,
  45. (
  46. (ALICE, BOB),
  47. (BOB, ALICE),
  48. ),
  49. )
  50. @defer.inlineCallbacks
  51. def test_search_user_dir(self):
  52. # normally when alice searches the directory she should just find
  53. # bob because bobby doesn't share a room with her.
  54. r = yield self.store.search_user_dir(ALICE, "bob", 10)
  55. self.assertFalse(r["limited"])
  56. self.assertEqual(1, len(r["results"]))
  57. self.assertDictEqual(r["results"][0], {
  58. "user_id": BOB,
  59. "display_name": "bob",
  60. "avatar_url": None,
  61. })
  62. @defer.inlineCallbacks
  63. def test_search_user_dir_all_users(self):
  64. self.hs.config.user_directory_search_all_users = True
  65. try:
  66. r = yield self.store.search_user_dir(ALICE, "bob", 10)
  67. self.assertFalse(r["limited"])
  68. self.assertEqual(2, len(r["results"]))
  69. self.assertDictEqual(r["results"][0], {
  70. "user_id": BOB,
  71. "display_name": "bob",
  72. "avatar_url": None,
  73. })
  74. self.assertDictEqual(r["results"][1], {
  75. "user_id": BOBBY,
  76. "display_name": "bobby",
  77. "avatar_url": None,
  78. })
  79. finally:
  80. self.hs.config.user_directory_search_all_users = False