test_sync.py 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950
  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. EventContentFields,
  23. EventTypes,
  24. ReceiptTypes,
  25. RelationTypes,
  26. )
  27. from synapse.rest.client import devices, knock, login, read_marker, receipts, room, sync
  28. from synapse.server import HomeServer
  29. from synapse.types import JsonDict
  30. from synapse.util import Clock
  31. from tests import unittest
  32. from tests.federation.transport.test_knocking import (
  33. KnockingStrippedStateEventHelperMixin,
  34. )
  35. from tests.server import TimedOutException
  36. from tests.unittest import override_config
  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 prepare(self, reactor: MemoryReactor, clock: Clock, hs: HomeServer) -> None:
  325. self.url = "/sync?since=%s"
  326. self.next_batch = "s0"
  327. # Register the first user
  328. self.user_id = self.register_user("kermit", "monkey")
  329. self.tok = self.login("kermit", "monkey")
  330. # Create the room
  331. self.room_id = self.helper.create_room_as(self.user_id, tok=self.tok)
  332. # Register the second user
  333. self.user2 = self.register_user("kermit2", "monkey")
  334. self.tok2 = self.login("kermit2", "monkey")
  335. # Join the second user
  336. self.helper.join(room=self.room_id, user=self.user2, tok=self.tok2)
  337. @override_config({"experimental_features": {"msc2285_enabled": True}})
  338. def test_private_read_receipts(self) -> None:
  339. # Send a message as the first user
  340. res = self.helper.send(self.room_id, body="hello", tok=self.tok)
  341. # Send a private read receipt to tell the server the first user's message was read
  342. channel = self.make_request(
  343. "POST",
  344. f"/rooms/{self.room_id}/receipt/org.matrix.msc2285.read.private/{res['event_id']}",
  345. {},
  346. access_token=self.tok2,
  347. )
  348. self.assertEqual(channel.code, 200)
  349. # Test that the first user can't see the other user's private read receipt
  350. self.assertIsNone(self._get_read_receipt())
  351. @override_config({"experimental_features": {"msc2285_enabled": True}})
  352. def test_public_receipt_can_override_private(self) -> None:
  353. """
  354. Sending a public read receipt to the same event which has a private read
  355. receipt should cause that receipt to become public.
  356. """
  357. # Send a message as the first user
  358. res = self.helper.send(self.room_id, body="hello", tok=self.tok)
  359. # Send a private read receipt
  360. channel = self.make_request(
  361. "POST",
  362. f"/rooms/{self.room_id}/receipt/{ReceiptTypes.READ_PRIVATE}/{res['event_id']}",
  363. {},
  364. access_token=self.tok2,
  365. )
  366. self.assertEqual(channel.code, 200)
  367. self.assertIsNone(self._get_read_receipt())
  368. # Send a public read receipt
  369. channel = self.make_request(
  370. "POST",
  371. f"/rooms/{self.room_id}/receipt/{ReceiptTypes.READ}/{res['event_id']}",
  372. {},
  373. access_token=self.tok2,
  374. )
  375. self.assertEqual(channel.code, 200)
  376. # Test that we did override the private read receipt
  377. self.assertNotEqual(self._get_read_receipt(), None)
  378. @override_config({"experimental_features": {"msc2285_enabled": True}})
  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"] == "m.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. "msc2285_enabled": True,
  452. }
  453. return config
  454. def prepare(self, reactor: MemoryReactor, clock: Clock, hs: HomeServer) -> None:
  455. self.url = "/sync?since=%s"
  456. self.next_batch = "s0"
  457. # Register the first user (used to check the unread counts).
  458. self.user_id = self.register_user("kermit", "monkey")
  459. self.tok = self.login("kermit", "monkey")
  460. # Create the room we'll check unread counts for.
  461. self.room_id = self.helper.create_room_as(self.user_id, tok=self.tok)
  462. # Register the second user (used to send events to the room).
  463. self.user2 = self.register_user("kermit2", "monkey")
  464. self.tok2 = self.login("kermit2", "monkey")
  465. # Change the power levels of the room so that the second user can send state
  466. # events.
  467. self.helper.send_state(
  468. self.room_id,
  469. EventTypes.PowerLevels,
  470. {
  471. "users": {self.user_id: 100, self.user2: 100},
  472. "users_default": 0,
  473. "events": {
  474. "m.room.name": 50,
  475. "m.room.power_levels": 100,
  476. "m.room.history_visibility": 100,
  477. "m.room.canonical_alias": 50,
  478. "m.room.avatar": 50,
  479. "m.room.tombstone": 100,
  480. "m.room.server_acl": 100,
  481. "m.room.encryption": 100,
  482. },
  483. "events_default": 0,
  484. "state_default": 50,
  485. "ban": 50,
  486. "kick": 50,
  487. "redact": 50,
  488. "invite": 0,
  489. },
  490. tok=self.tok,
  491. )
  492. def test_unread_counts(self) -> None:
  493. """Tests that /sync returns the right value for the unread count (MSC2654)."""
  494. # Check that our own messages don't increase the unread count.
  495. self.helper.send(self.room_id, "hello", tok=self.tok)
  496. self._check_unread_count(0)
  497. # Join the new user and check that this doesn't increase the unread count.
  498. self.helper.join(room=self.room_id, user=self.user2, tok=self.tok2)
  499. self._check_unread_count(0)
  500. # Check that the new user sending a message increases our unread count.
  501. res = self.helper.send(self.room_id, "hello", tok=self.tok2)
  502. self._check_unread_count(1)
  503. # Send a read receipt to tell the server we've read the latest event.
  504. body = json.dumps({ReceiptTypes.READ: res["event_id"]}).encode("utf8")
  505. channel = self.make_request(
  506. "POST",
  507. f"/rooms/{self.room_id}/read_markers",
  508. body,
  509. access_token=self.tok,
  510. )
  511. self.assertEqual(channel.code, 200, channel.json_body)
  512. # Check that the unread counter is back to 0.
  513. self._check_unread_count(0)
  514. # Check that private read receipts don't break unread counts
  515. res = self.helper.send(self.room_id, "hello", tok=self.tok2)
  516. self._check_unread_count(1)
  517. # Send a read receipt to tell the server we've read the latest event.
  518. channel = self.make_request(
  519. "POST",
  520. f"/rooms/{self.room_id}/receipt/org.matrix.msc2285.read.private/{res['event_id']}",
  521. {},
  522. access_token=self.tok,
  523. )
  524. self.assertEqual(channel.code, 200, channel.json_body)
  525. # Check that the unread counter is back to 0.
  526. self._check_unread_count(0)
  527. # Check that room name changes increase the unread counter.
  528. self.helper.send_state(
  529. self.room_id,
  530. "m.room.name",
  531. {"name": "my super room"},
  532. tok=self.tok2,
  533. )
  534. self._check_unread_count(1)
  535. # Check that room topic changes increase the unread counter.
  536. self.helper.send_state(
  537. self.room_id,
  538. "m.room.topic",
  539. {"topic": "welcome!!!"},
  540. tok=self.tok2,
  541. )
  542. self._check_unread_count(2)
  543. # Check that encrypted messages increase the unread counter.
  544. self.helper.send_event(self.room_id, EventTypes.Encrypted, {}, tok=self.tok2)
  545. self._check_unread_count(3)
  546. # Check that custom events with a body increase the unread counter.
  547. result = self.helper.send_event(
  548. self.room_id,
  549. "org.matrix.custom_type",
  550. {"body": "hello"},
  551. tok=self.tok2,
  552. )
  553. event_id = result["event_id"]
  554. self._check_unread_count(4)
  555. # Check that edits don't increase the unread counter.
  556. self.helper.send_event(
  557. room_id=self.room_id,
  558. type=EventTypes.Message,
  559. content={
  560. "body": "hello",
  561. "msgtype": "m.text",
  562. "m.relates_to": {
  563. "rel_type": RelationTypes.REPLACE,
  564. "event_id": event_id,
  565. },
  566. },
  567. tok=self.tok2,
  568. )
  569. self._check_unread_count(4)
  570. # Check that notices don't increase the unread counter.
  571. self.helper.send_event(
  572. room_id=self.room_id,
  573. type=EventTypes.Message,
  574. content={"body": "hello", "msgtype": "m.notice"},
  575. tok=self.tok2,
  576. )
  577. self._check_unread_count(4)
  578. # Check that tombstone events changes increase the unread counter.
  579. res1 = self.helper.send_state(
  580. self.room_id,
  581. EventTypes.Tombstone,
  582. {"replacement_room": "!someroom:test"},
  583. tok=self.tok2,
  584. )
  585. self._check_unread_count(5)
  586. res2 = self.helper.send(self.room_id, "hello", tok=self.tok2)
  587. # Make sure both m.read and org.matrix.msc2285.read.private advance
  588. channel = self.make_request(
  589. "POST",
  590. f"/rooms/{self.room_id}/receipt/m.read/{res1['event_id']}",
  591. {},
  592. access_token=self.tok,
  593. )
  594. self.assertEqual(channel.code, 200, channel.json_body)
  595. self._check_unread_count(1)
  596. channel = self.make_request(
  597. "POST",
  598. f"/rooms/{self.room_id}/receipt/org.matrix.msc2285.read.private/{res2['event_id']}",
  599. {},
  600. access_token=self.tok,
  601. )
  602. self.assertEqual(channel.code, 200, channel.json_body)
  603. self._check_unread_count(0)
  604. # We test for both receipt types that influence notification counts
  605. @parameterized.expand([ReceiptTypes.READ, ReceiptTypes.READ_PRIVATE])
  606. def test_read_receipts_only_go_down(self, receipt_type: ReceiptTypes) -> None:
  607. # Join the new user
  608. self.helper.join(room=self.room_id, user=self.user2, tok=self.tok2)
  609. # Send messages
  610. res1 = self.helper.send(self.room_id, "hello", tok=self.tok2)
  611. res2 = self.helper.send(self.room_id, "hello", tok=self.tok2)
  612. # Read last event
  613. channel = self.make_request(
  614. "POST",
  615. f"/rooms/{self.room_id}/receipt/{receipt_type}/{res2['event_id']}",
  616. {},
  617. access_token=self.tok,
  618. )
  619. self.assertEqual(channel.code, 200, channel.json_body)
  620. self._check_unread_count(0)
  621. # Make sure neither m.read nor org.matrix.msc2285.read.private make the
  622. # read receipt go up to an older event
  623. channel = self.make_request(
  624. "POST",
  625. f"/rooms/{self.room_id}/receipt/org.matrix.msc2285.read.private/{res1['event_id']}",
  626. {},
  627. access_token=self.tok,
  628. )
  629. self.assertEqual(channel.code, 200, channel.json_body)
  630. self._check_unread_count(0)
  631. channel = self.make_request(
  632. "POST",
  633. f"/rooms/{self.room_id}/receipt/m.read/{res1['event_id']}",
  634. {},
  635. access_token=self.tok,
  636. )
  637. self.assertEqual(channel.code, 200, channel.json_body)
  638. self._check_unread_count(0)
  639. def _check_unread_count(self, expected_count: int) -> None:
  640. """Syncs and compares the unread count with the expected value."""
  641. channel = self.make_request(
  642. "GET",
  643. self.url % self.next_batch,
  644. access_token=self.tok,
  645. )
  646. self.assertEqual(channel.code, 200, channel.json_body)
  647. room_entry = (
  648. channel.json_body.get("rooms", {}).get("join", {}).get(self.room_id, {})
  649. )
  650. self.assertEqual(
  651. room_entry.get("org.matrix.msc2654.unread_count", 0),
  652. expected_count,
  653. room_entry,
  654. )
  655. # Store the next batch for the next request.
  656. self.next_batch = channel.json_body["next_batch"]
  657. class SyncCacheTestCase(unittest.HomeserverTestCase):
  658. servlets = [
  659. synapse.rest.admin.register_servlets,
  660. login.register_servlets,
  661. sync.register_servlets,
  662. ]
  663. def test_noop_sync_does_not_tightloop(self) -> None:
  664. """If the sync times out, we shouldn't cache the result
  665. Essentially a regression test for #8518.
  666. """
  667. self.user_id = self.register_user("kermit", "monkey")
  668. self.tok = self.login("kermit", "monkey")
  669. # we should immediately get an initial sync response
  670. channel = self.make_request("GET", "/sync", access_token=self.tok)
  671. self.assertEqual(channel.code, 200, channel.json_body)
  672. # now, make an incremental sync request, with a timeout
  673. next_batch = channel.json_body["next_batch"]
  674. channel = self.make_request(
  675. "GET",
  676. f"/sync?since={next_batch}&timeout=10000",
  677. access_token=self.tok,
  678. await_result=False,
  679. )
  680. # that should block for 10 seconds
  681. with self.assertRaises(TimedOutException):
  682. channel.await_result(timeout_ms=9900)
  683. channel.await_result(timeout_ms=200)
  684. self.assertEqual(channel.code, 200, channel.json_body)
  685. # we expect the next_batch in the result to be the same as before
  686. self.assertEqual(channel.json_body["next_batch"], next_batch)
  687. # another incremental sync should also block.
  688. channel = self.make_request(
  689. "GET",
  690. f"/sync?since={next_batch}&timeout=10000",
  691. access_token=self.tok,
  692. await_result=False,
  693. )
  694. # that should block for 10 seconds
  695. with self.assertRaises(TimedOutException):
  696. channel.await_result(timeout_ms=9900)
  697. channel.await_result(timeout_ms=200)
  698. self.assertEqual(channel.code, 200, channel.json_body)
  699. class DeviceListSyncTestCase(unittest.HomeserverTestCase):
  700. servlets = [
  701. synapse.rest.admin.register_servlets,
  702. login.register_servlets,
  703. sync.register_servlets,
  704. devices.register_servlets,
  705. ]
  706. def test_user_with_no_rooms_receives_self_device_list_updates(self) -> None:
  707. """Tests that a user with no rooms still receives their own device list updates"""
  708. device_id = "TESTDEVICE"
  709. # Register a user and login, creating a device
  710. self.user_id = self.register_user("kermit", "monkey")
  711. self.tok = self.login("kermit", "monkey", device_id=device_id)
  712. # Request an initial sync
  713. channel = self.make_request("GET", "/sync", access_token=self.tok)
  714. self.assertEqual(channel.code, 200, channel.json_body)
  715. next_batch = channel.json_body["next_batch"]
  716. # Now, make an incremental sync request.
  717. # It won't return until something has happened
  718. incremental_sync_channel = self.make_request(
  719. "GET",
  720. f"/sync?since={next_batch}&timeout=30000",
  721. access_token=self.tok,
  722. await_result=False,
  723. )
  724. # Change our device's display name
  725. channel = self.make_request(
  726. "PUT",
  727. f"devices/{device_id}",
  728. {
  729. "display_name": "freeze ray",
  730. },
  731. access_token=self.tok,
  732. )
  733. self.assertEqual(channel.code, 200, channel.json_body)
  734. # The sync should now have returned
  735. incremental_sync_channel.await_result(timeout_ms=20000)
  736. self.assertEqual(incremental_sync_channel.code, 200, channel.json_body)
  737. # We should have received notification that the (user's) device has changed
  738. device_list_changes = incremental_sync_channel.json_body.get(
  739. "device_lists", {}
  740. ).get("changed", [])
  741. self.assertIn(
  742. self.user_id, device_list_changes, incremental_sync_channel.json_body
  743. )
  744. class ExcludeRoomTestCase(unittest.HomeserverTestCase):
  745. servlets = [
  746. synapse.rest.admin.register_servlets,
  747. login.register_servlets,
  748. sync.register_servlets,
  749. room.register_servlets,
  750. ]
  751. def prepare(
  752. self, reactor: MemoryReactor, clock: Clock, homeserver: HomeServer
  753. ) -> None:
  754. self.user_id = self.register_user("user", "password")
  755. self.tok = self.login("user", "password")
  756. self.excluded_room_id = self.helper.create_room_as(self.user_id, tok=self.tok)
  757. self.included_room_id = self.helper.create_room_as(self.user_id, tok=self.tok)
  758. # We need to manually append the room ID, because we can't know the ID before
  759. # creating the room, and we can't set the config after starting the homeserver.
  760. self.hs.get_sync_handler().rooms_to_exclude.append(self.excluded_room_id)
  761. def test_join_leave(self) -> None:
  762. """Tests that rooms are correctly excluded from the 'join' and 'leave' sections of
  763. sync responses.
  764. """
  765. channel = self.make_request("GET", "/sync", access_token=self.tok)
  766. self.assertEqual(channel.code, 200, channel.result)
  767. self.assertNotIn(self.excluded_room_id, channel.json_body["rooms"]["join"])
  768. self.assertIn(self.included_room_id, channel.json_body["rooms"]["join"])
  769. self.helper.leave(self.excluded_room_id, self.user_id, tok=self.tok)
  770. self.helper.leave(self.included_room_id, self.user_id, tok=self.tok)
  771. channel = self.make_request(
  772. "GET",
  773. "/sync?since=" + channel.json_body["next_batch"],
  774. access_token=self.tok,
  775. )
  776. self.assertEqual(channel.code, 200, channel.result)
  777. self.assertNotIn(self.excluded_room_id, channel.json_body["rooms"]["leave"])
  778. self.assertIn(self.included_room_id, channel.json_body["rooms"]["leave"])
  779. def test_invite(self) -> None:
  780. """Tests that rooms are correctly excluded from the 'invite' section of sync
  781. responses.
  782. """
  783. invitee = self.register_user("invitee", "password")
  784. invitee_tok = self.login("invitee", "password")
  785. self.helper.invite(self.excluded_room_id, self.user_id, invitee, tok=self.tok)
  786. self.helper.invite(self.included_room_id, self.user_id, invitee, tok=self.tok)
  787. channel = self.make_request("GET", "/sync", access_token=invitee_tok)
  788. self.assertEqual(channel.code, 200, channel.result)
  789. self.assertNotIn(self.excluded_room_id, channel.json_body["rooms"]["invite"])
  790. self.assertIn(self.included_room_id, channel.json_body["rooms"]["invite"])