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 (#13488)
Browse files Browse the repository at this point in the history
* Use literals in place of `HTTPStatus` constants in tests

* newsfile

* code style

* code style
  • Loading branch information
dklimpel authored Aug 10, 2022
1 parent 51c01d4 commit 2281427
Show file tree
Hide file tree
Showing 12 changed files with 177 additions and 447 deletions.
1 change: 1 addition & 0 deletions changelog.d/13488.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Use literals in place of `HTTPStatus` constants in tests.
7 changes: 3 additions & 4 deletions tests/rest/admin/test_background_updates.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
# 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.
from http import HTTPStatus
from typing import Collection

from parameterized import parameterized
Expand Down Expand Up @@ -81,7 +80,7 @@ def test_invalid_parameter(self) -> None:
access_token=self.admin_user_tok,
)

self.assertEqual(HTTPStatus.BAD_REQUEST, channel.code, msg=channel.json_body)
self.assertEqual(400, channel.code, msg=channel.json_body)
self.assertEqual(Codes.MISSING_PARAM, channel.json_body["errcode"])

# job_name invalid
Expand All @@ -92,7 +91,7 @@ def test_invalid_parameter(self) -> None:
access_token=self.admin_user_tok,
)

self.assertEqual(HTTPStatus.BAD_REQUEST, channel.code, msg=channel.json_body)
self.assertEqual(400, channel.code, msg=channel.json_body)
self.assertEqual(Codes.UNKNOWN, channel.json_body["errcode"])

def _register_bg_update(self) -> None:
Expand Down Expand Up @@ -365,4 +364,4 @@ def test_start_backround_job_twice(self) -> None:
access_token=self.admin_user_tok,
)

self.assertEqual(HTTPStatus.BAD_REQUEST, channel.code, msg=channel.json_body)
self.assertEqual(400, channel.code, msg=channel.json_body)
15 changes: 7 additions & 8 deletions tests/rest/admin/test_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
# 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

Expand Down Expand Up @@ -104,7 +103,7 @@ def test_user_does_not_exist(self, method: str) -> None:
@parameterized.expand(["GET", "PUT", "DELETE"])
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
Tests that a lookup for a user that is not a local returns a 400
"""
url = (
"/_synapse/admin/v2/users/@unknown_person:unknown_domain/devices/%s"
Expand All @@ -117,7 +116,7 @@ def test_user_is_not_local(self, method: str) -> None:
access_token=self.admin_user_tok,
)

self.assertEqual(HTTPStatus.BAD_REQUEST, channel.code, msg=channel.json_body)
self.assertEqual(400, channel.code, msg=channel.json_body)
self.assertEqual("Can only lookup local users", channel.json_body["error"])

def test_unknown_device(self) -> None:
Expand Down Expand Up @@ -179,7 +178,7 @@ def test_update_device_too_long_display_name(self) -> None:
content=update,
)

self.assertEqual(HTTPStatus.BAD_REQUEST, channel.code, msg=channel.json_body)
self.assertEqual(400, channel.code, msg=channel.json_body)
self.assertEqual(Codes.TOO_LARGE, channel.json_body["errcode"])

# Ensure the display name was not updated.
Expand Down Expand Up @@ -353,7 +352,7 @@ def test_user_does_not_exist(self) -> None:

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
Tests that a lookup for a user that is not a local returns a 400
"""
url = "/_synapse/admin/v2/users/@unknown_person:unknown_domain/devices"

Expand All @@ -363,7 +362,7 @@ def test_user_is_not_local(self) -> None:
access_token=self.admin_user_tok,
)

self.assertEqual(HTTPStatus.BAD_REQUEST, channel.code, msg=channel.json_body)
self.assertEqual(400, channel.code, msg=channel.json_body)
self.assertEqual("Can only lookup local users", channel.json_body["error"])

def test_user_has_no_devices(self) -> None:
Expand Down Expand Up @@ -479,7 +478,7 @@ def test_user_does_not_exist(self) -> None:

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
Tests that a lookup for a user that is not a local returns a 400
"""
url = "/_synapse/admin/v2/users/@unknown_person:unknown_domain/delete_devices"

Expand All @@ -489,7 +488,7 @@ def test_user_is_not_local(self) -> None:
access_token=self.admin_user_tok,
)

self.assertEqual(HTTPStatus.BAD_REQUEST, channel.code, msg=channel.json_body)
self.assertEqual(400, channel.code, msg=channel.json_body)
self.assertEqual("Can only lookup local users", channel.json_body["error"])

def test_unknown_devices(self) -> None:
Expand Down
75 changes: 15 additions & 60 deletions tests/rest/admin/test_event_reports.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
# 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.
from http import HTTPStatus
from typing import List

from twisted.test.proto_helpers import MemoryReactor
Expand Down Expand Up @@ -81,11 +80,7 @@ def test_no_auth(self) -> None:
"""
channel = self.make_request("GET", self.url, b"{}")

