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

Commit e5f426c

Browse files
authored
Add type hints to synapse/tests/rest/admin (#11501)
1 parent 8cd68b8 commit e5f426c

11 files changed

+257
-228
lines changed

changelog.d/11501.misc

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add type hints to `synapse/tests/rest/admin`.

mypy.ini

-3
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,6 @@ exclude = (?x)
8686
|tests/push/test_presentable_names.py
8787
|tests/push/test_push_rule_evaluator.py
8888
|tests/rest/admin/test_admin.py
89-
|tests/rest/admin/test_device.py
90-
|tests/rest/admin/test_media.py
91-
|tests/rest/admin/test_server_notice.py
9289
|tests/rest/admin/test_user.py
9390
|tests/rest/admin/test_username_available.py
9491
|tests/rest/client/test_account.py

tests/rest/admin/test_background_updates.py

+12-9
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,14 @@
1616

1717
from parameterized import parameterized
1818

19+
from twisted.test.proto_helpers import MemoryReactor
20+
1921
import synapse.rest.admin
2022
from synapse.api.errors import Codes
2123
from synapse.rest.client import login
2224
from synapse.server import HomeServer
2325
from synapse.storage.background_updates import BackgroundUpdater
26+
from synapse.util import Clock
2427

2528
from tests import unittest
2629

@@ -31,7 +34,7 @@ class BackgroundUpdatesTestCase(unittest.HomeserverTestCase):
3134
login.register_servlets,
3235
]
3336

34-
def prepare(self, reactor, clock, hs: HomeServer):
37+
def prepare(self, reactor: MemoryReactor, clock: Clock, hs: HomeServer) -> None:
3538
self.store = hs.get_datastore()
3639
self.admin_user = self.register_user("admin", "pass", admin=True)
3740
self.admin_user_tok = self.login("admin", "pass")
@@ -44,7 +47,7 @@ def prepare(self, reactor, clock, hs: HomeServer):
4447
("POST", "/_synapse/admin/v1/background_updates/start_job"),
4548
]
4649
)
47-
def test_requester_is_no_admin(self, method: str, url: str):
50+
def test_requester_is_no_admin(self, method: str, url: str) -> None:
4851
"""
4952
If the user is not a server admin, an error HTTPStatus.FORBIDDEN is returned.
5053
"""
@@ -62,7 +65,7 @@ def test_requester_is_no_admin(self, method: str, url: str):
6265
self.assertEqual(HTTPStatus.FORBIDDEN, channel.code, msg=channel.json_body)
6366
self.assertEqual(Codes.FORBIDDEN, channel.json_body["errcode"])
6467

65-
def test_invalid_parameter(self):
68+
def test_invalid_parameter(self) -> None:
6669
"""
6770
If parameters are invalid, an error is returned.
6871
"""
@@ -90,7 +93,7 @@ def test_invalid_parameter(self):
9093
self.assertEqual(HTTPStatus.BAD_REQUEST, channel.code, msg=channel.json_body)
9194
self.assertEqual(Codes.UNKNOWN, channel.json_body["errcode"])
9295

93-
def _register_bg_update(self):
96+
def _register_bg_update(self) -> None:
9497
"Adds a bg update but doesn't start it"
9598

9699
async def _fake_update(progress, batch_size) -> int:
@@ -112,7 +115,7 @@ async def _fake_update(progress, batch_size) -> int:
112115
)
113116
)
114117

115-
def test_status_empty(self):
118+
def test_status_empty(self) -> None:
116119
"""Test the status API works."""
117120

118121
channel = self.make_request(
@@ -127,7 +130,7 @@ def test_status_empty(self):
127130
channel.json_body, {"current_updates": {}, "enabled": True}
128131
)
129132

130-
def test_status_bg_update(self):
133+
def test_status_bg_update(self) -> None:
131134
"""Test the status API works with a background update."""
132135

133136
# Create a new background update
@@ -162,7 +165,7 @@ def test_status_bg_update(self):
162165
},
163166
)
164167

