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

Commit

Permalink
Add missing type hints to tests.handlers. (#14680)
Browse files Browse the repository at this point in the history
And do not allow untyped defs in tests.handlers.
  • Loading branch information
clokep authored Dec 16, 2022
1 parent 54c012c commit 652d166
Show file tree
Hide file tree
Showing 22 changed files with 527 additions and 378 deletions.
1 change: 1 addition & 0 deletions changelog.d/14680.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add missing type hints.
5 changes: 1 addition & 4 deletions mypy.ini
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,7 @@ disallow_untyped_defs = True
[mypy-tests.federation.transport.test_client]
disallow_untyped_defs = True

[mypy-tests.handlers.test_sso]
disallow_untyped_defs = True

[mypy-tests.handlers.test_user_directory]
[mypy-tests.handlers.*]
disallow_untyped_defs = True

[mypy-tests.metrics.test_background_process_metrics]
Expand Down
2 changes: 1 addition & 1 deletion synapse/handlers/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -2031,7 +2031,7 @@ def __init__(self) -> None:
self.is_3pid_allowed_callbacks: List[IS_3PID_ALLOWED_CALLBACK] = []

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

# Mapping from login type to auth checker callbacks
self.auth_checker_callbacks: Dict[str, List[CHECK_AUTH_CALLBACK]] = {}
Expand Down
54 changes: 29 additions & 25 deletions tests/handlers/test_appservice.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
from synapse.handlers.appservice import ApplicationServicesHandler
from synapse.rest.client import login, receipts, register, room, sendtodevice
from synapse.server import HomeServer
from synapse.types import RoomStreamToken
from synapse.types import JsonDict, RoomStreamToken
from synapse.util import Clock
from synapse.util.stringutils import random_string

Expand All @@ -44,7 +44,7 @@
class AppServiceHandlerTestCase(unittest.TestCase):
"""Tests the ApplicationServicesHandler."""

def setUp(self):
def setUp(self) -> None:
self.mock_store = Mock()
self.mock_as_api = Mock()
self.mock_scheduler = Mock()
Expand All @@ -61,7 +61,7 @@ def setUp(self):
self.handler = ApplicationServicesHandler(hs)
self.event_source = hs.get_event_sources()

def test_notify_interested_services(self):
def test_notify_interested_services(self) -> None:
interested_service = self._mkservice(is_interested_in_event=True)
services = [
self._mkservice(is_interested_in_event=False),
Expand Down Expand Up @@ -90,7 +90,7 @@ def test_notify_interested_services(self):
interested_service, events=[event]
)

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

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

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

def test_query_room_alias_exists(self):
def test_query_room_alias_exists(self) -> None:
room_alias_str = "#foo:bar"
room_alias = Mock()
room_alias.to_string.return_value = room_alias_str
Expand Down Expand Up @@ -157,15 +157,15 @@ def test_query_room_alias_exists(self):
self.assertEqual(result.room_id, room_id)
self.assertEqual(result.servers, servers)

def test_get_3pe_protocols_no_appservices(self):
def test_get_3pe_protocols_no_appservices(self) -> None:
self.mock_store.get_app_services.return_value = []
response = self.successResultOf(
defer.ensureDeferred(self.handler.get_3pe_protocols("my-protocol"))
)
self.mock_as_api.get_3pe_protocol.assert_not_called()
self.assertEqual(response, {})

def test_get_3pe_protocols_no_protocols(self):
def test_get_3pe_protocols_no_protocols(self) -> None:
service = self._mkservice(False, [])
self.mock_store.get_app_services.return_value = [service]
response = self.successResultOf(
Expand All @@ -174,7 +174,7 @@ def test_get_3pe_protocols_no_protocols(self):
self.mock_as_api.get_3pe_protocol.assert_not_called()
self.assertEqual(response, {})

def test_get_3pe_protocols_protocol_no_response(self):
def test_get_3pe_protocols_protocol_no_response(self) -> None:
service = self._mkservice(False, ["my-protocol"])
self.mock_store.get_app_services.return_value = [service]
self.mock_as_api.get_3pe_protocol.return_value = make_awaitable(None)
Expand All @@ -186,7 +186,7 @@ def test_get_3pe_protocols_protocol_no_response(self):
)
self.assertEqual(response, {})

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

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

def test_get_3pe_protocols_multiple_protocol(self):
def test_get_3pe_protocols_multiple_protocol(self) -> None:
service_one = self._mkservice(False, ["my-protocol"])
service_two = self._mkservice(False, ["other-protocol"])
self.mock_store.get_app_services.return_value = [service_one, service_two]
Expand All @@ -237,11 +237,13 @@ def test_get_3pe_protocols_multiple_protocol(self):
},
)

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

