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

Commit

Permalink
Add type hints to synapse/tests/rest/admin (#11501)
Browse files Browse the repository at this point in the history
  • Loading branch information
dklimpel authored Dec 3, 2021
1 parent 8cd68b8 commit e5f426c
Show file tree
Hide file tree
Showing 11 changed files with 257 additions and 228 deletions.
1 change: 1 addition & 0 deletions changelog.d/11501.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add type hints to `synapse/tests/rest/admin`.
3 changes: 0 additions & 3 deletions mypy.ini
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,6 @@ exclude = (?x)
|tests/push/test_presentable_names.py
|tests/push/test_push_rule_evaluator.py
|tests/rest/admin/test_admin.py
|tests/rest/admin/test_device.py
|tests/rest/admin/test_media.py
|tests/rest/admin/test_server_notice.py
|tests/rest/admin/test_user.py
|tests/rest/admin/test_username_available.py
|tests/rest/client/test_account.py
Expand Down
21 changes: 12 additions & 9 deletions tests/rest/admin/test_background_updates.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,14 @@

from parameterized import parameterized

from twisted.test.proto_helpers import MemoryReactor

import synapse.rest.admin
from synapse.api.errors import Codes
from synapse.rest.client import login
from synapse.server import HomeServer
from synapse.storage.background_updates import BackgroundUpdater
from synapse.util import Clock

from tests import unittest

Expand All @@ -31,7 +34,7 @@ class BackgroundUpdatesTestCase(unittest.HomeserverTestCase):
login.register_servlets,
]

def prepare(self, reactor, clock, hs: HomeServer):
def prepare(self, reactor: MemoryReactor, clock: Clock, hs: HomeServer) -> None:
self.store = hs.get_datastore()
self.admin_user = self.register_user("admin", "pass", admin=True)
self.admin_user_tok = self.login("admin", "pass")
Expand All @@ -44,7 +47,7 @@ def prepare(self, reactor, clock, hs: HomeServer):
("POST", "/_synapse/admin/v1/background_updates/start_job"),
]
)
def test_requester_is_no_admin(self, method: str, url: str):
def test_requester_is_no_admin(self, method: str, url: str) -> None:
"""
If the user is not a server admin, an error HTTPStatus.FORBIDDEN is returned.
"""
Expand All @@ -62,7 +65,7 @@ def test_requester_is_no_admin(self, method: str, url: str):
self.assertEqual(HTTPStatus.FORBIDDEN, channel.code, msg=channel.json_body)
self.assertEqual(Codes.FORBIDDEN, channel.json_body["errcode"])

def test_invalid_parameter(self):
def test_invalid_parameter(self) -> None:
"""
If parameters are invalid, an error is returned.
"""
Expand Down Expand Up @@ -90,7 +93,7 @@ def test_invalid_parameter(self):
self.assertEqual(HTTPStatus.BAD_REQUEST, channel.code, msg=channel.json_body)
self.assertEqual(Codes.UNKNOWN, channel.json_body["errcode"])

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

async def _fake_update(progress, batch_size) -> int:
Expand All @@ -112,7 +115,7 @@ async def _fake_update(progress, batch_size) -> int:
)
)

def test_status_empty(self):
def test_status_empty(self) -> None:
"""Test the status API works."""

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

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

# Create a new background update
Expand Down Expand Up @@ -162,7 +165,7 @@ def test_status_bg_update(self):
},
)

def test_enabled(self):
def test_enabled(self) -> None:
"""Test the enabled API works."""

