test_user_directory.py 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. # The localpart isn't 'Bela' on purpose so we can test looking up display names.
  22. BELA = "@somenickname: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 = self.hs.get_datastore()
  28. # alice and bob are both in !room_id. bobby is not but shares
  29. # a homeserver with alice.
  30. yield defer.ensureDeferred(
  31. self.store.update_profile_in_user_dir(ALICE, "alice", None)
  32. )
  33. yield defer.ensureDeferred(
  34. self.store.update_profile_in_user_dir(BOB, "bob", None)
  35. )
  36. yield defer.ensureDeferred(
  37. self.store.update_profile_in_user_dir(BOBBY, "bobby", None)
  38. )
  39. yield defer.ensureDeferred(
  40. self.store.update_profile_in_user_dir(BELA, "Bela", None)
  41. )
  42. yield defer.ensureDeferred(
  43. self.store.add_users_in_public_rooms("!room:id", (ALICE, BOB))
  44. )
  45. @defer.inlineCallbacks
  46. def test_search_user_dir(self):
  47. # normally when alice searches the directory she should just find
  48. # bob because bobby doesn't share a room with her.
  49. r = yield defer.ensureDeferred(self.store.search_user_dir(ALICE, "bob", 10))
  50. self.assertFalse(r["limited"])
  51. self.assertEqual(1, len(r["results"]))
  52. self.assertDictEqual(
  53. r["results"][0], {"user_id": BOB, "display_name": "bob", "avatar_url": None}
  54. )
  55. @defer.inlineCallbacks
  56. def test_search_user_dir_all_users(self):
  57. self.hs.config.user_directory_search_all_users = True
  58. try:
  59. r = yield defer.ensureDeferred(self.store.search_user_dir(ALICE, "bob", 10))
  60. self.assertFalse(r["limited"])
  61. self.assertEqual(2, len(r["results"]))
  62. self.assertDictEqual(
  63. r["results"][0],
  64. {"user_id": BOB, "display_name": "bob", "avatar_url": None},
  65. )
  66. self.assertDictEqual(
  67. r["results"][1],
  68. {"user_id": BOBBY, "display_name": "bobby", "avatar_url": None},
  69. )
  70. finally:
  71. self.hs.config.user_directory_search_all_users = False
  72. @defer.inlineCallbacks
  73. def test_search_user_dir_stop_words(self):
  74. """Tests that a user can look up another user by searching for the start if its
  75. display name even if that name happens to be a common English word that would
  76. usually be ignored in full text searches.
  77. """
  78. self.hs.config.user_directory_search_all_users = True
  79. try:
  80. r = yield defer.ensureDeferred(self.store.search_user_dir(ALICE, "be", 10))
  81. self.assertFalse(r["limited"])
  82. self.assertEqual(1, len(r["results"]))
  83. self.assertDictEqual(
  84. r["results"][0],
  85. {"user_id": BELA, "display_name": "Bela", "avatar_url": None},
  86. )
  87. finally:
  88. self.hs.config.user_directory_search_all_users = False