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

Commit 652d166

Browse files
authored
Add missing type hints to tests.handlers. (#14680)
And do not allow untyped defs in tests.handlers.
1 parent 54c012c commit 652d166

22 files changed

+527
-378
lines changed

changelog.d/14680.misc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add missing type hints.

mypy.ini

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,7 @@ disallow_untyped_defs = True
9595
[mypy-tests.federation.transport.test_client]
9696
disallow_untyped_defs = True
9797

98-
[mypy-tests.handlers.test_sso]
99-
disallow_untyped_defs = True
100-
101-
[mypy-tests.handlers.test_user_directory]
98+
[mypy-tests.handlers.*]
10299
disallow_untyped_defs = True
103100

104101
[mypy-tests.metrics.test_background_process_metrics]

synapse/handlers/auth.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2031,7 +2031,7 @@ def __init__(self) -> None:
20312031
self.is_3pid_allowed_callbacks: List[IS_3PID_ALLOWED_CALLBACK] = []
20322032

20332033
# Mapping from login type to login parameters
2034-
self._supported_login_types: Dict[str, Iterable[str]] = {}
2034+
self._supported_login_types: Dict[str, Tuple[str, ...]] = {}
20352035

20362036
# Mapping from login type to auth checker callbacks
20372037
self.auth_checker_callbacks: Dict[str, List[CHECK_AUTH_CALLBACK]] = {}

tests/handlers/test_appservice.py

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
from synapse.handlers.appservice import ApplicationServicesHandler
3232
from synapse.rest.client import login, receipts, register, room, sendtodevice
3333
from synapse.server import HomeServer
34-
from synapse.types import RoomStreamToken
34+
from synapse.types import JsonDict, RoomStreamToken
3535
from synapse.util import Clock
3636
from synapse.util.stringutils import random_string
3737

@@ -44,7 +44,7 @@
4444
class AppServiceHandlerTestCase(unittest.TestCase):
4545
"""Tests the ApplicationServicesHandler."""
4646

47-
def setUp(self):
47+
def setUp(self) -> None:
4848
self.mock_store = Mock()
4949
self.mock_as_api = Mock()
5050
self.mock_scheduler = Mock()
@@ -61,7 +61,7 @@ def setUp(self):
6161
self.handler = ApplicationServicesHandler(hs)
6262
self.event_source = hs.get_event_sources()
6363

64-
def test_notify_interested_services(self):
64+
def test_notify_interested_services(self) -> None:
6565
interested_service = self._mkservice(is_interested_in_event=True)
6666
services = [
6767
self._mkservice(is_interested_in_event=False),
@@ -90,7 +90,7 @@ def test_notify_interested_services(self):
9090
interested_service, events=[event]
9191
)
9292

93-
def test_query_user_exists_unknown_user(self):
93+
def test_query_user_exists_unknown_user(self) -> None:
9494
user_id = "@someone:anywhere"
9595
services = [self._mkservice(is_interested_in_event=True)]
9696
services[0].is_interested_in_user.return_value = True
@@ -107,7 +107,7 @@ def test_query_user_exists_unknown_user(self):
107107

108108
self.mock_as_api.query_user.assert_called_once_with(services[0], user_id)
109109

110-
def test_query_user_exists_known_user(self):
110+
def test_query_user_exists_known_user(self) -> None:
111111
user_id = "@someone:anywhere"
112112
services = [self._mkservice(is_interested_in_event=True)]
113113
services[0].is_interested_in_user.return_value = True
@@ -127,7 +127,7 @@ def test_query_user_exists_known_user(self):
127127
"query_user called when it shouldn't have been.",
128128
)
129129

130-
def test_query_room_alias_exists(self):
130+
def test_query_room_alias_exists(self) -> None:
131131
room_alias_str = "#foo:bar"
132132
room_alias = Mock()
133133
room_alias.to_string.return_value = room_alias_str
@@ -157,15 +157,15 @@ def test_query_room_alias_exists(self):
157157
self.assertEqual(result.room_id, room_id)
158158
self.assertEqual(result.servers, servers)
159159

