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

Add missing type hints to tests.handlers. #14680

Merged
merged 8 commits into from
Dec 16, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
12 changes: 7 additions & 5 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 Down Expand Up @@ -241,7 +241,9 @@ 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 @@ -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 Down Expand Up @@ -443,7 +445,7 @@ def _notify_interested_services(self) -> None:
)
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 @@ -915,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
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
40 changes: 26 additions & 14 deletions tests/handlers/test_e2e_room_keys.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@
import copy
from unittest import mock

from twisted.test.proto_helpers import MemoryReactor

from synapse.api.errors import SynapseError
from synapse.server import HomeServer
from synapse.util import Clock

from tests import unittest

Expand All @@ -39,10 +43,10 @@


class E2eRoomKeysHandlerTestCase(unittest.HomeserverTestCase):
def make_homeserver(self, reactor, clock):
def make_homeserver(self, reactor: MemoryReactor, clock: Clock) -> HomeServer:
return self.setup_test_homeserver(replication_layer=mock.Mock())

def prepare(self, reactor, clock, hs):
def prepare(self, reactor: MemoryReactor, clock: Clock, hs: HomeServer) -> None:
self.handler = hs.get_e2e_room_keys_handler()
self.local_user = "@boris:" + hs.hostname

Expand All @@ -69,7 +73,7 @@ def test_get_missing_version_info(self) -> None:

def test_create_version(self) -> None:
"""Check that we can create and then retrieve versions."""
res = self.get_success(
version = self.get_success(
self.handler.create_version(
self.local_user,
{
Expand All @@ -78,7 +82,7 @@ def test_create_version(self) -> None:
},
)
)
self.assertEqual(res, "1")
self.assertEqual(version, "1")
DMRobertson marked this conversation as resolved.
Show resolved Hide resolved

# check we can retrieve it as the current version
res = self.get_success(self.handler.get_version_info(self.local_user))
Expand Down Expand Up @@ -110,7 +114,7 @@ def test_create_version(self) -> None:
)

# upload a new one...
res = self.get_success(
version = self.get_success(
self.handler.create_version(
self.local_user,
{
Expand All @@ -119,7 +123,7 @@ def test_create_version(self) -> None:
},
)
)
self.assertEqual(res, "2")
self.assertEqual(version, "2")

# check we can retrieve it as the current version
res = self.get_success(self.handler.get_version_info(self.local_user))
Expand Down Expand Up @@ -271,7 +275,7 @@ def test_delete_missing_current_version(self) -> None:

def test_delete_version(self) -> None:
"""Check that we can create and then delete versions."""
res = self.get_success(
version = self.get_success(
self.handler.create_version(
self.local_user,
{
Expand All @@ -280,7 +284,7 @@ def test_delete_version(self) -> None:
},
)
)
self.assertEqual(res, "1")
self.assertEqual(version, "1")

# check we can delete it
self.get_success(self.handler.delete_version(self.local_user, "1"))
Expand Down Expand Up @@ -449,9 +453,11 @@ def test_upload_room_keys_merge(self) -> None:
self.handler.upload_room_keys(self.local_user, version, new_room_keys)
)

res = self.get_success(self.handler.get_room_keys(self.local_user, version))
res_keys = self.get_success(
self.handler.get_room_keys(self.local_user, version)
)
self.assertEqual(
res["rooms"]["!abc:matrix.org"]["sessions"]["c0ff33"]["session_data"],
res_keys["rooms"]["!abc:matrix.org"]["sessions"]["c0ff33"]["session_data"],
"SSBBTSBBIEZJU0gK",
)

Expand All @@ -465,9 +471,12 @@ def test_upload_room_keys_merge(self) -> None:
self.handler.upload_room_keys(self.local_user, version, new_room_keys)
)

res = self.get_success(self.handler.get_room_keys(self.local_user, version))
res_keys = self.get_success(
self.handler.get_room_keys(self.local_user, version)
)
self.assertEqual(
res["rooms"]["!abc:matrix.org"]["sessions"]["c0ff33"]["session_data"], "new"
res_keys["rooms"]["!abc:matrix.org"]["sessions"]["c0ff33"]["session_data"],
"new",
)

