test_user_directory.py 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086
  1. # Copyright 2018 New Vector
  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 typing import Tuple
  15. from unittest.mock import Mock, patch
  16. from urllib.parse import quote
  17. from twisted.test.proto_helpers import MemoryReactor
  18. import synapse.rest.admin
  19. from synapse.api.constants import UserTypes
  20. from synapse.api.room_versions import RoomVersion, RoomVersions
  21. from synapse.appservice import ApplicationService
  22. from synapse.rest.client import login, register, room, user_directory
  23. from synapse.server import HomeServer
  24. from synapse.storage.roommember import ProfileInfo
  25. from synapse.types import create_requester
  26. from synapse.util import Clock
  27. from tests import unittest
  28. from tests.storage.test_user_directory import GetUserDirectoryTables
  29. from tests.test_utils import make_awaitable
  30. from tests.test_utils.event_injection import inject_member_event
  31. from tests.unittest import override_config
  32. class UserDirectoryTestCase(unittest.HomeserverTestCase):
  33. """Tests the UserDirectoryHandler.
  34. We're broadly testing two kinds of things here.
  35. 1. Check that we correctly update the user directory in response
  36. to events (e.g. join a room, leave a room, change name, make public)
  37. 2. Check that the search logic behaves as expected.
  38. The background process that rebuilds the user directory is tested in
  39. tests/storage/test_user_directory.py.
  40. """
  41. servlets = [
  42. login.register_servlets,
  43. synapse.rest.admin.register_servlets,
  44. register.register_servlets,
  45. room.register_servlets,
  46. ]
  47. def make_homeserver(self, reactor: MemoryReactor, clock: Clock) -> HomeServer:
  48. config = self.default_config()
  49. config["update_user_directory"] = True
  50. self.appservice = ApplicationService(
  51. token="i_am_an_app_service",
  52. id="1234",
  53. namespaces={"users": [{"regex": r"@as_user.*", "exclusive": True}]},
  54. # Note: this user does not match the regex above, so that tests
  55. # can distinguish the sender from the AS user.
  56. sender="@as_main:test",
  57. )
  58. mock_load_appservices = Mock(return_value=[self.appservice])
  59. with patch(
  60. "synapse.storage.databases.main.appservice.load_appservices",
  61. mock_load_appservices,
  62. ):
  63. hs = self.setup_test_homeserver(config=config)
  64. return hs
  65. def prepare(self, reactor: MemoryReactor, clock: Clock, hs: HomeServer) -> None:
  66. self.store = hs.get_datastores().main
  67. self.handler = hs.get_user_directory_handler()
  68. self.event_builder_factory = self.hs.get_event_builder_factory()
  69. self.event_creation_handler = self.hs.get_event_creation_handler()
  70. self.user_dir_helper = GetUserDirectoryTables(self.store)
  71. def test_normal_user_pair(self) -> None:
  72. """Sanity check that the room-sharing tables are updated correctly."""
  73. alice = self.register_user("alice", "pass")
  74. alice_token = self.login(alice, "pass")
  75. bob = self.register_user("bob", "pass")
  76. bob_token = self.login(bob, "pass")
  77. public = self.helper.create_room_as(
  78. alice,
  79. is_public=True,
  80. extra_content={"visibility": "public"},
  81. tok=alice_token,
  82. )
  83. private = self.helper.create_room_as(alice, is_public=False, tok=alice_token)
  84. self.helper.invite(private, alice, bob, tok=alice_token)
  85. self.helper.join(public, bob, tok=bob_token)
  86. self.helper.join(private, bob, tok=bob_token)
  87. # Alice also makes a second public room but no-one else joins
  88. public2 = self.helper.create_room_as(
  89. alice,
  90. is_public=True,
  91. extra_content={"visibility": "public"},
  92. tok=alice_token,
  93. )
  94. # The user directory should reflect the room memberships above.
  95. users, in_public, in_private = self.get_success(
  96. self.user_dir_helper.get_tables()
  97. )
  98. self.assertEqual(users, {alice, bob})
  99. self.assertEqual(in_public, {(alice, public), (bob, public), (alice, public2)})
  100. self.assertEqual(
  101. in_private,
  102. {(alice, bob, private), (bob, alice, private)},
  103. )
  104. # The next four tests (test_excludes_*) all setup
  105. # - A normal user included in the user dir
  106. # - A public and private room created by that user
  107. # - A user excluded from the room dir, belonging to both rooms
  108. # They match similar logic in storage/test_user_directory. But that tests
  109. # rebuilding the directory; this tests updating it incrementally.
  110. def test_excludes_support_user(self) -> None:
  111. alice = self.register_user("alice", "pass")
  112. alice_token = self.login(alice, "pass")
  113. support = "@support1:test"
  114. self.get_success(
  115. self.store.register_user(
  116. user_id=support, password_hash=None, user_type=UserTypes.SUPPORT
  117. )
  118. )
  119. public, private = self._create_rooms_and_inject_memberships(
  120. alice, alice_token, support
  121. )
  122. self._check_only_one_user_in_directory(alice, public)
  123. def test_excludes_deactivated_user(self) -> None:
  124. admin = self.register_user("admin", "pass", admin=True)
  125. admin_token = self.login(admin, "pass")
  126. user = self.register_user("naughty", "pass")
  127. # Deactivate the user.
  128. channel = self.make_request(
  129. "PUT",
  130. f"/_synapse/admin/v2/users/{user}",
  131. access_token=admin_token,
  132. content={"deactivated": True},
  133. )
  134. self.assertEqual(channel.code, 200)
  135. self.assertEqual(channel.json_body["deactivated"], True)
  136. # Join the deactivated user to rooms owned by the admin.
  137. # Is this something that could actually happen outside of a test?
  138. public, private = self._create_rooms_and_inject_memberships(
  139. admin, admin_token, user
  140. )
  141. self._check_only_one_user_in_directory(admin, public)
  142. def test_excludes_appservices_user(self) -> None:
  143. # Register an AS user.
  144. user = self.register_user("user", "pass")
  145. token = self.login(user, "pass")
  146. as_user, _ = self.register_appservice_user(
  147. "as_user_potato", self.appservice.token
  148. )
  149. # Join the AS user to rooms owned by the normal user.
  150. public, private = self._create_rooms_and_inject_memberships(
  151. user, token, as_user
  152. )
  153. self._check_only_one_user_in_directory(user, public)
  154. def test_excludes_appservice_sender(self) -> None:
  155. user = self.register_user("user", "pass")
  156. token = self.login(user, "pass")
  157. room = self.helper.create_room_as(user, is_public=True, tok=token)
  158. self.helper.join(room, self.appservice.sender, tok=self.appservice.token)
  159. self._check_only_one_user_in_directory(user, room)
  160. def test_user_not_in_users_table(self) -> None:
  161. """Unclear how it happens, but on matrix.org we've seen join events
  162. for users who aren't in the users table. Test that we don't fall over
  163. when processing such a user.
  164. """
  165. user1 = self.register_user("user1", "pass")
  166. token1 = self.login(user1, "pass")
  167. room = self.helper.create_room_as(user1, is_public=True, tok=token1)
  168. # Inject a join event for a user who doesn't exist
  169. self.get_success(inject_member_event(self.hs, room, "@not-a-user:test", "join"))
  170. # Another new user registers and joins the room
  171. user2 = self.register_user("user2", "pass")
  172. token2 = self.login(user2, "pass")
  173. self.helper.join(room, user2, tok=token2)
  174. # The dodgy event should not have stopped us from processing user2's join.
  175. in_public = self.get_success(self.user_dir_helper.get_users_in_public_rooms())
  176. self.assertEqual(set(in_public), {(user1, room), (user2, room)})
  177. def test_excludes_users_when_making_room_public(self) -> None:
  178. # Create a regular user and a support user.
  179. alice = self.register_user("alice", "pass")
  180. alice_token = self.login(alice, "pass")
  181. support = "@support1:test"
  182. self.get_success(
  183. self.store.register_user(
  184. user_id=support, password_hash=None, user_type=UserTypes.SUPPORT
  185. )
  186. )
  187. # Make a public and private room containing Alice and the support user
  188. public, initially_private = self._create_rooms_and_inject_memberships(
  189. alice, alice_token, support
  190. )
  191. self._check_only_one_user_in_directory(alice, public)
  192. # Alice makes the private room public.
  193. self.helper.send_state(
  194. initially_private,
  195. "m.room.join_rules",
  196. {"join_rule": "public"},
  197. tok=alice_token,
  198. )
  199. users, in_public, in_private = self.get_success(
  200. self.user_dir_helper.get_tables()
  201. )
  202. self.assertEqual(users, {alice})
  203. self.assertEqual(in_public, {(alice, public), (alice, initially_private)})
  204. self.assertEqual(in_private, set())
  205. def test_switching_from_private_to_public_to_private(self) -> None:
  206. """Check we update the room sharing tables when switching a room
  207. from private to public, then back again to private."""
  208. # Alice and Bob share a private room.
  209. alice = self.register_user("alice", "pass")
  210. alice_token = self.login(alice, "pass")
  211. bob = self.register_user("bob", "pass")
  212. bob_token = self.login(bob, "pass")
  213. room = self.helper.create_room_as(alice, is_public=False, tok=alice_token)
  214. self.helper.invite(room, alice, bob, tok=alice_token)
  215. self.helper.join(room, bob, tok=bob_token)
  216. # The user directory should reflect this.
  217. def check_user_dir_for_private_room() -> None:
  218. users, in_public, in_private = self.get_success(
  219. self.user_dir_helper.get_tables()
  220. )
  221. self.assertEqual(users, {alice, bob})
  222. self.assertEqual(in_public, set())
  223. self.assertEqual(in_private, {(alice, bob, room), (bob, alice, room)})
  224. check_user_dir_for_private_room()
  225. # Alice makes the room public.
  226. self.helper.send_state(
  227. room,
  228. "m.room.join_rules",
  229. {"join_rule": "public"},
  230. tok=alice_token,
  231. )
  232. # The user directory should be updated accordingly
  233. users, in_public, in_private = self.get_success(
  234. self.user_dir_helper.get_tables()
  235. )
  236. self.assertEqual(users, {alice, bob})
  237. self.assertEqual(in_public, {(alice, room), (bob, room)})
  238. self.assertEqual(in_private, set())
  239. # Alice makes the room private.
  240. self.helper.send_state(
  241. room,
  242. "m.room.join_rules",
  243. {"join_rule": "invite"},
  244. tok=alice_token,
  245. )
  246. # The user directory should be updated accordingly
  247. check_user_dir_for_private_room()
  248. def _create_rooms_and_inject_memberships(
  249. self, creator: str, token: str, joiner: str
  250. ) -> Tuple[str, str]:
  251. """Create a public and private room as a normal user.
  252. Then get the `joiner` into those rooms.
  253. """
  254. # TODO: Duplicates the same-named method in UserDirectoryInitialPopulationTest.
  255. public_room = self.helper.create_room_as(
  256. creator,
  257. is_public=True,
  258. # See https://github.com/matrix-org/synapse/issues/10951
  259. extra_content={"visibility": "public"},
  260. tok=token,
  261. )
  262. private_room = self.helper.create_room_as(creator, is_public=False, tok=token)
  263. # HACK: get the user into these rooms
  264. self.get_success(inject_member_event(self.hs, public_room, joiner, "join"))
  265. self.get_success(inject_member_event(self.hs, private_room, joiner, "join"))
  266. return public_room, private_room
  267. def _check_only_one_user_in_directory(self, user: str, public: str) -> None:
  268. """Check that the user directory DB tables show that:
  269. - only one user is in the user directory
  270. - they belong to exactly one public room
  271. - they don't share a private room with anyone.
  272. """
  273. users, in_public, in_private = self.get_success(
  274. self.user_dir_helper.get_tables()
  275. )
  276. self.assertEqual(users, {user})
  277. self.assertEqual(in_public, {(user, public)})
  278. self.assertEqual(in_private, set())
  279. def test_handle_local_profile_change_with_support_user(self) -> None:
  280. support_user_id = "@support:test"
  281. self.get_success(
  282. self.store.register_user(
  283. user_id=support_user_id, password_hash=None, user_type=UserTypes.SUPPORT
  284. )
  285. )
  286. regular_user_id = "@regular:test"
  287. self.get_success(
  288. self.store.register_user(user_id=regular_user_id, password_hash=None)
  289. )
  290. self.get_success(
  291. self.handler.handle_local_profile_change(
  292. support_user_id, ProfileInfo("I love support me", None)
  293. )
  294. )
  295. profile = self.get_success(self.store.get_user_in_directory(support_user_id))
  296. self.assertIsNone(profile)
  297. display_name = "display_name"
  298. profile_info = ProfileInfo(avatar_url="avatar_url", display_name=display_name)
  299. self.get_success(
  300. self.handler.handle_local_profile_change(regular_user_id, profile_info)
  301. )
  302. profile = self.get_success(self.store.get_user_in_directory(regular_user_id))
  303. assert profile is not None
  304. self.assertTrue(profile["display_name"] == display_name)
  305. def test_handle_local_profile_change_with_deactivated_user(self) -> None:
  306. # create user
  307. r_user_id = "@regular:test"
  308. self.get_success(
  309. self.store.register_user(user_id=r_user_id, password_hash=None)
  310. )
  311. # update profile
  312. display_name = "Regular User"
  313. profile_info = ProfileInfo(avatar_url="avatar_url", display_name=display_name)
  314. self.get_success(
  315. self.handler.handle_local_profile_change(r_user_id, profile_info)
  316. )
  317. # profile is in directory
  318. profile = self.get_success(self.store.get_user_in_directory(r_user_id))
  319. assert profile is not None
  320. self.assertTrue(profile["display_name"] == display_name)
  321. # deactivate user
  322. self.get_success(self.store.set_user_deactivated_status(r_user_id, True))
  323. self.get_success(self.handler.handle_local_user_deactivated(r_user_id))
  324. # profile is not in directory
  325. profile = self.get_success(self.store.get_user_in_directory(r_user_id))
  326. self.assertIsNone(profile)
  327. # update profile after deactivation
  328. self.get_success(
  329. self.handler.handle_local_profile_change(r_user_id, profile_info)
  330. )
  331. # profile is furthermore not in directory
  332. profile = self.get_success(self.store.get_user_in_directory(r_user_id))
  333. self.assertIsNone(profile)
  334. def test_handle_local_profile_change_with_appservice_user(self) -> None:
  335. # create user
  336. as_user_id, _ = self.register_appservice_user(
  337. "as_user_alice", self.appservice.token
  338. )
  339. # profile is not in directory
  340. profile = self.get_success(self.store.get_user_in_directory(as_user_id))
  341. self.assertIsNone(profile)
  342. # update profile
  343. profile_info = ProfileInfo(avatar_url="avatar_url", display_name="4L1c3")
  344. self.get_success(
  345. self.handler.handle_local_profile_change(as_user_id, profile_info)
  346. )
  347. # profile is still not in directory
  348. profile = self.get_success(self.store.get_user_in_directory(as_user_id))
  349. self.assertIsNone(profile)
  350. def test_handle_local_profile_change_with_appservice_sender(self) -> None:
  351. # profile is not in directory
  352. profile = self.get_success(
  353. self.store.get_user_in_directory(self.appservice.sender)
  354. )
  355. self.assertIsNone(profile)
  356. # update profile
  357. profile_info = ProfileInfo(avatar_url="avatar_url", display_name="4L1c3")
  358. self.get_success(
  359. self.handler.handle_local_profile_change(
  360. self.appservice.sender, profile_info
  361. )
  362. )
  363. # profile is still not in directory
  364. profile = self.get_success(
  365. self.store.get_user_in_directory(self.appservice.sender)
  366. )
  367. self.assertIsNone(profile)
  368. def test_handle_user_deactivated_support_user(self) -> None:
  369. s_user_id = "@support:test"
  370. self.get_success(
  371. self.store.register_user(
  372. user_id=s_user_id, password_hash=None, user_type=UserTypes.SUPPORT
  373. )
  374. )
  375. mock_remove_from_user_dir = Mock(return_value=make_awaitable(None))
  376. with patch.object(
  377. self.store, "remove_from_user_dir", mock_remove_from_user_dir
  378. ):
  379. self.get_success(self.handler.handle_local_user_deactivated(s_user_id))
  380. # BUG: the correct spelling is assert_not_called, but that makes the test fail
  381. # and it's not clear that this is actually the behaviour we want.
  382. mock_remove_from_user_dir.not_called()
  383. def test_handle_user_deactivated_regular_user(self) -> None:
  384. r_user_id = "@regular:test"
  385. self.get_success(
  386. self.store.register_user(user_id=r_user_id, password_hash=None)
  387. )
  388. mock_remove_from_user_dir = Mock(return_value=make_awaitable(None))
  389. with patch.object(
  390. self.store, "remove_from_user_dir", mock_remove_from_user_dir
  391. ):
  392. self.get_success(self.handler.handle_local_user_deactivated(r_user_id))
  393. mock_remove_from_user_dir.assert_called_once_with(r_user_id)
  394. def test_reactivation_makes_regular_user_searchable(self) -> None:
  395. user = self.register_user("regular", "pass")
  396. user_token = self.login(user, "pass")
  397. admin_user = self.register_user("admin", "pass", admin=True)
  398. admin_token = self.login(admin_user, "pass")
  399. # Ensure the regular user is publicly visible and searchable.
  400. self.helper.create_room_as(user, is_public=True, tok=user_token)
  401. s = self.get_success(self.handler.search_users(admin_user, user, 10))
  402. self.assertEqual(len(s["results"]), 1)
  403. self.assertEqual(s["results"][0]["user_id"], user)
  404. # Deactivate the user and check they're not searchable.
  405. deactivate_handler = self.hs.get_deactivate_account_handler()
  406. self.get_success(
  407. deactivate_handler.deactivate_account(
  408. user, erase_data=False, requester=create_requester(admin_user)
  409. )
  410. )
  411. s = self.get_success(self.handler.search_users(admin_user, user, 10))
  412. self.assertEqual(s["results"], [])
  413. # Reactivate the user
  414. channel = self.make_request(
  415. "PUT",
  416. f"/_synapse/admin/v2/users/{quote(user)}",
  417. access_token=admin_token,
  418. content={"deactivated": False, "password": "pass"},
  419. )
  420. self.assertEqual(channel.code, 200)
  421. user_token = self.login(user, "pass")
  422. self.helper.create_room_as(user, is_public=True, tok=user_token)
  423. # Check they're searchable.
  424. s = self.get_success(self.handler.search_users(admin_user, user, 10))
  425. self.assertEqual(len(s["results"]), 1)
  426. self.assertEqual(s["results"][0]["user_id"], user)
  427. def test_process_join_after_server_leaves_room(self) -> None:
  428. alice = self.register_user("alice", "pass")
  429. alice_token = self.login(alice, "pass")
  430. bob = self.register_user("bob", "pass")
  431. bob_token = self.login(bob, "pass")
  432. # Alice makes two rooms. Bob joins one of them.
  433. room1 = self.helper.create_room_as(alice, tok=alice_token)
  434. room2 = self.helper.create_room_as(alice, tok=alice_token)
  435. self.helper.join(room1, bob, tok=bob_token)
  436. # The user sharing tables should have been updated.
  437. public1 = self.get_success(self.user_dir_helper.get_users_in_public_rooms())
  438. self.assertEqual(set(public1), {(alice, room1), (alice, room2), (bob, room1)})
  439. # Alice leaves room1. The user sharing tables should be updated.
  440. self.helper.leave(room1, alice, tok=alice_token)
  441. public2 = self.get_success(self.user_dir_helper.get_users_in_public_rooms())
  442. self.assertEqual(set(public2), {(alice, room2), (bob, room1)})
  443. # Pause the processing of new events.
  444. dir_handler = self.hs.get_user_directory_handler()
  445. dir_handler.update_user_directory = False
  446. # Bob leaves one room and joins the other.
  447. self.helper.leave(room1, bob, tok=bob_token)
  448. self.helper.join(room2, bob, tok=bob_token)
  449. # Process the leave and join in one go.
  450. dir_handler.update_user_directory = True
  451. dir_handler.notify_new_event()
  452. self.wait_for_background_updates()
  453. # The user sharing tables should have been updated.
  454. public3 = self.get_success(self.user_dir_helper.get_users_in_public_rooms())
  455. self.assertEqual(set(public3), {(alice, room2), (bob, room2)})
  456. def test_per_room_profile_doesnt_alter_directory_entry(self) -> None:
  457. alice = self.register_user("alice", "pass")
  458. alice_token = self.login(alice, "pass")
  459. bob = self.register_user("bob", "pass")
  460. # Alice should have a user directory entry created at registration.
  461. users = self.get_success(self.user_dir_helper.get_profiles_in_user_directory())
  462. self.assertEqual(
  463. users[alice], ProfileInfo(display_name="alice", avatar_url=None)
  464. )
  465. # Alice makes a room for herself.
  466. room = self.helper.create_room_as(alice, is_public=True, tok=alice_token)
  467. # Alice sets a nickname unique to that room.
  468. self.helper.send_state(
  469. room,
  470. "m.room.member",
  471. {
  472. "displayname": "Freddy Mercury",
  473. "membership": "join",
  474. },
  475. alice_token,
  476. state_key=alice,
  477. )
  478. # Alice's display name remains the same in the user directory.
  479. search_result = self.get_success(self.handler.search_users(bob, alice, 10))
  480. self.assertEqual(
  481. search_result["results"],
  482. [{"display_name": "alice", "avatar_url": None, "user_id": alice}],
  483. 0,
  484. )
  485. def test_making_room_public_doesnt_alter_directory_entry(self) -> None:
  486. """Per-room names shouldn't go to the directory when the room becomes public.
  487. This isn't about preventing a leak (the room is now public, so the nickname
  488. is too). It's about preserving the invariant that we only show a user's public
  489. profile in the user directory results.
  490. I made this a Synapse test case rather than a Complement one because
  491. I think this is (strictly speaking) an implementation choice. Synapse
  492. has chosen to only ever use the public profile when responding to a user
  493. directory search. There's no privacy leak here, because making the room
  494. public discloses the per-room name.
  495. The spec doesn't mandate anything about _how_ a user
  496. should appear in a /user_directory/search result. Hypothetical example:
  497. suppose Bob searches for Alice. When representing Alice in a search
  498. result, it's reasonable to use any of Alice's nicknames that Bob is
  499. aware of. Heck, maybe we even want to use lots of them in a combined
  500. displayname like `Alice (aka "ali", "ally", "41iC3")`.
  501. """
  502. # TODO the same should apply when Alice is a remote user.
  503. alice = self.register_user("alice", "pass")
  504. alice_token = self.login(alice, "pass")
  505. bob = self.register_user("bob", "pass")
  506. bob_token = self.login(bob, "pass")
  507. # Alice and Bob are in a private room.
  508. room = self.helper.create_room_as(alice, is_public=False, tok=alice_token)
  509. self.helper.invite(room, src=alice, targ=bob, tok=alice_token)
  510. self.helper.join(room, user=bob, tok=bob_token)
  511. # Alice has a nickname unique to that room.
  512. self.helper.send_state(
  513. room,
  514. "m.room.member",
  515. {
  516. "displayname": "Freddy Mercury",
  517. "membership": "join",
  518. },
  519. alice_token,
  520. state_key=alice,
  521. )
  522. # Check Alice isn't recorded as being in a public room.
  523. public = self.get_success(self.user_dir_helper.get_users_in_public_rooms())
  524. self.assertNotIn((alice, room), public)
  525. # One of them makes the room public.
  526. self.helper.send_state(
  527. room,
  528. "m.room.join_rules",
  529. {"join_rule": "public"},
  530. alice_token,
  531. )
  532. # Check that Alice is now recorded as being in a public room
  533. public = self.get_success(self.user_dir_helper.get_users_in_public_rooms())
  534. self.assertIn((alice, room), public)
  535. # Alice's display name remains the same in the user directory.
  536. search_result = self.get_success(self.handler.search_users(bob, alice, 10))
  537. self.assertEqual(
  538. search_result["results"],
  539. [{"display_name": "alice", "avatar_url": None, "user_id": alice}],
  540. 0,
  541. )
  542. def test_private_room(self) -> None:
  543. """
  544. A user can be searched for only by people that are either in a public
  545. room, or that share a private chat.
  546. """
  547. u1 = self.register_user("user1", "pass")
  548. u1_token = self.login(u1, "pass")
  549. u2 = self.register_user("user2", "pass")
  550. u2_token = self.login(u2, "pass")
  551. u3 = self.register_user("user3", "pass")
  552. # u1 can't see u2 until they share a private room, or u1 is in a public room.
  553. s = self.get_success(self.handler.search_users(u1, "user2", 10))
  554. self.assertEqual(len(s["results"]), 0)
  555. # Get u1 and u2 into a private room.
  556. room = self.helper.create_room_as(u1, is_public=False, tok=u1_token)
  557. self.helper.invite(room, src=u1, targ=u2, tok=u1_token)
  558. self.helper.join(room, user=u2, tok=u2_token)
  559. # Check we have populated the database correctly.
  560. users, public_users, shares_private = self.get_success(
  561. self.user_dir_helper.get_tables()
  562. )
  563. self.assertEqual(users, {u1, u2, u3})
  564. self.assertEqual(shares_private, {(u1, u2, room), (u2, u1, room)})
  565. self.assertEqual(public_users, set())
  566. # We get one search result when searching for user2 by user1.
  567. s = self.get_success(self.handler.search_users(u1, "user2", 10))
  568. self.assertEqual(len(s["results"]), 1)
  569. # We get NO search results when searching for user2 by user3.
  570. s = self.get_success(self.handler.search_users(u3, "user2", 10))
  571. self.assertEqual(len(s["results"]), 0)
  572. # We get NO search results when searching for user3 by user1.
  573. s = self.get_success(self.handler.search_users(u1, "user3", 10))
  574. self.assertEqual(len(s["results"]), 0)
  575. # User 2 then leaves.
  576. self.helper.leave(room, user=u2, tok=u2_token)
  577. # Check this is reflected in the DB.
  578. users, public_users, shares_private = self.get_success(
  579. self.user_dir_helper.get_tables()
  580. )
  581. self.assertEqual(users, {u1, u2, u3})
  582. self.assertEqual(shares_private, set())
  583. self.assertEqual(public_users, set())
  584. # User1 now gets no search results for any of the other users.
  585. s = self.get_success(self.handler.search_users(u1, "user2", 10))
  586. self.assertEqual(len(s["results"]), 0)
  587. s = self.get_success(self.handler.search_users(u1, "user3", 10))
  588. self.assertEqual(len(s["results"]), 0)
  589. def test_joining_private_room_with_excluded_user(self) -> None:
  590. """
  591. When a user excluded from the user directory, E say, joins a private
  592. room, E will not appear in the `users_who_share_private_rooms` table.
  593. When a normal user, U say, joins a private room containing E, then
  594. U will appear in the `users_who_share_private_rooms` table, but E will
  595. not.
  596. """
  597. # Setup a support and two normal users.
  598. alice = self.register_user("alice", "pass")
  599. alice_token = self.login(alice, "pass")
  600. bob = self.register_user("bob", "pass")
  601. bob_token = self.login(bob, "pass")
  602. support = "@support1:test"
  603. self.get_success(
  604. self.store.register_user(
  605. user_id=support, password_hash=None, user_type=UserTypes.SUPPORT
  606. )
  607. )
  608. # Alice makes a room. Inject the support user into the room.
  609. room = self.helper.create_room_as(alice, is_public=False, tok=alice_token)
  610. self.get_success(inject_member_event(self.hs, room, support, "join"))
  611. # Check the DB state. The support user should not be in the directory.
  612. users, in_public, in_private = self.get_success(
  613. self.user_dir_helper.get_tables()
  614. )
  615. self.assertEqual(users, {alice, bob})
  616. self.assertEqual(in_public, set())
  617. self.assertEqual(in_private, set())
  618. # Then invite Bob, who accepts.
  619. self.helper.invite(room, alice, bob, tok=alice_token)
  620. self.helper.join(room, bob, tok=bob_token)
  621. # Check the DB state. The support user should not be in the directory.
  622. users, in_public, in_private = self.get_success(
  623. self.user_dir_helper.get_tables()
  624. )
  625. self.assertEqual(users, {alice, bob})
  626. self.assertEqual(in_public, set())
  627. self.assertEqual(in_private, {(alice, bob, room), (bob, alice, room)})
  628. def test_spam_checker(self) -> None:
  629. """
  630. A user which fails the spam checks will not appear in search results.
  631. """
  632. u1 = self.register_user("user1", "pass")
  633. u1_token = self.login(u1, "pass")
  634. u2 = self.register_user("user2", "pass")
  635. u2_token = self.login(u2, "pass")
  636. # We do not add users to the directory until they join a room.
  637. s = self.get_success(self.handler.search_users(u1, "user2", 10))
  638. self.assertEqual(len(s["results"]), 0)
  639. room = self.helper.create_room_as(u1, is_public=False, tok=u1_token)
  640. self.helper.invite(room, src=u1, targ=u2, tok=u1_token)
  641. self.helper.join(room, user=u2, tok=u2_token)
  642. # Check we have populated the database correctly.
  643. shares_private = self.get_success(
  644. self.user_dir_helper.get_users_who_share_private_rooms()
  645. )
  646. public_users = self.get_success(
  647. self.user_dir_helper.get_users_in_public_rooms()
  648. )
  649. self.assertEqual(shares_private, {(u1, u2, room), (u2, u1, room)})
  650. self.assertEqual(public_users, set())
  651. # We get one search result when searching for user2 by user1.
  652. s = self.get_success(self.handler.search_users(u1, "user2", 10))
  653. self.assertEqual(len(s["results"]), 1)
  654. async def allow_all(user_profile: ProfileInfo) -> bool:
  655. # Allow all users.
  656. return False
  657. # Configure a spam checker that does not filter any users.
  658. spam_checker = self.hs.get_spam_checker()
  659. spam_checker._check_username_for_spam_callbacks = [allow_all]
  660. # The results do not change:
  661. # We get one search result when searching for user2 by user1.
  662. s = self.get_success(self.handler.search_users(u1, "user2", 10))
  663. self.assertEqual(len(s["results"]), 1)
  664. # Configure a spam checker that filters all users.
  665. async def block_all(user_profile: ProfileInfo) -> bool:
  666. # All users are spammy.
  667. return True
  668. spam_checker._check_username_for_spam_callbacks = [block_all]
  669. # User1 now gets no search results for any of the other users.
  670. s = self.get_success(self.handler.search_users(u1, "user2", 10))
  671. self.assertEqual(len(s["results"]), 0)
  672. def test_legacy_spam_checker(self) -> None:
  673. """
  674. A spam checker without the expected method should be ignored.
  675. """
  676. u1 = self.register_user("user1", "pass")
  677. u1_token = self.login(u1, "pass")
  678. u2 = self.register_user("user2", "pass")
  679. u2_token = self.login(u2, "pass")
  680. # We do not add users to the directory until they join a room.
  681. s = self.get_success(self.handler.search_users(u1, "user2", 10))
  682. self.assertEqual(len(s["results"]), 0)
  683. room = self.helper.create_room_as(u1, is_public=False, tok=u1_token)
  684. self.helper.invite(room, src=u1, targ=u2, tok=u1_token)
  685. self.helper.join(room, user=u2, tok=u2_token)
  686. # Check we have populated the database correctly.
  687. shares_private = self.get_success(
  688. self.user_dir_helper.get_users_who_share_private_rooms()
  689. )
  690. public_users = self.get_success(
  691. self.user_dir_helper.get_users_in_public_rooms()
  692. )
  693. self.assertEqual(shares_private, {(u1, u2, room), (u2, u1, room)})
  694. self.assertEqual(public_users, set())
  695. # Configure a spam checker.
  696. spam_checker = self.hs.get_spam_checker()
  697. # The spam checker doesn't need any methods, so create a bare object.
  698. spam_checker.spam_checker = object()
  699. # We get one search result when searching for user2 by user1.
  700. s = self.get_success(self.handler.search_users(u1, "user2", 10))
  701. self.assertEqual(len(s["results"]), 1)
  702. def test_initial_share_all_users(self) -> None:
  703. """
  704. Search all users = True means that a user does not have to share a
  705. private room with the searching user or be in a public room to be search
  706. visible.
  707. """
  708. self.handler.search_all_users = True
  709. self.hs.config.userdirectory.user_directory_search_all_users = True
  710. u1 = self.register_user("user1", "pass")
  711. self.register_user("user2", "pass")
  712. u3 = self.register_user("user3", "pass")
  713. shares_private = self.get_success(
  714. self.user_dir_helper.get_users_who_share_private_rooms()
  715. )
  716. public_users = self.get_success(
  717. self.user_dir_helper.get_users_in_public_rooms()
  718. )
  719. # No users share rooms
  720. self.assertEqual(public_users, set())
  721. self.assertEqual(shares_private, set())
  722. # Despite not sharing a room, search_all_users means we get a search
  723. # result.
  724. s = self.get_success(self.handler.search_users(u1, u3, 10))
  725. self.assertEqual(len(s["results"]), 1)
  726. # We can find the other two users
  727. s = self.get_success(self.handler.search_users(u1, "user", 10))
  728. self.assertEqual(len(s["results"]), 2)
  729. # Registering a user and then searching for them works.
  730. u4 = self.register_user("user4", "pass")
  731. s = self.get_success(self.handler.search_users(u1, u4, 10))
  732. self.assertEqual(len(s["results"]), 1)
  733. @override_config(
  734. {
  735. "user_directory": {
  736. "enabled": True,
  737. "search_all_users": True,
  738. "prefer_local_users": True,
  739. }
  740. }
  741. )
  742. def test_prefer_local_users(self) -> None:
  743. """Tests that local users are shown higher in search results when
  744. user_directory.prefer_local_users is True.
  745. """
  746. # Create a room and few users to test the directory with
  747. searching_user = self.register_user("searcher", "password")
  748. searching_user_tok = self.login("searcher", "password")
  749. room_id = self.helper.create_room_as(
  750. searching_user,
  751. room_version=RoomVersions.V1.identifier,
  752. tok=searching_user_tok,
  753. )
  754. # Create a few local users and join them to the room
  755. local_user_1 = self.register_user("user_xxxxx", "password")
  756. local_user_2 = self.register_user("user_bbbbb", "password")
  757. local_user_3 = self.register_user("user_zzzzz", "password")
  758. self._add_user_to_room(room_id, RoomVersions.V1, local_user_1)
  759. self._add_user_to_room(room_id, RoomVersions.V1, local_user_2)
  760. self._add_user_to_room(room_id, RoomVersions.V1, local_user_3)
  761. # Create a few "remote" users and join them to the room
  762. remote_user_1 = "@user_aaaaa:remote_server"
  763. remote_user_2 = "@user_yyyyy:remote_server"
  764. remote_user_3 = "@user_ccccc:remote_server"
  765. self._add_user_to_room(room_id, RoomVersions.V1, remote_user_1)
  766. self._add_user_to_room(room_id, RoomVersions.V1, remote_user_2)
  767. self._add_user_to_room(room_id, RoomVersions.V1, remote_user_3)
  768. local_users = [local_user_1, local_user_2, local_user_3]
  769. remote_users = [remote_user_1, remote_user_2, remote_user_3]
  770. # The local searching user searches for the term "user", which other users have
  771. # in their user id
  772. results = self.get_success(
  773. self.handler.search_users(searching_user, "user", 20)
  774. )["results"]
  775. received_user_id_ordering = [result["user_id"] for result in results]
  776. # Typically we'd expect Synapse to return users in lexicographical order,
  777. # assuming they have similar User IDs/display names, and profile information.
  778. # Check that the order of returned results using our module is as we expect,
  779. # i.e our local users show up first, despite all users having lexographically mixed
  780. # user IDs.
  781. [self.assertIn(user, local_users) for user in received_user_id_ordering[:3]]
  782. [self.assertIn(user, remote_users) for user in received_user_id_ordering[3:]]
  783. def _add_user_to_room(
  784. self,
  785. room_id: str,
  786. room_version: RoomVersion,
  787. user_id: str,
  788. ) -> None:
  789. # Add a user to the room.
  790. builder = self.event_builder_factory.for_room_version(
  791. room_version,
  792. {
  793. "type": "m.room.member",
  794. "sender": user_id,
  795. "state_key": user_id,
  796. "room_id": room_id,
  797. "content": {"membership": "join"},
  798. },
  799. )
  800. event, context = self.get_success(
  801. self.event_creation_handler.create_new_client_event(builder)
  802. )
  803. self.get_success(
  804. self.hs.get_storage_controllers().persistence.persist_event(event, context)
  805. )
  806. def test_local_user_leaving_room_remains_in_user_directory(self) -> None:
  807. """We've chosen to simplify the user directory's implementation by
  808. always including local users. Ensure this invariant is maintained when
  809. a local user
  810. - leaves a room, and
  811. - leaves the last room they're in which is visible to this server.
  812. This is user-visible if the "search_all_users" config option is on: the
  813. local user who left a room would no longer be searchable if this test fails!
  814. """
  815. alice = self.register_user("alice", "pass")
  816. alice_token = self.login(alice, "pass")
  817. bob = self.register_user("bob", "pass")
  818. bob_token = self.login(bob, "pass")
  819. # Alice makes two public rooms, which Bob joins.
  820. room1 = self.helper.create_room_as(alice, is_public=True, tok=alice_token)
  821. room2 = self.helper.create_room_as(alice, is_public=True, tok=alice_token)
  822. self.helper.join(room1, bob, tok=bob_token)
  823. self.helper.join(room2, bob, tok=bob_token)
  824. # The user directory tables are updated.
  825. users, in_public, in_private = self.get_success(
  826. self.user_dir_helper.get_tables()
  827. )
  828. self.assertEqual(users, {alice, bob})
  829. self.assertEqual(
  830. in_public, {(alice, room1), (alice, room2), (bob, room1), (bob, room2)}
  831. )
  832. self.assertEqual(in_private, set())
  833. # Alice leaves one room. She should still be in the directory.
  834. self.helper.leave(room1, alice, tok=alice_token)
  835. users, in_public, in_private = self.get_success(
  836. self.user_dir_helper.get_tables()
  837. )
  838. self.assertEqual(users, {alice, bob})
  839. self.assertEqual(in_public, {(alice, room2), (bob, room1), (bob, room2)})
  840. self.assertEqual(in_private, set())
  841. # Alice leaves the other. She should still be in the directory.
  842. self.helper.leave(room2, alice, tok=alice_token)
  843. self.wait_for_background_updates()
  844. users, in_public, in_private = self.get_success(
  845. self.user_dir_helper.get_tables()
  846. )
  847. self.assertEqual(users, {alice, bob})
  848. self.assertEqual(in_public, {(bob, room1), (bob, room2)})
  849. self.assertEqual(in_private, set())
  850. def test_ignore_display_names_with_null_codepoints(self) -> None:
  851. MXC_DUMMY = "mxc://dummy"
  852. # Alice creates a public room.
  853. alice = self.register_user("alice", "pass")
  854. # Alice has a user directory entry to start with.
  855. self.assertIn(
  856. alice,
  857. self.get_success(self.user_dir_helper.get_profiles_in_user_directory()),
  858. )
  859. # Alice changes her name to include a null codepoint.
  860. self.get_success(
  861. self.hs.get_user_directory_handler().handle_local_profile_change(
  862. alice,
  863. ProfileInfo(
  864. display_name="abcd\u0000efgh",
  865. avatar_url=MXC_DUMMY,
  866. ),
  867. )
  868. )
  869. # Alice's profile should be updated with the new avatar, but no display name.
  870. self.assertEqual(
  871. self.get_success(self.user_dir_helper.get_profiles_in_user_directory()),
  872. {alice: ProfileInfo(display_name=None, avatar_url=MXC_DUMMY)},
  873. )
  874. class TestUserDirSearchDisabled(unittest.HomeserverTestCase):
  875. servlets = [
  876. user_directory.register_servlets,
  877. room.register_servlets,
  878. login.register_servlets,
  879. synapse.rest.admin.register_servlets_for_client_rest_resource,
  880. ]
  881. def make_homeserver(self, reactor: MemoryReactor, clock: Clock) -> HomeServer:
  882. config = self.default_config()
  883. config["update_user_directory"] = True
  884. hs = self.setup_test_homeserver(config=config)
  885. self.config = hs.config
  886. return hs
  887. def test_disabling_room_list(self) -> None:
  888. self.config.userdirectory.user_directory_search_enabled = True
  889. # Create two users and put them in the same room.
  890. u1 = self.register_user("user1", "pass")
  891. u1_token = self.login(u1, "pass")
  892. u2 = self.register_user("user2", "pass")
  893. u2_token = self.login(u2, "pass")
  894. room = self.helper.create_room_as(u1, tok=u1_token)
  895. self.helper.join(room, user=u2, tok=u2_token)
  896. # Each should see the other when searching the user directory.
  897. channel = self.make_request(
  898. "POST",
  899. b"user_directory/search",
  900. b'{"search_term":"user2"}',
  901. access_token=u1_token,
  902. )
  903. self.assertEqual(200, channel.code, channel.result)
  904. self.assertTrue(len(channel.json_body["results"]) > 0)
  905. # Disable user directory and check search returns nothing
  906. self.config.userdirectory.user_directory_search_enabled = False
  907. channel = self.make_request(
  908. "POST",
  909. b"user_directory/search",
  910. b'{"search_term":"user2"}',
  911. access_token=u1_token,
  912. )
  913. self.assertEqual(200, channel.code, channel.result)
  914. self.assertTrue(len(channel.json_body["results"]) == 0)