160-
def test_get_3pe_protocols_no_appservices(self):
160+
def test_get_3pe_protocols_no_appservices(self) -> None:
161161
self.mock_store.get_app_services.return_value = []
162162
response = self.successResultOf(
163163
defer.ensureDeferred(self.handler.get_3pe_protocols("my-protocol"))
164164
)
165165
self.mock_as_api.get_3pe_protocol.assert_not_called()
166166
self.assertEqual(response, {})
167167

168-
def test_get_3pe_protocols_no_protocols(self):
168+
def test_get_3pe_protocols_no_protocols(self) -> None:
169169
service = self._mkservice(False, [])
170170
self.mock_store.get_app_services.return_value = [service]
171171
response = self.successResultOf(
@@ -174,7 +174,7 @@ def test_get_3pe_protocols_no_protocols(self):
174174
self.mock_as_api.get_3pe_protocol.assert_not_called()
175175
self.assertEqual(response, {})
176176

177-
def test_get_3pe_protocols_protocol_no_response(self):
177+
def test_get_3pe_protocols_protocol_no_response(self) -> None:
178178
service = self._mkservice(False, ["my-protocol"])
179179
self.mock_store.get_app_services.return_value = [service]
180180
self.mock_as_api.get_3pe_protocol.return_value = make_awaitable(None)
@@ -186,7 +186,7 @@ def test_get_3pe_protocols_protocol_no_response(self):
186186
)
187187
self.assertEqual(response, {})
188188

189-
def test_get_3pe_protocols_select_one_protocol(self):
189+
def test_get_3pe_protocols_select_one_protocol(self) -> None:
190190
service = self._mkservice(False, ["my-protocol"])
191191
self.mock_store.get_app_services.return_value = [service]
192192
self.mock_as_api.get_3pe_protocol.return_value = make_awaitable(
@@ -202,7 +202,7 @@ def test_get_3pe_protocols_select_one_protocol(self):
202202
response, {"my-protocol": {"x-protocol-data": 42, "instances": []}}
203203
)
204204

205-
def test_get_3pe_protocols_one_protocol(self):
205+
def test_get_3pe_protocols_one_protocol(self) -> None:
206206
service = self._mkservice(False, ["my-protocol"])
207207
self.mock_store.get_app_services.return_value = [service]
208208
self.mock_as_api.get_3pe_protocol.return_value = make_awaitable(
@@ -218,7 +218,7 @@ def test_get_3pe_protocols_one_protocol(self):
218218
response, {"my-protocol": {"x-protocol-data": 42, "instances": []}}
219219
)
220220

221-
def test_get_3pe_protocols_multiple_protocol(self):
221+
def test_get_3pe_protocols_multiple_protocol(self) -> None:
222222
service_one = self._mkservice(False, ["my-protocol"])
223223
service_two = self._mkservice(False, ["other-protocol"])
224224
self.mock_store.get_app_services.return_value = [service_one, service_two]
@@ -237,11 +237,13 @@ def test_get_3pe_protocols_multiple_protocol(self):
237237
},
238238
)
239239

240-
def test_get_3pe_protocols_multiple_info(self):
240+
def test_get_3pe_protocols_multiple_info(self) -> None:
241241
service_one = self._mkservice(False, ["my-protocol"])
242242
service_two = self._mkservice(False, ["my-protocol"])
243243

244-
async def get_3pe_protocol(service, unusedProtocol):
244+
async def get_3pe_protocol(
245+
service: ApplicationService, protocol: str
246+
) -> Optional[JsonDict]:
245247
if service == service_one:
246248
return {
247249
"x-protocol-data": 42,
@@ -276,7 +278,7 @@ async def get_3pe_protocol(service, unusedProtocol):
276278
},
277279
)
278280

279-
def test_notify_interested_services_ephemeral(self):
281+
def test_notify_interested_services_ephemeral(self) -> None:
280282
"""
281283
Test sending ephemeral events to the appservice handler are scheduled
282284
to be pushed out to interested appservices, and that the stream ID is
@@ -306,7 +308,7 @@ def test_notify_interested_services_ephemeral(self):
306308
580,
307309
)
308310

309-
def test_notify_interested_services_ephemeral_out_of_order(self):
311+
def test_notify_interested_services_ephemeral_out_of_order(self) -> None:
310312
"""
311313
Test sending out of order ephemeral events to the appservice handler
312314
are ignored.
@@ -390,7 +392,7 @@ class ApplicationServicesHandlerSendEventsTestCase(unittest.HomeserverTestCase):
390392
receipts.register_servlets,
391393
]
392394

