1
0

test_presence_router.py 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  1. # Copyright 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 typing import Dict, Iterable, List, Optional, Set, Tuple, Union
  15. from unittest.mock import AsyncMock, Mock
  16. import attr
  17. from twisted.test.proto_helpers import MemoryReactor
  18. from synapse.api.constants import EduTypes
  19. from synapse.events.presence_router import PresenceRouter, load_legacy_presence_router
  20. from synapse.federation.units import Transaction
  21. from synapse.handlers.presence import UserPresenceState
  22. from synapse.module_api import ModuleApi
  23. from synapse.rest import admin
  24. from synapse.rest.client import login, presence, room
  25. from synapse.server import HomeServer
  26. from synapse.types import JsonDict, StreamToken, create_requester
  27. from synapse.util import Clock
  28. from tests.handlers.test_sync import generate_sync_config
  29. from tests.unittest import (
  30. FederatingHomeserverTestCase,
  31. HomeserverTestCase,
  32. override_config,
  33. )
  34. @attr.s
  35. class PresenceRouterTestConfig:
  36. users_who_should_receive_all_presence = attr.ib(type=List[str], default=[])
  37. class LegacyPresenceRouterTestModule:
  38. def __init__(self, config: PresenceRouterTestConfig, module_api: ModuleApi):
  39. self._config = config
  40. self._module_api = module_api
  41. async def get_users_for_states(
  42. self, state_updates: Iterable[UserPresenceState]
  43. ) -> Dict[str, Set[UserPresenceState]]:
  44. users_to_state = {
  45. user_id: set(state_updates)
  46. for user_id in self._config.users_who_should_receive_all_presence
  47. }
  48. return users_to_state
  49. async def get_interested_users(self, user_id: str) -> Union[Set[str], str]:
  50. if user_id in self._config.users_who_should_receive_all_presence:
  51. return PresenceRouter.ALL_USERS
  52. return set()
  53. @staticmethod
  54. def parse_config(config_dict: dict) -> PresenceRouterTestConfig:
  55. """Parse a configuration dictionary from the homeserver config, do
  56. some validation and return a typed PresenceRouterConfig.
  57. Args:
  58. config_dict: The configuration dictionary.
  59. Returns:
  60. A validated config object.
  61. """
  62. # Initialise a typed config object
  63. config = PresenceRouterTestConfig()
  64. users_who_should_receive_all_presence = config_dict.get(
  65. "users_who_should_receive_all_presence"
  66. )
  67. assert isinstance(users_who_should_receive_all_presence, list)
  68. config.users_who_should_receive_all_presence = (
  69. users_who_should_receive_all_presence
  70. )
  71. return config
  72. class PresenceRouterTestModule:
  73. def __init__(self, config: PresenceRouterTestConfig, api: ModuleApi):
  74. self._config = config
  75. self._module_api = api
  76. api.register_presence_router_callbacks(
  77. get_users_for_states=self.get_users_for_states,
  78. get_interested_users=self.get_interested_users,
  79. )
  80. async def get_users_for_states(
  81. self, state_updates: Iterable[UserPresenceState]
  82. ) -> Dict[str, Set[UserPresenceState]]:
  83. users_to_state = {
  84. user_id: set(state_updates)
  85. for user_id in self._config.users_who_should_receive_all_presence
  86. }
  87. return users_to_state
  88. async def get_interested_users(self, user_id: str) -> Union[Set[str], str]:
  89. if user_id in self._config.users_who_should_receive_all_presence:
  90. return PresenceRouter.ALL_USERS
  91. return set()
  92. @staticmethod
  93. def parse_config(config_dict: dict) -> PresenceRouterTestConfig:
  94. """Parse a configuration dictionary from the homeserver config, do
  95. some validation and return a typed PresenceRouterConfig.
  96. Args:
  97. config_dict: The configuration dictionary.
  98. Returns:
  99. A validated config object.
  100. """
  101. # Initialise a typed config object
  102. config = PresenceRouterTestConfig()
  103. users_who_should_receive_all_presence = config_dict.get(
  104. "users_who_should_receive_all_presence"
  105. )
  106. assert isinstance(users_who_should_receive_all_presence, list)
  107. config.users_who_should_receive_all_presence = (
  108. users_who_should_receive_all_presence
  109. )
  110. return config
  111. class PresenceRouterTestCase(FederatingHomeserverTestCase):
  112. """
  113. Test cases using a custom PresenceRouter
  114. By default in test cases, federation sending is disabled. This class re-enables it
  115. for the main process by setting `federation_sender_instances` to None.
  116. """
  117. servlets = [
  118. admin.register_servlets,
  119. login.register_servlets,
  120. room.register_servlets,
  121. presence.register_servlets,
  122. ]
  123. def make_homeserver(self, reactor: MemoryReactor, clock: Clock) -> HomeServer:
  124. # Mock out the calls over federation.
  125. self.fed_transport_client = Mock(spec=["send_transaction"])
  126. self.fed_transport_client.send_transaction = AsyncMock(return_value={})
  127. hs = self.setup_test_homeserver(
  128. federation_transport_client=self.fed_transport_client,
  129. )
  130. load_legacy_presence_router(hs)
  131. return hs
  132. def prepare(
  133. self, reactor: MemoryReactor, clock: Clock, homeserver: HomeServer
  134. ) -> None:
  135. self.sync_handler = self.hs.get_sync_handler()
  136. self.module_api = homeserver.get_module_api()
  137. def default_config(self) -> JsonDict:
  138. config = super().default_config()
  139. config["federation_sender_instances"] = None
  140. return config
  141. @override_config(
  142. {
  143. "presence": {
  144. "presence_router": {
  145. "module": __name__ + ".LegacyPresenceRouterTestModule",
  146. "config": {
  147. "users_who_should_receive_all_presence": [
  148. "@presence_gobbler:test",
  149. ]
  150. },
  151. }
  152. },
  153. }
  154. )
  155. def test_receiving_all_presence_legacy(self) -> None:
  156. self.receiving_all_presence_test_body()
  157. @override_config(
  158. {
  159. "modules": [
  160. {
  161. "module": __name__ + ".PresenceRouterTestModule",
  162. "config": {
  163. "users_who_should_receive_all_presence": [
  164. "@presence_gobbler:test",
  165. ]
  166. },
  167. },
  168. ],
  169. }
  170. )
  171. def test_receiving_all_presence(self) -> None:
  172. self.receiving_all_presence_test_body()
  173. def receiving_all_presence_test_body(self) -> None:
  174. """Test that a user that does not share a room with another other can receive
  175. presence for them, due to presence routing.
  176. """
  177. # Create a user who should receive all presence of others
  178. self.presence_receiving_user_id = self.register_user(
  179. "presence_gobbler", "monkey"
  180. )
  181. self.presence_receiving_user_tok = self.login("presence_gobbler", "monkey")
  182. # And two users who should not have any special routing
  183. self.other_user_one_id = self.register_user("other_user_one", "monkey")
  184. self.other_user_one_tok = self.login("other_user_one", "monkey")
  185. self.other_user_two_id = self.register_user("other_user_two", "monkey")
  186. self.other_user_two_tok = self.login("other_user_two", "monkey")
  187. # Put the other two users in a room with each other
  188. room_id = self.helper.create_room_as(
  189. self.other_user_one_id, tok=self.other_user_one_tok
  190. )
  191. self.helper.invite(
  192. room_id,
  193. self.other_user_one_id,
  194. self.other_user_two_id,
  195. tok=self.other_user_one_tok,
  196. )
  197. self.helper.join(room_id, self.other_user_two_id, tok=self.other_user_two_tok)
  198. # User one sends some presence
  199. send_presence_update(
  200. self,
  201. self.other_user_one_id,
  202. self.other_user_one_tok,
  203. "online",
  204. "boop",
  205. )
  206. # Check that the presence receiving user gets user one's presence when syncing
  207. presence_updates, sync_token = sync_presence(
  208. self, self.presence_receiving_user_id
  209. )
  210. self.assertEqual(len(presence_updates), 1)
  211. presence_update: UserPresenceState = presence_updates[0]
  212. self.assertEqual(presence_update.user_id, self.other_user_one_id)
  213. self.assertEqual(presence_update.state, "online")
  214. self.assertEqual(presence_update.status_msg, "boop")
  215. # Have all three users send presence
  216. send_presence_update(
  217. self,
  218. self.other_user_one_id,
  219. self.other_user_one_tok,
  220. "online",
  221. "user_one",
  222. )
  223. send_presence_update(
  224. self,
  225. self.other_user_two_id,
  226. self.other_user_two_tok,
  227. "online",
  228. "user_two",
  229. )
  230. send_presence_update(
  231. self,
  232. self.presence_receiving_user_id,
  233. self.presence_receiving_user_tok,
  234. "online",
  235. "presence_gobbler",
  236. )
  237. # Check that the presence receiving user gets everyone's presence
  238. presence_updates, _ = sync_presence(
  239. self, self.presence_receiving_user_id, sync_token
  240. )
  241. self.assertEqual(len(presence_updates), 3)
  242. # But that User One only get itself and User Two's presence
  243. presence_updates, _ = sync_presence(self, self.other_user_one_id)
  244. self.assertEqual(len(presence_updates), 2)
  245. found = False
  246. for update in presence_updates:
  247. if update.user_id == self.other_user_two_id:
  248. self.assertEqual(update.state, "online")
  249. self.assertEqual(update.status_msg, "user_two")
  250. found = True
  251. self.assertTrue(found)
  252. @override_config(
  253. {
  254. "presence": {
  255. "presence_router": {
  256. "module": __name__ + ".LegacyPresenceRouterTestModule",
  257. "config": {
  258. "users_who_should_receive_all_presence": [
  259. "@presence_gobbler1:test",
  260. "@presence_gobbler2:test",
  261. "@far_away_person:island",
  262. ]
  263. },
  264. }
  265. },
  266. }
  267. )
  268. def test_send_local_online_presence_to_with_module_legacy(self) -> None:
  269. self.send_local_online_presence_to_with_module_test_body()
  270. @override_config(
  271. {
  272. "modules": [
  273. {
  274. "module": __name__ + ".PresenceRouterTestModule",
  275. "config": {
  276. "users_who_should_receive_all_presence": [
  277. "@presence_gobbler1:test",
  278. "@presence_gobbler2:test",
  279. "@far_away_person:island",
  280. ]
  281. },
  282. },
  283. ],
  284. }
  285. )
  286. def test_send_local_online_presence_to_with_module(self) -> None:
  287. self.send_local_online_presence_to_with_module_test_body()
  288. def send_local_online_presence_to_with_module_test_body(self) -> None:
  289. """Tests that send_local_presence_to_users sends local online presence to a set
  290. of specified local and remote users, with a custom PresenceRouter module enabled.
  291. """
  292. # Create a user who will send presence updates
  293. self.other_user_id = self.register_user("other_user", "monkey")
  294. self.other_user_tok = self.login("other_user", "monkey")
  295. # And another two users that will also send out presence updates, as well as receive
  296. # theirs and everyone else's
  297. self.presence_receiving_user_one_id = self.register_user(
  298. "presence_gobbler1", "monkey"
  299. )
  300. self.presence_receiving_user_one_tok = self.login("presence_gobbler1", "monkey")
  301. self.presence_receiving_user_two_id = self.register_user(
  302. "presence_gobbler2", "monkey"
  303. )
  304. self.presence_receiving_user_two_tok = self.login("presence_gobbler2", "monkey")
  305. # Have all three users send some presence updates
  306. send_presence_update(
  307. self,
  308. self.other_user_id,
  309. self.other_user_tok,
  310. "online",
  311. "I'm online!",
  312. )
  313. send_presence_update(
  314. self,
  315. self.presence_receiving_user_one_id,
  316. self.presence_receiving_user_one_tok,
  317. "online",
  318. "I'm also online!",
  319. )
  320. send_presence_update(
  321. self,
  322. self.presence_receiving_user_two_id,
  323. self.presence_receiving_user_two_tok,
  324. "unavailable",
  325. "I'm in a meeting!",
  326. )
  327. # Mark each presence-receiving user for receiving all user presence
  328. self.get_success(
  329. self.module_api.send_local_online_presence_to(
  330. [
  331. self.presence_receiving_user_one_id,
  332. self.presence_receiving_user_two_id,
  333. ]
  334. )
  335. )
  336. # Perform a sync for each user
  337. # The other user should only receive their own presence
  338. presence_updates, _ = sync_presence(self, self.other_user_id)
  339. self.assertEqual(len(presence_updates), 1)
  340. presence_update: UserPresenceState = presence_updates[0]
  341. self.assertEqual(presence_update.user_id, self.other_user_id)
  342. self.assertEqual(presence_update.state, "online")
  343. self.assertEqual(presence_update.status_msg, "I'm online!")
  344. # Whereas both presence receiving users should receive everyone's presence updates
  345. presence_updates, _ = sync_presence(self, self.presence_receiving_user_one_id)
  346. self.assertEqual(len(presence_updates), 3)
  347. presence_updates, _ = sync_presence(self, self.presence_receiving_user_two_id)
  348. self.assertEqual(len(presence_updates), 3)
  349. # We stagger sending of presence, so we need to wait a bit for them to
  350. # get sent out.
  351. self.reactor.advance(60)
  352. # Test that sending to a remote user works
  353. remote_user_id = "@far_away_person:island"
  354. # Note that due to the remote user being in our module's
  355. # users_who_should_receive_all_presence config, they would have
  356. # received user presence updates already.
  357. #
  358. # Thus we reset the mock, and try sending all online local user
  359. # presence again
  360. self.fed_transport_client.send_transaction.reset_mock()
  361. # Broadcast local user online presence
  362. self.get_success(
  363. self.module_api.send_local_online_presence_to([remote_user_id])
  364. )
  365. # We stagger sending of presence, so we need to wait a bit for them to
  366. # get sent out.
  367. self.reactor.advance(60)
  368. # Check that the expected presence updates were sent
  369. # We explicitly compare using sets as we expect that calling
  370. # module_api.send_local_online_presence_to will create a presence
  371. # update that is a duplicate of the specified user's current presence.
  372. # These are sent to clients and will be picked up below, thus we use a
  373. # set to deduplicate. We're just interested that non-offline updates were
  374. # sent out for each user ID.
  375. expected_users = {
  376. self.other_user_id,
  377. self.presence_receiving_user_one_id,
  378. self.presence_receiving_user_two_id,
  379. }
  380. found_users = set()
  381. calls = self.fed_transport_client.send_transaction.call_args_list
  382. for call in calls:
  383. call_args = call[0]
  384. federation_transaction: Transaction = call_args[0]
  385. # Get the sent EDUs in this transaction
  386. edus = federation_transaction.get_dict()["edus"]
  387. for edu in edus:
  388. # Make sure we're only checking presence-type EDUs
  389. if edu["edu_type"] != EduTypes.PRESENCE:
  390. continue
  391. # EDUs can contain multiple presence updates
  392. for presence_edu in edu["content"]["push"]:
  393. # Check for presence updates that contain the user IDs we're after
  394. found_users.add(presence_edu["user_id"])
  395. # Ensure that no offline states are being sent out
  396. self.assertNotEqual(presence_edu["presence"], "offline")
  397. self.assertEqual(found_users, expected_users)
  398. def send_presence_update(
  399. testcase: HomeserverTestCase,
  400. user_id: str,
  401. access_token: str,
  402. presence_state: str,
  403. status_message: Optional[str] = None,
  404. ) -> JsonDict:
  405. # Build the presence body
  406. body = {"presence": presence_state}
  407. if status_message:
  408. body["status_msg"] = status_message
  409. # Update the user's presence state
  410. channel = testcase.make_request(
  411. "PUT", "/presence/%s/status" % (user_id,), body, access_token=access_token
  412. )
  413. testcase.assertEqual(channel.code, 200)
  414. return channel.json_body
  415. def sync_presence(
  416. testcase: HomeserverTestCase,
  417. user_id: str,
  418. since_token: Optional[StreamToken] = None,
  419. ) -> Tuple[List[UserPresenceState], StreamToken]:
  420. """Perform a sync request for the given user and return the user presence updates
  421. they've received, as well as the next_batch token.
  422. This method assumes testcase.sync_handler points to the homeserver's sync handler.
  423. Args:
  424. testcase: The testcase that is currently being run.
  425. user_id: The ID of the user to generate a sync response for.
  426. since_token: An optional token to indicate from at what point to sync from.
  427. Returns:
  428. A tuple containing a list of presence updates, and the sync response's
  429. next_batch token.
  430. """
  431. requester = create_requester(user_id)
  432. sync_config = generate_sync_config(requester.user.to_string())
  433. sync_result = testcase.get_success(
  434. testcase.hs.get_sync_handler().wait_for_sync_for_user(
  435. requester, sync_config, since_token
  436. )
  437. )
  438. return sync_result.presence, sync_result.next_batch