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

Commit

Permalink
Add some type hints to the tests.handlers module. (#12207)
Browse files Browse the repository at this point in the history
  • Loading branch information
clokep authored Mar 11, 2022
1 parent bc9dff1 commit e10a2fe
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 61 deletions.
2 changes: 1 addition & 1 deletion changelog.d/12108.misc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Add type hints to `tests/rest/client`.
Add type hints to tests files.
2 changes: 1 addition & 1 deletion changelog.d/12146.misc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Add type hints to `tests/rest`.
Add type hints to tests files.
1 change: 1 addition & 0 deletions changelog.d/12207.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add type hints to tests files.
18 changes: 11 additions & 7 deletions tests/handlers/test_admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,15 @@
from collections import Counter
from unittest.mock import Mock

from twisted.test.proto_helpers import MemoryReactor

import synapse.rest.admin
import synapse.storage
from synapse.api.constants import EventTypes, JoinRules
from synapse.api.room_versions import RoomVersions
from synapse.rest.client import knock, login, room
from synapse.server import HomeServer
from synapse.util import Clock

from tests import unittest

Expand All @@ -32,7 +36,7 @@ class ExfiltrateData(unittest.HomeserverTestCase):
knock.register_servlets,
]

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

self.user1 = self.register_user("user1", "password")
Expand All @@ -41,7 +45,7 @@ def prepare(self, reactor, clock, hs):
self.user2 = self.register_user("user2", "password")
self.token2 = self.login("user2", "password")

def test_single_public_joined_room(self):
def test_single_public_joined_room(self) -> None:
"""Test that we write *all* events for a public room"""
room_id = self.helper.create_room_as(
self.user1, tok=self.token1, is_public=True
Expand Down Expand Up @@ -74,7 +78,7 @@ def test_single_public_joined_room(self):
self.assertEqual(counter[(EventTypes.Member, self.user1)], 1)
self.assertEqual(counter[(EventTypes.Member, self.user2)], 1)

def test_single_private_joined_room(self):
def test_single_private_joined_room(self) -> None:
"""Tests that we correctly write state when we can't see all events in
a room.
"""
Expand Down Expand Up @@ -112,7 +116,7 @@ def test_single_private_joined_room(self):
self.assertEqual(counter[(EventTypes.Member, self.user1)], 1)
self.assertEqual(counter[(EventTypes.Member, self.user2)], 1)

def test_single_left_room(self):
def test_single_left_room(self) -> None:
"""Tests that we don't see events in the room after we leave."""
room_id = self.helper.create_room_as(self.user1, tok=self.token1)
self.helper.send(room_id, body="Hello!", tok=self.token1)
Expand Down Expand Up @@ -144,7 +148,7 @@ def test_single_left_room(self):
self.assertEqual(counter[(EventTypes.Member, self.user1)], 1)
self.assertEqual(counter[(EventTypes.Member, self.user2)], 2)

def test_single_left_rejoined_private_room(self):
def test_single_left_rejoined_private_room(self) -> None:
"""Tests that see the correct events in private rooms when we
repeatedly join and leave.
"""
Expand Down Expand Up @@ -185,7 +189,7 @@ def test_single_left_rejoined_private_room(self):
self.assertEqual(counter[(EventTypes.Member, self.user1)], 1)
self.assertEqual(counter[(EventTypes.Member, self.user2)], 3)

def test_invite(self):
def test_invite(self) -> None:
"""Tests that pending invites get handled correctly."""
room_id = self.helper.create_room_as(self.user1, tok=self.token1)
self.helper.send(room_id, body="Hello!", tok=self.token1)
Expand All @@ -204,7 +208,7 @@ def test_invite(self):
self.assertEqual(args[1].content["membership"], "invite")
self.assertTrue(args[2]) # Assert there is at least one bit of state

def test_knock(self):
def test_knock(self) -> None:
"""Tests that knock get handled correctly."""
# create a knockable v7 room
room_id = self.helper.create_room_as(
Expand Down
34 changes: 19 additions & 15 deletions tests/handlers/test_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,12 @@

import pymacaroons

from twisted.test.proto_helpers import MemoryReactor

from synapse.api.errors import AuthError, ResourceLimitError
from synapse.rest import admin
from synapse.server import HomeServer
from synapse.util import Clock

from tests import unittest
from tests.test_utils import make_awaitable
Expand All @@ -27,7 +31,7 @@ class AuthTestCase(unittest.HomeserverTestCase):
admin.register_servlets,
]

def prepare(self, reactor, clock, hs):
def prepare(self, reactor: MemoryReactor, clock: Clock, hs: HomeServer) -> None:
self.auth_handler = hs.get_auth_handler()
self.macaroon_generator = hs.get_macaroon_generator()

Expand All @@ -42,23 +46,23 @@ def prepare(self, reactor, clock, hs):

self.user1 = self.register_user("a_user", "pass")

def test_macaroon_caveats(self):
def test_macaroon_caveats(self) -> None:
token = self.macaroon_generator.generate_guest_access_token("a_user")
macaroon = pymacaroons.Macaroon.deserialize(token)

def verify_gen(caveat):
def verify_gen(caveat: str) -> bool:
return caveat == "gen = 1"

def verify_user(caveat):
def verify_user(caveat: str) -> bool:
return caveat == "user_id = a_user"

def verify_type(caveat):
def verify_type(caveat: str) -> bool:
return caveat == "type = access"

def verify_nonce(caveat):
def verify_nonce(caveat: str) -> bool:
return caveat.startswith("nonce =")

def verify_guest(caveat):
def verify_guest(caveat: str) -> bool:
return caveat == "guest = true"

v = pymacaroons.Verifier()
Expand All @@ -69,7 +73,7 @@ def verify_guest(caveat):
v.satisfy_general(verify_guest)
v.verify(macaroon, self.hs.config.key.macaroon_secret_key)

def test_short_term_login_token_gives_user_id(self):
def test_short_term_login_token_gives_user_id(self) -> None:
token = self.macaroon_generator.generate_short_term_login_token(
self.user1, "", duration_in_ms=5000
)
Expand All @@ -84,15 +88,15 @@ def test_short_term_login_token_gives_user_id(self):
AuthError,
)