# Create a new background update
Expand Down Expand Up @@ -299,7 +302,7 @@ def test_enabled(self):
),
]
)
def test_start_backround_job(self, job_name: str, updates: Collection[str]):
def test_start_backround_job(self, job_name: str, updates: Collection[str]) -> None:
"""
Test that background updates add to database and be processed.
Expand Down Expand Up @@ -341,7 +344,7 @@ def test_start_backround_job(self, job_name: str, updates: Collection[str]):
)
)

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

# add job to database
Expand Down
55 changes: 29 additions & 26 deletions tests/rest/admin/test_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,18 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import urllib.parse
from http import HTTPStatus

from parameterized import parameterized

from twisted.test.proto_helpers import MemoryReactor

import synapse.rest.admin
from synapse.api.errors import Codes
from synapse.rest.client import login
from synapse.server import HomeServer
from synapse.util import Clock

from tests import unittest

Expand All @@ -31,7 +34,7 @@ class DeviceRestTestCase(unittest.HomeserverTestCase):
login.register_servlets,
]

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

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

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

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

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

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

def test_unknown_device(self):
def test_unknown_device(self) -> None:
"""
Tests that a lookup for a device that does not exist returns either HTTPStatus.NOT_FOUND or HTTPStatus.OK.
"""
Expand Down Expand Up @@ -151,7 +154,7 @@ def test_unknown_device(self):
# Delete unknown device returns status HTTPStatus.OK
self.assertEqual(HTTPStatus.OK, channel.code, msg=channel.json_body)

def test_update_device_too_long_display_name(self):
def test_update_device_too_long_display_name(self) -> None:
"""
Update a device with a display name that is invalid (too long).
"""
Expand Down Expand Up @@ -189,7 +192,7 @@ def test_update_device_too_long_display_name(self):
self.assertEqual(HTTPStatus.OK, channel.code, msg=channel.json_body)
self.assertEqual("new display", channel.json_body["display_name"])

def test_update_no_display_name(self):
def test_update_no_display_name(self) -> None:
"""
Tests that a update for a device without JSON returns a HTTPStatus.OK
"""
Expand Down Expand Up @@ -219,7 +222,7 @@ def test_update_no_display_name(self):
self.assertEqual(HTTPStatus.OK, channel.code, msg=channel.json_body)
self.assertEqual("new display", channel.json_body["display_name"])

def test_update_display_name(self):
def test_update_display_name(self) -> None:
"""
Tests a normal successful update of display name
"""
Expand All @@ -243,7 +246,7 @@ def test_update_display_name(self):
self.assertEqual(HTTPStatus.OK, channel.code, msg=channel.json_body)
self.assertEqual("new displayname", channel.json_body["display_name"])

def test_get_device(self):
def test_get_device(self) -> None:
"""
Tests that a normal lookup for a device is successfully
"""
Expand All @@ -262,7 +265,7 @@ def test_get_device(self):
self.assertIn("last_seen_ip", channel.json_body)
self.assertIn("last_seen_ts", channel.json_body)

def test_delete_device(self):
def test_delete_device(self) -> None:
"""
Tests that a remove of a device is successfully
"""
Expand Down Expand Up @@ -292,7 +295,7 @@ class DevicesRestTestCase(unittest.HomeserverTestCase):
login.register_servlets,
]

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

Expand All @@ -302,7 +305,7 @@ def prepare(self, reactor, clock, hs):
self.other_user
)

def test_no_auth(self):
def test_no_auth(self) -> None:
"""
Try to list devices of an user without authentication.
"""
Expand All @@ -315,7 +318,7 @@ def test_no_auth(self):
)
self.assertEqual(Codes.MISSING_TOKEN, channel.json_body["errcode"])

def test_requester_is_no_admin(self):
def test_requester_is_no_admin(self) -> None:
"""
If the user is not a server admin, an error is returned.
"""
Expand All @@ -334,7 +337,7 @@ def test_requester_is_no_admin(self):
)
self.assertEqual(Codes.FORBIDDEN, channel.json_body["errcode"])

def test_user_does_not_exist(self):
def test_user_does_not_exist(self) -> None:
"""
Tests that a lookup for a user that does not exist returns a HTTPStatus.NOT_FOUND
"""
Expand All @@ -348,7 +351,7 @@ def test_user_does_not_exist(self):
self.assertEqual(HTTPStatus.NOT_FOUND, channel.code, msg=channel.json_body)
self.assertEqual(Codes.NOT_FOUND, channel.json_body["errcode"])

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

def test_user_has_no_devices(self):
def test_user_has_no_devices(self) -> None:
"""
Tests that a normal lookup for devices is successfully
if user has no devices
Expand All @@ -380,7 +383,7 @@ def test_user_has_no_devices(self):
self.assertEqual(0, channel.json_body["total"])
self.assertEqual(0, len(channel.json_body["devices"]))

def test_get_devices(self):
def test_get_devices(self) -> None:
"""
Tests that a normal lookup for devices is successfully
"""
Expand Down Expand Up @@ -416,7 +419,7 @@ class DeleteDevicesRestTestCase(unittest.HomeserverTestCase):
login.register_servlets,
]

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

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

def test_no_auth(self):
def test_no_auth(self) -> None:
"""
Try to delete devices of an user without authentication.
"""
Expand All @@ -441,7 +444,7 @@ def test_no_auth(self):
)
self.assertEqual(Codes.MISSING_TOKEN, channel.json_body["errcode"])

def test_requester_is_no_admin(self):
def test_requester_is_no_admin(self) -> None:
"""
If the user is not a server admin, an error is returned.
"""
Expand All @@ -460,7 +463,7 @@ def test_requester_is_no_admin(self):
)
self.assertEqual(Codes.FORBIDDEN, channel.json_body["errcode"])

def test_user_does_not_exist(self):
def test_user_does_not_exist(self) -> None:
"""
Tests that a lookup for a user that does not exist returns a HTTPStatus.NOT_FOUND
"""
Expand All @@ -474,7 +477,7 @@ def test_user_does_not_exist(self):
self.assertEqual(HTTPStatus.NOT_FOUND, channel.code, msg=channel.json_body)
self.assertEqual(Codes.NOT_FOUND, channel.json_body["errcode"])

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

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

def test_delete_devices(self):
def test_delete_devices(self) -> None:
"""
Tests that a remove of devices is successfully
"""
Expand Down
Loading

0 comments on commit e5f426c

Please sign in to comment.