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

Commit

Permalink
Add more missing type hints to tests. (#15028)
Browse files Browse the repository at this point in the history
  • Loading branch information
clokep authored Feb 8, 2023
1 parent 4eed7b2 commit 30509a1
Show file tree
Hide file tree
Showing 14 changed files with 124 additions and 111 deletions.
1 change: 1 addition & 0 deletions changelog.d/15028.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Improve type hints.
18 changes: 0 additions & 18 deletions mypy.ini
Original file line number Diff line number Diff line change
Expand Up @@ -60,24 +60,6 @@ disallow_untyped_defs = False
[mypy-synapse.storage.database]
disallow_untyped_defs = False

[mypy-tests.scripts.test_new_matrix_user]
disallow_untyped_defs = False

[mypy-tests.server_notices.test_consent]
disallow_untyped_defs = False

[mypy-tests.server_notices.test_resource_limits_server_notices]
disallow_untyped_defs = False

[mypy-tests.test_federation]
disallow_untyped_defs = False

[mypy-tests.test_utils.*]
disallow_untyped_defs = False

[mypy-tests.test_visibility]
disallow_untyped_defs = False

[mypy-tests.unittest]
disallow_untyped_defs = False

Expand Down
4 changes: 2 additions & 2 deletions tests/handlers/test_oidc.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ def make_homeserver(self, reactor: MemoryReactor, clock: Clock) -> HomeServer:

hs = self.setup_test_homeserver()
self.hs_patcher = self.fake_server.patch_homeserver(hs=hs)
self.hs_patcher.start()
self.hs_patcher.start() # type: ignore[attr-defined]

self.handler = hs.get_oidc_handler()
self.provider = self.handler._providers["oidc"]
Expand All @@ -170,7 +170,7 @@ def make_homeserver(self, reactor: MemoryReactor, clock: Clock) -> HomeServer:
return hs

def tearDown(self) -> None:
self.hs_patcher.stop()
self.hs_patcher.stop() # type: ignore[attr-defined]
return super().tearDown()

def reset_mocks(self) -> None:
Expand Down
25 changes: 16 additions & 9 deletions tests/scripts/test_new_matrix_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,29 +12,33 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from typing import List
from typing import List, Optional
from unittest.mock import Mock, patch

from synapse._scripts.register_new_matrix_user import request_registration
from synapse.types import JsonDict

from tests.unittest import TestCase


class RegisterTestCase(TestCase):
def test_success(self):
def test_success(self) -> None:
"""
The script will fetch a nonce, and then generate a MAC with it, and then
post that MAC.
"""

def get(url, verify=None):
def get(url: str, verify: Optional[bool] = None) -> Mock:
r = Mock()
r.status_code = 200
r.json = lambda: {"nonce": "a"}
return r

def post(url, json=None, verify=None):
def post(
url: str, json: Optional[JsonDict] = None, verify: Optional[bool] = None
) -> Mock:
# Make sure we are sent the correct info
assert json is not None
self.assertEqual(json["username"], "user")
self.assertEqual(json["password"], "pass")
self.assertEqual(json["nonce"], "a")
Expand Down Expand Up @@ -70,12 +74,12 @@ def post(url, json=None, verify=None):
# sys.exit shouldn't have been called.
self.assertEqual(err_code, [])

def test_failure_nonce(self):
def test_failure_nonce(self) -> None:
"""
If the script fails to fetch a nonce, it throws an error and quits.
"""

def get(url, verify=None):
def get(url: str, verify: Optional[bool] = None) -> Mock:
r = Mock()
r.status_code = 404
r.reason = "Not Found"
Expand Down Expand Up @@ -107,20 +111,23 @@ def get(url, verify=None):
self.assertIn("ERROR! Received 404 Not Found", out)
self.assertNotIn("Success!", out)

def test_failure_post(self):
def test_failure_post(self) -> None:
"""
The script will fetch a nonce, and then if the final POST fails, will
report an error and quit.
"""

def get(url, verify=None):
def get(url: str, verify: Optional[bool] = None) -> Mock:
r = Mock()
r.status_code = 200
r.json = lambda: {"nonce": "a"}
return r

def post(url, json=None, verify=None):
def post(
url: str, json: Optional[JsonDict] = None, verify: Optional[bool] = None
) -> Mock:
# Make sure we are sent the correct info
assert json is not None
self.assertEqual(json["username"], "user")
self.assertEqual(json["password"], "pass")
self.assertEqual(json["nonce"], "a")
Expand Down
14 changes: 8 additions & 6 deletions tests/server_notices/test_consent.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,12 @@

import os

from twisted.test.proto_helpers import MemoryReactor

import synapse.rest.admin
from synapse.rest.client import login, room, sync
from synapse.server import HomeServer
from synapse.util import Clock

from tests import unittest

Expand All @@ -29,7 +33,7 @@ class ConsentNoticesTests(unittest.HomeserverTestCase):
room.register_servlets,
]

