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

Commit 54e74cc

Browse files
authored
Add type hints to tests/rest/client (#12072)
1 parent 2cc5ea9 commit 54e74cc

11 files changed

+160
-102
lines changed

changelog.d/12072.misc

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add type hints to `tests/rest/client`.

tests/rest/client/test_consent.py

+12-7
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,16 @@
1313
# limitations under the License.
1414

1515
import os
16+
from http import HTTPStatus
17+
18+
from twisted.test.proto_helpers import MemoryReactor
1619

1720
import synapse.rest.admin
1821
from synapse.api.urls import ConsentURIBuilder
1922
from synapse.rest.client import login, room
2023
from synapse.rest.consent import consent_resource
24+
from synapse.server import HomeServer
25+
from synapse.util import Clock
2126

2227
from tests import unittest
2328
from tests.server import FakeSite, make_request
@@ -32,7 +37,7 @@ class ConsentResourceTestCase(unittest.HomeserverTestCase):
3237
user_id = True
3338
hijack_auth = False
3439

35-
def make_homeserver(self, reactor, clock):
40+
def make_homeserver(self, reactor: MemoryReactor, clock: Clock) -> HomeServer:
3641

3742
config = self.default_config()
3843
config["form_secret"] = "123abc"
@@ -56,7 +61,7 @@ def make_homeserver(self, reactor, clock):
5661
hs = self.setup_test_homeserver(config=config)
5762
return hs
5863

59-
def test_render_public_consent(self):
64+
def test_render_public_consent(self) -> None:
6065
"""You can observe the terms form without specifying a user"""
6166
resource = consent_resource.ConsentResource(self.hs)
6267
channel = make_request(
@@ -66,9 +71,9 @@ def test_render_public_consent(self):
6671
"/consent?v=1",
6772
shorthand=False,
6873
)
69-
self.assertEqual(channel.code, 200)
74+
self.assertEqual(channel.code, HTTPStatus.OK)
7075

71-
def test_accept_consent(self):
76+
def test_accept_consent(self) -> None:
7277
"""
7378
A user can use the consent form to accept the terms.
7479
"""
@@ -92,7 +97,7 @@ def test_accept_consent(self):
9297
access_token=access_token,
9398
shorthand=False,
9499
)
95-
self.assertEqual(channel.code, 200)
100+
self.assertEqual(channel.code, HTTPStatus.OK)
96101

97102
# Get the version from the body, and whether we've consented
98103
version, consented = channel.result["body"].decode("ascii").split(",")
@@ -107,7 +112,7 @@ def test_accept_consent(self):
107112
access_token=access_token,
108113
shorthand=False,
109114
)
110-
self.assertEqual(channel.code, 200)
115+
self.assertEqual(channel.code, HTTPStatus.OK)
111116

112117
# Fetch the consent page, to get the consent version -- it should have
113118
# changed
@@ -119,7 +124,7 @@ def test_accept_consent(self):
119124
access_token=access_token,
120125
shorthand=False,
121126
)
122-
self.assertEqual(channel.code, 200)
127+
self.assertEqual(channel.code, HTTPStatus.OK)
123128

124129
# Get the version from the body, and check that it's the version we
125130
# agreed to, and that we've consented to it.

tests/rest/client/test_device_lists.py

+10-6
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
14+
from http import HTTPStatus
15+
1416
from synapse.rest import admin, devices, room, sync
1517
from synapse.rest.client import account, login, register
1618

@@ -30,7 +32,7 @@ class DeviceListsTestCase(unittest.HomeserverTestCase):
3032
devices.register_servlets,
3133
]
3234

33-
def test_receiving_local_device_list_changes(self):
35+
def test_receiving_local_device_list_changes(self) -> None:
3436
"""Tests that a local users that share a room receive each other's device list
3537
changes.
3638
"""
@@ -84,7 +86,7 @@ def test_receiving_local_device_list_changes(self):
8486
},
8587
access_token=alice_access_token,
8688
)
87-
self.assertEqual(channel.code, 200, channel.json_body)
89+
self.assertEqual(channel.code, HTTPStatus.OK, channel.json_body)
8890