# the etag should NOT be equal now, since the key changed
Expand All @@ -483,9 +492,12 @@ def test_upload_room_keys_merge(self) -> None:
self.handler.upload_room_keys(self.local_user, version, new_room_keys)
)

res = self.get_success(self.handler.get_room_keys(self.local_user, version))
res_keys = self.get_success(
self.handler.get_room_keys(self.local_user, version)
)
self.assertEqual(
res["rooms"]["!abc:matrix.org"]["sessions"]["c0ff33"]["session_data"], "new"
res_keys["rooms"]["!abc:matrix.org"]["sessions"]["c0ff33"]["session_data"],
"new",
)

# the etag should be the same since the session did not change
Expand Down
2 changes: 1 addition & 1 deletion tests/handlers/test_federation.py
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,7 @@ def test_invite_by_user_ratelimit(self) -> None:
user_id = self.register_user("kermit", "test")
tok = self.login("kermit", "test")

def create_invite():
def create_invite() -> EventBase:
room_id = self.helper.create_room_as(room_creator=user_id, tok=tok)
room_version = self.get_success(self.store.get_room_version(room_id))
return event_from_pdu_json(
Expand Down
10 changes: 8 additions & 2 deletions tests/handlers/test_federation_event.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
from typing import Optional
from unittest import mock

from twisted.test.proto_helpers import MemoryReactor

from synapse.api.errors import AuthError, StoreError
from synapse.api.room_versions import RoomVersion
from synapse.event_auth import (
Expand All @@ -26,8 +28,10 @@
from synapse.logging.context import LoggingContext
from synapse.rest import admin
from synapse.rest.client import login, room
from synapse.server import HomeServer
from synapse.state.v2 import _mainline_sort, _reverse_topological_power_sort
from synapse.types import JsonDict
from synapse.util import Clock

from tests import unittest
from tests.test_utils import event_injection, make_awaitable
Expand All @@ -40,7 +44,7 @@ class FederationEventHandlerTests(unittest.FederatingHomeserverTestCase):
room.register_servlets,
]

def make_homeserver(self, reactor, clock):
def make_homeserver(self, reactor: MemoryReactor, clock: Clock) -> HomeServer:
# mock out the federation transport client
self.mock_federation_transport_client = mock.Mock(
spec=["get_room_state_ids", "get_room_state", "get_event", "backfill"]
Expand Down Expand Up @@ -165,7 +169,9 @@ def _test_process_pulled_event_with_missing_state(
)
else:

async def get_event(destination: str, event_id: str, timeout=None):
async def get_event(
destination: str, event_id: str, timeout: Optional[int] = None
) -> JsonDict:
self.assertEqual(destination, self.OTHER_SERVER_NAME)
self.assertEqual(event_id, prev_event.event_id)
return {"pdus": [prev_event.get_pdu_json()]}
Expand Down
8 changes: 6 additions & 2 deletions tests/handlers/test_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,16 @@
import logging
from typing import Tuple

from twisted.test.proto_helpers import MemoryReactor

from synapse.api.constants import EventTypes
from synapse.events import EventBase
from synapse.events.snapshot import EventContext
from synapse.rest import admin
from synapse.rest.client import login, room
from synapse.server import HomeServer
from synapse.types import create_requester
from synapse.util import Clock
from synapse.util.stringutils import random_string

from tests import unittest
Expand All @@ -35,7 +39,7 @@ class EventCreationTestCase(unittest.HomeserverTestCase):
room.register_servlets,
]

def prepare(self, reactor, clock, hs):
def prepare(self, reactor: MemoryReactor, clock: Clock, hs: HomeServer) -> None:
self.handler = self.hs.get_event_creation_handler()
self._persist_event_storage_controller = (
self.hs.get_storage_controllers().persistence
Expand Down Expand Up @@ -279,7 +283,7 @@ class ServerAclValidationTestCase(unittest.HomeserverTestCase):
room.register_servlets,
]

def prepare(self, reactor, clock, hs):
def prepare(self, reactor: MemoryReactor, clock: Clock, hs: HomeServer) -> None:
self.user_id = self.register_user("tester", "foobar")
self.access_token = self.login("tester", "foobar")
self.room_id = self.helper.create_room_as(self.user_id, tok=self.access_token)
Expand Down
Loading