Skip to content
This repository was archived by the owner on Apr 26, 2024. It is now read-only.

Commit 63fb220

Browse files
authored
Tests for to-device messages (#9965)
1 parent 27c375f commit 63fb220

File tree

2 files changed

+202
-0
lines changed

2 files changed

+202
-0
lines changed

changelog.d/9965.bugfix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix a bug introduced in Synapse 1.29.0 which caused `m.room_key_request` to-device messages sent from one user to another to be dropped.
Lines changed: 201 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,201 @@
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+
15+
from synapse.rest import admin
16+
from synapse.rest.client.v1 import login
17+
from synapse.rest.client.v2_alpha import sendtodevice, sync
18+
19+
from tests.unittest import HomeserverTestCase, override_config
20+
21+
22+
class SendToDeviceTestCase(HomeserverTestCase):
23+
servlets = [
24+
admin.register_servlets,
25+
login.register_servlets,
26+
sendtodevice.register_servlets,
27+
sync.register_servlets,
28+
]
29+
30+
def test_user_to_user(self):
31+
"""A to-device message from one user to another should get delivered"""
32+
33+
user1 = self.register_user("u1", "pass")
34+
user1_tok = self.login("u1", "pass", "d1")
35+
36+
user2 = self.register_user("u2", "pass")
37+
user2_tok = self.login("u2", "pass", "d2")
38+
39+
# send the message
40+
test_msg = {"foo": "bar"}
41+
chan = self.make_request(
42+
"PUT",
43+
"/_matrix/client/r0/sendToDevice/m.test/1234",
44+
content={"messages": {user2: {"d2": test_msg}}},
45+
access_token=user1_tok,
46+
)
47+
self.assertEqual(chan.code, 200, chan.result)
48+
49+
# check it appears
50+
channel = self.make_request("GET", "/sync", access_token=user2_tok)
51+
self.assertEqual(channel.code, 200, channel.result)
52+
expected_result = {
53+
"events": [
54+
{
55+
"sender": user1,
56+
"type": "m.test",
57+
"content": test_msg,
58+
}
59+
]
60+
}
61+
self.assertEqual(channel.json_body["to_device"], expected_result)
62+
63+
# it should re-appear if we do another sync
64+
channel = self.make_request("GET", "/sync", access_token=user2_tok)
65+
self.assertEqual(channel.code, 200, channel.result)
66+
self.assertEqual(channel.json_body["to_device"], expected_result)
67+
68+
# it should *not* appear if we do an incremental sync
69+
sync_token = channel.json_body["next_batch"]
70+
channel = self.make_request(
71+
"GET", f"/sync?since={sync_token}", access_token=user2_tok
72+
)
73+
self.assertEqual(channel.code, 200, channel.result)
74+
self.assertEqual(channel.json_body.get("to_device", {}).get("events", []), [])
75+
76+
@override_config({"rc_key_requests": {"per_second": 10, "burst_count": 2}})
77+
def test_local_room_key_request(self):
78+
"""m.room_key_request has special-casing; test from local user"""
79+
user1 = self.register_user("u1", "pass")
80+
user1_tok = self.login("u1", "pass", "d1")
81+
82+
user2 = self.register_user("u2", "pass")
83+
user2_tok = self.login("u2", "pass", "d2")
84+
85+
# send three messages
86+
for i in range(3):
87+
chan = self.make_request(
88+
"PUT",
89+
f"/_matrix/client/r0/sendToDevice/m.room_key_request/{i}",
90+
content={"messages": {user2: {"d2": {"idx": i}}}},
91+
access_token=user1_tok,
92+
)
93+
self.assertEqual(chan.code, 200, chan.result)
94+
95+
# now sync: we should get two of the three
96+
channel = self.make_request("GET", "/sync", access_token=user2_tok)
97+
self.assertEqual(channel.code, 200, channel.result)
98+
msgs = channel.json_body["to_device"]["events"]
99+
self.assertEqual(len(msgs), 2)
100+
for i in range(2):
101+
self.assertEqual(
102+
msgs[i],
103+
{"sender": user1, "type": "m.room_key_request", "content": {"idx": i}},
104+
)
105+
sync_token = channel.json_body["next_batch"]
106+
107+
# ... time passes
108+
self.reactor.advance(1)
109+
110+
# and we can send more messages
111+
chan = self.make_request(
112+
"PUT",
113+
"/_matrix/client/r0/sendToDevice/m.room_key_request/3",
114+
content={"messages": {user2: {"d2": {"idx": 3}}}},
115+
access_token=user1_tok,
116+
)
117+
self.assertEqual(chan.code, 200, chan.result)
118+
119+
# ... which should arrive
120+
channel = self.make_request(
121+
"GET", f"/sync?since={sync_token}", access_token=user2_tok
122+
)
123+
self.assertEqual(channel.code, 200, channel.result)
124+
msgs = channel.json_body["to_device"]["events"]
125+
self.assertEqual(len(msgs), 1)
126+
self.assertEqual(
127+
msgs[0],
128+
{"sender": user1, "type": "m.room_key_request", "content": {"idx": 3}},
129+
)
130+
131+
@override_config({"rc_key_requests": {"per_second": 10, "burst_count": 2}})
132+
def test_remote_room_key_request(self):
133+
"""m.room_key_request has special-casing; test from remote user"""
134+
user2 = self.register_user("u2", "pass")
135+
user2_tok = self.login("u2", "pass", "d2")
136+
137+
federation_registry = self.hs.get_federation_registry()
138+
139+
# send three messages
140+
for i in range(3):
141+
self.get_success(
142+
federation_registry.on_edu(
143+
"m.direct_to_device",
144+
"remote_server",
145+
{
146+
"sender": "@user:remote_server",
147+
"type": "m.room_key_request",
148+
"messages": {user2: {"d2": {"idx": i}}},
149+
"message_id": f"{i}",
150+
},
151+
)
152+
)
153+
154+
# now sync: we should get two of the three
155+
channel = self.make_request("GET", "/sync", access_token=user2_tok)
156+
self.assertEqual(channel.code, 200, channel.result)
157+
msgs = channel.json_body["to_device"]["events"]
158+
self.assertEqual(len(msgs), 2)
159+
for i in range(2):
160+
self.assertEqual(
161+
msgs[i],
162+
{
163+
"sender": "@user:remote_server",
164+
"type": "m.room_key_request",
165+
"content": {"idx": i},
166+
},
167+
)
168+
sync_token = channel.json_body["next_batch"]
169+
170+
# ... time passes
171+
self.reactor.advance(1)
172+
173+
# and we can send more messages
174+
self.get_success(
175+
federation_registry.on_edu(
176+
"m.direct_to_device",
177+
"remote_server",
178+
{
179+
"sender": "@user:remote_server",
180+
"type": "m.room_key_request",
181+
"messages": {user2: {"d2": {"idx": 3}}},
182+
"message_id": "3",
183+
},
184+
)
185+
)
186+
187+
# ... which should arrive
188+
channel = self.make_request(
189+
"GET", f"/sync?since={sync_token}", access_token=user2_tok
190+
)
191+
self.assertEqual(channel.code, 200, channel.result)
192+
msgs = channel.json_body["to_device"]["events"]
193+
self.assertEqual(len(msgs), 1)
194+
self.assertEqual(
195+
msgs[0],
196+
{
197+
"sender": "@user:remote_server",
198+
"type": "m.room_key_request",
199+
"content": {"idx": 3},
200+
},
201+
)

0 commit comments

Comments
 (0)