test_user_directory.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. # Copyright 2018-2021 The Matrix.org Foundation C.I.C.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. from tests.unittest import HomeserverTestCase, override_config
  15. ALICE = "@alice:a"
  16. BOB = "@bob:b"
  17. BOBBY = "@bobby:a"
  18. # The localpart isn't 'Bela' on purpose so we can test looking up display names.
  19. BELA = "@somenickname:a"
  20. class UserDirectoryStoreTestCase(HomeserverTestCase):
  21. def prepare(self, reactor, clock, hs):
  22. self.store = hs.get_datastore()
  23. # alice and bob are both in !room_id. bobby is not but shares
  24. # a homeserver with alice.
  25. self.get_success(self.store.update_profile_in_user_dir(ALICE, "alice", None))
  26. self.get_success(self.store.update_profile_in_user_dir(BOB, "bob", None))
  27. self.get_success(self.store.update_profile_in_user_dir(BOBBY, "bobby", None))
  28. self.get_success(self.store.update_profile_in_user_dir(BELA, "Bela", None))
  29. self.get_success(self.store.add_users_in_public_rooms("!room:id", (ALICE, BOB)))
  30. def test_search_user_dir(self):
  31. # normally when alice searches the directory she should just find
  32. # bob because bobby doesn't share a room with her.
  33. r = self.get_success(self.store.search_user_dir(ALICE, "bob", 10))
  34. self.assertFalse(r["limited"])
  35. self.assertEqual(1, len(r["results"]))
  36. self.assertDictEqual(
  37. r["results"][0], {"user_id": BOB, "display_name": "bob", "avatar_url": None}
  38. )
  39. @override_config({"user_directory": {"search_all_users": True}})
  40. def test_search_user_dir_all_users(self):
  41. r = self.get_success(self.store.search_user_dir(ALICE, "bob", 10))
  42. self.assertFalse(r["limited"])
  43. self.assertEqual(2, len(r["results"]))
  44. self.assertDictEqual(
  45. r["results"][0],
  46. {"user_id": BOB, "display_name": "bob", "avatar_url": None},
  47. )
  48. self.assertDictEqual(
  49. r["results"][1],
  50. {"user_id": BOBBY, "display_name": "bobby", "avatar_url": None},
  51. )
  52. @override_config({"user_directory": {"search_all_users": True}})
  53. def test_search_user_dir_stop_words(self):
  54. """Tests that a user can look up another user by searching for the start if its
  55. display name even if that name happens to be a common English word that would
  56. usually be ignored in full text searches.
  57. """
  58. r = self.get_success(self.store.search_user_dir(ALICE, "be", 10))
  59. self.assertFalse(r["limited"])
  60. self.assertEqual(1, len(r["results"]))
  61. self.assertDictEqual(
  62. r["results"][0],
  63. {"user_id": BELA, "display_name": "Bela", "avatar_url": None},
  64. )