def make_homeserver(self, reactor, clock):
def make_homeserver(self, reactor: MemoryReactor, clock: Clock) -> HomeServer:

tmpdir = self.mktemp()
os.mkdir(tmpdir)
Expand All @@ -53,15 +57,13 @@ def make_homeserver(self, reactor, clock):
"room_name": "Server Notices",
}

hs = self.setup_test_homeserver(config=config)

return hs
return self.setup_test_homeserver(config=config)

def prepare(self, reactor, clock, hs):
def prepare(self, reactor: MemoryReactor, clock: Clock, hs: HomeServer) -> None:
self.user_id = self.register_user("bob", "abc123")
self.access_token = self.login("bob", "abc123")

def test_get_sync_message(self):
def test_get_sync_message(self) -> None:
"""
When user consent server notices are enabled, a sync will cause a notice
to fire (in a room which the user is invited to). The notice contains
Expand Down
35 changes: 20 additions & 15 deletions tests/server_notices/test_resource_limits_server_notices.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
from synapse.server_notices.resource_limits_server_notices import (
ResourceLimitsServerNotices,
)
from synapse.types import JsonDict
from synapse.util import Clock

from tests import unittest
Expand All @@ -33,7 +34,7 @@


class TestResourceLimitsServerNotices(unittest.HomeserverTestCase):
def default_config(self):
def default_config(self) -> JsonDict:
config = default_config("test")

config.update(
Expand Down Expand Up @@ -86,18 +87,18 @@ def prepare(self, reactor: MemoryReactor, clock: Clock, hs: HomeServer) -> None:
self._rlsn._store.get_tags_for_room = Mock(return_value=make_awaitable({})) # type: ignore[assignment]

@override_config({"hs_disabled": True})
def test_maybe_send_server_notice_disabled_hs(self):
def test_maybe_send_server_notice_disabled_hs(self) -> None:
"""If the HS is disabled, we should not send notices"""
self.get_success(self._rlsn.maybe_send_server_notice_to_user(self.user_id))
self._send_notice.assert_not_called()

@override_config({"limit_usage_by_mau": False})
def test_maybe_send_server_notice_to_user_flag_off(self):
def test_maybe_send_server_notice_to_user_flag_off(self) -> None:
"""If mau limiting is disabled, we should not send notices"""
self.get_success(self._rlsn.maybe_send_server_notice_to_user(self.user_id))
self._send_notice.assert_not_called()

def test_maybe_send_server_notice_to_user_remove_blocked_notice(self):
def test_maybe_send_server_notice_to_user_remove_blocked_notice(self) -> None:
"""Test when user has blocked notice, but should have it removed"""

self._rlsn._auth_blocking.check_auth_blocking = Mock(
Expand All @@ -114,7 +115,7 @@ def test_maybe_send_server_notice_to_user_remove_blocked_notice(self):
self._rlsn._server_notices_manager.maybe_get_notice_room_for_user.assert_called_once()
self._send_notice.assert_called_once()

def test_maybe_send_server_notice_to_user_remove_blocked_notice_noop(self):
def test_maybe_send_server_notice_to_user_remove_blocked_notice_noop(self) -> None:
"""
Test when user has blocked notice, but notice ought to be there (NOOP)
"""
Expand All @@ -134,7 +135,7 @@ def test_maybe_send_server_notice_to_user_remove_blocked_notice_noop(self):

self._send_notice.assert_not_called()

def test_maybe_send_server_notice_to_user_add_blocked_notice(self):
def test_maybe_send_server_notice_to_user_add_blocked_notice(self) -> None:
"""
Test when user does not have blocked notice, but should have one
"""
Expand All @@ -147,7 +148,7 @@ def test_maybe_send_server_notice_to_user_add_blocked_notice(self):
# Would be better to check contents, but 2 calls == set blocking event
self.assertEqual(self._send_notice.call_count, 2)

def test_maybe_send_server_notice_to_user_add_blocked_notice_noop(self):
def test_maybe_send_server_notice_to_user_add_blocked_notice_noop(self) -> None:
"""
Test when user does not have blocked notice, nor should they (NOOP)
"""
Expand All @@ -159,7 +160,7 @@ def test_maybe_send_server_notice_to_user_add_blocked_notice_noop(self):

self._send_notice.assert_not_called()

def test_maybe_send_server_notice_to_user_not_in_mau_cohort(self):
def test_maybe_send_server_notice_to_user_not_in_mau_cohort(self) -> None:
"""
Test when user is not part of the MAU cohort - this should not ever
happen - but ...
Expand All @@ -175,7 +176,9 @@ def test_maybe_send_server_notice_to_user_not_in_mau_cohort(self):
self._send_notice.assert_not_called()