8991
# Check that bob's incremental sync contains the updated device list.
9092
# If not, the client would only receive the device list update on the
@@ -97,7 +99,7 @@ def test_receiving_local_device_list_changes(self):
9799
)
98100
self.assertIn(alice_user_id, changed_device_lists, bob_sync_channel.json_body)
99101

100-
def test_not_receiving_local_device_list_changes(self):
102+
def test_not_receiving_local_device_list_changes(self) -> None:
101103
"""Tests a local users DO NOT receive device updates from each other if they do not
102104
share a room.
103105
"""
@@ -119,7 +121,7 @@ def test_not_receiving_local_device_list_changes(self):
119121
"/sync",
120122
access_token=bob_access_token,
121123
)
122-
self.assertEqual(channel.code, 200, channel.json_body)
124+
self.assertEqual(channel.code, HTTPStatus.OK, channel.json_body)
123125
next_batch_token = channel.json_body["next_batch"]
124126

125127
# ...and then an incremental sync. This should block until the sync stream is woken up,
@@ -141,11 +143,13 @@ def test_not_receiving_local_device_list_changes(self):
141143
},
142144
access_token=alice_access_token,
143145
)
144-
self.assertEqual(channel.code, 200, channel.json_body)
146+
self.assertEqual(channel.code, HTTPStatus.OK, channel.json_body)
145147

146148
# Check that bob's incremental sync does not contain the updated device list.
147149
bob_sync_channel.await_result()
148-
self.assertEqual(bob_sync_channel.code, 200, bob_sync_channel.json_body)
150+
self.assertEqual(
151+
bob_sync_channel.code, HTTPStatus.OK, bob_sync_channel.json_body
152+
)
149153

150154
changed_device_lists = bob_sync_channel.json_body.get("device_lists", {}).get(
151155
"changed", []

tests/rest/client/test_ephemeral_message.py

+14-5
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,16 @@
1111
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
14+
from http import HTTPStatus
15+
16+
from twisted.test.proto_helpers import MemoryReactor
17+
1418
from synapse.api.constants import EventContentFields, EventTypes
1519
from synapse.rest import admin
1620
from synapse.rest.client import room
21+
from synapse.server import HomeServer
22+
from synapse.types import JsonDict
23+
from synapse.util import Clock
1724

1825
from tests import unittest
1926

@@ -27,18 +34,18 @@ class EphemeralMessageTestCase(unittest.HomeserverTestCase):
2734
room.register_servlets,
2835
]
2936

30-
def make_homeserver(self, reactor, clock):
37+
def make_homeserver(self, reactor: MemoryReactor, clock: Clock) -> HomeServer:
3138
config = self.default_config()
3239

3340
config["enable_ephemeral_messages"] = True
3441

3542
self.hs = self.setup_test_homeserver(config=config)
3643
return self.hs
3744

38-
def prepare(self, reactor, clock, homeserver):
45+
def prepare(self, reactor: MemoryReactor, clock: Clock, hs: HomeServer) -> None:
3946
self.room_id = self.helper.create_room_as(self.user_id)
4047

41-
def test_message_expiry_no_delay(self):
48+
def test_message_expiry_no_delay(self) -> None:
4249
"""Tests that sending a message sent with a m.self_destruct_after field set to the
4350
past results in that event being deleted right away.
4451
"""
@@ -61,7 +68,7 @@ def test_message_expiry_no_delay(self):
6168
event_content = self.get_event(self.room_id, event_id)["content"]
6269
self.assertFalse(bool(event_content), event_content)
6370