def test_short_term_login_token_gives_auth_provider(self):
def test_short_term_login_token_gives_auth_provider(self) -> None:
token = self.macaroon_generator.generate_short_term_login_token(
self.user1, auth_provider_id="my_idp"
)
res = self.get_success(self.auth_handler.validate_short_term_login_token(token))
self.assertEqual(self.user1, res.user_id)
self.assertEqual("my_idp", res.auth_provider_id)

def test_short_term_login_token_cannot_replace_user_id(self):
def test_short_term_login_token_cannot_replace_user_id(self) -> None:
token = self.macaroon_generator.generate_short_term_login_token(
self.user1, "", duration_in_ms=5000
)
Expand All @@ -112,7 +116,7 @@ def test_short_term_login_token_cannot_replace_user_id(self):
AuthError,
)

def test_mau_limits_disabled(self):
def test_mau_limits_disabled(self) -> None:
self.auth_blocking._limit_usage_by_mau = False
# Ensure does not throw exception
self.get_success(
Expand All @@ -127,7 +131,7 @@ def test_mau_limits_disabled(self):
)
)

def test_mau_limits_exceeded_large(self):
def test_mau_limits_exceeded_large(self) -> None:
self.auth_blocking._limit_usage_by_mau = True
self.hs.get_datastores().main.get_monthly_active_count = Mock(
return_value=make_awaitable(self.large_number_of_users)
Expand All @@ -150,7 +154,7 @@ def test_mau_limits_exceeded_large(self):
ResourceLimitError,
)

def test_mau_limits_parity(self):
def test_mau_limits_parity(self) -> None:
# Ensure we're not at the unix epoch.
self.reactor.advance(1)
self.auth_blocking._limit_usage_by_mau = True
Expand Down Expand Up @@ -189,7 +193,7 @@ def test_mau_limits_parity(self):
)
)

def test_mau_limits_not_exceeded(self):
def test_mau_limits_not_exceeded(self) -> None:
self.auth_blocking._limit_usage_by_mau = True

self.hs.get_datastores().main.get_monthly_active_count = Mock(
Expand All @@ -211,7 +215,7 @@ def test_mau_limits_not_exceeded(self):
)
)

def _get_macaroon(self):
def _get_macaroon(self) -> pymacaroons.Macaroon:
token = self.macaroon_generator.generate_short_term_login_token(
self.user1, "", duration_in_ms=5000
)
Expand Down
2 changes: 1 addition & 1 deletion tests/handlers/test_deactivate_account.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def prepare(self, reactor: MemoryReactor, clock: Clock, hs: HomeServer) -> None:
self.user = self.register_user("user", "pass")
self.token = self.login("user", "pass")

def _deactivate_my_account(self):
def _deactivate_my_account(self) -> None:
"""
Deactivates the account `self.user` using `self.token` and asserts
that it returns a 200 success code.
Expand Down
Loading

0 comments on commit e10a2fe

Please sign in to comment.