test_sendtodevice.py 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. # Copyright 2021 The Matrix.org Foundation C.I.C.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. from synapse.rest import admin
  15. from synapse.rest.client import login, sendtodevice, sync
  16. from tests.unittest import HomeserverTestCase, override_config
  17. class SendToDeviceTestCase(HomeserverTestCase):
  18. servlets = [
  19. admin.register_servlets,
  20. login.register_servlets,
  21. sendtodevice.register_servlets,
  22. sync.register_servlets,
  23. ]
  24. def test_user_to_user(self):
  25. """A to-device message from one user to another should get delivered"""
  26. user1 = self.register_user("u1", "pass")
  27. user1_tok = self.login("u1", "pass", "d1")
  28. user2 = self.register_user("u2", "pass")
  29. user2_tok = self.login("u2", "pass", "d2")
  30. # send the message
  31. test_msg = {"foo": "bar"}
  32. chan = self.make_request(
  33. "PUT",
  34. "/_matrix/client/r0/sendToDevice/m.test/1234",
  35. content={"messages": {user2: {"d2": test_msg}}},
  36. access_token=user1_tok,
  37. )
  38. self.assertEqual(chan.code, 200, chan.result)
  39. # check it appears
  40. channel = self.make_request("GET", "/sync", access_token=user2_tok)
  41. self.assertEqual(channel.code, 200, channel.result)
  42. expected_result = {
  43. "events": [
  44. {
  45. "sender": user1,
  46. "type": "m.test",
  47. "content": test_msg,
  48. }
  49. ]
  50. }
  51. self.assertEqual(channel.json_body["to_device"], expected_result)
  52. # it should re-appear if we do another sync
  53. channel = self.make_request("GET", "/sync", access_token=user2_tok)
  54. self.assertEqual(channel.code, 200, channel.result)
  55. self.assertEqual(channel.json_body["to_device"], expected_result)
  56. # it should *not* appear if we do an incremental sync
  57. sync_token = channel.json_body["next_batch"]
  58. channel = self.make_request(
  59. "GET", f"/sync?since={sync_token}", access_token=user2_tok
  60. )
  61. self.assertEqual(channel.code, 200, channel.result)
  62. self.assertEqual(channel.json_body.get("to_device", {}).get("events", []), [])
  63. @override_config({"rc_key_requests": {"per_second": 10, "burst_count": 2}})
  64. def test_local_room_key_request(self):
  65. """m.room_key_request has special-casing; test from local user"""
  66. user1 = self.register_user("u1", "pass")
  67. user1_tok = self.login("u1", "pass", "d1")
  68. user2 = self.register_user("u2", "pass")
  69. user2_tok = self.login("u2", "pass", "d2")
  70. # send three messages
  71. for i in range(3):
  72. chan = self.make_request(
  73. "PUT",
  74. f"/_matrix/client/r0/sendToDevice/m.room_key_request/{i}",
  75. content={"messages": {user2: {"d2": {"idx": i}}}},
  76. access_token=user1_tok,
  77. )
  78. self.assertEqual(chan.code, 200, chan.result)
  79. # now sync: we should get two of the three
  80. channel = self.make_request("GET", "/sync", access_token=user2_tok)
  81. self.assertEqual(channel.code, 200, channel.result)
  82. msgs = channel.json_body["to_device"]["events"]
  83. self.assertEqual(len(msgs), 2)
  84. for i in range(2):
  85. self.assertEqual(
  86. msgs[i],
  87. {"sender": user1, "type": "m.room_key_request", "content": {"idx": i}},
  88. )
  89. sync_token = channel.json_body["next_batch"]
  90. # ... time passes
  91. self.reactor.advance(1)
  92. # and we can send more messages
  93. chan = self.make_request(
  94. "PUT",
  95. "/_matrix/client/r0/sendToDevice/m.room_key_request/3",
  96. content={"messages": {user2: {"d2": {"idx": 3}}}},
  97. access_token=user1_tok,
  98. )
  99. self.assertEqual(chan.code, 200, chan.result)
  100. # ... which should arrive
  101. channel = self.make_request(
  102. "GET", f"/sync?since={sync_token}", access_token=user2_tok
  103. )
  104. self.assertEqual(channel.code, 200, channel.result)
  105. msgs = channel.json_body["to_device"]["events"]
  106. self.assertEqual(len(msgs), 1)
  107. self.assertEqual(
  108. msgs[0],
  109. {"sender": user1, "type": "m.room_key_request", "content": {"idx": 3}},
  110. )
  111. @override_config({"rc_key_requests": {"per_second": 10, "burst_count": 2}})
  112. def test_remote_room_key_request(self):
  113. """m.room_key_request has special-casing; test from remote user"""
  114. user2 = self.register_user("u2", "pass")
  115. user2_tok = self.login("u2", "pass", "d2")
  116. federation_registry = self.hs.get_federation_registry()
  117. # send three messages
  118. for i in range(3):
  119. self.get_success(
  120. federation_registry.on_edu(
  121. "m.direct_to_device",
  122. "remote_server",
  123. {
  124. "sender": "@user:remote_server",
  125. "type": "m.room_key_request",
  126. "messages": {user2: {"d2": {"idx": i}}},
  127. "message_id": f"{i}",
  128. },
  129. )
  130. )
  131. # now sync: we should get two of the three
  132. channel = self.make_request("GET", "/sync", access_token=user2_tok)
  133. self.assertEqual(channel.code, 200, channel.result)
  134. msgs = channel.json_body["to_device"]["events"]
  135. self.assertEqual(len(msgs), 2)
  136. for i in range(2):
  137. self.assertEqual(
  138. msgs[i],
  139. {
  140. "sender": "@user:remote_server",
  141. "type": "m.room_key_request",
  142. "content": {"idx": i},
  143. },
  144. )
  145. sync_token = channel.json_body["next_batch"]
  146. # ... time passes
  147. self.reactor.advance(1)
  148. # and we can send more messages
  149. self.get_success(
  150. federation_registry.on_edu(
  151. "m.direct_to_device",
  152. "remote_server",
  153. {
  154. "sender": "@user:remote_server",
  155. "type": "m.room_key_request",
  156. "messages": {user2: {"d2": {"idx": 3}}},
  157. "message_id": "3",
  158. },
  159. )
  160. )
  161. # ... which should arrive
  162. channel = self.make_request(
  163. "GET", f"/sync?since={sync_token}", access_token=user2_tok
  164. )
  165. self.assertEqual(channel.code, 200, channel.result)
  166. msgs = channel.json_body["to_device"]["events"]
  167. self.assertEqual(len(msgs), 1)
  168. self.assertEqual(
  169. msgs[0],
  170. {
  171. "sender": "@user:remote_server",
  172. "type": "m.room_key_request",
  173. "content": {"idx": 3},
  174. },
  175. )