self.assertEqual(
401,
channel.code,
msg=channel.json_body,
)
self.assertEqual(401, channel.code, msg=channel.json_body)
self.assertEqual(Codes.MISSING_TOKEN, channel.json_body["errcode"])

def test_requester_is_no_admin(self) -> None:
Expand All @@ -99,11 +94,7 @@ def test_requester_is_no_admin(self) -> None:
access_token=self.other_user_tok,
)

self.assertEqual(
403,
channel.code,
msg=channel.json_body,
)
self.assertEqual(403, channel.code, msg=channel.json_body)
self.assertEqual(Codes.FORBIDDEN, channel.json_body["errcode"])

def test_default_success(self) -> None:
Expand Down Expand Up @@ -278,7 +269,7 @@ def test_valid_search_order(self) -> None:

def test_invalid_search_order(self) -> None:
"""
Testing that a invalid search order returns a HTTPStatus.BAD_REQUEST
Testing that a invalid search order returns a 400
"""

channel = self.make_request(
Expand All @@ -287,17 +278,13 @@ def test_invalid_search_order(self) -> None:
access_token=self.admin_user_tok,
)

self.assertEqual(
HTTPStatus.BAD_REQUEST,
channel.code,
msg=channel.json_body,
)
self.assertEqual(400, channel.code, msg=channel.json_body)
self.assertEqual(Codes.INVALID_PARAM, channel.json_body["errcode"])
self.assertEqual("Unknown direction: bar", channel.json_body["error"])

def test_limit_is_negative(self) -> None:
"""
Testing that a negative limit parameter returns a HTTPStatus.BAD_REQUEST
Testing that a negative limit parameter returns a 400
"""

channel = self.make_request(
Expand All @@ -306,16 +293,12 @@ def test_limit_is_negative(self) -> None:
access_token=self.admin_user_tok,
)

self.assertEqual(
HTTPStatus.BAD_REQUEST,
channel.code,
msg=channel.json_body,
)
self.assertEqual(400, channel.code, msg=channel.json_body)
self.assertEqual(Codes.INVALID_PARAM, channel.json_body["errcode"])

def test_from_is_negative(self) -> None:
"""
Testing that a negative from parameter returns a HTTPStatus.BAD_REQUEST
Testing that a negative from parameter returns a 400
"""

channel = self.make_request(
Expand All @@ -324,11 +307,7 @@ def test_from_is_negative(self) -> None:
access_token=self.admin_user_tok,
)

self.assertEqual(
HTTPStatus.BAD_REQUEST,
channel.code,
msg=channel.json_body,
)
self.assertEqual(400, channel.code, msg=channel.json_body)
self.assertEqual(Codes.INVALID_PARAM, channel.json_body["errcode"])