165-
def test_enabled(self):
168+
def test_enabled(self) -> None:
166169
"""Test the enabled API works."""
167170

168171
# Create a new background update
@@ -299,7 +302,7 @@ def test_enabled(self):
299302
),
300303
]
301304
)
302-
def test_start_backround_job(self, job_name: str, updates: Collection[str]):
305+
def test_start_backround_job(self, job_name: str, updates: Collection[str]) -> None:
303306
"""
304307
Test that background updates add to database and be processed.
305308
@@ -341,7 +344,7 @@ def test_start_backround_job(self, job_name: str, updates: Collection[str]):
341344
)
342345
)
343346

344-
def test_start_backround_job_twice(self):
347+
def test_start_backround_job_twice(self) -> None:
345348
"""Test that add a background update twice return an error."""
346349

347350
# add job to database

tests/rest/admin/test_device.py

+29-26
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,18 @@
1111
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
14-
1514
import urllib.parse
1615
from http import HTTPStatus
1716

1817
from parameterized import parameterized
1918

19+
from twisted.test.proto_helpers import MemoryReactor
20+
2021
import synapse.rest.admin
2122
from synapse.api.errors import Codes
2223
from synapse.rest.client import login
24+
from synapse.server import HomeServer
25+
from synapse.util import Clock
2326

2427
from tests import unittest
2528

@@ -31,7 +34,7 @@ class DeviceRestTestCase(unittest.HomeserverTestCase):
3134
login.register_servlets,
3235
]
3336

34-
def prepare(self, reactor, clock, hs):
37+
def prepare(self, reactor: MemoryReactor, clock: Clock, hs: HomeServer) -> None:
3538
self.handler = hs.get_device_handler()
3639

3740
self.admin_user = self.register_user("admin", "pass", admin=True)
@@ -48,7 +51,7 @@ def prepare(self, reactor, clock, hs):
4851
)
4952

5053
@parameterized.expand(["GET", "PUT", "DELETE"])
51-
def test_no_auth(self, method: str):
54+
def test_no_auth(self, method: str) -> None:
5255
"""
5356
Try to get a device of an user without authentication.
5457
"""
@@ -62,7 +65,7 @@ def test_no_auth(self, method: str):
6265
self.assertEqual(Codes.MISSING_TOKEN, channel.json_body["errcode"])
6366

6467
@parameterized.expand(["GET", "PUT", "DELETE"])
65-
def test_requester_is_no_admin(self, method: str):
68+
def test_requester_is_no_admin(self, method: str) -> None:
6669
"""
6770
If the user is not a server admin, an error is returned.
6871
"""
@@ -80,7 +83,7 @@ def test_requester_is_no_admin(self, method: str):
8083
self.assertEqual(Codes.FORBIDDEN, channel.json_body["errcode"])
8184

8285
@parameterized.expand(["GET", "PUT", "DELETE"])
83-
def test_user_does_not_exist(self, method: str):
86+
def test_user_does_not_exist(self, method: str) -> None:
8487
"""
8588
Tests that a lookup for a user that does not exist returns a HTTPStatus.NOT_FOUND
8689
"""
@@ -99,7 +102,7 @@ def test_user_does_not_exist(self, method: str):
99102
self.assertEqual(Codes.NOT_FOUND, channel.json_body["errcode"])
100103

101104
@parameterized.expand(["GET", "PUT", "DELETE"])
102-
def test_user_is_not_local(self, method: str):
105+
def test_user_is_not_local(self, method: str) -> None:
103106
"""
104107
Tests that a lookup for a user that is not a local returns a HTTPStatus.BAD_REQUEST
105108
"""
@@ -117,7 +120,7 @@ def test_user_is_not_local(self, method: str):
117120
self.assertEqual(HTTPStatus.BAD_REQUEST, channel.code, msg=channel.json_body)
118121
self.assertEqual("Can only lookup local users", channel.json_body["error"])
119122

120-
def test_unknown_device(self):
123+
def test_unknown_device(self) -> None:
121124
"""
122125
Tests that a lookup for a device that does not exist returns either HTTPStatus.NOT_FOUND or HTTPStatus.OK.
123126
"""
@@ -151,7 +154,7 @@ def test_unknown_device(self):
151154
# Delete unknown device returns status HTTPStatus.OK
152155
self.assertEqual(HTTPStatus.OK, channel.code, msg=channel.json_body)
153156

154-
def test_update_device_too_long_display_name(self):
157+
def test_update_device_too_long_display_name(self) -> None:
155158
"""
156159
Update a device with a display name that is invalid (too long).
157160
"""
@@ -189,7 +192,7 @@ def test_update_device_too_long_display_name(self):
189192
self.assertEqual(HTTPStatus.OK, channel.code, msg=channel.json_body)
190193
self.assertEqual("new display", channel.json_body["display_name"])
191194

192-
def test_update_no_display_name(self):
195+
def test_update_no_display_name(self) -> None:
193196
"""
194197
Tests that a update for a device without JSON returns a HTTPStatus.OK
195198
"""
@@ -219,7 +222,7 @@ def test_update_no_display_name(self):
219222
self.assertEqual(HTTPStatus.OK, channel.code, msg=channel.json_body)
220223
self.assertEqual("new display", channel.json_body["display_name"])
221224

222-
def test_update_display_name(self):
225+
def test_update_display_name(self) -> None:
223226
"""
224227
Tests a normal successful update of display name
225228
"""
@@ -243,7 +246,7 @@ def test_update_display_name(self):
243246
self.assertEqual(HTTPStatus.OK, channel.code, msg=channel.json_body)
244247
self.assertEqual("new displayname", channel.json_body["display_name"])
245248

246-
def test_get_device(self):
249+
def test_get_device(self) -> None:
247250
"""
248251
Tests that a normal lookup for a device is successfully
249252
"""
@@ -262,7 +265,7 @@ def test_get_device(self):
262265
self.assertIn("last_seen_ip", channel.json_body)
263266
self.assertIn("last_seen_ts", channel.json_body)
264267

265-
def test_delete_device(self):
268+
def test_delete_device(self) -> None:
266269
"""
267270
Tests that a remove of a device is successfully
268271
"""
@@ -292,7 +295,7 @@ class DevicesRestTestCase(unittest.HomeserverTestCase):
292295
login.register_servlets,
293296
]
294297

295-
def prepare(self, reactor, clock, hs):
298+
def prepare(self, reactor: MemoryReactor, clock: Clock, hs: HomeServer) -> None:
296299
self.admin_user = self.register_user("admin", "pass", admin=True)
297300
self.admin_user_tok = self.login("admin", "pass")
298301

@@ -302,7 +305,7 @@ def prepare(self, reactor, clock, hs):
302305
self.other_user
303306
)
304307

305-
def test_no_auth(self):
308+
def test_no_auth(self) -> None:
306309
"""
307310
Try to list devices of an user without authentication.
308311
"""
@@ -315,7 +318,7 @@ def test_no_auth(self):
315318
)
316319
self.assertEqual(Codes.MISSING_TOKEN, channel.json_body["errcode"])
317320

318-
def test_requester_is_no_admin(self):
321+
def test_requester_is_no_admin(self) -> None:
319322
"""
320323
If the user is not a server admin, an error is returned.
321324
"""
@@ -334,7 +337,7 @@ def test_requester_is_no_admin(self):
334337
)
335338
self.assertEqual(Codes.FORBIDDEN, channel.json_body["errcode"])
336339

337-
def test_user_does_not_exist(self):
340+
def test_user_does_not_exist(self) -> None:
338341
"""
339342
Tests that a lookup for a user that does not exist returns a HTTPStatus.NOT_FOUND
340343
"""
@@ -348,7 +351,7 @@ def test_user_does_not_exist(self):
348351
self.assertEqual(HTTPStatus.NOT_FOUND, channel.code, msg=channel.json_body)
349352
self.assertEqual(Codes.NOT_FOUND, channel.json_body["errcode"])
350353

351-
def test_user_is_not_local(self):
354+
def test_user_is_not_local(self) -> None:
352355
"""
353356
Tests that a lookup for a user that is not a local returns a HTTPStatus.BAD_REQUEST
354357
"""
@@ -363,7 +366,7 @@ def test_user_is_not_local(self):
363366
self.assertEqual(HTTPStatus.BAD_REQUEST, channel.code, msg=channel.json_body)
364367
self.assertEqual("Can only lookup local users", channel.json_body["error"])
365368

366-
def test_user_has_no_devices(self):
369+
def test_user_has_no_devices(self) -> None:
367370
"""
368371
Tests that a normal lookup for devices is successfully
369372
if user has no devices
@@ -380,7 +383,7 @@ def test_user_has_no_devices(self):
380383
self.assertEqual(0, channel.json_body["total"])
381384
self.assertEqual(0, len(channel.json_body["devices"]))
382385

