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

Commit 30509a1

Browse files
authored
Add more missing type hints to tests. (#15028)
1 parent 4eed7b2 commit 30509a1

File tree

14 files changed

+124
-111
lines changed

14 files changed

+124
-111
lines changed

changelog.d/15028.misc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Improve type hints.

mypy.ini

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -60,24 +60,6 @@ disallow_untyped_defs = False
6060
[mypy-synapse.storage.database]
6161
disallow_untyped_defs = False
6262

63-
[mypy-tests.scripts.test_new_matrix_user]
64-
disallow_untyped_defs = False
65-
66-
[mypy-tests.server_notices.test_consent]
67-
disallow_untyped_defs = False
68-
69-
[mypy-tests.server_notices.test_resource_limits_server_notices]
70-
disallow_untyped_defs = False
71-
72-
[mypy-tests.test_federation]
73-
disallow_untyped_defs = False
74-
75-
[mypy-tests.test_utils.*]
76-
disallow_untyped_defs = False
77-
78-
[mypy-tests.test_visibility]
79-
disallow_untyped_defs = False
80-
8163
[mypy-tests.unittest]
8264
disallow_untyped_defs = False
8365

tests/handlers/test_oidc.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ def make_homeserver(self, reactor: MemoryReactor, clock: Clock) -> HomeServer:
150150

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

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

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

176176
def reset_mocks(self) -> None:

tests/scripts/test_new_matrix_user.py

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,29 +12,33 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
from typing import List
15+
from typing import List, Optional
1616
from unittest.mock import Mock, patch
1717

1818
from synapse._scripts.register_new_matrix_user import request_registration
19+
from synapse.types import JsonDict
1920

2021
from tests.unittest import TestCase
2122

2223

2324
class RegisterTestCase(TestCase):
24-
def test_success(self):
25+
def test_success(self) -> None:
2526
"""
2627
The script will fetch a nonce, and then generate a MAC with it, and then
2728
post that MAC.
2829
"""
2930

30-
def get(url, verify=None):
31+
def get(url: str, verify: Optional[bool] = None) -> Mock:
3132
r = Mock()
3233
r.status_code = 200
3334
r.json = lambda: {"nonce": "a"}
3435
return r
3536

36-
def post(url, json=None, verify=None):
37+
def post(
38+
url: str, json: Optional[JsonDict] = None, verify: Optional[bool] = None
39+
) -> Mock:
3740
# Make sure we are sent the correct info
41+
assert json is not None
3842
self.assertEqual(json["username"], "user")
3943
self.assertEqual(json["password"], "pass")
4044
self.assertEqual(json["nonce"], "a")
@@ -70,12 +74,12 @@ def post(url, json=None, verify=None):
7074
# sys.exit shouldn't have been called.
7175
self.assertEqual(err_code, [])
7276

73-
def test_failure_nonce(self):
77+
def test_failure_nonce(self) -> None:
7478
"""
7579
If the script fails to fetch a nonce, it throws an error and quits.
7680
"""
7781

78-
def get(url, verify=None):
82+
def get(url: str, verify: Optional[bool] = None) -> Mock:
7983
r = Mock()
8084
r.status_code = 404
8185
r.reason = "Not Found"
@@ -107,20 +111,23 @@ def get(url, verify=None):
107111
self.assertIn("ERROR! Received 404 Not Found", out)
108112
self.assertNotIn("Success!", out)
109113

110-
def test_failure_post(self):
114+
def test_failure_post(self) -> None:
111115
"""
112116
The script will fetch a nonce, and then if the final POST fails, will
113117
report an error and quit.
114118
"""
115119

116-
def get(url, verify=None):
120+
def get(url: str, verify: Optional[bool] = None) -> Mock:
117121
r = Mock()
118122
r.status_code = 200
119123
r.json = lambda: {"nonce": "a"}
120124
return r
121125

122-
def post(url, json=None, verify=None):
126+
def post(
127+
url: str, json: Optional[JsonDict] = None, verify: Optional[bool] = None
128+
) -> Mock:
123129
# Make sure we are sent the correct info
130+
assert json is not None
124131
self.assertEqual(json["username"], "user")
125132
self.assertEqual(json["password"], "pass")
126133
self.assertEqual(json["nonce"], "a")

tests/server_notices/test_consent.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,12 @@
1414

1515
import os
1616

17+
from twisted.test.proto_helpers import MemoryReactor
18+
1719
import synapse.rest.admin
1820
from synapse.rest.client import login, room, sync
21+
from synapse.server import HomeServer
22+
from synapse.util import Clock
1923

2024
from tests import unittest
2125

@@ -29,7 +33,7 @@ class ConsentNoticesTests(unittest.HomeserverTestCase):
2933
room.register_servlets,
3034
]
3135

32-
def make_homeserver(self, reactor, clock):
36+
def make_homeserver(self, reactor: MemoryReactor, clock: Clock) -> HomeServer:
3337

3438
tmpdir = self.mktemp()
3539
os.mkdir(tmpdir)
@@ -53,15 +57,13 @@ def make_homeserver(self, reactor, clock):
5357
"room_name": "Server Notices",
5458
}
5559

56-
hs = self.setup_test_homeserver(config=config)
57-
58-
return hs
60+
return self.setup_test_homeserver(config=config)
5961

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