async def get_3pe_protocol(service, unusedProtocol):
async def get_3pe_protocol(
service: ApplicationService, protocol: str
) -> Optional[JsonDict]:
if service == service_one:
return {
"x-protocol-data": 42,
Expand Down Expand Up @@ -276,7 +278,7 @@ async def get_3pe_protocol(service, unusedProtocol):
},
)

def test_notify_interested_services_ephemeral(self):
def test_notify_interested_services_ephemeral(self) -> None:
"""
Test sending ephemeral events to the appservice handler are scheduled
to be pushed out to interested appservices, and that the stream ID is
Expand Down Expand Up @@ -306,7 +308,7 @@ def test_notify_interested_services_ephemeral(self):
580,
)

def test_notify_interested_services_ephemeral_out_of_order(self):
def test_notify_interested_services_ephemeral_out_of_order(self) -> None:
"""
Test sending out of order ephemeral events to the appservice handler
are ignored.
Expand Down Expand Up @@ -390,7 +392,7 @@ class ApplicationServicesHandlerSendEventsTestCase(unittest.HomeserverTestCase):
receipts.register_servlets,
]

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

def _notify_interested_services(self):
def _notify_interested_services(self) -> None:
# This is normally set in `notify_interested_services` but we need to call the
# internal async version so the reactor gets pushed to completion.
self.hs.get_application_service_handler().current_max += 1
Expand All @@ -443,7 +445,7 @@ def _notify_interested_services(self):
)
def test_match_interesting_room_members(
self, interesting_user: str, should_notify: bool
):
) -> None:
"""
Test to make sure that a interesting user (local or remote) in the room is
notified as expected when someone else in the room sends a message.
Expand Down Expand Up @@ -512,7 +514,9 @@ def test_match_interesting_room_members(
else:
self.send_mock.assert_not_called()

def test_application_services_receive_events_sent_by_interesting_local_user(self):
def test_application_services_receive_events_sent_by_interesting_local_user(
self,
) -> None:
"""
Test to make sure that a messages sent from a local user can be interesting and
picked up by the appservice.
Expand Down Expand Up @@ -568,7 +572,7 @@ def test_application_services_receive_events_sent_by_interesting_local_user(self
self.assertEqual(events[0]["type"], "m.room.message")
self.assertEqual(events[0]["sender"], alice)

def test_sending_read_receipt_batches_to_application_services(self):
def test_sending_read_receipt_batches_to_application_services(self) -> None:
"""Tests that a large batch of read receipts are sent correctly to
interested application services.
"""
Expand Down Expand Up @@ -644,7 +648,7 @@ def test_sending_read_receipt_batches_to_application_services(self):
@unittest.override_config(
{"experimental_features": {"msc2409_to_device_messages_enabled": True}}
)
def test_application_services_receive_local_to_device(self):
def test_application_services_receive_local_to_device(self) -> None:
"""
Test that when a user sends a to-device message to another user
that is an application service's user namespace, the
Expand Down Expand Up @@ -722,7 +726,7 @@ def test_application_services_receive_local_to_device(self):
@unittest.override_config(
{"experimental_features": {"msc2409_to_device_messages_enabled": True}}
)
def test_application_services_receive_bursts_of_to_device(self):
def test_application_services_receive_bursts_of_to_device(self) -> None:
"""
Test that when a user sends >100 to-device messages at once, any
interested AS's will receive them in separate transactions.
Expand Down Expand Up @@ -913,7 +917,7 @@ def test_application_service_receives_device_list_updates(
experimental_feature_enabled: bool,
as_supports_txn_extensions: bool,
as_should_receive_device_list_updates: bool,
):
) -> None:
"""
Tests that an application service receives notice of changed device
lists for a user, when a user changes their device lists.
Expand Down Expand Up @@ -1070,7 +1074,7 @@ def _set_up_devices_and_a_room(self) -> str:
and a room for the users to talk in.
"""