383-
def test_get_devices(self):
386+
def test_get_devices(self) -> None:
384387
"""
385388
Tests that a normal lookup for devices is successfully
386389
"""
@@ -416,7 +419,7 @@ class DeleteDevicesRestTestCase(unittest.HomeserverTestCase):
416419
login.register_servlets,
417420
]
418421

419-
def prepare(self, reactor, clock, hs):
422+
def prepare(self, reactor: MemoryReactor, clock: Clock, hs: HomeServer) -> None:
420423
self.handler = hs.get_device_handler()
421424

422425
self.admin_user = self.register_user("admin", "pass", admin=True)
@@ -428,7 +431,7 @@ def prepare(self, reactor, clock, hs):
428431
self.other_user
429432
)
430433

431-
def test_no_auth(self):
434+
def test_no_auth(self) -> None:
432435
"""
433436
Try to delete devices of an user without authentication.
434437
"""
@@ -441,7 +444,7 @@ def test_no_auth(self):
441444
)
442445
self.assertEqual(Codes.MISSING_TOKEN, channel.json_body["errcode"])
443446

444-
def test_requester_is_no_admin(self):
447+
def test_requester_is_no_admin(self) -> None:
445448
"""
446449
If the user is not a server admin, an error is returned.
447450
"""
@@ -460,7 +463,7 @@ def test_requester_is_no_admin(self):
460463
)
461464
self.assertEqual(Codes.FORBIDDEN, channel.json_body["errcode"])
462465