64-
def test_get_sync_message(self):
66+
def test_get_sync_message(self) -> None:
6567
"""
6668
When user consent server notices are enabled, a sync will cause a notice
6769
to fire (in a room which the user is invited to). The notice contains

tests/server_notices/test_resource_limits_server_notices.py

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
from synapse.server_notices.resource_limits_server_notices import (
2525
ResourceLimitsServerNotices,
2626
)
27+
from synapse.types import JsonDict
2728
from synapse.util import Clock
2829

2930
from tests import unittest
@@ -33,7 +34,7 @@
3334

3435

3536
class TestResourceLimitsServerNotices(unittest.HomeserverTestCase):
36-
def default_config(self):
37+
def default_config(self) -> JsonDict:
3738
config = default_config("test")
3839

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

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

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

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

103104
self._rlsn._auth_blocking.check_auth_blocking = Mock(
@@ -114,7 +115,7 @@ def test_maybe_send_server_notice_to_user_remove_blocked_notice(self):
114115
self._rlsn._server_notices_manager.maybe_get_notice_room_for_user.assert_called_once()
115116
self._send_notice.assert_called_once()
116117

117-
def test_maybe_send_server_notice_to_user_remove_blocked_notice_noop(self):
118+
def test_maybe_send_server_notice_to_user_remove_blocked_notice_noop(self) -> None:
118119
"""
119120
Test when user has blocked notice, but notice ought to be there (NOOP)
120121
"""
@@ -134,7 +135,7 @@ def test_maybe_send_server_notice_to_user_remove_blocked_notice_noop(self):
134135

135136
self._send_notice.assert_not_called()
136137

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

150-
def test_maybe_send_server_notice_to_user_add_blocked_notice_noop(self):
151+
def test_maybe_send_server_notice_to_user_add_blocked_notice_noop(self) -> None:
151152
"""
152153
Test when user does not have blocked notice, nor should they (NOOP)
153154
"""
@@ -159,7 +160,7 @@ def test_maybe_send_server_notice_to_user_add_blocked_notice_noop(self):
159160

160161
self._send_notice.assert_not_called()
161162

162-
def test_maybe_send_server_notice_to_user_not_in_mau_cohort(self):
163+
def test_maybe_send_server_notice_to_user_not_in_mau_cohort(self) -> None:
163164
"""
164165
Test when user is not part of the MAU cohort - this should not ever
165166
happen - but ...
@@ -175,7 +176,9 @@ def test_maybe_send_server_notice_to_user_not_in_mau_cohort(self):
175176
self._send_notice.assert_not_called()
176177

177178
@override_config({"mau_limit_alerting": False})
178-
def test_maybe_send_server_notice_when_alerting_suppressed_room_unblocked(self):
179+
def test_maybe_send_server_notice_when_alerting_suppressed_room_unblocked(
180+
self,
181+
) -> None:
179182
"""
180183
Test that when server is over MAU limit and alerting is suppressed, then
181184
an alert message is not sent into the room
@@ -191,7 +194,7 @@ def test_maybe_send_server_notice_when_alerting_suppressed_room_unblocked(self):
191194
self.assertEqual(self._send_notice.call_count, 0)
192195

193196
@override_config({"mau_limit_alerting": False})
194-
def test_check_hs_disabled_unaffected_by_mau_alert_suppression(self):
197+
def test_check_hs_disabled_unaffected_by_mau_alert_suppression(self) -> None:
195198
"""
196199
Test that when a server is disabled, that MAU limit alerting is ignored.
197200
"""
@@ -207,7 +210,9 @@ def test_check_hs_disabled_unaffected_by_mau_alert_suppression(self):
207210
self.assertEqual(self._send_notice.call_count, 2)
208211

209212
@override_config({"mau_limit_alerting": False})
210-
def test_maybe_send_server_notice_when_alerting_suppressed_room_blocked(self):
213+
def test_maybe_send_server_notice_when_alerting_suppressed_room_blocked(
214+
self,
215+
) -> None:
211216
"""
212217
When the room is already in a blocked state, test that when alerting
213218
is suppressed that the room is returned to an unblocked state.
@@ -242,7 +247,7 @@ class TestResourceLimitsServerNoticesWithRealRooms(unittest.HomeserverTestCase):
242247
sync.register_servlets,
243248
]
244249

245-
def default_config(self):
250+
def default_config(self) -> JsonDict:
246251
c = super().default_config()
247252
c["server_notices"] = {
248253
"system_mxid_localpart": "server",
@@ -270,7 +275,7 @@ def prepare(self, reactor: MemoryReactor, clock: Clock, hs: HomeServer) -> None:
270275

271276
self.user_id = "@user_id:test"
272277

273-
def test_server_notice_only_sent_once(self):
278+
def test_server_notice_only_sent_once(self) -> None:
274279
self.store.get_monthly_active_count = Mock(return_value=make_awaitable(1000))
275280

276281
self.store.user_last_seen_monthly_active = Mock(
@@ -306,7 +311,7 @@ def test_server_notice_only_sent_once(self):
306311

307312
self.assertEqual(count, 1)
308313

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

329334
m.assert_called_once_with(user_id)
330335

331-
def test_invite_with_notice(self):
336+
def test_invite_with_notice(self) -> None:
332337
"""Tests that, if the MAU limit is hit, the server notices user invites each user
333338
to a room in which it has sent a notice.
334339
"""

0 commit comments

Comments
 (0)