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

Commit 8428ef6

Browse files
authored
Add type hints to synapse/tests/rest/admin (#11590)
1 parent 1847d02 commit 8428ef6

File tree

7 files changed

+74
-57
lines changed

7 files changed

+74
-57
lines changed

changelog.d/11590.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
@@ -242,6 +242,9 @@ disallow_untyped_defs = True
242242
[mypy-tests.storage.test_user_directory]
243243
disallow_untyped_defs = True
244244

245+
[mypy-tests.rest.admin.*]
246+
disallow_untyped_defs = True
247+
245248
[mypy-tests.rest.client.test_directory]
246249
disallow_untyped_defs = True
247250

tests/rest/admin/test_background_updates.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
from synapse.rest.client import login
2424
from synapse.server import HomeServer
2525
from synapse.storage.background_updates import BackgroundUpdater
26+
from synapse.types import JsonDict
2627
from synapse.util import Clock
2728

2829
from tests import unittest
@@ -96,7 +97,7 @@ def test_invalid_parameter(self) -> None:
9697
def _register_bg_update(self) -> None:
9798
"Adds a bg update but doesn't start it"
9899

99-
async def _fake_update(progress, batch_size) -> int:
100+
async def _fake_update(progress: JsonDict, batch_size: int) -> int:
100101
await self.clock.sleep(0.2)
101102
return batch_size
102103

tests/rest/admin/test_federation.py

+18-15
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.types import JsonDict
26+
from synapse.util import Clock
2427

2528
from tests import unittest
2629

@@ -31,7 +34,7 @@ class FederationTestCase(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.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
("/_synapse/admin/v1/federation/destinations/dummy",),
4548
]
4649
)
47-
def test_requester_is_no_admin(self, url: str):
50+
def test_requester_is_no_admin(self, url: str) -> None:
4851
"""
4952
If the user is not a server admin, an error 403 is returned.
5053
"""
@@ -62,7 +65,7 @@ def test_requester_is_no_admin(self, 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
"""
@@ -117,7 +120,7 @@ def test_invalid_parameter(self):
117120
self.assertEqual(HTTPStatus.NOT_FOUND, channel.code, msg=channel.json_body)
118121
self.assertEqual(Codes.NOT_FOUND, channel.json_body["errcode"])
119122

120-
def test_limit(self):
123+
def test_limit(self) -> None:
121124
"""
122125
Testing list of destinations with limit
123126
"""
@@ -137,7 +140,7 @@ def test_limit(self):
137140
self.assertEqual(channel.json_body["next_token"], "5")
138141
self._check_fields(channel.json_body["destinations"])
139142

140-
def test_from(self):
143+
def test_from(self) -> None:
141144
"""
142145
Testing list of destinations with a defined starting point (from)
143146
"""
@@ -157,7 +160,7 @@ def test_from(self):
157160
self.assertNotIn("next_token", channel.json_body)
158161
self._check_fields(channel.json_body["destinations"])
159162

160-
def test_limit_and_from(self):
163+
def test_limit_and_from(self) -> None:
161164
"""
162165
Testing list of destinations with a defined starting point and limit
163166
"""
@@ -177,7 +180,7 @@ def test_limit_and_from(self):
177180
self.assertEqual(len(channel.json_body["destinations"]), 10)
178181
self._check_fields(channel.json_body["destinations"])
179182

180-
def test_next_token(self):
183+
def test_next_token(self) -> None:
181184
"""
182185
Testing that `next_token` appears at the right place
183186
"""
@@ -238,7 +241,7 @@ def test_next_token(self):
238241
self.assertEqual(len(channel.json_body["destinations"]), 1)
239242
self.assertNotIn("next_token", channel.json_body)
240243

241-
def test_list_all_destinations(self):
244+
def test_list_all_destinations(self) -> None:
242245
"""
243246
List all destinations.
244247
"""
@@ -259,7 +262,7 @@ def test_list_all_destinations(self):
259262
# Check that all fields are available
260263
self._check_fields(channel.json_body["destinations"])
261264

262-
def test_order_by(self):
265+
def test_order_by(self) -> None:
263266
"""
264267
Testing order list with parameter `order_by`
265268
"""
@@ -268,7 +271,7 @@ def _order_test(
268271
expected_destination_list: List[str],
269272
order_by: Optional[str],
270273
dir: Optional[str] = None,
271-
):
274+
) -> None:
272275
"""Request the list of destinations in a certain order.
273276
Assert that order is what we expect
274277
@@ -358,13 +361,13 @@ def _order_test(
358361
[dest[0][0], dest[2][0], dest[1][0]], "last_successful_stream_ordering", "b"
359362
)
360363

361-
def test_search_term(self):
364+
def test_search_term(self) -> None:
362365
"""Test that searching for a destination works correctly"""
363366

364367
def _search_test(
365368
expected_destination: Optional[str],
366369
search_term: str,
367-
):
370+
) -> None:
368371
"""Search for a destination and check that the returned destinationis a match
369372
370373
Args:
@@ -410,7 +413,7 @@ def _search_test(
410413
_search_test(None, "foo")
411414
_search_test(None, "bar")
412415

413-
def test_get_single_destination(self):
416+
def test_get_single_destination(self) -> None:
414417
"""
415418
Get one specific destinations.
416419
"""
@@ -429,7 +432,7 @@ def test_get_single_destination(self):
429432
# convert channel.json_body into a List
430433
self._check_fields([channel.json_body])
431434

432-
def _create_destinations(self, number_destinations: int):
435+
def _create_destinations(self, number_destinations: int) -> None:
433436
"""Create a number of destinations
434437
435438
Args:
@@ -442,7 +445,7 @@ def _create_destinations(self, number_destinations: int):
442445
self.store.set_destination_last_successful_stream_ordering(dest, 100)
443446
)
444447

