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

Add type hints to tests files #12224

Merged
merged 3 commits into from
Mar 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions changelog.d/12224.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add type hints to tests files.
5 changes: 0 additions & 5 deletions mypy.ini
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,8 @@ exclude = (?x)
|tests/federation/transport/test_knocking.py
|tests/federation/transport/test_server.py
|tests/handlers/test_cas.py
|tests/handlers/test_directory.py
|tests/handlers/test_e2e_keys.py
|tests/handlers/test_federation.py
|tests/handlers/test_oidc.py
|tests/handlers/test_presence.py
|tests/handlers/test_profile.py
|tests/handlers/test_saml.py
|tests/handlers/test_typing.py
|tests/http/federation/test_matrix_federation_agent.py
|tests/http/federation/test_srv_resolver.py
Expand Down
84 changes: 47 additions & 37 deletions tests/handlers/test_directory.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,18 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from typing import Any, Awaitable, Callable, Dict
from unittest.mock import Mock

from twisted.test.proto_helpers import MemoryReactor

import synapse.api.errors
import synapse.rest.admin
from synapse.api.constants import EventTypes
from synapse.rest.client import directory, login, room
from synapse.types import RoomAlias, create_requester
from synapse.server import HomeServer
from synapse.types import JsonDict, RoomAlias, create_requester
from synapse.util import Clock

from tests import unittest
from tests.test_utils import make_awaitable
Expand All @@ -28,13 +32,15 @@
class DirectoryTestCase(unittest.HomeserverTestCase):
"""Tests the directory service."""

def make_homeserver(self, reactor, clock):
def make_homeserver(self, reactor: MemoryReactor, clock: Clock) -> HomeServer:
self.mock_federation = Mock()
self.mock_registry = Mock()

self.query_handlers = {}
self.query_handlers: Dict[str, Callable[[dict], Awaitable[JsonDict]]] = {}

def register_query_handler(query_type, handler):
def register_query_handler(
query_type: str, handler: Callable[[dict], Awaitable[JsonDict]]
) -> None:
self.query_handlers[query_type] = handler

self.mock_registry.register_query_handler = register_query_handler
Expand All @@ -54,7 +60,7 @@ def register_query_handler(query_type, handler):

return hs

def test_get_local_association(self):
def test_get_local_association(self) -> None:
self.get_success(
self.store.create_room_alias_association(
self.my_room, "!8765qwer:test", ["test"]
Expand All @@ -65,7 +71,7 @@ def test_get_local_association(self):

self.assertEqual({"room_id": "!8765qwer:test", "servers": ["test"]}, result)

def test_get_remote_association(self):
def test_get_remote_association(self) -> None:
self.mock_federation.make_query.return_value = make_awaitable(
{"room_id": "!8765qwer:test", "servers": ["test", "remote"]}
)
Expand All @@ -83,7 +89,7 @@ def test_get_remote_association(self):
ignore_backoff=True,
)

def test_incoming_fed_query(self):
def test_incoming_fed_query(self) -> None:
self.get_success(
self.store.create_room_alias_association(
self.your_room, "!8765asdf:test", ["test"]
Expand All @@ -105,7 +111,7 @@ class TestCreateAlias(unittest.HomeserverTestCase):
directory.register_servlets,
]

def prepare(self, reactor, clock, hs):
def prepare(self, reactor: MemoryReactor, clock: Clock, hs: HomeServer) -> None:
self.handler = hs.get_directory_handler()

# Create user
Expand All @@ -125,7 +131,7 @@ def prepare(self, reactor, clock, hs):
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 test_create_alias_joined_room(self):
def test_create_alias_joined_room(self) -> None:
"""A user can create an alias for a room they're in."""
self.get_success(
self.handler.create_association(
Expand All @@ -135,7 +141,7 @@ def test_create_alias_joined_room(self):
)
)

def test_create_alias_other_room(self):
def test_create_alias_other_room(self) -> None:
"""A user cannot create an alias for a room they're NOT in."""
other_room_id = self.helper.create_room_as(
self.admin_user, tok=self.admin_user_tok
Expand All @@ -150,7 +156,7 @@ def test_create_alias_other_room(self):
synapse.api.errors.SynapseError,
)

def test_create_alias_admin(self):
def test_create_alias_admin(self) -> None:
"""An admin can create an alias for a room they're NOT in."""
other_room_id = self.helper.create_room_as(
self.test_user, tok=self.test_user_tok
Expand All @@ -173,7 +179,7 @@ class TestDeleteAlias(unittest.HomeserverTestCase):
directory.register_servlets,
]

def prepare(self, reactor, clock, hs):
def prepare(self, reactor: MemoryReactor, clock: Clock, hs: HomeServer) -> None:
self.store = hs.get_datastores().main
self.handler = hs.get_directory_handler()
self.state_handler = hs.get_state_handler()
Expand All @@ -195,15 +201,15 @@ def prepare(self, reactor, clock, hs):
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):
def _create_alias(self, user) -> None:
# Create a new alias to this room.
self.get_success(
self.store.create_room_alias_association(
self.room_alias, self.room_id, ["test"], user
)
)

def test_delete_alias_not_allowed(self):
def test_delete_alias_not_allowed(self) -> None:
"""A user that doesn't meet the expected guidelines cannot delete an alias."""
self._create_alias(self.admin_user)
self.get_failure(
Expand All @@ -213,7 +219,7 @@ def test_delete_alias_not_allowed(self):
synapse.api.errors.AuthError,
)

def test_delete_alias_creator(self):
def test_delete_alias_creator(self) -> None:
"""An alias creator can delete their own alias."""
# Create an alias from a different user.
self._create_alias(self.test_user)
Expand All @@ -232,7 +238,7 @@ def test_delete_alias_creator(self):
synapse.api.errors.SynapseError,
)