64-
def test_message_expiry_delay(self):
71+
def test_message_expiry_delay(self) -> None:
6572
"""Tests that sending a message with a m.self_destruct_after field set to the
6673
future results in that event not being deleted right away, but advancing the
6774
clock to after that expiry timestamp causes the event to be deleted.
@@ -89,7 +96,9 @@ def test_message_expiry_delay(self):
8996
event_content = self.get_event(self.room_id, event_id)["content"]
9097
self.assertFalse(bool(event_content), event_content)
9198

92-
def get_event(self, room_id, event_id, expected_code=200):
99+
def get_event(
100+
self, room_id: str, event_id: str, expected_code: int = HTTPStatus.OK
101+
) -> JsonDict:
93102
url = "/_matrix/client/r0/rooms/%s/event/%s" % (room_id, event_id)
94103

95104
channel = self.make_request("GET", url)

tests/rest/client/test_identity.py

+9-4
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,14 @@
1313
# limitations under the License.
1414

1515
import json
16+
from http import HTTPStatus
17+
18+
from twisted.test.proto_helpers import MemoryReactor
1619

1720
import synapse.rest.admin
1821
from synapse.rest.client import login, room
22+
from synapse.server import HomeServer
23+
from synapse.util import Clock
1924

2025
from tests import unittest
2126

@@ -28,22 +33,22 @@ class IdentityTestCase(unittest.HomeserverTestCase):
2833
login.register_servlets,
2934
]
3035

31-
def make_homeserver(self, reactor, clock):
36+
def make_homeserver(self, reactor: MemoryReactor, clock: Clock) -> HomeServer:
3237

3338
config = self.default_config()
3439
config["enable_3pid_lookup"] = False
3540
self.hs = self.setup_test_homeserver(config=config)
3641

3742
return self.hs
3843

39-
def test_3pid_lookup_disabled(self):
44+
def test_3pid_lookup_disabled(self) -> None:
4045
self.hs.config.registration.enable_3pid_lookup = False
4146

4247
self.register_user("kermit", "monkey")
4348
tok = self.login("kermit", "monkey")
4449

4550
channel = self.make_request(b"POST", "/createRoom", b"{}", access_token=tok)
46-
self.assertEquals(channel.result["code"], b"200", channel.result)
51+
self.assertEqual(channel.code, HTTPStatus.OK, channel.result)
4752
room_id = channel.json_body["room_id"]
4853

4954
params = {
@@ -56,4 +61,4 @@ def test_3pid_lookup_disabled(self):
5661
channel = self.make_request(
5762
b"POST", request_url, request_data, access_token=tok
5863
)
59-
self.assertEquals(channel.result["code"], b"403", channel.result)
64+
self.assertEqual(channel.code, HTTPStatus.FORBIDDEN, channel.result)

tests/rest/client/test_keys.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class KeyQueryTestCase(unittest.HomeserverTestCase):
2828
login.register_servlets,
2929
]
3030

31-
def test_rejects_device_id_ice_key_outside_of_list(self):
31+
def test_rejects_device_id_ice_key_outside_of_list(self) -> None:
3232
self.register_user("alice", "wonderland")
3333
alice_token = self.login("alice", "wonderland")
3434
bob = self.register_user("bob", "uncle")
@@ -49,7 +49,7 @@ def test_rejects_device_id_ice_key_outside_of_list(self):
4949
channel.result,
5050
)
5151

52-
def test_rejects_device_key_given_as_map_to_bool(self):
52+
def test_rejects_device_key_given_as_map_to_bool(self) -> None:
5353
self.register_user("alice", "wonderland")
5454
alice_token = self.login("alice", "wonderland")
5555
bob = self.register_user("bob", "uncle")
@@ -73,7 +73,7 @@ def test_rejects_device_key_given_as_map_to_bool(self):
7373
channel.result,
7474
)
7575

76-
def test_requires_device_key(self):
76+
def test_requires_device_key(self) -> None:
7777
"""`device_keys` is required. We should complain if it's missing."""
7878
self.register_user("alice", "wonderland")
7979
alice_token = self.login("alice", "wonderland")

0 commit comments

Comments
 (0)