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

Commit

Permalink
Use literals in place of HTTPStatus constants in tests (#13469)
Browse files Browse the repository at this point in the history
  • Loading branch information
dklimpel authored Aug 8, 2022
1 parent 7a19995 commit c97042f
Show file tree
Hide file tree
Showing 13 changed files with 329 additions and 331 deletions.
1 change: 1 addition & 0 deletions changelog.d/13469.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Use literals in place of `HTTPStatus` constants in tests.
24 changes: 12 additions & 12 deletions tests/rest/admin/test_admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def create_test_resource(self) -> JsonResource:
def test_version_string(self) -> None:
channel = self.make_request("GET", self.url, shorthand=False)

self.assertEqual(HTTPStatus.OK, channel.code, msg=channel.json_body)
self.assertEqual(200, channel.code, msg=channel.json_body)
self.assertEqual(
{"server_version", "python_version"}, set(channel.json_body.keys())
)
Expand Down Expand Up @@ -139,7 +139,7 @@ def test_quarantine_media_by_id(self) -> None:
)

# Should be successful
self.assertEqual(HTTPStatus.OK, channel.code)
self.assertEqual(200, channel.code)

# Quarantine the media
url = "/_synapse/admin/v1/media/quarantine/%s/%s" % (
Expand All @@ -152,7 +152,7 @@ def test_quarantine_media_by_id(self) -> None:
access_token=admin_user_tok,
)
self.pump(1.0)
self.assertEqual(HTTPStatus.OK, channel.code, msg=channel.json_body)
self.assertEqual(200, channel.code, msg=channel.json_body)

# Attempt to access the media
self._ensure_quarantined(admin_user_tok, server_name_and_media_id)
Expand Down Expand Up @@ -209,7 +209,7 @@ def test_quarantine_all_media_in_room(self, url: str) -> None:
access_token=admin_user_tok,
)
self.pump(1.0)
self.assertEqual(HTTPStatus.OK, channel.code, msg=channel.json_body)
self.assertEqual(200, channel.code, msg=channel.json_body)
self.assertEqual(
channel.json_body, {"num_quarantined": 2}, "Expected 2 quarantined items"
)
Expand Down Expand Up @@ -251,7 +251,7 @@ def test_quarantine_all_media_by_user(self) -> None:
access_token=admin_user_tok,
)
self.pump(1.0)
self.assertEqual(HTTPStatus.OK, channel.code, msg=channel.json_body)
self.assertEqual(200, channel.code, msg=channel.json_body)
self.assertEqual(
channel.json_body, {"num_quarantined": 2}, "Expected 2 quarantined items"
)
Expand Down Expand Up @@ -285,7 +285,7 @@ def test_cannot_quarantine_safe_media(self) -> None:
url = "/_synapse/admin/v1/media/protect/%s" % (urllib.parse.quote(media_id_2),)
channel = self.make_request("POST", url, access_token=admin_user_tok)
self.pump(1.0)
self.assertEqual(HTTPStatus.OK, channel.code, msg=channel.json_body)
self.assertEqual(200, channel.code, msg=channel.json_body)

# Quarantine all media by this user
url = "/_synapse/admin/v1/user/%s/media/quarantine" % urllib.parse.quote(
Expand All @@ -297,7 +297,7 @@ def test_cannot_quarantine_safe_media(self) -> None:
access_token=admin_user_tok,
)
self.pump(1.0)
self.assertEqual(HTTPStatus.OK, channel.code, msg=channel.json_body)
self.assertEqual(200, channel.code, msg=channel.json_body)
self.assertEqual(
channel.json_body, {"num_quarantined": 1}, "Expected 1 quarantined item"
)
Expand All @@ -318,10 +318,10 @@ def test_cannot_quarantine_safe_media(self) -> None:

# Shouldn't be quarantined
self.assertEqual(
HTTPStatus.OK,
200,
channel.code,
msg=(
"Expected to receive a HTTPStatus.OK on accessing not-quarantined media: %s"
"Expected to receive a 200 on accessing not-quarantined media: %s"
% server_and_media_id_2
),
)
Expand Down Expand Up @@ -350,7 +350,7 @@ def prepare(self, reactor: MemoryReactor, clock: Clock, hs: HomeServer) -> None:
def test_purge_history(self) -> None:
"""
Simple test of purge history API.
Test only that is is possible to call, get status HTTPStatus.OK and purge_id.
Test only that is is possible to call, get status 200 and purge_id.
"""

channel = self.make_request(
Expand All @@ -360,7 +360,7 @@ def test_purge_history(self) -> None:
access_token=self.admin_user_tok,
)

self.assertEqual(HTTPStatus.OK, channel.code, msg=channel.json_body)
self.assertEqual(200, channel.code, msg=channel.json_body)
self.assertIn("purge_id", channel.json_body)
purge_id = channel.json_body["purge_id"]

Expand All @@ -371,5 +371,5 @@ def test_purge_history(self) -> None:
access_token=self.admin_user_tok,
)

