test_sync.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2018-2019 New Vector Ltd
  3. # Copyright 2019 The Matrix.org Foundation C.I.C.
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License");
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. import json
  17. import synapse.rest.admin
  18. from synapse.api.constants import EventContentFields, EventTypes, RelationTypes
  19. from synapse.rest.client.v1 import login, room
  20. from synapse.rest.client.v2_alpha import read_marker, sync
  21. from tests import unittest
  22. from tests.server import TimedOutException
  23. class FilterTestCase(unittest.HomeserverTestCase):
  24. user_id = "@apple:test"
  25. servlets = [
  26. synapse.rest.admin.register_servlets_for_client_rest_resource,
  27. room.register_servlets,
  28. login.register_servlets,
  29. sync.register_servlets,
  30. ]
  31. def test_sync_argless(self):
  32. channel = self.make_request("GET", "/sync")
  33. self.assertEqual(channel.code, 200)
  34. self.assertTrue(
  35. {
  36. "next_batch",
  37. "rooms",
  38. "presence",
  39. "account_data",
  40. "to_device",
  41. "device_lists",
  42. }.issubset(set(channel.json_body.keys()))
  43. )
  44. def test_sync_presence_disabled(self):
  45. """
  46. When presence is disabled, the key does not appear in /sync.
  47. """
  48. self.hs.config.use_presence = False
  49. channel = self.make_request("GET", "/sync")
  50. self.assertEqual(channel.code, 200)
  51. self.assertTrue(
  52. {
  53. "next_batch",
  54. "rooms",
  55. "account_data",
  56. "to_device",
  57. "device_lists",
  58. }.issubset(set(channel.json_body.keys()))
  59. )
  60. class SyncFilterTestCase(unittest.HomeserverTestCase):
  61. servlets = [
  62. synapse.rest.admin.register_servlets_for_client_rest_resource,
  63. room.register_servlets,
  64. login.register_servlets,
  65. sync.register_servlets,
  66. ]
  67. def test_sync_filter_labels(self):
  68. """Test that we can filter by a label."""
  69. sync_filter = json.dumps(
  70. {
  71. "room": {
  72. "timeline": {
  73. "types": [EventTypes.Message],
  74. "org.matrix.labels": ["#fun"],
  75. }
  76. }
  77. }
  78. )
  79. events = self._test_sync_filter_labels(sync_filter)
  80. self.assertEqual(len(events), 2, [event["content"] for event in events])
  81. self.assertEqual(events[0]["content"]["body"], "with right label", events[0])
  82. self.assertEqual(events[1]["content"]["body"], "with right label", events[1])
  83. def test_sync_filter_not_labels(self):
  84. """Test that we can filter by the absence of a label."""
  85. sync_filter = json.dumps(
  86. {
  87. "room": {
  88. "timeline": {
  89. "types": [EventTypes.Message],
  90. "org.matrix.not_labels": ["#fun"],
  91. }
  92. }
  93. }
  94. )
  95. events = self._test_sync_filter_labels(sync_filter)
  96. self.assertEqual(len(events), 3, [event["content"] for event in events])
  97. self.assertEqual(events[0]["content"]["body"], "without label", events[0])
  98. self.assertEqual(events[1]["content"]["body"], "with wrong label", events[1])
  99. self.assertEqual(
  100. events[2]["content"]["body"], "with two wrong labels", events[2]
  101. )
  102. def test_sync_filter_labels_not_labels(self):
  103. """Test that we can filter by both a label and the absence of another label."""
  104. sync_filter = json.dumps(
  105. {
  106. "room": {
  107. "timeline": {
  108. "types": [EventTypes.Message],
  109. "org.matrix.labels": ["#work"],
  110. "org.matrix.not_labels": ["#notfun"],
  111. }
  112. }
  113. }
  114. )
  115. events = self._test_sync_filter_labels(sync_filter)
  116. self.assertEqual(len(events), 1, [event["content"] for event in events])
  117. self.assertEqual(events[0]["content"]["body"], "with wrong label", events[0])
  118. def _test_sync_filter_labels(self, sync_filter):
  119. user_id = self.register_user("kermit", "test")
  120. tok = self.login("kermit", "test")
  121. room_id = self.helper.create_room_as(user_id, tok=tok)
  122. self.helper.send_event(
  123. room_id=room_id,
  124. type=EventTypes.Message,
  125. content={
  126. "msgtype": "m.text",
  127. "body": "with right label",
  128. EventContentFields.LABELS: ["#fun"],
  129. },
  130. tok=tok,
  131. )
  132. self.helper.send_event(
  133. room_id=room_id,
  134. type=EventTypes.Message,
  135. content={"msgtype": "m.text", "body": "without label"},
  136. tok=tok,
  137. )
  138. self.helper.send_event(
  139. room_id=room_id,
  140. type=EventTypes.Message,
  141. content={
  142. "msgtype": "m.text",
  143. "body": "with wrong label",
  144. EventContentFields.LABELS: ["#work"],
  145. },
  146. tok=tok,
  147. )
  148. self.helper.send_event(
  149. room_id=room_id,
  150. type=EventTypes.Message,
  151. content={
  152. "msgtype": "m.text",
  153. "body": "with two wrong labels",
  154. EventContentFields.LABELS: ["#work", "#notfun"],
  155. },
  156. tok=tok,
  157. )
  158. self.helper.send_event(
  159. room_id=room_id,
  160. type=EventTypes.Message,
  161. content={
  162. "msgtype": "m.text",
  163. "body": "with right label",
  164. EventContentFields.LABELS: ["#fun"],
  165. },
  166. tok=tok,
  167. )
  168. channel = self.make_request(
  169. "GET", "/sync?filter=%s" % sync_filter, access_token=tok
  170. )
  171. self.assertEqual(channel.code, 200, channel.result)
  172. return channel.json_body["rooms"]["join"][room_id]["timeline"]["events"]
  173. class SyncTypingTests(unittest.HomeserverTestCase):
  174. servlets = [
  175. synapse.rest.admin.register_servlets_for_client_rest_resource,
  176. room.register_servlets,
  177. login.register_servlets,
  178. sync.register_servlets,
  179. ]
  180. user_id = True
  181. hijack_auth = False
  182. def test_sync_backwards_typing(self):
  183. """
  184. If the typing serial goes backwards and the typing handler is then reset
  185. (such as when the master restarts and sets the typing serial to 0), we
  186. do not incorrectly return typing information that had a serial greater
  187. than the now-reset serial.
  188. """
  189. typing_url = "/rooms/%s/typing/%s?access_token=%s"
  190. sync_url = "/sync?timeout=3000000&access_token=%s&since=%s"
  191. # Register the user who gets notified
  192. user_id = self.register_user("user", "pass")
  193. access_token = self.login("user", "pass")
  194. # Register the user who sends the message
  195. other_user_id = self.register_user("otheruser", "pass")
  196. other_access_token = self.login("otheruser", "pass")
  197. # Create a room
  198. room = self.helper.create_room_as(user_id, tok=access_token)
  199. # Invite the other person
  200. self.helper.invite(room=room, src=user_id, tok=access_token, targ=other_user_id)
  201. # The other user joins
  202. self.helper.join(room=room, user=other_user_id, tok=other_access_token)
  203. # The other user sends some messages
  204. self.helper.send(room, body="Hi!", tok=other_access_token)
  205. self.helper.send(room, body="There!", tok=other_access_token)
  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. channel = self.make_request("GET", "/sync?access_token=%s" % (access_token,))
  214. self.assertEquals(200, channel.code)
  215. next_batch = channel.json_body["next_batch"]
  216. # Stop typing.
  217. channel = self.make_request(
  218. "PUT",
  219. typing_url % (room, other_user_id, other_access_token),
  220. b'{"typing": false}',
  221. )
  222. self.assertEquals(200, channel.code)
  223. # Start typing.
  224. channel = self.make_request(
  225. "PUT",
  226. typing_url % (room, other_user_id, other_access_token),
  227. b'{"typing": true, "timeout": 30000}',
  228. )
  229. self.assertEquals(200, channel.code)
  230. # Should return immediately
  231. channel = self.make_request("GET", sync_url % (access_token, next_batch))
  232. self.assertEquals(200, channel.code)
  233. next_batch = channel.json_body["next_batch"]
  234. # Reset typing serial back to 0, as if the master had.
  235. typing = self.hs.get_typing_handler()
  236. typing._latest_room_serial = 0
  237. # Since it checks the state token, we need some state to update to
  238. # invalidate the stream token.
  239. self.helper.send(room, body="There!", tok=other_access_token)
  240. channel = self.make_request("GET", sync_url % (access_token, next_batch))
  241. self.assertEquals(200, channel.code)
  242. next_batch = channel.json_body["next_batch"]
  243. # This should time out! But it does not, because our stream token is
  244. # ahead, and therefore it's saying the typing (that we've actually
  245. # already seen) is new, since it's got a token above our new, now-reset
  246. # stream token.
  247. channel = self.make_request("GET", sync_url % (access_token, next_batch))
  248. self.assertEquals(200, channel.code)
  249. next_batch = channel.json_body["next_batch"]
  250. # Clear the typing information, so that it doesn't think everything is
  251. # in the future.
  252. typing._reset()
  253. # Now it SHOULD fail as it never completes!
  254. with self.assertRaises(TimedOutException):
  255. self.make_request("GET", sync_url % (access_token, next_batch))
  256. class UnreadMessagesTestCase(unittest.HomeserverTestCase):
  257. servlets = [
  258. synapse.rest.admin.register_servlets,
  259. login.register_servlets,
  260. read_marker.register_servlets,
  261. room.register_servlets,
  262. sync.register_servlets,
  263. ]
  264. def prepare(self, reactor, clock, hs):
  265. self.url = "/sync?since=%s"
  266. self.next_batch = "s0"
  267. # Register the first user (used to check the unread counts).
  268. self.user_id = self.register_user("kermit", "monkey")
  269. self.tok = self.login("kermit", "monkey")
  270. # Create the room we'll check unread counts for.
  271. self.room_id = self.helper.create_room_as(self.user_id, tok=self.tok)
  272. # Register the second user (used to send events to the room).
  273. self.user2 = self.register_user("kermit2", "monkey")
  274. self.tok2 = self.login("kermit2", "monkey")
  275. # Change the power levels of the room so that the second user can send state
  276. # events.
  277. self.helper.send_state(
  278. self.room_id,
  279. EventTypes.PowerLevels,
  280. {
  281. "users": {self.user_id: 100, self.user2: 100},
  282. "users_default": 0,
  283. "events": {
  284. "m.room.name": 50,
  285. "m.room.power_levels": 100,
  286. "m.room.history_visibility": 100,
  287. "m.room.canonical_alias": 50,
  288. "m.room.avatar": 50,
  289. "m.room.tombstone": 100,
  290. "m.room.server_acl": 100,
  291. "m.room.encryption": 100,
  292. },
  293. "events_default": 0,
  294. "state_default": 50,
  295. "ban": 50,
  296. "kick": 50,
  297. "redact": 50,
  298. "invite": 0,
  299. },
  300. tok=self.tok,
  301. )
  302. def test_unread_counts(self):
  303. """Tests that /sync returns the right value for the unread count (MSC2654)."""
  304. # Check that our own messages don't increase the unread count.
  305. self.helper.send(self.room_id, "hello", tok=self.tok)
  306. self._check_unread_count(0)
  307. # Join the new user and check that this doesn't increase the unread count.
  308. self.helper.join(room=self.room_id, user=self.user2, tok=self.tok2)
  309. self._check_unread_count(0)
  310. # Check that the new user sending a message increases our unread count.
  311. res = self.helper.send(self.room_id, "hello", tok=self.tok2)
  312. self._check_unread_count(1)
  313. # Send a read receipt to tell the server we've read the latest event.
  314. body = json.dumps({"m.read": res["event_id"]}).encode("utf8")
  315. channel = self.make_request(
  316. "POST",
  317. "/rooms/%s/read_markers" % self.room_id,
  318. body,
  319. access_token=self.tok,
  320. )
  321. self.assertEqual(channel.code, 200, channel.json_body)
  322. # Check that the unread counter is back to 0.
  323. self._check_unread_count(0)
  324. # Check that room name changes increase the unread counter.
  325. self.helper.send_state(
  326. self.room_id, "m.room.name", {"name": "my super room"}, tok=self.tok2,
  327. )
  328. self._check_unread_count(1)
  329. # Check that room topic changes increase the unread counter.
  330. self.helper.send_state(
  331. self.room_id, "m.room.topic", {"topic": "welcome!!!"}, tok=self.tok2,
  332. )
  333. self._check_unread_count(2)
  334. # Check that encrypted messages increase the unread counter.
  335. self.helper.send_event(self.room_id, EventTypes.Encrypted, {}, tok=self.tok2)
  336. self._check_unread_count(3)
  337. # Check that custom events with a body increase the unread counter.
  338. self.helper.send_event(
  339. self.room_id, "org.matrix.custom_type", {"body": "hello"}, tok=self.tok2,
  340. )
  341. self._check_unread_count(4)
  342. # Check that edits don't increase the unread counter.
  343. self.helper.send_event(
  344. room_id=self.room_id,
  345. type=EventTypes.Message,
  346. content={
  347. "body": "hello",
  348. "msgtype": "m.text",
  349. "m.relates_to": {"rel_type": RelationTypes.REPLACE},
  350. },
  351. tok=self.tok2,
  352. )
  353. self._check_unread_count(4)
  354. # Check that notices don't increase the unread counter.
  355. self.helper.send_event(
  356. room_id=self.room_id,
  357. type=EventTypes.Message,
  358. content={"body": "hello", "msgtype": "m.notice"},
  359. tok=self.tok2,
  360. )
  361. self._check_unread_count(4)
  362. # Check that tombstone events changes increase the unread counter.
  363. self.helper.send_state(
  364. self.room_id,
  365. EventTypes.Tombstone,
  366. {"replacement_room": "!someroom:test"},
  367. tok=self.tok2,
  368. )
  369. self._check_unread_count(5)
  370. def _check_unread_count(self, expected_count: True):
  371. """Syncs and compares the unread count with the expected value."""
  372. channel = self.make_request(
  373. "GET", self.url % self.next_batch, access_token=self.tok,
  374. )
  375. self.assertEqual(channel.code, 200, channel.json_body)
  376. room_entry = channel.json_body["rooms"]["join"][self.room_id]
  377. self.assertEqual(
  378. room_entry["org.matrix.msc2654.unread_count"], expected_count, room_entry,
  379. )
  380. # Store the next batch for the next request.
  381. self.next_batch = channel.json_body["next_batch"]