445-
def _check_fields(self, content: List[JsonDict]):
448+
def _check_fields(self, content: List[JsonDict]) -> None:
446449
"""Checks that the expected destination attributes are present in content
447450
448451
Args:

tests/rest/admin/test_media.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -580,7 +580,9 @@ def _create_media(self) -> str:
580580

581581
return server_and_media_id
582582

583-
def _access_media(self, server_and_media_id, expect_success=True) -> None:
583+
def _access_media(
584+
self, server_and_media_id: str, expect_success: bool = True
585+
) -> None:
584586
"""
585587
Try to access a media and check the result
586588
"""

tests/rest/admin/test_registration_tokens.py

+16-9
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import random
1515
import string
1616
from http import HTTPStatus
17+
from typing import Optional
1718

1819
from twisted.test.proto_helpers import MemoryReactor
1920

@@ -42,21 +43,27 @@ def prepare(self, reactor: MemoryReactor, clock: Clock, hs: HomeServer) -> None:
4243

4344
self.url = "/_synapse/admin/v1/registration_tokens"
4445

45-
def _new_token(self, **kwargs) -> str:
46+
def _new_token(
47+
self,
48+
token: Optional[str] = None,
49+
uses_allowed: Optional[int] = None,
50+
pending: int = 0,
51+
completed: int = 0,
52+
expiry_time: Optional[int] = None,
53+
) -> str:
4654
"""Helper function to create a token."""
47-
token = kwargs.get(
48-
"token",
49-
"".join(random.choices(string.ascii_letters, k=8)),
50-
)
55+
if token is None:
56+
token = "".join(random.choices(string.ascii_letters, k=8))
57+
5158
self.get_success(
5259
self.store.db_pool.simple_insert(
5360
"registration_tokens",
5461
{
5562
"token": token,
56-
"uses_allowed": kwargs.get("uses_allowed", None),
57-
"pending": kwargs.get("pending", 0),
58-
"completed": kwargs.get("completed", 0),
59-
"expiry_time": kwargs.get("expiry_time", None),
63+
"uses_allowed": uses_allowed,
64+
"pending": pending,
65+
"completed": completed,
66+
"expiry_time": expiry_time,
6067
},
6168
)
6269
)

0 commit comments

Comments
 (0)