test_sync.py 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2018 New Vector
  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. from mock import Mock
  16. import synapse.rest.admin
  17. from synapse.rest.client.v1 import login, room
  18. from synapse.rest.client.v2_alpha import sync
  19. from tests import unittest
  20. from tests.server import TimedOutException
  21. class FilterTestCase(unittest.HomeserverTestCase):
  22. user_id = "@apple:test"
  23. servlets = [sync.register_servlets]
  24. def make_homeserver(self, reactor, clock):
  25. hs = self.setup_test_homeserver(
  26. "red", http_client=None, federation_client=Mock()
  27. )
  28. return hs
  29. def test_sync_argless(self):
  30. request, channel = self.make_request("GET", "/sync")
  31. self.render(request)
  32. self.assertEqual(channel.code, 200)
  33. self.assertTrue(
  34. set(
  35. [
  36. "next_batch",
  37. "rooms",
  38. "presence",
  39. "account_data",
  40. "to_device",
  41. "device_lists",
  42. ]
  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. set(
  55. ["next_batch", "rooms", "account_data", "to_device", "device_lists"]
  56. ).issubset(set(channel.json_body.keys()))
  57. )
  58. class SyncTypingTests(unittest.HomeserverTestCase):
  59. servlets = [
  60. synapse.rest.admin.register_servlets_for_client_rest_resource,
  61. room.register_servlets,
  62. login.register_servlets,
  63. sync.register_servlets,
  64. ]
  65. user_id = True
  66. hijack_auth = False
  67. def test_sync_backwards_typing(self):
  68. """
  69. If the typing serial goes backwards and the typing handler is then reset
  70. (such as when the master restarts and sets the typing serial to 0), we
  71. do not incorrectly return typing information that had a serial greater
  72. than the now-reset serial.
  73. """
  74. typing_url = "/rooms/%s/typing/%s?access_token=%s"
  75. sync_url = "/sync?timeout=3000000&access_token=%s&since=%s"
  76. # Register the user who gets notified
  77. user_id = self.register_user("user", "pass")
  78. access_token = self.login("user", "pass")
  79. # Register the user who sends the message
  80. other_user_id = self.register_user("otheruser", "pass")
  81. other_access_token = self.login("otheruser", "pass")
  82. # Create a room
  83. room = self.helper.create_room_as(user_id, tok=access_token)
  84. # Invite the other person
  85. self.helper.invite(room=room, src=user_id, tok=access_token, targ=other_user_id)
  86. # The other user joins
  87. self.helper.join(room=room, user=other_user_id, tok=other_access_token)
  88. # The other user sends some messages
  89. self.helper.send(room, body="Hi!", tok=other_access_token)
  90. self.helper.send(room, body="There!", tok=other_access_token)
  91. # Start typing.
  92. request, channel = self.make_request(
  93. "PUT",
  94. typing_url % (room, other_user_id, other_access_token),
  95. b'{"typing": true, "timeout": 30000}',
  96. )
  97. self.render(request)
  98. self.assertEquals(200, channel.code)
  99. request, channel = self.make_request(
  100. "GET", "/sync?access_token=%s" % (access_token,)
  101. )
  102. self.render(request)
  103. self.assertEquals(200, channel.code)
  104. next_batch = channel.json_body["next_batch"]
  105. # Stop typing.
  106. request, channel = self.make_request(
  107. "PUT",
  108. typing_url % (room, other_user_id, other_access_token),
  109. b'{"typing": false}',
  110. )
  111. self.render(request)
  112. self.assertEquals(200, channel.code)
  113. # Start typing.
  114. request, channel = self.make_request(
  115. "PUT",
  116. typing_url % (room, other_user_id, other_access_token),
  117. b'{"typing": true, "timeout": 30000}',
  118. )
  119. self.render(request)
  120. self.assertEquals(200, channel.code)
  121. # Should return immediately
  122. request, channel = self.make_request(
  123. "GET", sync_url % (access_token, next_batch)
  124. )
  125. self.render(request)
  126. self.assertEquals(200, channel.code)
  127. next_batch = channel.json_body["next_batch"]
  128. # Reset typing serial back to 0, as if the master had.
  129. typing = self.hs.get_typing_handler()
  130. typing._latest_room_serial = 0
  131. # Since it checks the state token, we need some state to update to
  132. # invalidate the stream token.
  133. self.helper.send(room, body="There!", tok=other_access_token)
  134. request, channel = self.make_request(
  135. "GET", sync_url % (access_token, next_batch)
  136. )
  137. self.render(request)
  138. self.assertEquals(200, channel.code)
  139. next_batch = channel.json_body["next_batch"]
  140. # This should time out! But it does not, because our stream token is
  141. # ahead, and therefore it's saying the typing (that we've actually
  142. # already seen) is new, since it's got a token above our new, now-reset
  143. # stream token.
  144. request, channel = self.make_request(
  145. "GET", sync_url % (access_token, next_batch)
  146. )
  147. self.render(request)
  148. self.assertEquals(200, channel.code)
  149. next_batch = channel.json_body["next_batch"]
  150. # Clear the typing information, so that it doesn't think everything is
  151. # in the future.
  152. typing._reset()
  153. # Now it SHOULD fail as it never completes!
  154. request, channel = self.make_request(
  155. "GET", sync_url % (access_token, next_batch)
  156. )
  157. self.assertRaises(TimedOutException, self.render, request)