def test_next_token(self) -> None:
Expand Down Expand Up @@ -466,11 +445,7 @@ def test_no_auth(self) -> None:
"""
channel = self.make_request("GET", self.url, b"{}")

self.assertEqual(
401,
channel.code,
msg=channel.json_body,
)
self.assertEqual(401, channel.code, msg=channel.json_body)
self.assertEqual(Codes.MISSING_TOKEN, channel.json_body["errcode"])

def test_requester_is_no_admin(self) -> None:
Expand All @@ -484,11 +459,7 @@ def test_requester_is_no_admin(self) -> None:
access_token=self.other_user_tok,
)

self.assertEqual(
403,
channel.code,
msg=channel.json_body,
)
self.assertEqual(403, channel.code, msg=channel.json_body)
self.assertEqual(Codes.FORBIDDEN, channel.json_body["errcode"])

def test_default_success(self) -> None:
Expand All @@ -507,7 +478,7 @@ def test_default_success(self) -> None:

def test_invalid_report_id(self) -> None:
"""
Testing that an invalid `report_id` returns a HTTPStatus.BAD_REQUEST.
Testing that an invalid `report_id` returns a 400.
"""

# `report_id` is negative
Expand All @@ -517,11 +488,7 @@ def test_invalid_report_id(self) -> None:
access_token=self.admin_user_tok,
)

self.assertEqual(
HTTPStatus.BAD_REQUEST,
channel.code,
msg=channel.json_body,
)
self.assertEqual(400, channel.code, msg=channel.json_body)
self.assertEqual(Codes.INVALID_PARAM, channel.json_body["errcode"])
self.assertEqual(
"The report_id parameter must be a string representing a positive integer.",
Expand All @@ -535,11 +502,7 @@ def test_invalid_report_id(self) -> None:
access_token=self.admin_user_tok,
)

self.assertEqual(
HTTPStatus.BAD_REQUEST,
channel.code,
msg=channel.json_body,
)
self.assertEqual(400, channel.code, msg=channel.json_body)
self.assertEqual(Codes.INVALID_PARAM, channel.json_body["errcode"])
self.assertEqual(
"The report_id parameter must be a string representing a positive integer.",
Expand All @@ -553,11 +516,7 @@ def test_invalid_report_id(self) -> None:
access_token=self.admin_user_tok,
)

self.assertEqual(
HTTPStatus.BAD_REQUEST,
channel.code,
msg=channel.json_body,
)
self.assertEqual(400, channel.code, msg=channel.json_body)
self.assertEqual(Codes.INVALID_PARAM, channel.json_body["errcode"])
self.assertEqual(
"The report_id parameter must be a string representing a positive integer.",
Expand All @@ -575,11 +534,7 @@ def test_report_id_not_found(self) -> None:
access_token=self.admin_user_tok,
)

self.assertEqual(
404,
channel.code,
msg=channel.json_body,
)
self.assertEqual(404, channel.code, msg=channel.json_body)
self.assertEqual(Codes.NOT_FOUND, channel.json_body["errcode"])
self.assertEqual("Event report not found", channel.json_body["error"])

Expand Down
17 changes: 8 additions & 9 deletions tests/rest/admin/test_federation.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
# 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.
from http import HTTPStatus
from typing import List, Optional

from parameterized import parameterized
Expand Down Expand Up @@ -77,7 +76,7 @@ def test_invalid_parameter(self) -> None:
access_token=self.admin_user_tok,
)

self.assertEqual(HTTPStatus.BAD_REQUEST, channel.code, msg=channel.json_body)
self.assertEqual(400, channel.code, msg=channel.json_body)
self.assertEqual(Codes.INVALID_PARAM, channel.json_body["errcode"])

# negative from
Expand All @@ -87,7 +86,7 @@ def test_invalid_parameter(self) -> None:
access_token=self.admin_user_tok,
)

self.assertEqual(HTTPStatus.BAD_REQUEST, channel.code, msg=channel.json_body)
self.assertEqual(400, channel.code, msg=channel.json_body)
self.assertEqual(Codes.INVALID_PARAM, channel.json_body["errcode"])

# unkown order_by
Expand All @@ -97,7 +96,7 @@ def test_invalid_parameter(self) -> None:
access_token=self.admin_user_tok,
)

self.assertEqual(HTTPStatus.BAD_REQUEST, channel.code, msg=channel.json_body)
self.assertEqual(400, channel.code, msg=channel.json_body)
self.assertEqual(Codes.INVALID_PARAM, channel.json_body["errcode"])

# invalid search order
Expand All @@ -107,7 +106,7 @@ def test_invalid_parameter(self) -> None:
access_token=self.admin_user_tok,
)

self.assertEqual(HTTPStatus.BAD_REQUEST, channel.code, msg=channel.json_body)
self.assertEqual(400, channel.code, msg=channel.json_body)
self.assertEqual(Codes.INVALID_PARAM, channel.json_body["errcode"])

# invalid destination
Expand Down Expand Up @@ -469,7 +468,7 @@ def test_destination_reset_connection_not_required(self) -> None:
access_token=self.admin_user_tok,
)

self.assertEqual(HTTPStatus.BAD_REQUEST, channel.code, msg=channel.json_body)
self.assertEqual(400, channel.code, msg=channel.json_body)
self.assertEqual(
"The retry timing does not need to be reset for this destination.",
channel.json_body["error"],
Expand Down Expand Up @@ -574,7 +573,7 @@ def test_invalid_parameter(self) -> None:
access_token=self.admin_user_tok,
)

self.assertEqual(HTTPStatus.BAD_REQUEST, channel.code, msg=channel.json_body)
self.assertEqual(400, channel.code, msg=channel.json_body)
self.assertEqual(Codes.INVALID_PARAM, channel.json_body["errcode"])

# negative from
Expand All @@ -584,7 +583,7 @@ def test_invalid_parameter(self) -> None:
access_token=self.admin_user_tok,
)

self.assertEqual(HTTPStatus.BAD_REQUEST, channel.code, msg=channel.json_body)
self.assertEqual(400, channel.code, msg=channel.json_body)
self.assertEqual(Codes.INVALID_PARAM, channel.json_body["errcode"])

# invalid search order
Expand All @@ -594,7 +593,7 @@ def test_invalid_parameter(self) -> None:
access_token=self.admin_user_tok,
)

self.assertEqual(HTTPStatus.BAD_REQUEST, channel.code, msg=channel.json_body)
self.assertEqual(400, channel.code, msg=channel.json_body)
self.assertEqual(Codes.INVALID_PARAM, channel.json_body["errcode"])

# invalid destination
Expand Down
Loading

0 comments on commit 2281427

Please sign in to comment.