463-
def test_user_does_not_exist(self):
466+
def test_user_does_not_exist(self) -> None:
464467
"""
465468
Tests that a lookup for a user that does not exist returns a HTTPStatus.NOT_FOUND
466469
"""
@@ -474,7 +477,7 @@ def test_user_does_not_exist(self):
474477
self.assertEqual(HTTPStatus.NOT_FOUND, channel.code, msg=channel.json_body)
475478
self.assertEqual(Codes.NOT_FOUND, channel.json_body["errcode"])
476479

477-
def test_user_is_not_local(self):
480+
def test_user_is_not_local(self) -> None:
478481
"""
479482
Tests that a lookup for a user that is not a local returns a HTTPStatus.BAD_REQUEST
480483
"""
@@ -489,7 +492,7 @@ def test_user_is_not_local(self):
489492
self.assertEqual(HTTPStatus.BAD_REQUEST, channel.code, msg=channel.json_body)
490493
self.assertEqual("Can only lookup local users", channel.json_body["error"])
491494

492-
def test_unknown_devices(self):
495+
def test_unknown_devices(self) -> None:
493496
"""
494497
Tests that a remove of a device that does not exist returns HTTPStatus.OK.
495498
"""
@@ -503,7 +506,7 @@ def test_unknown_devices(self):
503506
# Delete unknown devices returns status HTTPStatus.OK
504507
self.assertEqual(HTTPStatus.OK, channel.code, msg=channel.json_body)
505508

506-
def test_delete_devices(self):
509+
def test_delete_devices(self) -> None:
507510
"""
508511
Tests that a remove of devices is successfully
509512
"""

0 commit comments

Comments
 (0)