test_sync.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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
  19. from synapse.rest.client.v1 import login, room
  20. from synapse.rest.client.v2_alpha import 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. request, channel = self.make_request("GET", "/sync")
  33. self.render(request)
  34. self.assertEqual(channel.code, 200)
  35. self.assertTrue(
  36. {
  37. "next_batch",
  38. "rooms",
  39. "presence",
  40. "account_data",
  41. "to_device",
  42. "device_lists",
  43. }.issubset(set(channel.json_body.keys()))
  44. )
  45. def test_sync_presence_disabled(self):
  46. """
  47. When presence is disabled, the key does not appear in /sync.
  48. """
  49. self.hs.config.use_presence = False
  50. request, channel = self.make_request("GET", "/sync")
  51. self.render(request)
  52. self.assertEqual(channel.code, 200)
  53. self.assertTrue(
  54. {
  55. "next_batch",
  56. "rooms",
  57. "account_data",
  58. "to_device",
  59. "device_lists",
  60. }.issubset(set(channel.json_body.keys()))
  61. )
  62. class SyncFilterTestCase(unittest.HomeserverTestCase):
  63. servlets = [
  64. synapse.rest.admin.register_servlets_for_client_rest_resource,
  65. room.register_servlets,
  66. login.register_servlets,
  67. sync.register_servlets,
  68. ]
  69. def test_sync_filter_labels(self):
  70. """Test that we can filter by a label."""
  71. sync_filter = json.dumps(
  72. {
  73. "room": {
  74. "timeline": {
  75. "types": [EventTypes.Message],
  76. "org.matrix.labels": ["#fun"],
  77. }
  78. }
  79. }
  80. )
  81. events = self._test_sync_filter_labels(sync_filter)
  82. self.assertEqual(len(events), 2, [event["content"] for event in events])
  83. self.assertEqual(events[0]["content"]["body"], "with right label", events[0])
  84. self.assertEqual(events[1]["content"]["body"], "with right label", events[1])
  85. def test_sync_filter_not_labels(self):
  86. """Test that we can filter by the absence of a label."""
  87. sync_filter = json.dumps(
  88. {
  89. "room": {
  90. "timeline": {
  91. "types": [EventTypes.Message],
  92. "org.matrix.not_labels": ["#fun"],
  93. }
  94. }
  95. }
  96. )
  97. events = self._test_sync_filter_labels(sync_filter)
  98. self.assertEqual(len(events), 3, [event["content"] for event in events])
  99. self.assertEqual(events[0]["content"]["body"], "without label", events[0])
  100. self.assertEqual(events[1]["content"]["body"], "with wrong label", events[1])
  101. self.assertEqual(
  102. events[2]["content"]["body"], "with two wrong labels", events[2]
  103. )
  104. def test_sync_filter_labels_not_labels(self):
  105. """Test that we can filter by both a label and the absence of another label."""
  106. sync_filter = json.dumps(
  107. {
  108. "room": {
  109. "timeline": {
  110. "types": [EventTypes.Message],
  111. "org.matrix.labels": ["#work"],
  112. "org.matrix.not_labels": ["#notfun"],
  113. }
  114. }
  115. }
  116. )
  117. events = self._test_sync_filter_labels(sync_filter)
  118. self.assertEqual(len(events), 1, [event["content"] for event in events])
  119. self.assertEqual(events[0]["content"]["body"], "with wrong label", events[0])
  120. def _test_sync_filter_labels(self, sync_filter):
  121. user_id = self.register_user("kermit", "test")
  122. tok = self.login("kermit", "test")
  123. room_id = self.helper.create_room_as(user_id, tok=tok)
  124. self.helper.send_event(
  125. room_id=room_id,
  126. type=EventTypes.Message,
  127. content={
  128. "msgtype": "m.text",
  129. "body": "with right label",
  130. EventContentFields.LABELS: ["#fun"],
  131. },
  132. tok=tok,
  133. )
  134. self.helper.send_event(
  135. room_id=room_id,
  136. type=EventTypes.Message,
  137. content={"msgtype": "m.text", "body": "without label"},
  138. tok=tok,
  139. )
  140. self.helper.send_event(
  141. room_id=room_id,
  142. type=EventTypes.Message,
  143. content={
  144. "msgtype": "m.text",
  145. "body": "with wrong label",
  146. EventContentFields.LABELS: ["#work"],
  147. },
  148. tok=tok,
  149. )
  150. self.helper.send_event(
  151. room_id=room_id,
  152. type=EventTypes.Message,
  153. content={
  154. "msgtype": "m.text",
  155. "body": "with two wrong labels",
  156. EventContentFields.LABELS: ["#work", "#notfun"],
  157. },
  158. tok=tok,
  159. )
  160. self.helper.send_event(
  161. room_id=room_id,
  162. type=EventTypes.Message,
  163. content={
  164. "msgtype": "m.text",
  165. "body": "with right label",
  166. EventContentFields.LABELS: ["#fun"],
  167. },
  168. tok=tok,
  169. )
  170. request, channel = self.make_request(
  171. "GET", "/sync?filter=%s" % sync_filter, access_token=tok
  172. )
  173. self.render(request)
  174. self.assertEqual(channel.code, 200, channel.result)
  175. return channel.json_body["rooms"]["join"][room_id]["timeline"]["events"]
  176. class SyncTypingTests(unittest.HomeserverTestCase):
  177. servlets = [
  178. synapse.rest.admin.register_servlets_for_client_rest_resource,
  179. room.register_servlets,
  180. login.register_servlets,
  181. sync.register_servlets,
  182. ]
  183. user_id = True
  184. hijack_auth = False
  185. def test_sync_backwards_typing(self):
  186. """
  187. If the typing serial goes backwards and the typing handler is then reset
  188. (such as when the master restarts and sets the typing serial to 0), we
  189. do not incorrectly return typing information that had a serial greater
  190. than the now-reset serial.
  191. """
  192. typing_url = "/rooms/%s/typing/%s?access_token=%s"
  193. sync_url = "/sync?timeout=3000000&access_token=%s&since=%s"
  194. # Register the user who gets notified
  195. user_id = self.register_user("user", "pass")
  196. access_token = self.login("user", "pass")
  197. # Register the user who sends the message
  198. other_user_id = self.register_user("otheruser", "pass")
  199. other_access_token = self.login("otheruser", "pass")
  200. # Create a room
  201. room = self.helper.create_room_as(user_id, tok=access_token)
  202. # Invite the other person
  203. self.helper.invite(room=room, src=user_id, tok=access_token, targ=other_user_id)
  204. # The other user joins
  205. self.helper.join(room=room, user=other_user_id, tok=other_access_token)
  206. # The other user sends some messages
  207. self.helper.send(room, body="Hi!", tok=other_access_token)
  208. self.helper.send(room, body="There!", tok=other_access_token)
  209. # Start typing.
  210. request, channel = self.make_request(
  211. "PUT",
  212. typing_url % (room, other_user_id, other_access_token),
  213. b'{"typing": true, "timeout": 30000}',
  214. )
  215. self.render(request)
  216. self.assertEquals(200, channel.code)
  217. request, channel = self.make_request(
  218. "GET", "/sync?access_token=%s" % (access_token,)
  219. )
  220. self.render(request)
  221. self.assertEquals(200, channel.code)
  222. next_batch = channel.json_body["next_batch"]
  223. # Stop typing.
  224. request, channel = self.make_request(
  225. "PUT",
  226. typing_url % (room, other_user_id, other_access_token),
  227. b'{"typing": false}',
  228. )
  229. self.render(request)
  230. self.assertEquals(200, channel.code)
  231. # Start typing.
  232. request, channel = self.make_request(
  233. "PUT",
  234. typing_url % (room, other_user_id, other_access_token),
  235. b'{"typing": true, "timeout": 30000}',
  236. )
  237. self.render(request)
  238. self.assertEquals(200, channel.code)
  239. # Should return immediately
  240. request, channel = self.make_request(
  241. "GET", sync_url % (access_token, next_batch)
  242. )
  243. self.render(request)
  244. self.assertEquals(200, channel.code)
  245. next_batch = channel.json_body["next_batch"]
  246. # Reset typing serial back to 0, as if the master had.
  247. typing = self.hs.get_typing_handler()
  248. typing._latest_room_serial = 0
  249. # Since it checks the state token, we need some state to update to
  250. # invalidate the stream token.
  251. self.helper.send(room, body="There!", tok=other_access_token)
  252. request, channel = self.make_request(
  253. "GET", sync_url % (access_token, next_batch)
  254. )
  255. self.render(request)
  256. self.assertEquals(200, channel.code)
  257. next_batch = channel.json_body["next_batch"]
  258. # This should time out! But it does not, because our stream token is
  259. # ahead, and therefore it's saying the typing (that we've actually
  260. # already seen) is new, since it's got a token above our new, now-reset
  261. # stream token.
  262. request, channel = self.make_request(
  263. "GET", sync_url % (access_token, next_batch)
  264. )
  265. self.render(request)
  266. self.assertEquals(200, channel.code)
  267. next_batch = channel.json_body["next_batch"]
  268. # Clear the typing information, so that it doesn't think everything is
  269. # in the future.
  270. typing._reset()
  271. # Now it SHOULD fail as it never completes!
  272. request, channel = self.make_request(
  273. "GET", sync_url % (access_token, next_batch)
  274. )
  275. self.assertRaises(TimedOutException, self.render, request)