test_sync.py 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712
  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 parameterized import parameterized
  17. import synapse.rest.admin
  18. from synapse.api.constants import (
  19. EventContentFields,
  20. EventTypes,
  21. ReadReceiptEventFields,
  22. RelationTypes,
  23. )
  24. from synapse.rest.client import knock, login, read_marker, receipts, room, sync
  25. from tests import unittest
  26. from tests.federation.transport.test_knocking import (
  27. KnockingStrippedStateEventHelperMixin,
  28. )
  29. from tests.server import TimedOutException
  30. from tests.unittest import override_config
  31. class FilterTestCase(unittest.HomeserverTestCase):
  32. user_id = "@apple:test"
  33. servlets = [
  34. synapse.rest.admin.register_servlets_for_client_rest_resource,
  35. room.register_servlets,
  36. login.register_servlets,
  37. sync.register_servlets,
  38. ]
  39. def test_sync_argless(self):
  40. channel = self.make_request("GET", "/sync")
  41. self.assertEqual(channel.code, 200)
  42. self.assertIn("next_batch", channel.json_body)
  43. class SyncFilterTestCase(unittest.HomeserverTestCase):
  44. servlets = [
  45. synapse.rest.admin.register_servlets_for_client_rest_resource,
  46. room.register_servlets,
  47. login.register_servlets,
  48. sync.register_servlets,
  49. ]
  50. def test_sync_filter_labels(self):
  51. """Test that we can filter by a label."""
  52. sync_filter = json.dumps(
  53. {
  54. "room": {
  55. "timeline": {
  56. "types": [EventTypes.Message],
  57. "org.matrix.labels": ["#fun"],
  58. }
  59. }
  60. }
  61. )
  62. events = self._test_sync_filter_labels(sync_filter)
  63. self.assertEqual(len(events), 2, [event["content"] for event in events])
  64. self.assertEqual(events[0]["content"]["body"], "with right label", events[0])
  65. self.assertEqual(events[1]["content"]["body"], "with right label", events[1])
  66. def test_sync_filter_not_labels(self):
  67. """Test that we can filter by the absence of a label."""
  68. sync_filter = json.dumps(
  69. {
  70. "room": {
  71. "timeline": {
  72. "types": [EventTypes.Message],
  73. "org.matrix.not_labels": ["#fun"],
  74. }
  75. }
  76. }
  77. )
  78. events = self._test_sync_filter_labels(sync_filter)
  79. self.assertEqual(len(events), 3, [event["content"] for event in events])
  80. self.assertEqual(events[0]["content"]["body"], "without label", events[0])
  81. self.assertEqual(events[1]["content"]["body"], "with wrong label", events[1])
  82. self.assertEqual(
  83. events[2]["content"]["body"], "with two wrong labels", events[2]
  84. )
  85. def test_sync_filter_labels_not_labels(self):
  86. """Test that we can filter by both a label and the absence of another label."""
  87. sync_filter = json.dumps(
  88. {
  89. "room": {
  90. "timeline": {
  91. "types": [EventTypes.Message],
  92. "org.matrix.labels": ["#work"],
  93. "org.matrix.not_labels": ["#notfun"],
  94. }
  95. }
  96. }
  97. )
  98. events = self._test_sync_filter_labels(sync_filter)
  99. self.assertEqual(len(events), 1, [event["content"] for event in events])
  100. self.assertEqual(events[0]["content"]["body"], "with wrong label", events[0])
  101. def _test_sync_filter_labels(self, sync_filter):
  102. user_id = self.register_user("kermit", "test")
  103. tok = self.login("kermit", "test")
  104. room_id = self.helper.create_room_as(user_id, tok=tok)
  105. self.helper.send_event(
  106. room_id=room_id,
  107. type=EventTypes.Message,
  108. content={
  109. "msgtype": "m.text",
  110. "body": "with right label",
  111. EventContentFields.LABELS: ["#fun"],
  112. },
  113. tok=tok,
  114. )
  115. self.helper.send_event(
  116. room_id=room_id,
  117. type=EventTypes.Message,
  118. content={"msgtype": "m.text", "body": "without label"},
  119. tok=tok,
  120. )
  121. self.helper.send_event(
  122. room_id=room_id,
  123. type=EventTypes.Message,
  124. content={
  125. "msgtype": "m.text",
  126. "body": "with wrong label",
  127. EventContentFields.LABELS: ["#work"],
  128. },
  129. tok=tok,
  130. )
  131. self.helper.send_event(
  132. room_id=room_id,
  133. type=EventTypes.Message,
  134. content={
  135. "msgtype": "m.text",
  136. "body": "with two wrong labels",
  137. EventContentFields.LABELS: ["#work", "#notfun"],
  138. },
  139. tok=tok,
  140. )
  141. self.helper.send_event(
  142. room_id=room_id,
  143. type=EventTypes.Message,
  144. content={
  145. "msgtype": "m.text",
  146. "body": "with right label",
  147. EventContentFields.LABELS: ["#fun"],
  148. },
  149. tok=tok,
  150. )
  151. channel = self.make_request(
  152. "GET", "/sync?filter=%s" % sync_filter, access_token=tok
  153. )
  154. self.assertEqual(channel.code, 200, channel.result)
  155. return channel.json_body["rooms"]["join"][room_id]["timeline"]["events"]
  156. class SyncTypingTests(unittest.HomeserverTestCase):
  157. servlets = [
  158. synapse.rest.admin.register_servlets_for_client_rest_resource,
  159. room.register_servlets,
  160. login.register_servlets,
  161. sync.register_servlets,
  162. ]
  163. user_id = True
  164. hijack_auth = False
  165. def test_sync_backwards_typing(self):
  166. """
  167. If the typing serial goes backwards and the typing handler is then reset
  168. (such as when the master restarts and sets the typing serial to 0), we
  169. do not incorrectly return typing information that had a serial greater
  170. than the now-reset serial.
  171. """
  172. typing_url = "/rooms/%s/typing/%s?access_token=%s"
  173. sync_url = "/sync?timeout=3000000&access_token=%s&since=%s"
  174. # Register the user who gets notified
  175. user_id = self.register_user("user", "pass")
  176. access_token = self.login("user", "pass")
  177. # Register the user who sends the message
  178. other_user_id = self.register_user("otheruser", "pass")
  179. other_access_token = self.login("otheruser", "pass")
  180. # Create a room
  181. room = self.helper.create_room_as(user_id, tok=access_token)
  182. # Invite the other person
  183. self.helper.invite(room=room, src=user_id, tok=access_token, targ=other_user_id)
  184. # The other user joins
  185. self.helper.join(room=room, user=other_user_id, tok=other_access_token)
  186. # The other user sends some messages
  187. self.helper.send(room, body="Hi!", tok=other_access_token)
  188. self.helper.send(room, body="There!", tok=other_access_token)
  189. # Start typing.
  190. channel = self.make_request(
  191. "PUT",
  192. typing_url % (room, other_user_id, other_access_token),
  193. b'{"typing": true, "timeout": 30000}',
  194. )
  195. self.assertEquals(200, channel.code)
  196. channel = self.make_request("GET", "/sync?access_token=%s" % (access_token,))
  197. self.assertEquals(200, channel.code)
  198. next_batch = channel.json_body["next_batch"]
  199. # Stop typing.
  200. channel = self.make_request(
  201. "PUT",
  202. typing_url % (room, other_user_id, other_access_token),
  203. b'{"typing": false}',
  204. )
  205. self.assertEquals(200, channel.code)
  206. # Start typing.
  207. channel = self.make_request(
  208. "PUT",
  209. typing_url % (room, other_user_id, other_access_token),
  210. b'{"typing": true, "timeout": 30000}',
  211. )
  212. self.assertEquals(200, channel.code)
  213. # Should return immediately
  214. channel = self.make_request("GET", sync_url % (access_token, next_batch))
  215. self.assertEquals(200, channel.code)
  216. next_batch = channel.json_body["next_batch"]
  217. # Reset typing serial back to 0, as if the master had.
  218. typing = self.hs.get_typing_handler()
  219. typing._latest_room_serial = 0
  220. # Since it checks the state token, we need some state to update to
  221. # invalidate the stream token.
  222. self.helper.send(room, body="There!", tok=other_access_token)
  223. channel = self.make_request("GET", sync_url % (access_token, next_batch))
  224. self.assertEquals(200, channel.code)
  225. next_batch = channel.json_body["next_batch"]
  226. # This should time out! But it does not, because our stream token is
  227. # ahead, and therefore it's saying the typing (that we've actually
  228. # already seen) is new, since it's got a token above our new, now-reset
  229. # stream token.
  230. channel = self.make_request("GET", sync_url % (access_token, next_batch))
  231. self.assertEquals(200, channel.code)
  232. next_batch = channel.json_body["next_batch"]
  233. # Clear the typing information, so that it doesn't think everything is
  234. # in the future.
  235. typing._reset()
  236. # Now it SHOULD fail as it never completes!
  237. with self.assertRaises(TimedOutException):
  238. self.make_request("GET", sync_url % (access_token, next_batch))
  239. class SyncKnockTestCase(
  240. unittest.HomeserverTestCase, KnockingStrippedStateEventHelperMixin
  241. ):
  242. servlets = [
  243. synapse.rest.admin.register_servlets,
  244. login.register_servlets,
  245. room.register_servlets,
  246. sync.register_servlets,
  247. knock.register_servlets,
  248. ]
  249. def prepare(self, reactor, clock, hs):
  250. self.store = hs.get_datastore()
  251. self.url = "/sync?since=%s"
  252. self.next_batch = "s0"
  253. # Register the first user (used to create the room to knock on).
  254. self.user_id = self.register_user("kermit", "monkey")
  255. self.tok = self.login("kermit", "monkey")
  256. # Create the room we'll knock on.
  257. self.room_id = self.helper.create_room_as(
  258. self.user_id,
  259. is_public=False,
  260. room_version="7",
  261. tok=self.tok,
  262. )
  263. # Register the second user (used to knock on the room).
  264. self.knocker = self.register_user("knocker", "monkey")
  265. self.knocker_tok = self.login("knocker", "monkey")
  266. # Perform an initial sync for the knocking user.
  267. channel = self.make_request(
  268. "GET",
  269. self.url % self.next_batch,
  270. access_token=self.tok,
  271. )
  272. self.assertEqual(channel.code, 200, channel.json_body)
  273. # Store the next batch for the next request.
  274. self.next_batch = channel.json_body["next_batch"]
  275. # Set up some room state to test with.
  276. self.expected_room_state = self.send_example_state_events_to_room(
  277. hs, self.room_id, self.user_id
  278. )
  279. @override_config({"experimental_features": {"msc2403_enabled": True}})
  280. def test_knock_room_state(self):
  281. """Tests that /sync returns state from a room after knocking on it."""
  282. # Knock on a room
  283. channel = self.make_request(
  284. "POST",
  285. "/_matrix/client/r0/knock/%s" % (self.room_id,),
  286. b"{}",
  287. self.knocker_tok,
  288. )
  289. self.assertEquals(200, channel.code, channel.result)
  290. # We expect to see the knock event in the stripped room state later
  291. self.expected_room_state[EventTypes.Member] = {
  292. "content": {"membership": "knock", "displayname": "knocker"},
  293. "state_key": "@knocker:test",
  294. }
  295. # Check that /sync includes stripped state from the room
  296. channel = self.make_request(
  297. "GET",
  298. self.url % self.next_batch,
  299. access_token=self.knocker_tok,
  300. )
  301. self.assertEqual(channel.code, 200, channel.json_body)
  302. # Extract the stripped room state events from /sync
  303. knock_entry = channel.json_body["rooms"]["knock"]
  304. room_state_events = knock_entry[self.room_id]["knock_state"]["events"]
  305. # Validate that the knock membership event came last
  306. self.assertEqual(room_state_events[-1]["type"], EventTypes.Member)
  307. # Validate the stripped room state events
  308. self.check_knock_room_state_against_room_state(
  309. room_state_events, self.expected_room_state
  310. )
  311. class ReadReceiptsTestCase(unittest.HomeserverTestCase):
  312. servlets = [
  313. synapse.rest.admin.register_servlets,
  314. login.register_servlets,
  315. receipts.register_servlets,
  316. room.register_servlets,
  317. sync.register_servlets,
  318. ]
  319. def prepare(self, reactor, clock, hs):
  320. self.url = "/sync?since=%s"
  321. self.next_batch = "s0"
  322. # Register the first user
  323. self.user_id = self.register_user("kermit", "monkey")
  324. self.tok = self.login("kermit", "monkey")
  325. # Create the room
  326. self.room_id = self.helper.create_room_as(self.user_id, tok=self.tok)
  327. # Register the second user
  328. self.user2 = self.register_user("kermit2", "monkey")
  329. self.tok2 = self.login("kermit2", "monkey")
  330. # Join the second user
  331. self.helper.join(room=self.room_id, user=self.user2, tok=self.tok2)
  332. @override_config({"experimental_features": {"msc2285_enabled": True}})
  333. def test_hidden_read_receipts(self):
  334. # Send a message as the first user
  335. res = self.helper.send(self.room_id, body="hello", tok=self.tok)
  336. # Send a read receipt to tell the server the first user's message was read
  337. body = json.dumps({ReadReceiptEventFields.MSC2285_HIDDEN: True}).encode("utf8")
  338. channel = self.make_request(
  339. "POST",
  340. "/rooms/%s/receipt/m.read/%s" % (self.room_id, res["event_id"]),
  341. body,
  342. access_token=self.tok2,
  343. )
  344. self.assertEqual(channel.code, 200)
  345. # Test that the first user can't see the other user's hidden read receipt
  346. self.assertEqual(self._get_read_receipt(), None)
  347. @parameterized.expand(
  348. [
  349. # Old Element version, expected to send an empty body
  350. (
  351. "agent1",
  352. "Element/1.2.2 (Linux; U; Android 9; MatrixAndroidSDK_X 0.0.1)",
  353. 200,
  354. ),
  355. # Old SchildiChat version, expected to send an empty body
  356. ("agent2", "SchildiChat/1.2.1 (Android 10)", 200),
  357. # Expected 400: Denies empty body starting at version 1.3+
  358. ("agent3", "Element/1.3.6 (Android 10)", 400),
  359. ("agent4", "SchildiChat/1.3.6 (Android 11)", 400),
  360. # Contains "Riot": Receipts with empty bodies expected
  361. ("agent5", "Element (Riot.im) (Android 9)", 200),
  362. # Expected 400: Does not contain "Android"
  363. ("agent6", "Element/1.2.1", 400),
  364. # Expected 400: Different format, missing "/" after Element; existing build that should allow empty bodies, but minimal ongoing usage
  365. ("agent7", "Element dbg/1.1.8-dev (Android)", 400),
  366. ]
  367. )
  368. def test_read_receipt_with_empty_body(
  369. self, name, user_agent: str, expected_status_code: int
  370. ):
  371. # Send a message as the first user
  372. res = self.helper.send(self.room_id, body="hello", tok=self.tok)
  373. # Send a read receipt for this message with an empty body
  374. channel = self.make_request(
  375. "POST",
  376. "/rooms/%s/receipt/m.read/%s" % (self.room_id, res["event_id"]),
  377. access_token=self.tok2,
  378. custom_headers=[("User-Agent", user_agent)],
  379. )
  380. self.assertEqual(channel.code, expected_status_code)
  381. def _get_read_receipt(self):
  382. """Syncs and returns the read receipt."""
  383. # Checks if event is a read receipt
  384. def is_read_receipt(event):
  385. return event["type"] == "m.receipt"
  386. # Sync
  387. channel = self.make_request(
  388. "GET",
  389. self.url % self.next_batch,
  390. access_token=self.tok,
  391. )
  392. self.assertEqual(channel.code, 200)
  393. # Store the next batch for the next request.
  394. self.next_batch = channel.json_body["next_batch"]
  395. # Return the read receipt
  396. ephemeral_events = channel.json_body["rooms"]["join"][self.room_id][
  397. "ephemeral"
  398. ]["events"]
  399. return next(filter(is_read_receipt, ephemeral_events), None)
  400. class UnreadMessagesTestCase(unittest.HomeserverTestCase):
  401. servlets = [
  402. synapse.rest.admin.register_servlets,
  403. login.register_servlets,
  404. read_marker.register_servlets,
  405. room.register_servlets,
  406. sync.register_servlets,
  407. receipts.register_servlets,
  408. ]
  409. def prepare(self, reactor, clock, hs):
  410. self.url = "/sync?since=%s"
  411. self.next_batch = "s0"
  412. # Register the first user (used to check the unread counts).
  413. self.user_id = self.register_user("kermit", "monkey")
  414. self.tok = self.login("kermit", "monkey")
  415. # Create the room we'll check unread counts for.
  416. self.room_id = self.helper.create_room_as(self.user_id, tok=self.tok)
  417. # Register the second user (used to send events to the room).
  418. self.user2 = self.register_user("kermit2", "monkey")
  419. self.tok2 = self.login("kermit2", "monkey")
  420. # Change the power levels of the room so that the second user can send state
  421. # events.
  422. self.helper.send_state(
  423. self.room_id,
  424. EventTypes.PowerLevels,
  425. {
  426. "users": {self.user_id: 100, self.user2: 100},
  427. "users_default": 0,
  428. "events": {
  429. "m.room.name": 50,
  430. "m.room.power_levels": 100,
  431. "m.room.history_visibility": 100,
  432. "m.room.canonical_alias": 50,
  433. "m.room.avatar": 50,
  434. "m.room.tombstone": 100,
  435. "m.room.server_acl": 100,
  436. "m.room.encryption": 100,
  437. },
  438. "events_default": 0,
  439. "state_default": 50,
  440. "ban": 50,
  441. "kick": 50,
  442. "redact": 50,
  443. "invite": 0,
  444. },
  445. tok=self.tok,
  446. )
  447. def test_unread_counts(self):
  448. """Tests that /sync returns the right value for the unread count (MSC2654)."""
  449. # Check that our own messages don't increase the unread count.
  450. self.helper.send(self.room_id, "hello", tok=self.tok)
  451. self._check_unread_count(0)
  452. # Join the new user and check that this doesn't increase the unread count.
  453. self.helper.join(room=self.room_id, user=self.user2, tok=self.tok2)
  454. self._check_unread_count(0)
  455. # Check that the new user sending a message increases our unread count.
  456. res = self.helper.send(self.room_id, "hello", tok=self.tok2)
  457. self._check_unread_count(1)
  458. # Send a read receipt to tell the server we've read the latest event.
  459. body = json.dumps({"m.read": res["event_id"]}).encode("utf8")
  460. channel = self.make_request(
  461. "POST",
  462. "/rooms/%s/read_markers" % self.room_id,
  463. body,
  464. access_token=self.tok,
  465. )
  466. self.assertEqual(channel.code, 200, channel.json_body)
  467. # Check that the unread counter is back to 0.
  468. self._check_unread_count(0)
  469. # Check that hidden read receipts don't break unread counts
  470. res = self.helper.send(self.room_id, "hello", tok=self.tok2)
  471. self._check_unread_count(1)
  472. # Send a read receipt to tell the server we've read the latest event.
  473. body = json.dumps({ReadReceiptEventFields.MSC2285_HIDDEN: True}).encode("utf8")
  474. channel = self.make_request(
  475. "POST",
  476. "/rooms/%s/receipt/m.read/%s" % (self.room_id, res["event_id"]),
  477. body,
  478. access_token=self.tok,
  479. )
  480. self.assertEqual(channel.code, 200, channel.json_body)
  481. # Check that the unread counter is back to 0.
  482. self._check_unread_count(0)
  483. # Check that room name changes increase the unread counter.
  484. self.helper.send_state(
  485. self.room_id,
  486. "m.room.name",
  487. {"name": "my super room"},
  488. tok=self.tok2,
  489. )
  490. self._check_unread_count(1)
  491. # Check that room topic changes increase the unread counter.
  492. self.helper.send_state(
  493. self.room_id,
  494. "m.room.topic",
  495. {"topic": "welcome!!!"},
  496. tok=self.tok2,
  497. )
  498. self._check_unread_count(2)
  499. # Check that encrypted messages increase the unread counter.
  500. self.helper.send_event(self.room_id, EventTypes.Encrypted, {}, tok=self.tok2)
  501. self._check_unread_count(3)
  502. # Check that custom events with a body increase the unread counter.
  503. self.helper.send_event(
  504. self.room_id,
  505. "org.matrix.custom_type",
  506. {"body": "hello"},
  507. tok=self.tok2,
  508. )
  509. self._check_unread_count(4)
  510. # Check that edits don't increase the unread counter.
  511. self.helper.send_event(
  512. room_id=self.room_id,
  513. type=EventTypes.Message,
  514. content={
  515. "body": "hello",
  516. "msgtype": "m.text",
  517. "m.relates_to": {"rel_type": RelationTypes.REPLACE},
  518. },
  519. tok=self.tok2,
  520. )
  521. self._check_unread_count(4)
  522. # Check that notices don't increase the unread counter.
  523. self.helper.send_event(
  524. room_id=self.room_id,
  525. type=EventTypes.Message,
  526. content={"body": "hello", "msgtype": "m.notice"},
  527. tok=self.tok2,
  528. )
  529. self._check_unread_count(4)
  530. # Check that tombstone events changes increase the unread counter.
  531. self.helper.send_state(
  532. self.room_id,
  533. EventTypes.Tombstone,
  534. {"replacement_room": "!someroom:test"},
  535. tok=self.tok2,
  536. )
  537. self._check_unread_count(5)
  538. def _check_unread_count(self, expected_count: int):
  539. """Syncs and compares the unread count with the expected value."""
  540. channel = self.make_request(
  541. "GET",
  542. self.url % self.next_batch,
  543. access_token=self.tok,
  544. )
  545. self.assertEqual(channel.code, 200, channel.json_body)
  546. room_entry = channel.json_body["rooms"]["join"][self.room_id]
  547. self.assertEqual(
  548. room_entry["org.matrix.msc2654.unread_count"],
  549. expected_count,
  550. room_entry,
  551. )
  552. # Store the next batch for the next request.
  553. self.next_batch = channel.json_body["next_batch"]
  554. class SyncCacheTestCase(unittest.HomeserverTestCase):
  555. servlets = [
  556. synapse.rest.admin.register_servlets,
  557. login.register_servlets,
  558. sync.register_servlets,
  559. ]
  560. def test_noop_sync_does_not_tightloop(self):
  561. """If the sync times out, we shouldn't cache the result
  562. Essentially a regression test for #8518.
  563. """
  564. self.user_id = self.register_user("kermit", "monkey")
  565. self.tok = self.login("kermit", "monkey")
  566. # we should immediately get an initial sync response
  567. channel = self.make_request("GET", "/sync", access_token=self.tok)
  568. self.assertEqual(channel.code, 200, channel.json_body)
  569. # now, make an incremental sync request, with a timeout
  570. next_batch = channel.json_body["next_batch"]
  571. channel = self.make_request(
  572. "GET",
  573. f"/sync?since={next_batch}&timeout=10000",
  574. access_token=self.tok,
  575. await_result=False,
  576. )
  577. # that should block for 10 seconds
  578. with self.assertRaises(TimedOutException):
  579. channel.await_result(timeout_ms=9900)
  580. channel.await_result(timeout_ms=200)
  581. self.assertEqual(channel.code, 200, channel.json_body)
  582. # we expect the next_batch in the result to be the same as before
  583. self.assertEqual(channel.json_body["next_batch"], next_batch)
  584. # another incremental sync should also block.
  585. channel = self.make_request(
  586. "GET",
  587. f"/sync?since={next_batch}&timeout=10000",
  588. access_token=self.tok,
  589. await_result=False,
  590. )
  591. # that should block for 10 seconds
  592. with self.assertRaises(TimedOutException):
  593. channel.await_result(timeout_ms=9900)
  594. channel.await_result(timeout_ms=200)
  595. self.assertEqual(channel.code, 200, channel.json_body)