@override_config({"mau_limit_alerting": False})
def test_maybe_send_server_notice_when_alerting_suppressed_room_unblocked(self):
def test_maybe_send_server_notice_when_alerting_suppressed_room_unblocked(
self,
) -> None:
"""
Test that when server is over MAU limit and alerting is suppressed, then
an alert message is not sent into the room
Expand All @@ -191,7 +194,7 @@ def test_maybe_send_server_notice_when_alerting_suppressed_room_unblocked(self):
self.assertEqual(self._send_notice.call_count, 0)

@override_config({"mau_limit_alerting": False})
def test_check_hs_disabled_unaffected_by_mau_alert_suppression(self):
def test_check_hs_disabled_unaffected_by_mau_alert_suppression(self) -> None:
"""
Test that when a server is disabled, that MAU limit alerting is ignored.
"""
Expand All @@ -207,7 +210,9 @@ def test_check_hs_disabled_unaffected_by_mau_alert_suppression(self):
self.assertEqual(self._send_notice.call_count, 2)

@override_config({"mau_limit_alerting": False})
def test_maybe_send_server_notice_when_alerting_suppressed_room_blocked(self):
def test_maybe_send_server_notice_when_alerting_suppressed_room_blocked(
self,
) -> None:
"""
When the room is already in a blocked state, test that when alerting
is suppressed that the room is returned to an unblocked state.
Expand Down Expand Up @@ -242,7 +247,7 @@ class TestResourceLimitsServerNoticesWithRealRooms(unittest.HomeserverTestCase):
sync.register_servlets,
]

def default_config(self):
def default_config(self) -> JsonDict:
c = super().default_config()
c["server_notices"] = {
"system_mxid_localpart": "server",
Expand Down Expand Up @@ -270,7 +275,7 @@ def prepare(self, reactor: MemoryReactor, clock: Clock, hs: HomeServer) -> None:

self.user_id = "@user_id:test"

def test_server_notice_only_sent_once(self):
def test_server_notice_only_sent_once(self) -> None:
self.store.get_monthly_active_count = Mock(return_value=make_awaitable(1000))

self.store.user_last_seen_monthly_active = Mock(
Expand Down Expand Up @@ -306,7 +311,7 @@ def test_server_notice_only_sent_once(self):

self.assertEqual(count, 1)

def test_no_invite_without_notice(self):
def test_no_invite_without_notice(self) -> None:
"""Tests that a user doesn't get invited to a server notices room without a
server notice being sent.
Expand All @@ -328,7 +333,7 @@ def test_no_invite_without_notice(self):

m.assert_called_once_with(user_id)

def test_invite_with_notice(self):
def test_invite_with_notice(self) -> None:
"""Tests that, if the MAU limit is hit, the server notices user invites each user
to a room in which it has sent a notice.
"""
Expand Down
Loading

0 comments on commit 30509a1

Please sign in to comment.