self.assertEqual(HTTPStatus.OK, channel.code, msg=channel.json_body)
self.assertEqual(200, channel.code, msg=channel.json_body)
self.assertEqual("complete", channel.json_body["status"])
18 changes: 9 additions & 9 deletions tests/rest/admin/test_background_updates.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ def test_status_empty(self) -> None:
"/_synapse/admin/v1/background_updates/status",
access_token=self.admin_user_tok,
)
self.assertEqual(HTTPStatus.OK, channel.code, msg=channel.json_body)
self.assertEqual(200, channel.code, msg=channel.json_body)

# Background updates should be enabled, but none should be running.
self.assertDictEqual(
Expand All @@ -147,7 +147,7 @@ def test_status_bg_update(self) -> None:
"/_synapse/admin/v1/background_updates/status",
access_token=self.admin_user_tok,
)
self.assertEqual(HTTPStatus.OK, channel.code, msg=channel.json_body)
self.assertEqual(200, channel.code, msg=channel.json_body)

# Background updates should be enabled, and one should be running.
self.assertDictEqual(
Expand Down Expand Up @@ -181,7 +181,7 @@ def test_enabled(self) -> None:
"/_synapse/admin/v1/background_updates/enabled",
access_token=self.admin_user_tok,
)
self.assertEqual(HTTPStatus.OK, channel.code, msg=channel.json_body)
self.assertEqual(200, channel.code, msg=channel.json_body)
self.assertDictEqual(channel.json_body, {"enabled": True})

# Disable the BG updates
Expand All @@ -191,7 +191,7 @@ def test_enabled(self) -> None:
content={"enabled": False},
access_token=self.admin_user_tok,
)
self.assertEqual(HTTPStatus.OK, channel.code, msg=channel.json_body)
self.assertEqual(200, channel.code, msg=channel.json_body)
self.assertDictEqual(channel.json_body, {"enabled": False})

# Advance a bit and get the current status, note this will finish the in
Expand All @@ -204,7 +204,7 @@ def test_enabled(self) -> None:
"/_synapse/admin/v1/background_updates/status",
access_token=self.admin_user_tok,
)
self.assertEqual(HTTPStatus.OK, channel.code, msg=channel.json_body)
self.assertEqual(200, channel.code, msg=channel.json_body)
self.assertDictEqual(
channel.json_body,
{
Expand All @@ -231,7 +231,7 @@ def test_enabled(self) -> None:
"/_synapse/admin/v1/background_updates/status",
access_token=self.admin_user_tok,
)
self.assertEqual(HTTPStatus.OK, channel.code, msg=channel.json_body)
self.assertEqual(200, channel.code, msg=channel.json_body)

# There should be no change from the previous /status response.
self.assertDictEqual(
Expand Down Expand Up @@ -259,7 +259,7 @@ def test_enabled(self) -> None:
content={"enabled": True},
access_token=self.admin_user_tok,
)
self.assertEqual(HTTPStatus.OK, channel.code, msg=channel.json_body)
self.assertEqual(200, channel.code, msg=channel.json_body)

self.assertDictEqual(channel.json_body, {"enabled": True})

Expand All @@ -270,7 +270,7 @@ def test_enabled(self) -> None:
"/_synapse/admin/v1/background_updates/status",
access_token=self.admin_user_tok,
)
self.assertEqual(HTTPStatus.OK, channel.code, msg=channel.json_body)
self.assertEqual(200, channel.code, msg=channel.json_body)

# Background updates should be enabled and making progress.
self.assertDictEqual(
Expand Down Expand Up @@ -325,7 +325,7 @@ def test_start_backround_job(self, job_name: str, updates: Collection[str]) -> N
access_token=self.admin_user_tok,
)

self.assertEqual(HTTPStatus.OK, channel.code, msg=channel.json_body)
self.assertEqual(200, channel.code, msg=channel.json_body)

# test that each background update is waiting now
for update in updates:
Expand Down
36 changes: 18 additions & 18 deletions tests/rest/admin/test_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ def test_user_is_not_local(self, method: str) -> None:

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.
Tests that a lookup for a device that does not exist returns either HTTPStatus.NOT_FOUND or 200.
"""
url = "/_synapse/admin/v2/users/%s/devices/unknown_device" % urllib.parse.quote(
self.other_user
Expand All @@ -143,16 +143,16 @@ def test_unknown_device(self) -> None:
access_token=self.admin_user_tok,
)

self.assertEqual(HTTPStatus.OK, channel.code, msg=channel.json_body)
self.assertEqual(200, channel.code, msg=channel.json_body)

channel = self.make_request(
"DELETE",
url,
access_token=self.admin_user_tok,
)

# Delete unknown device returns status HTTPStatus.OK
self.assertEqual(HTTPStatus.OK, channel.code, msg=channel.json_body)
# Delete unknown device returns status 200
self.assertEqual(200, channel.code, msg=channel.json_body)

def test_update_device_too_long_display_name(self) -> None:
"""
Expand Down Expand Up @@ -189,12 +189,12 @@ def test_update_device_too_long_display_name(self) -> None:
access_token=self.admin_user_tok,
)