def test_delete_alias_admin(self):
def test_delete_alias_admin(self) -> None:
"""A server admin can delete an alias created by another user."""
# Create an alias from a different user.
self._create_alias(self.test_user)
Expand All @@ -251,7 +257,7 @@ def test_delete_alias_admin(self):
synapse.api.errors.SynapseError,
)

def test_delete_alias_sufficient_power(self):
def test_delete_alias_sufficient_power(self) -> None:
"""A user with a sufficient power level should be able to delete an alias."""
self._create_alias(self.admin_user)

Expand Down Expand Up @@ -288,7 +294,7 @@ class CanonicalAliasTestCase(unittest.HomeserverTestCase):
directory.register_servlets,
]

def prepare(self, reactor, clock, hs):
def prepare(self, reactor: MemoryReactor, clock: Clock, hs: HomeServer) -> None:
self.store = hs.get_datastores().main
self.handler = hs.get_directory_handler()
self.state_handler = hs.get_state_handler()
Expand Down Expand Up @@ -317,7 +323,7 @@ def _add_alias(self, alias: str) -> RoomAlias:
)
return room_alias

def _set_canonical_alias(self, content):
def _set_canonical_alias(self, content) -> None:
"""Configure the canonical alias state on the room."""
self.helper.send_state(
self.room_id,
Expand All @@ -334,7 +340,7 @@ def _get_canonical_alias(self):
)
)