393-
def prepare(self, reactor: MemoryReactor, clock: Clock, hs: HomeServer):
395+
def prepare(self, reactor: MemoryReactor, clock: Clock, hs: HomeServer) -> None:
394396
self.hs = hs
395397
# Mock the ApplicationServiceScheduler's _TransactionController's send method so that
396398
# we can track any outgoing ephemeral events
@@ -417,7 +419,7 @@ def prepare(self, reactor: MemoryReactor, clock: Clock, hs: HomeServer):
417419
"exclusive_as_user", "password", self.exclusive_as_user_device_id
418420
)
419421

420-
def _notify_interested_services(self):
422+
def _notify_interested_services(self) -> None:
421423
# This is normally set in `notify_interested_services` but we need to call the
422424
# internal async version so the reactor gets pushed to completion.
423425
self.hs.get_application_service_handler().current_max += 1
@@ -443,7 +445,7 @@ def _notify_interested_services(self):
443445
)
444446
def test_match_interesting_room_members(
445447
self, interesting_user: str, should_notify: bool
446-
):
448+
) -> None:
447449
"""
448450
Test to make sure that a interesting user (local or remote) in the room is
449451
notified as expected when someone else in the room sends a message.
@@ -512,7 +514,9 @@ def test_match_interesting_room_members(
512514
else:
513515
self.send_mock.assert_not_called()
514516

515-
def test_application_services_receive_events_sent_by_interesting_local_user(self):
517+
def test_application_services_receive_events_sent_by_interesting_local_user(
518+
self,
519+
) -> None:
516520
"""
517521
Test to make sure that a messages sent from a local user can be interesting and
518522
picked up by the appservice.
@@ -568,7 +572,7 @@ def test_application_services_receive_events_sent_by_interesting_local_user(self
568572
self.assertEqual(events[0]["type"], "m.room.message")
569573
self.assertEqual(events[0]["sender"], alice)
570574

571-
def test_sending_read_receipt_batches_to_application_services(self):
575+
def test_sending_read_receipt_batches_to_application_services(self) -> None:
572576
"""Tests that a large batch of read receipts are sent correctly to
573577
interested application services.
574578
"""
@@ -644,7 +648,7 @@ def test_sending_read_receipt_batches_to_application_services(self):
644648
@unittest.override_config(
645649
{"experimental_features": {"msc2409_to_device_messages_enabled": True}}
646650
)
647-
def test_application_services_receive_local_to_device(self):
651+
def test_application_services_receive_local_to_device(self) -> None:
648652
"""
649653
Test that when a user sends a to-device message to another user
650654
that is an application service's user namespace, the
@@ -722,7 +726,7 @@ def test_application_services_receive_local_to_device(self):
722726
@unittest.override_config(
723727
{"experimental_features": {"msc2409_to_device_messages_enabled": True}}
724728
)
725-
def test_application_services_receive_bursts_of_to_device(self):
729+
def test_application_services_receive_bursts_of_to_device(self) -> None:
726730
"""
727731
Test that when a user sends >100 to-device messages at once, any
728732
interested AS's will receive them in separate transactions.
@@ -913,7 +917,7 @@ def test_application_service_receives_device_list_updates(
913917
experimental_feature_enabled: bool,
914918
as_supports_txn_extensions: bool,
915919
as_should_receive_device_list_updates: bool,
916-
):
920+
) -> None:
917921
"""
918922
Tests that an application service receives notice of changed device
919923
lists for a user, when a user changes their device lists.
@@ -1070,7 +1074,7 @@ def _set_up_devices_and_a_room(self) -> str:
10701074
and a room for the users to talk in.
10711075
"""
10721076

1073-
async def preparation():
1077+
async def preparation() -> None:
10741078
await self._add_otks_for_device(self._sender_user, self._sender_device, 42)
10751079
await self._add_fallback_key_for_device(
10761080
self._sender_user, self._sender_device, used=True

tests/handlers/test_cas.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ def test_required_attributes(self) -> None:
199199
)
200200

201201

