test_sync.py 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976
  1. # Copyright 2018-2019 New Vector Ltd
  2. # Copyright 2019 The Matrix.org Foundation C.I.C.
  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. import json
  16. from http import HTTPStatus
  17. from typing import List, Optional
  18. from parameterized import parameterized
  19. from twisted.test.proto_helpers import MemoryReactor
  20. import synapse.rest.admin
  21. from synapse.api.constants import (
  22. EduTypes,
  23. EventContentFields,
  24. EventTypes,
  25. ReceiptTypes,
  26. RelationTypes,
  27. )
  28. from synapse.rest.client import devices, knock, login, read_marker, receipts, room, sync
  29. from synapse.server import HomeServer
  30. from synapse.types import JsonDict
  31. from synapse.util import Clock
  32. from tests import unittest
  33. from tests.federation.transport.test_knocking import (
  34. KnockingStrippedStateEventHelperMixin,
  35. )
  36. from tests.server import TimedOutException
  37. class FilterTestCase(unittest.HomeserverTestCase):
  38. user_id = "@apple:test"
  39. servlets = [
  40. synapse.rest.admin.register_servlets_for_client_rest_resource,
  41. room.register_servlets,
  42. login.register_servlets,
  43. sync.register_servlets,
  44. ]
  45. def test_sync_argless(self) -> None:
  46. channel = self.make_request("GET", "/sync")
  47. self.assertEqual(channel.code, 200)
  48. self.assertIn("next_batch", channel.json_body)
  49. class SyncFilterTestCase(unittest.HomeserverTestCase):
  50. servlets = [
  51. synapse.rest.admin.register_servlets_for_client_rest_resource,
  52. room.register_servlets,
  53. login.register_servlets,
  54. sync.register_servlets,
  55. ]
  56. def test_sync_filter_labels(self) -> None:
  57. """Test that we can filter by a label."""
  58. sync_filter = json.dumps(
  59. {
  60. "room": {
  61. "timeline": {
  62. "types": [EventTypes.Message],
  63. "org.matrix.labels": ["#fun"],
  64. }
  65. }
  66. }
  67. )
  68. events = self._test_sync_filter_labels(sync_filter)
  69. self.assertEqual(len(events), 2, [event["content"] for event in events])
  70. self.assertEqual(events[0]["content"]["body"], "with right label", events[0])
  71. self.assertEqual(events[1]["content"]["body"], "with right label", events[1])
  72. def test_sync_filter_not_labels(self) -> None:
  73. """Test that we can filter by the absence of a label."""
  74. sync_filter = json.dumps(
  75. {
  76. "room": {
  77. "timeline": {
  78. "types": [EventTypes.Message],
  79. "org.matrix.not_labels": ["#fun"],
  80. }
  81. }
  82. }
  83. )
  84. events = self._test_sync_filter_labels(sync_filter)
  85. self.assertEqual(len(events), 3, [event["content"] for event in events])
  86. self.assertEqual(events[0]["content"]["body"], "without label", events[0])
  87. self.assertEqual(events[1]["content"]["body"], "with wrong label", events[1])
  88. self.assertEqual(
  89. events[2]["content"]["body"], "with two wrong labels", events[2]
  90. )
  91. def test_sync_filter_labels_not_labels(self) -> None:
  92. """Test that we can filter by both a label and the absence of another label."""
  93. sync_filter = json.dumps(
  94. {
  95. "room": {
  96. "timeline": {
  97. "types": [EventTypes.Message],
  98. "org.matrix.labels": ["#work"],
  99. "org.matrix.not_labels": ["#notfun"],
  100. }
  101. }
  102. }
  103. )
  104. events = self._test_sync_filter_labels(sync_filter)
  105. self.assertEqual(len(events), 1, [event["content"] for event in events])
  106. self.assertEqual(events[0]["content"]["body"], "with wrong label", events[0])
  107. def _test_sync_filter_labels(self, sync_filter: str) -> List[JsonDict]:
  108. user_id = self.register_user("kermit", "test")
  109. tok = self.login("kermit", "test")
  110. room_id = self.helper.create_room_as(user_id, tok=tok)
  111. self.helper.send_event(
  112. room_id=room_id,
  113. type=EventTypes.Message,
  114. content={
  115. "msgtype": "m.text",
  116. "body": "with right label",
  117. EventContentFields.LABELS: ["#fun"],
  118. },
  119. tok=tok,
  120. )
  121. self.helper.send_event(
  122. room_id=room_id,
  123. type=EventTypes.Message,
  124. content={"msgtype": "m.text", "body": "without label"},
  125. tok=tok,
  126. )
  127. self.helper.send_event(
  128. room_id=room_id,
  129. type=EventTypes.Message,
  130. content={
  131. "msgtype": "m.text",
  132. "body": "with wrong label",
  133. EventContentFields.LABELS: ["#work"],
  134. },
  135. tok=tok,
  136. )
  137. self.helper.send_event(
  138. room_id=room_id,
  139. type=EventTypes.Message,
  140. content={
  141. "msgtype": "m.text",
  142. "body": "with two wrong labels",
  143. EventContentFields.LABELS: ["#work", "#notfun"],
  144. },
  145. tok=tok,
  146. )
  147. self.helper.send_event(
  148. room_id=room_id,
  149. type=EventTypes.Message,
  150. content={
  151. "msgtype": "m.text",
  152. "body": "with right label",
  153. EventContentFields.LABELS: ["#fun"],
  154. },
  155. tok=tok,
  156. )
  157. channel = self.make_request(
  158. "GET", "/sync?filter=%s" % sync_filter, access_token=tok
  159. )
  160. self.assertEqual(channel.code, 200, channel.result)
  161. return channel.json_body["rooms"]["join"][room_id]["timeline"]["events"]
  162. class SyncTypingTests(unittest.HomeserverTestCase):
  163. servlets = [
  164. synapse.rest.admin.register_servlets_for_client_rest_resource,
  165. room.register_servlets,
  166. login.register_servlets,
  167. sync.register_servlets,
  168. ]
  169. user_id = True
  170. hijack_auth = False
  171. def test_sync_backwards_typing(self) -> None:
  172. """
  173. If the typing serial goes backwards and the typing handler is then reset
  174. (such as when the master restarts and sets the typing serial to 0), we
  175. do not incorrectly return typing information that had a serial greater
  176. than the now-reset serial.
  177. """
  178. typing_url = "/rooms/%s/typing/%s?access_token=%s"
  179. sync_url = "/sync?timeout=3000000&access_token=%s&since=%s"
  180. # Register the user who gets notified
  181. user_id = self.register_user("user", "pass")
  182. access_token = self.login("user", "pass")
  183. # Register the user who sends the message
  184. other_user_id = self.register_user("otheruser", "pass")
  185. other_access_token = self.login("otheruser", "pass")
  186. # Create a room
  187. room = self.helper.create_room_as(user_id, tok=access_token)
  188. # Invite the other person
  189. self.helper.invite(room=room, src=user_id, tok=access_token, targ=other_user_id)
  190. # The other user joins
  191. self.helper.join(room=room, user=other_user_id, tok=other_access_token)
  192. # The other user sends some messages
  193. self.helper.send(room, body="Hi!", tok=other_access_token)
  194. self.helper.send(room, body="There!", tok=other_access_token)
  195. # Start typing.
  196. channel = self.make_request(
  197. "PUT",
  198. typing_url % (room, other_user_id, other_access_token),
  199. b'{"typing": true, "timeout": 30000}',
  200. )
  201. self.assertEqual(200, channel.code)
  202. channel = self.make_request("GET", "/sync?access_token=%s" % (access_token,))
  203. self.assertEqual(200, channel.code)
  204. next_batch = channel.json_body["next_batch"]
  205. # Stop typing.
  206. channel = self.make_request(
  207. "PUT",
  208. typing_url % (room, other_user_id, other_access_token),
  209. b'{"typing": false}',
  210. )
  211. self.assertEqual(200, channel.code)
  212. # Start typing.
  213. channel = self.make_request(
  214. "PUT",
  215. typing_url % (room, other_user_id, other_access_token),
  216. b'{"typing": true, "timeout": 30000}',
  217. )
  218. self.assertEqual(200, channel.code)
  219. # Should return immediately
  220. channel = self.make_request("GET", sync_url % (access_token, next_batch))
  221. self.assertEqual(200, channel.code)
  222. next_batch = channel.json_body["next_batch"]
  223. # Reset typing serial back to 0, as if the master had.
  224. typing = self.hs.get_typing_handler()
  225. typing._latest_room_serial = 0
  226. # Since it checks the state token, we need some state to update to
  227. # invalidate the stream token.
  228. self.helper.send(room, body="There!", tok=other_access_token)
  229. channel = self.make_request("GET", sync_url % (access_token, next_batch))
  230. self.assertEqual(200, channel.code)
  231. next_batch = channel.json_body["next_batch"]
  232. # This should time out! But it does not, because our stream token is
  233. # ahead, and therefore it's saying the typing (that we've actually
  234. # already seen) is new, since it's got a token above our new, now-reset
  235. # stream token.
  236. channel = self.make_request("GET", sync_url % (access_token, next_batch))
  237. self.assertEqual(200, channel.code)
  238. next_batch = channel.json_body["next_batch"]
  239. # Clear the typing information, so that it doesn't think everything is
  240. # in the future.
  241. typing._reset()
  242. # Now it SHOULD fail as it never completes!
  243. with self.assertRaises(TimedOutException):
  244. self.make_request("GET", sync_url % (access_token, next_batch))
  245. class SyncKnockTestCase(
  246. unittest.HomeserverTestCase, KnockingStrippedStateEventHelperMixin
  247. ):
  248. servlets = [
  249. synapse.rest.admin.register_servlets,
  250. login.register_servlets,
  251. room.register_servlets,
  252. sync.register_servlets,
  253. knock.register_servlets,
  254. ]
  255. def prepare(self, reactor: MemoryReactor, clock: Clock, hs: HomeServer) -> None:
  256. self.store = hs.get_datastores().main
  257. self.url = "/sync?since=%s"
  258. self.next_batch = "s0"
  259. # Register the first user (used to create the room to knock on).
  260. self.user_id = self.register_user("kermit", "monkey")
  261. self.tok = self.login("kermit", "monkey")
  262. # Create the room we'll knock on.
  263. self.room_id = self.helper.create_room_as(
  264. self.user_id,
  265. is_public=False,
  266. room_version="7",
  267. tok=self.tok,
  268. )
  269. # Register the second user (used to knock on the room).
  270. self.knocker = self.register_user("knocker", "monkey")
  271. self.knocker_tok = self.login("knocker", "monkey")
  272. # Perform an initial sync for the knocking user.
  273. channel = self.make_request(
  274. "GET",
  275. self.url % self.next_batch,
  276. access_token=self.tok,
  277. )
  278. self.assertEqual(channel.code, 200, channel.json_body)
  279. # Store the next batch for the next request.
  280. self.next_batch = channel.json_body["next_batch"]
  281. # Set up some room state to test with.
  282. self.expected_room_state = self.send_example_state_events_to_room(
  283. hs, self.room_id, self.user_id
  284. )
  285. def test_knock_room_state(self) -> None:
  286. """Tests that /sync returns state from a room after knocking on it."""
  287. # Knock on a room
  288. channel = self.make_request(
  289. "POST",
  290. f"/_matrix/client/r0/knock/{self.room_id}",
  291. b"{}",
  292. self.knocker_tok,
  293. )
  294. self.assertEqual(200, channel.code, channel.result)
  295. # We expect to see the knock event in the stripped room state later
  296. self.expected_room_state[EventTypes.Member] = {
  297. "content": {"membership": "knock", "displayname": "knocker"},
  298. "state_key": "@knocker:test",
  299. }
  300. # Check that /sync includes stripped state from the room
  301. channel = self.make_request(
  302. "GET",
  303. self.url % self.next_batch,
  304. access_token=self.knocker_tok,
  305. )
  306. self.assertEqual(channel.code, 200, channel.json_body)
  307. # Extract the stripped room state events from /sync
  308. knock_entry = channel.json_body["rooms"]["knock"]
  309. room_state_events = knock_entry[self.room_id]["knock_state"]["events"]
  310. # Validate that the knock membership event came last
  311. self.assertEqual(room_state_events[-1]["type"], EventTypes.Member)
  312. # Validate the stripped room state events
  313. self.check_knock_room_state_against_room_state(
  314. room_state_events, self.expected_room_state
  315. )
  316. class ReadReceiptsTestCase(unittest.HomeserverTestCase):
  317. servlets = [
  318. synapse.rest.admin.register_servlets,
  319. login.register_servlets,
  320. receipts.register_servlets,
  321. room.register_servlets,
  322. sync.register_servlets,
  323. ]
  324. def make_homeserver(self, reactor: MemoryReactor, clock: Clock) -> HomeServer:
  325. config = self.default_config()
  326. return self.setup_test_homeserver(config=config)
  327. def prepare(self, reactor: MemoryReactor, clock: Clock, hs: HomeServer) -> None:
  328. self.url = "/sync?since=%s"
  329. self.next_batch = "s0"
  330. # Register the first user
  331. self.user_id = self.register_user("kermit", "monkey")
  332. self.tok = self.login("kermit", "monkey")
  333. # Create the room
  334. self.room_id = self.helper.create_room_as(self.user_id, tok=self.tok)
  335. # Register the second user
  336. self.user2 = self.register_user("kermit2", "monkey")
  337. self.tok2 = self.login("kermit2", "monkey")
  338. # Join the second user
  339. self.helper.join(room=self.room_id, user=self.user2, tok=self.tok2)
  340. def test_private_read_receipts(self) -> None:
  341. # Send a message as the first user
  342. res = self.helper.send(self.room_id, body="hello", tok=self.tok)
  343. # Send a private read receipt to tell the server the first user's message was read
  344. channel = self.make_request(
  345. "POST",
  346. f"/rooms/{self.room_id}/receipt/{ReceiptTypes.READ_PRIVATE}/{res['event_id']}",
  347. {},
  348. access_token=self.tok2,
  349. )
  350. self.assertEqual(channel.code, 200)
  351. # Test that the first user can't see the other user's private read receipt
  352. self.assertIsNone(self._get_read_receipt())
  353. def test_public_receipt_can_override_private(self) -> None:
  354. """
  355. Sending a public read receipt to the same event which has a private read
  356. receipt should cause that receipt to become public.
  357. """
  358. # Send a message as the first user
  359. res = self.helper.send(self.room_id, body="hello", tok=self.tok)
  360. # Send a private read receipt
  361. channel = self.make_request(
  362. "POST",
  363. f"/rooms/{self.room_id}/receipt/{ReceiptTypes.READ_PRIVATE}/{res['event_id']}",
  364. {},
  365. access_token=self.tok2,
  366. )
  367. self.assertEqual(channel.code, 200)
  368. self.assertIsNone(self._get_read_receipt())
  369. # Send a public read receipt
  370. channel = self.make_request(
  371. "POST",
  372. f"/rooms/{self.room_id}/receipt/{ReceiptTypes.READ}/{res['event_id']}",
  373. {},
  374. access_token=self.tok2,
  375. )
  376. self.assertEqual(channel.code, 200)
  377. # Test that we did override the private read receipt
  378. self.assertNotEqual(self._get_read_receipt(), None)
  379. def test_private_receipt_cannot_override_public(self) -> None:
  380. """
  381. Sending a private read receipt to the same event which has a public read
  382. receipt should cause no change.
  383. """
  384. # Send a message as the first user
  385. res = self.helper.send(self.room_id, body="hello", tok=self.tok)
  386. # Send a public read receipt
  387. channel = self.make_request(
  388. "POST",
  389. f"/rooms/{self.room_id}/receipt/{ReceiptTypes.READ}/{res['event_id']}",
  390. {},
  391. access_token=self.tok2,
  392. )
  393. self.assertEqual(channel.code, 200)
  394. self.assertNotEqual(self._get_read_receipt(), None)
  395. # Send a private read receipt
  396. channel = self.make_request(
  397. "POST",
  398. f"/rooms/{self.room_id}/receipt/{ReceiptTypes.READ_PRIVATE}/{res['event_id']}",
  399. {},
  400. access_token=self.tok2,
  401. )
  402. self.assertEqual(channel.code, 200)
  403. # Test that we didn't override the public read receipt
  404. self.assertIsNone(self._get_read_receipt())
  405. def test_read_receipt_with_empty_body_is_rejected(self) -> None:
  406. # Send a message as the first user
  407. res = self.helper.send(self.room_id, body="hello", tok=self.tok)
  408. # Send a read receipt for this message with an empty body
  409. channel = self.make_request(
  410. "POST",
  411. f"/rooms/{self.room_id}/receipt/m.read/{res['event_id']}",
  412. access_token=self.tok2,
  413. )
  414. self.assertEqual(channel.code, HTTPStatus.BAD_REQUEST)
  415. self.assertEqual(channel.json_body["errcode"], "M_NOT_JSON", channel.json_body)
  416. def _get_read_receipt(self) -> Optional[JsonDict]:
  417. """Syncs and returns the read receipt."""
  418. # Checks if event is a read receipt
  419. def is_read_receipt(event: JsonDict) -> bool:
  420. return event["type"] == EduTypes.RECEIPT
  421. # Sync
  422. channel = self.make_request(
  423. "GET",
  424. self.url % self.next_batch,
  425. access_token=self.tok,
  426. )
  427. self.assertEqual(channel.code, 200)
  428. # Store the next batch for the next request.
  429. self.next_batch = channel.json_body["next_batch"]
  430. if channel.json_body.get("rooms", None) is None:
  431. return None
  432. # Return the read receipt
  433. ephemeral_events = channel.json_body["rooms"]["join"][self.room_id][
  434. "ephemeral"
  435. ]["events"]
  436. receipt_event = filter(is_read_receipt, ephemeral_events)
  437. return next(receipt_event, None)
  438. class UnreadMessagesTestCase(unittest.HomeserverTestCase):
  439. servlets = [
  440. synapse.rest.admin.register_servlets,
  441. login.register_servlets,
  442. read_marker.register_servlets,
  443. room.register_servlets,
  444. sync.register_servlets,
  445. receipts.register_servlets,
  446. ]
  447. def default_config(self) -> JsonDict:
  448. config = super().default_config()
  449. config["experimental_features"] = {
  450. "msc2654_enabled": True,
  451. }
  452. return config
  453. def prepare(self, reactor: MemoryReactor, clock: Clock, hs: HomeServer) -> None:
  454. self.url = "/sync?since=%s"
  455. self.next_batch = "s0"
  456. # Register the first user (used to check the unread counts).
  457. self.user_id = self.register_user("kermit", "monkey")
  458. self.tok = self.login("kermit", "monkey")
  459. # Create the room we'll check unread counts for.
  460. self.room_id = self.helper.create_room_as(self.user_id, tok=self.tok)
  461. # Register the second user (used to send events to the room).
  462. self.user2 = self.register_user("kermit2", "monkey")
  463. self.tok2 = self.login("kermit2", "monkey")
  464. # Change the power levels of the room so that the second user can send state
  465. # events.
  466. self.helper.send_state(
  467. self.room_id,
  468. EventTypes.PowerLevels,
  469. {
  470. "users": {self.user_id: 100, self.user2: 100},
  471. "users_default": 0,
  472. "events": {
  473. "m.room.name": 50,
  474. "m.room.power_levels": 100,
  475. "m.room.history_visibility": 100,
  476. "m.room.canonical_alias": 50,
  477. "m.room.avatar": 50,
  478. "m.room.tombstone": 100,
  479. "m.room.server_acl": 100,
  480. "m.room.encryption": 100,
  481. },
  482. "events_default": 0,
  483. "state_default": 50,
  484. "ban": 50,
  485. "kick": 50,
  486. "redact": 50,
  487. "invite": 0,
  488. },
  489. tok=self.tok,
  490. )
  491. def test_unread_counts(self) -> None:
  492. """Tests that /sync returns the right value for the unread count (MSC2654)."""
  493. # Check that our own messages don't increase the unread count.
  494. self.helper.send(self.room_id, "hello", tok=self.tok)
  495. self._check_unread_count(0)
  496. # Join the new user and check that this doesn't increase the unread count.
  497. self.helper.join(room=self.room_id, user=self.user2, tok=self.tok2)
  498. self._check_unread_count(0)
  499. # Check that the new user sending a message increases our unread count.
  500. res = self.helper.send(self.room_id, "hello", tok=self.tok2)
  501. self._check_unread_count(1)
  502. # Send a read receipt to tell the server we've read the latest event.
  503. channel = self.make_request(
  504. "POST",
  505. f"/rooms/{self.room_id}/read_markers",
  506. {ReceiptTypes.READ: res["event_id"]},
  507. access_token=self.tok,
  508. )
  509. self.assertEqual(channel.code, 200, channel.json_body)
  510. # Check that the unread counter is back to 0.
  511. self._check_unread_count(0)
  512. # Check that private read receipts don't break unread counts
  513. res = self.helper.send(self.room_id, "hello", tok=self.tok2)
  514. self._check_unread_count(1)
  515. # Send a read receipt to tell the server we've read the latest event.
  516. channel = self.make_request(
  517. "POST",
  518. f"/rooms/{self.room_id}/receipt/{ReceiptTypes.READ_PRIVATE}/{res['event_id']}",
  519. {},
  520. access_token=self.tok,
  521. )
  522. self.assertEqual(channel.code, 200, channel.json_body)
  523. # Check that the unread counter is back to 0.
  524. self._check_unread_count(0)
  525. # Check that room name changes increase the unread counter.
  526. self.helper.send_state(
  527. self.room_id,
  528. "m.room.name",
  529. {"name": "my super room"},
  530. tok=self.tok2,
  531. )
  532. self._check_unread_count(1)
  533. # Check that room topic changes increase the unread counter.
  534. self.helper.send_state(
  535. self.room_id,
  536. "m.room.topic",
  537. {"topic": "welcome!!!"},
  538. tok=self.tok2,
  539. )
  540. self._check_unread_count(2)
  541. # Check that encrypted messages increase the unread counter.
  542. self.helper.send_event(self.room_id, EventTypes.Encrypted, {}, tok=self.tok2)
  543. self._check_unread_count(3)
  544. # Check that custom events with a body increase the unread counter.
  545. result = self.helper.send_event(
  546. self.room_id,
  547. "org.matrix.custom_type",
  548. {"body": "hello"},
  549. tok=self.tok2,
  550. )
  551. event_id = result["event_id"]
  552. self._check_unread_count(4)
  553. # Check that edits don't increase the unread counter.
  554. self.helper.send_event(
  555. room_id=self.room_id,
  556. type=EventTypes.Message,
  557. content={
  558. "body": "hello",
  559. "msgtype": "m.text",
  560. "m.relates_to": {
  561. "rel_type": RelationTypes.REPLACE,
  562. "event_id": event_id,
  563. },
  564. },
  565. tok=self.tok2,
  566. )
  567. self._check_unread_count(4)
  568. # Check that notices don't increase the unread counter.
  569. self.helper.send_event(
  570. room_id=self.room_id,
  571. type=EventTypes.Message,
  572. content={"body": "hello", "msgtype": "m.notice"},
  573. tok=self.tok2,
  574. )
  575. self._check_unread_count(4)
  576. # Check that tombstone events changes increase the unread counter.
  577. res1 = self.helper.send_state(
  578. self.room_id,
  579. EventTypes.Tombstone,
  580. {"replacement_room": "!someroom:test"},
  581. tok=self.tok2,
  582. )
  583. self._check_unread_count(5)
  584. res2 = self.helper.send(self.room_id, "hello", tok=self.tok2)
  585. # Make sure both m.read and m.read.private advance
  586. channel = self.make_request(
  587. "POST",
  588. f"/rooms/{self.room_id}/receipt/m.read/{res1['event_id']}",
  589. {},
  590. access_token=self.tok,
  591. )
  592. self.assertEqual(channel.code, 200, channel.json_body)
  593. self._check_unread_count(1)
  594. channel = self.make_request(
  595. "POST",
  596. f"/rooms/{self.room_id}/receipt/{ReceiptTypes.READ_PRIVATE}/{res2['event_id']}",
  597. {},
  598. access_token=self.tok,
  599. )
  600. self.assertEqual(channel.code, 200, channel.json_body)
  601. self._check_unread_count(0)
  602. # We test for all three receipt types that influence notification counts
  603. @parameterized.expand(
  604. [
  605. ReceiptTypes.READ,
  606. ReceiptTypes.READ_PRIVATE,
  607. ]
  608. )
  609. def test_read_receipts_only_go_down(self, receipt_type: str) -> None:
  610. # Join the new user
  611. self.helper.join(room=self.room_id, user=self.user2, tok=self.tok2)
  612. # Send messages
  613. res1 = self.helper.send(self.room_id, "hello", tok=self.tok2)
  614. res2 = self.helper.send(self.room_id, "hello", tok=self.tok2)
  615. # Read last event
  616. channel = self.make_request(
  617. "POST",
  618. f"/rooms/{self.room_id}/receipt/{ReceiptTypes.READ_PRIVATE}/{res2['event_id']}",
  619. {},
  620. access_token=self.tok,
  621. )
  622. self.assertEqual(channel.code, 200, channel.json_body)
  623. self._check_unread_count(0)
  624. # Make sure neither m.read nor m.read.private make the
  625. # read receipt go up to an older event
  626. channel = self.make_request(
  627. "POST",
  628. f"/rooms/{self.room_id}/receipt/{ReceiptTypes.READ_PRIVATE}/{res1['event_id']}",
  629. {},
  630. access_token=self.tok,
  631. )
  632. self.assertEqual(channel.code, 200, channel.json_body)
  633. self._check_unread_count(0)
  634. channel = self.make_request(
  635. "POST",
  636. f"/rooms/{self.room_id}/receipt/m.read/{res1['event_id']}",
  637. {},
  638. access_token=self.tok,
  639. )
  640. self.assertEqual(channel.code, 200, channel.json_body)
  641. self._check_unread_count(0)
  642. def _check_unread_count(self, expected_count: int) -> None:
  643. """Syncs and compares the unread count with the expected value."""
  644. channel = self.make_request(
  645. "GET",
  646. self.url % self.next_batch,
  647. access_token=self.tok,
  648. )
  649. self.assertEqual(channel.code, 200, channel.json_body)
  650. room_entry = (
  651. channel.json_body.get("rooms", {}).get("join", {}).get(self.room_id, {})
  652. )
  653. self.assertEqual(
  654. room_entry.get("org.matrix.msc2654.unread_count", 0),
  655. expected_count,
  656. room_entry,
  657. )
  658. # Store the next batch for the next request.
  659. self.next_batch = channel.json_body["next_batch"]
  660. class SyncCacheTestCase(unittest.HomeserverTestCase):
  661. servlets = [
  662. synapse.rest.admin.register_servlets,
  663. login.register_servlets,
  664. sync.register_servlets,
  665. ]
  666. def test_noop_sync_does_not_tightloop(self) -> None:
  667. """If the sync times out, we shouldn't cache the result
  668. Essentially a regression test for #8518.
  669. """
  670. self.user_id = self.register_user("kermit", "monkey")
  671. self.tok = self.login("kermit", "monkey")
  672. # we should immediately get an initial sync response
  673. channel = self.make_request("GET", "/sync", access_token=self.tok)
  674. self.assertEqual(channel.code, 200, channel.json_body)
  675. # now, make an incremental sync request, with a timeout
  676. next_batch = channel.json_body["next_batch"]
  677. channel = self.make_request(
  678. "GET",
  679. f"/sync?since={next_batch}&timeout=10000",
  680. access_token=self.tok,
  681. await_result=False,
  682. )
  683. # that should block for 10 seconds
  684. with self.assertRaises(TimedOutException):
  685. channel.await_result(timeout_ms=9900)
  686. channel.await_result(timeout_ms=200)
  687. self.assertEqual(channel.code, 200, channel.json_body)
  688. # we expect the next_batch in the result to be the same as before
  689. self.assertEqual(channel.json_body["next_batch"], next_batch)
  690. # another incremental sync should also block.
  691. channel = self.make_request(
  692. "GET",
  693. f"/sync?since={next_batch}&timeout=10000",
  694. access_token=self.tok,
  695. await_result=False,
  696. )
  697. # that should block for 10 seconds
  698. with self.assertRaises(TimedOutException):
  699. channel.await_result(timeout_ms=9900)
  700. channel.await_result(timeout_ms=200)
  701. self.assertEqual(channel.code, 200, channel.json_body)
  702. class DeviceListSyncTestCase(unittest.HomeserverTestCase):
  703. servlets = [
  704. synapse.rest.admin.register_servlets,
  705. login.register_servlets,
  706. sync.register_servlets,
  707. devices.register_servlets,
  708. ]
  709. def test_user_with_no_rooms_receives_self_device_list_updates(self) -> None:
  710. """Tests that a user with no rooms still receives their own device list updates"""
  711. device_id = "TESTDEVICE"
  712. # Register a user and login, creating a device
  713. self.user_id = self.register_user("kermit", "monkey")
  714. self.tok = self.login("kermit", "monkey", device_id=device_id)
  715. # Request an initial sync
  716. channel = self.make_request("GET", "/sync", access_token=self.tok)
  717. self.assertEqual(channel.code, 200, channel.json_body)
  718. next_batch = channel.json_body["next_batch"]
  719. # Now, make an incremental sync request.
  720. # It won't return until something has happened
  721. incremental_sync_channel = self.make_request(
  722. "GET",
  723. f"/sync?since={next_batch}&timeout=30000",
  724. access_token=self.tok,
  725. await_result=False,
  726. )
  727. # Change our device's display name
  728. channel = self.make_request(
  729. "PUT",
  730. f"devices/{device_id}",
  731. {
  732. "display_name": "freeze ray",
  733. },
  734. access_token=self.tok,
  735. )
  736. self.assertEqual(channel.code, 200, channel.json_body)
  737. # The sync should now have returned
  738. incremental_sync_channel.await_result(timeout_ms=20000)
  739. self.assertEqual(incremental_sync_channel.code, 200, channel.json_body)
  740. # We should have received notification that the (user's) device has changed
  741. device_list_changes = incremental_sync_channel.json_body.get(
  742. "device_lists", {}
  743. ).get("changed", [])
  744. self.assertIn(
  745. self.user_id, device_list_changes, incremental_sync_channel.json_body
  746. )
  747. class ExcludeRoomTestCase(unittest.HomeserverTestCase):
  748. servlets = [
  749. synapse.rest.admin.register_servlets,
  750. login.register_servlets,
  751. sync.register_servlets,
  752. room.register_servlets,
  753. ]
  754. def prepare(
  755. self, reactor: MemoryReactor, clock: Clock, homeserver: HomeServer
  756. ) -> None:
  757. self.user_id = self.register_user("user", "password")
  758. self.tok = self.login("user", "password")
  759. self.excluded_room_id = self.helper.create_room_as(self.user_id, tok=self.tok)
  760. self.included_room_id = self.helper.create_room_as(self.user_id, tok=self.tok)
  761. # We need to manually append the room ID, because we can't know the ID before
  762. # creating the room, and we can't set the config after starting the homeserver.
  763. self.hs.get_sync_handler().rooms_to_exclude.append(self.excluded_room_id)
  764. def test_join_leave(self) -> None:
  765. """Tests that rooms are correctly excluded from the 'join' and 'leave' sections of
  766. sync responses.
  767. """
  768. channel = self.make_request("GET", "/sync", access_token=self.tok)
  769. self.assertEqual(channel.code, 200, channel.result)
  770. self.assertNotIn(self.excluded_room_id, channel.json_body["rooms"]["join"])
  771. self.assertIn(self.included_room_id, channel.json_body["rooms"]["join"])
  772. self.helper.leave(self.excluded_room_id, self.user_id, tok=self.tok)
  773. self.helper.leave(self.included_room_id, self.user_id, tok=self.tok)
  774. channel = self.make_request(
  775. "GET",
  776. "/sync?since=" + channel.json_body["next_batch"],
  777. access_token=self.tok,
  778. )
  779. self.assertEqual(channel.code, 200, channel.result)
  780. self.assertNotIn(self.excluded_room_id, channel.json_body["rooms"]["leave"])
  781. self.assertIn(self.included_room_id, channel.json_body["rooms"]["leave"])
  782. def test_invite(self) -> None:
  783. """Tests that rooms are correctly excluded from the 'invite' section of sync
  784. responses.
  785. """
  786. invitee = self.register_user("invitee", "password")
  787. invitee_tok = self.login("invitee", "password")
  788. self.helper.invite(self.excluded_room_id, self.user_id, invitee, tok=self.tok)
  789. self.helper.invite(self.included_room_id, self.user_id, invitee, tok=self.tok)
  790. channel = self.make_request("GET", "/sync", access_token=invitee_tok)
  791. self.assertEqual(channel.code, 200, channel.result)
  792. self.assertNotIn(self.excluded_room_id, channel.json_body["rooms"]["invite"])
  793. self.assertIn(self.included_room_id, channel.json_body["rooms"]["invite"])
  794. def test_incremental_sync(self) -> None:
  795. """Tests that activity in the room is properly filtered out of incremental
  796. syncs.
  797. """
  798. channel = self.make_request("GET", "/sync", access_token=self.tok)
  799. self.assertEqual(channel.code, 200, channel.result)
  800. next_batch = channel.json_body["next_batch"]
  801. self.helper.send(self.excluded_room_id, tok=self.tok)
  802. self.helper.send(self.included_room_id, tok=self.tok)
  803. channel = self.make_request(
  804. "GET",
  805. f"/sync?since={next_batch}",
  806. access_token=self.tok,
  807. )
  808. self.assertEqual(channel.code, 200, channel.result)
  809. self.assertNotIn(self.excluded_room_id, channel.json_body["rooms"]["join"])
  810. self.assertIn(self.included_room_id, channel.json_body["rooms"]["join"])