def test_remove_alias(self):
def test_remove_alias(self) -> None:
"""Removing an alias that is the canonical alias should remove it there too."""
# Set this new alias as the canonical alias for this room
self._set_canonical_alias(
Expand All @@ -356,7 +362,7 @@ def test_remove_alias(self):
self.assertNotIn("alias", data["content"])
self.assertNotIn("alt_aliases", data["content"])

def test_remove_other_alias(self):
def test_remove_other_alias(self) -> None:
"""Removing an alias listed as in alt_aliases should remove it there too."""
# Create a second alias.
other_test_alias = "#test2:test"
Expand Down Expand Up @@ -393,7 +399,7 @@ class TestCreateAliasACL(unittest.HomeserverTestCase):

servlets = [directory.register_servlets, room.register_servlets]

def default_config(self):
def default_config(self) -> Dict[str, Any]:
config = super().default_config()

# Add custom alias creation rules to the config.
Expand All @@ -403,7 +409,7 @@ def default_config(self):

return config

def test_denied(self):
def test_denied(self) -> None:
room_id = self.helper.create_room_as(self.user_id)

channel = self.make_request(
Expand All @@ -413,7 +419,7 @@ def test_denied(self):
)
self.assertEqual(403, channel.code, channel.result)

def test_allowed(self):
def test_allowed(self) -> None:
room_id = self.helper.create_room_as(self.user_id)

channel = self.make_request(
Expand All @@ -423,7 +429,7 @@ def test_allowed(self):
)
self.assertEqual(200, channel.code, channel.result)

def test_denied_during_creation(self):
def test_denied_during_creation(self) -> None:
"""A room alias that is not allowed should be rejected during creation."""
# Invalid room alias.
self.helper.create_room_as(
Expand All @@ -432,7 +438,7 @@ def test_denied_during_creation(self):
extra_content={"room_alias_name": "foo"},
)

def test_allowed_during_creation(self):
def test_allowed_during_creation(self) -> None:
"""A valid room alias should be allowed during creation."""
room_id = self.helper.create_room_as(
self.user_id,
Expand All @@ -459,7 +465,7 @@ class TestCreatePublishedRoomACL(unittest.HomeserverTestCase):
data = {"room_alias_name": "unofficial_test"}
allowed_localpart = "allowed"

def default_config(self):
def default_config(self) -> Dict[str, Any]:
config = super().default_config()

# Add custom room list publication rules to the config.
Expand All @@ -474,7 +480,9 @@ def default_config(self):

return config

def prepare(self, reactor, clock, hs):
def prepare(
self, reactor: MemoryReactor, clock: Clock, hs: HomeServer
) -> HomeServer:
self.allowed_user_id = self.register_user(self.allowed_localpart, "pass")
self.allowed_access_token = self.login(self.allowed_localpart, "pass")

Expand All @@ -483,7 +491,7 @@ def prepare(self, reactor, clock, hs):

return hs

def test_denied_without_publication_permission(self):
def test_denied_without_publication_permission(self) -> None:
"""
Try to create a room, register an alias for it, and publish it,
as a user without permission to publish rooms.
Expand All @@ -497,7 +505,7 @@ def test_denied_without_publication_permission(self):
expect_code=403,
)

def test_allowed_when_creating_private_room(self):
def test_allowed_when_creating_private_room(self) -> None:
"""
Try to create a room, register an alias for it, and NOT publish it,
as a user without permission to publish rooms.
Expand All @@ -511,7 +519,7 @@ def test_allowed_when_creating_private_room(self):
expect_code=200,
)

def test_allowed_with_publication_permission(self):
def test_allowed_with_publication_permission(self) -> None:
"""
Try to create a room, register an alias for it, and publish it,
as a user WITH permission to publish rooms.
Expand All @@ -525,7 +533,7 @@ def test_allowed_with_publication_permission(self):
expect_code=200,
)

def test_denied_publication_with_invalid_alias(self):
def test_denied_publication_with_invalid_alias(self) -> None:
"""
Try to create a room, register an alias for it, and publish it,
as a user WITH permission to publish rooms.
Expand All @@ -538,7 +546,7 @@ def test_denied_publication_with_invalid_alias(self):
expect_code=403,
)

def test_can_create_as_private_room_after_rejection(self):
def test_can_create_as_private_room_after_rejection(self) -> None:
"""
After failing to publish a room with an alias as a user without publish permission,
retry as the same user, but without publishing the room.
Expand All @@ -549,7 +557,7 @@ def test_can_create_as_private_room_after_rejection(self):
self.test_denied_without_publication_permission()
self.test_allowed_when_creating_private_room()

def test_can_create_with_permission_after_rejection(self):
def test_can_create_with_permission_after_rejection(self) -> None:
"""
After failing to publish a room with an alias as a user without publish permission,
retry as someone with permission, using the same alias.
Expand All @@ -566,7 +574,9 @@ class TestRoomListSearchDisabled(unittest.HomeserverTestCase):

servlets = [directory.register_servlets, room.register_servlets]

def prepare(self, reactor, clock, hs):
def prepare(
self, reactor: MemoryReactor, clock: Clock, hs: HomeServer
) -> HomeServer:
room_id = self.helper.create_room_as(self.user_id)

channel = self.make_request(
Expand All @@ -579,7 +589,7 @@ def prepare(self, reactor, clock, hs):

return hs

def test_disabling_room_list(self):
def test_disabling_room_list(self) -> None:
self.room_list_handler.enable_room_list_search = True
self.directory_handler.enable_room_list_search = True

Expand Down
Loading