202-
def _mock_request():
202+
def _mock_request() -> Mock:
203203
"""Returns a mock which will stand in as a SynapseRequest"""
204204
mock = Mock(
205205
spec=[

tests/handlers/test_directory.py

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import synapse.api.errors
2121
import synapse.rest.admin
2222
from synapse.api.constants import EventTypes
23+
from synapse.events import EventBase
2324
from synapse.rest.client import directory, login, room
2425
from synapse.server import HomeServer
2526
from synapse.types import JsonDict, RoomAlias, create_requester
@@ -201,7 +202,7 @@ def prepare(self, reactor: MemoryReactor, clock: Clock, hs: HomeServer) -> None:
201202
self.test_user_tok = self.login("user", "pass")
202203
self.helper.join(room=self.room_id, user=self.test_user, tok=self.test_user_tok)
203204

204-
def _create_alias(self, user) -> None:
205+
def _create_alias(self, user: str) -> None:
205206
# Create a new alias to this room.
206207
self.get_success(
207208
self.store.create_room_alias_association(
@@ -324,7 +325,7 @@ def _add_alias(self, alias: str) -> RoomAlias:
324325
)
325326
return room_alias
326327

327-
def _set_canonical_alias(self, content) -> None:
328+
def _set_canonical_alias(self, content: JsonDict) -> None:
328329
"""Configure the canonical alias state on the room."""
329330
self.helper.send_state(
330331
self.room_id,
@@ -333,13 +334,15 @@ def _set_canonical_alias(self, content) -> None:
333334
tok=self.admin_user_tok,
334335
)
335336

336-
def _get_canonical_alias(self):
337+
def _get_canonical_alias(self) -> EventBase:
337338
"""Get the canonical alias state of the room."""
338-
return self.get_success(
339+
result = self.get_success(
339340
self._storage_controllers.state.get_current_state_event(
340341
self.room_id, EventTypes.CanonicalAlias, ""
341342
)
342343
)
344+
assert result is not None
345+
return result
343346

344347
def test_remove_alias(self) -> None:
345348
"""Removing an alias that is the canonical alias should remove it there too."""
@@ -349,8 +352,8 @@ def test_remove_alias(self) -> None:
349352
)
350353

351354
data = self._get_canonical_alias()
352-
self.assertEqual(data["content"]["alias"], self.test_alias)
353-
self.assertEqual(data["content"]["alt_aliases"], [self.test_alias])
355+
self.assertEqual(data.content["alias"], self.test_alias)
356+
self.assertEqual(data.content["alt_aliases"], [self.test_alias])
354357

355358
# Finally, delete the alias.
356359
self.get_success(
@@ -360,8 +363,8 @@ def test_remove_alias(self) -> None:
360363
)
361364

362365
data = self._get_canonical_alias()
363-
self.assertNotIn("alias", data["content"])
364-
self.assertNotIn("alt_aliases", data["content"])
366+
self.assertNotIn("alias", data.content)
367+
self.assertNotIn("alt_aliases", data.content)
365368

366369
def test_remove_other_alias(self) -> None:
367370
"""Removing an alias listed as in alt_aliases should remove it there too."""
@@ -378,9 +381,9 @@ def test_remove_other_alias(self) -> None:
378381
)
379382

380383
data = self._get_canonical_alias()
381-
self.assertEqual(data["content"]["alias"], self.test_alias)
384+
self.assertEqual(data.content["alias"], self.test_alias)
382385
self.assertEqual(
383-
data["content"]["alt_aliases"], [self.test_alias, other_test_alias]
386+
data.content["alt_aliases"], [self.test_alias, other_test_alias]
384387
)
385388

386389
# Delete the second alias.
@@ -391,8 +394,8 @@ def test_remove_other_alias(self) -> None:
391394
)
392395

393396
data = self._get_canonical_alias()
394-
self.assertEqual(data["content"]["alias"], self.test_alias)
395-
self.assertEqual(data["content"]["alt_aliases"], [self.test_alias])
397+
self.assertEqual(data.content["alias"], self.test_alias)
398+
self.assertEqual(data.content["alt_aliases"], [self.test_alias])
396399

397400

398401
class TestCreateAliasACL(unittest.HomeserverTestCase):

0 commit comments

Comments
 (0)