self.assertEqual(HTTPStatus.OK, channel.code, msg=channel.json_body)
self.assertEqual(200, channel.code, msg=channel.json_body)
self.assertEqual("new display", channel.json_body["display_name"])

def test_update_no_display_name(self) -> None:
"""
Tests that a update for a device without JSON returns a HTTPStatus.OK
Tests that a update for a device without JSON returns a 200
"""
# Set iniital display name.
update = {"display_name": "new display"}
Expand All @@ -210,7 +210,7 @@ def test_update_no_display_name(self) -> None:
access_token=self.admin_user_tok,
)

self.assertEqual(HTTPStatus.OK, channel.code, msg=channel.json_body)
self.assertEqual(200, channel.code, msg=channel.json_body)

# Ensure the display name was not updated.
channel = self.make_request(
Expand All @@ -219,7 +219,7 @@ def test_update_no_display_name(self) -> None:
access_token=self.admin_user_tok,
)

self.assertEqual(HTTPStatus.OK, channel.code, msg=channel.json_body)
self.assertEqual(200, channel.code, msg=channel.json_body)
self.assertEqual("new display", channel.json_body["display_name"])

def test_update_display_name(self) -> None:
Expand All @@ -234,7 +234,7 @@ def test_update_display_name(self) -> None:
content={"display_name": "new displayname"},
)

self.assertEqual(HTTPStatus.OK, channel.code, msg=channel.json_body)
self.assertEqual(200, channel.code, msg=channel.json_body)

# Check new display_name
channel = self.make_request(
Expand All @@ -243,7 +243,7 @@ def test_update_display_name(self) -> None:
access_token=self.admin_user_tok,
)

self.assertEqual(HTTPStatus.OK, channel.code, msg=channel.json_body)
self.assertEqual(200, channel.code, msg=channel.json_body)
self.assertEqual("new displayname", channel.json_body["display_name"])

def test_get_device(self) -> None:
Expand All @@ -256,7 +256,7 @@ def test_get_device(self) -> None:
access_token=self.admin_user_tok,
)

self.assertEqual(HTTPStatus.OK, channel.code, msg=channel.json_body)
self.assertEqual(200, channel.code, msg=channel.json_body)
self.assertEqual(self.other_user, channel.json_body["user_id"])
# Check that all fields are available
self.assertIn("user_id", channel.json_body)
Expand All @@ -281,7 +281,7 @@ def test_delete_device(self) -> None:
access_token=self.admin_user_tok,
)

self.assertEqual(HTTPStatus.OK, channel.code, msg=channel.json_body)
self.assertEqual(200, channel.code, msg=channel.json_body)

# Ensure that the number of devices is decreased
res = self.get_success(self.handler.get_devices_by_user(self.other_user))
Expand Down Expand Up @@ -379,7 +379,7 @@ def test_user_has_no_devices(self) -> None:
access_token=self.admin_user_tok,
)

self.assertEqual(HTTPStatus.OK, channel.code, msg=channel.json_body)
self.assertEqual(200, channel.code, msg=channel.json_body)
self.assertEqual(0, channel.json_body["total"])
self.assertEqual(0, len(channel.json_body["devices"]))

Expand All @@ -399,7 +399,7 @@ def test_get_devices(self) -> None:
access_token=self.admin_user_tok,
)

self.assertEqual(HTTPStatus.OK, channel.code, msg=channel.json_body)
self.assertEqual(200, channel.code, msg=channel.json_body)
self.assertEqual(number_devices, channel.json_body["total"])
self.assertEqual(number_devices, len(channel.json_body["devices"]))
self.assertEqual(self.other_user, channel.json_body["devices"][0]["user_id"])
Expand Down Expand Up @@ -494,7 +494,7 @@ def test_user_is_not_local(self) -> None:

def test_unknown_devices(self) -> None:
"""
Tests that a remove of a device that does not exist returns HTTPStatus.OK.
Tests that a remove of a device that does not exist returns 200.
"""
channel = self.make_request(
"POST",
Expand All @@ -503,8 +503,8 @@ def test_unknown_devices(self) -> None:
content={"devices": ["unknown_device1", "unknown_device2"]},
)

# Delete unknown devices returns status HTTPStatus.OK
self.assertEqual(HTTPStatus.OK, channel.code, msg=channel.json_body)
# Delete unknown devices returns status 200
self.assertEqual(200, channel.code, msg=channel.json_body)

def test_delete_devices(self) -> None:
"""
Expand Down Expand Up @@ -533,7 +533,7 @@ def test_delete_devices(self) -> None:
content={"devices": device_ids},
)

self.assertEqual(HTTPStatus.OK, channel.code, msg=channel.json_body)
self.assertEqual(200, channel.code, msg=channel.json_body)

res = self.get_success(self.handler.get_devices_by_user(self.other_user))
self.assertEqual(0, len(res))
Loading

0 comments on commit c97042f

Please sign in to comment.