async def preparation():
async def preparation() -> None:
await self._add_otks_for_device(self._sender_user, self._sender_device, 42)
await self._add_fallback_key_for_device(
self._sender_user, self._sender_device, used=True
Expand Down
2 changes: 1 addition & 1 deletion tests/handlers/test_cas.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ def test_required_attributes(self) -> None:
)


def _mock_request():
def _mock_request() -> Mock:
"""Returns a mock which will stand in as a SynapseRequest"""
mock = Mock(
spec=[
Expand Down
27 changes: 15 additions & 12 deletions tests/handlers/test_directory.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import synapse.api.errors
import synapse.rest.admin
from synapse.api.constants import EventTypes
from synapse.events import EventBase
from synapse.rest.client import directory, login, room
from synapse.server import HomeServer
from synapse.types import JsonDict, RoomAlias, create_requester
Expand Down Expand Up @@ -201,7 +202,7 @@ def prepare(self, reactor: MemoryReactor, clock: Clock, hs: HomeServer) -> None:
self.test_user_tok = self.login("user", "pass")
self.helper.join(room=self.room_id, user=self.test_user, tok=self.test_user_tok)

def _create_alias(self, user) -> None:
def _create_alias(self, user: str) -> None:
# Create a new alias to this room.
self.get_success(
self.store.create_room_alias_association(
Expand Down Expand Up @@ -324,7 +325,7 @@ def _add_alias(self, alias: str) -> RoomAlias:
)
return room_alias

def _set_canonical_alias(self, content) -> None:
def _set_canonical_alias(self, content: JsonDict) -> None:
"""Configure the canonical alias state on the room."""
self.helper.send_state(
self.room_id,
Expand All @@ -333,13 +334,15 @@ def _set_canonical_alias(self, content) -> None:
tok=self.admin_user_tok,
)

def _get_canonical_alias(self):
def _get_canonical_alias(self) -> EventBase:
"""Get the canonical alias state of the room."""
return self.get_success(
result = self.get_success(
self._storage_controllers.state.get_current_state_event(
self.room_id, EventTypes.CanonicalAlias, ""
)
)
assert result is not None
return result

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

data = self._get_canonical_alias()
self.assertEqual(data["content"]["alias"], self.test_alias)
self.assertEqual(data["content"]["alt_aliases"], [self.test_alias])
self.assertEqual(data.content["alias"], self.test_alias)
self.assertEqual(data.content["alt_aliases"], [self.test_alias])

# Finally, delete the alias.
self.get_success(
Expand All @@ -360,8 +363,8 @@ def test_remove_alias(self) -> None:
)

data = self._get_canonical_alias()
self.assertNotIn("alias", data["content"])
self.assertNotIn("alt_aliases", data["content"])
self.assertNotIn("alias", data.content)
self.assertNotIn("alt_aliases", data.content)

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

data = self._get_canonical_alias()
self.assertEqual(data["content"]["alias"], self.test_alias)
self.assertEqual(data.content["alias"], self.test_alias)
self.assertEqual(
data["content"]["alt_aliases"], [self.test_alias, other_test_alias]
data.content["alt_aliases"], [self.test_alias, other_test_alias]
)

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

data = self._get_canonical_alias()
self.assertEqual(data["content"]["alias"], self.test_alias)
self.assertEqual(data["content"]["alt_aliases"], [self.test_alias])
self.assertEqual(data.content["alias"], self.test_alias)
self.assertEqual(data.content["alt_aliases"], [self.test_alias])


class TestCreateAliasACL(unittest.HomeserverTestCase):
Expand Down
Loading

0 comments on commit 652d166

Please sign in to comment.