Convert status codes to HTTPStatus in tests.rest.admin (#11455)

This commit is contained in:
Dirk Klimpel 2021-11-30 10:53:54 +01:00 committed by GitHub
parent e8ae94a223
commit 35b1900f00
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
12 changed files with 887 additions and 557 deletions

1
changelog.d/11455.misc Normal file
View file

@ -0,0 +1 @@
Convert status codes to `HTTPStatus` in `synapse.rest.admin`.

View file

@ -12,9 +12,9 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
import json
import os import os
import urllib.parse import urllib.parse
from http import HTTPStatus
from unittest.mock import Mock from unittest.mock import Mock
from twisted.internet.defer import Deferred from twisted.internet.defer import Deferred
@ -41,7 +41,7 @@ class VersionTestCase(unittest.HomeserverTestCase):
def test_version_string(self): def test_version_string(self):
channel = self.make_request("GET", self.url, shorthand=False) channel = self.make_request("GET", self.url, shorthand=False)
self.assertEqual(200, int(channel.result["code"]), msg=channel.result["body"]) self.assertEqual(HTTPStatus.OK, channel.code, msg=channel.json_body)
self.assertEqual( self.assertEqual(
{"server_version", "python_version"}, set(channel.json_body.keys()) {"server_version", "python_version"}, set(channel.json_body.keys())
) )
@ -70,11 +70,11 @@ class DeleteGroupTestCase(unittest.HomeserverTestCase):
content={"localpart": "test"}, content={"localpart": "test"},
) )
self.assertEqual(200, int(channel.result["code"]), msg=channel.result["body"]) self.assertEqual(HTTPStatus.OK, channel.code, msg=channel.json_body)
group_id = channel.json_body["group_id"] group_id = channel.json_body["group_id"]
self._check_group(group_id, expect_code=200) self._check_group(group_id, expect_code=HTTPStatus.OK)
# Invite/join another user # Invite/join another user
@ -82,13 +82,13 @@ class DeleteGroupTestCase(unittest.HomeserverTestCase):
channel = self.make_request( channel = self.make_request(
"PUT", url.encode("ascii"), access_token=self.admin_user_tok, content={} "PUT", url.encode("ascii"), access_token=self.admin_user_tok, content={}
) )
self.assertEqual(200, int(channel.result["code"]), msg=channel.result["body"]) self.assertEqual(HTTPStatus.OK, channel.code, msg=channel.json_body)
url = "/groups/%s/self/accept_invite" % (group_id,) url = "/groups/%s/self/accept_invite" % (group_id,)
channel = self.make_request( channel = self.make_request(
"PUT", url.encode("ascii"), access_token=self.other_user_token, content={} "PUT", url.encode("ascii"), access_token=self.other_user_token, content={}
) )
self.assertEqual(200, int(channel.result["code"]), msg=channel.result["body"]) self.assertEqual(HTTPStatus.OK, channel.code, msg=channel.json_body)
# Check other user knows they're in the group # Check other user knows they're in the group
self.assertIn(group_id, self._get_groups_user_is_in(self.admin_user_tok)) self.assertIn(group_id, self._get_groups_user_is_in(self.admin_user_tok))
@ -103,10 +103,10 @@ class DeleteGroupTestCase(unittest.HomeserverTestCase):
content={"localpart": "test"}, content={"localpart": "test"},
) )
self.assertEqual(200, int(channel.result["code"]), msg=channel.result["body"]) self.assertEqual(HTTPStatus.OK, channel.code, msg=channel.json_body)
# Check group returns 404 # Check group returns HTTPStatus.NOT_FOUND
self._check_group(group_id, expect_code=404) self._check_group(group_id, expect_code=HTTPStatus.NOT_FOUND)
# Check users don't think they're in the group # Check users don't think they're in the group
self.assertNotIn(group_id, self._get_groups_user_is_in(self.admin_user_tok)) self.assertNotIn(group_id, self._get_groups_user_is_in(self.admin_user_tok))
@ -122,15 +122,13 @@ class DeleteGroupTestCase(unittest.HomeserverTestCase):
"GET", url.encode("ascii"), access_token=self.admin_user_tok "GET", url.encode("ascii"), access_token=self.admin_user_tok
) )
self.assertEqual( self.assertEqual(expect_code, channel.code, msg=channel.json_body)
expect_code, int(channel.result["code"]), msg=channel.result["body"]
)
def _get_groups_user_is_in(self, access_token): def _get_groups_user_is_in(self, access_token):
"""Returns the list of groups the user is in (given their access token)""" """Returns the list of groups the user is in (given their access token)"""
channel = self.make_request("GET", b"/joined_groups", access_token=access_token) channel = self.make_request("GET", b"/joined_groups", access_token=access_token)
self.assertEqual(200, int(channel.result["code"]), msg=channel.result["body"]) self.assertEqual(HTTPStatus.OK, channel.code, msg=channel.json_body)
return channel.json_body["groups"] return channel.json_body["groups"]
@ -210,10 +208,10 @@ class QuarantineMediaTestCase(unittest.HomeserverTestCase):
# Should be quarantined # Should be quarantined
self.assertEqual( self.assertEqual(
404, HTTPStatus.NOT_FOUND,
int(channel.code), channel.code,
msg=( msg=(
"Expected to receive a 404 on accessing quarantined media: %s" "Expected to receive a HTTPStatus.NOT_FOUND on accessing quarantined media: %s"
% server_and_media_id % server_and_media_id
), ),
) )
@ -232,8 +230,8 @@ class QuarantineMediaTestCase(unittest.HomeserverTestCase):
# Expect a forbidden error # Expect a forbidden error
self.assertEqual( self.assertEqual(
403, HTTPStatus.FORBIDDEN,
int(channel.result["code"]), channel.code,
msg="Expected forbidden on quarantining media as a non-admin", msg="Expected forbidden on quarantining media as a non-admin",
) )
@ -247,8 +245,8 @@ class QuarantineMediaTestCase(unittest.HomeserverTestCase):
# Expect a forbidden error # Expect a forbidden error
self.assertEqual( self.assertEqual(
403, HTTPStatus.FORBIDDEN,
int(channel.result["code"]), channel.code,
msg="Expected forbidden on quarantining media as a non-admin", msg="Expected forbidden on quarantining media as a non-admin",
) )
@ -279,7 +277,7 @@ class QuarantineMediaTestCase(unittest.HomeserverTestCase):
) )
# Should be successful # Should be successful
self.assertEqual(200, int(channel.code), msg=channel.result["body"]) self.assertEqual(HTTPStatus.OK, channel.code)
# Quarantine the media # Quarantine the media
url = "/_synapse/admin/v1/media/quarantine/%s/%s" % ( url = "/_synapse/admin/v1/media/quarantine/%s/%s" % (
@ -292,7 +290,7 @@ class QuarantineMediaTestCase(unittest.HomeserverTestCase):
access_token=admin_user_tok, access_token=admin_user_tok,
) )
self.pump(1.0) self.pump(1.0)
self.assertEqual(200, int(channel.code), msg=channel.result["body"]) self.assertEqual(HTTPStatus.OK, channel.code, msg=channel.json_body)
# Attempt to access the media # Attempt to access the media
self._ensure_quarantined(admin_user_tok, server_name_and_media_id) self._ensure_quarantined(admin_user_tok, server_name_and_media_id)
@ -348,11 +346,9 @@ class QuarantineMediaTestCase(unittest.HomeserverTestCase):
access_token=admin_user_tok, access_token=admin_user_tok,
) )
self.pump(1.0) self.pump(1.0)
self.assertEqual(200, int(channel.code), msg=channel.result["body"]) self.assertEqual(HTTPStatus.OK, channel.code, msg=channel.json_body)
self.assertEqual( self.assertEqual(
json.loads(channel.result["body"].decode("utf-8")), channel.json_body, {"num_quarantined": 2}, "Expected 2 quarantined items"
{"num_quarantined": 2},
"Expected 2 quarantined items",
) )
# Convert mxc URLs to server/media_id strings # Convert mxc URLs to server/media_id strings
@ -396,11 +392,9 @@ class QuarantineMediaTestCase(unittest.HomeserverTestCase):
access_token=admin_user_tok, access_token=admin_user_tok,
) )
self.pump(1.0) self.pump(1.0)
self.assertEqual(200, int(channel.result["code"]), msg=channel.result["body"]) self.assertEqual(HTTPStatus.OK, channel.code, msg=channel.json_body)
self.assertEqual( self.assertEqual(
json.loads(channel.result["body"].decode("utf-8")), channel.json_body, {"num_quarantined": 2}, "Expected 2 quarantined items"
{"num_quarantined": 2},
"Expected 2 quarantined items",
) )
# Attempt to access each piece of media # Attempt to access each piece of media
@ -432,7 +426,7 @@ class QuarantineMediaTestCase(unittest.HomeserverTestCase):
url = "/_synapse/admin/v1/media/protect/%s" % (urllib.parse.quote(media_id_2),) url = "/_synapse/admin/v1/media/protect/%s" % (urllib.parse.quote(media_id_2),)
channel = self.make_request("POST", url, access_token=admin_user_tok) channel = self.make_request("POST", url, access_token=admin_user_tok)
self.pump(1.0) self.pump(1.0)
self.assertEqual(200, int(channel.code), msg=channel.result["body"]) self.assertEqual(HTTPStatus.OK, channel.code, msg=channel.json_body)
# Quarantine all media by this user # Quarantine all media by this user
url = "/_synapse/admin/v1/user/%s/media/quarantine" % urllib.parse.quote( url = "/_synapse/admin/v1/user/%s/media/quarantine" % urllib.parse.quote(
@ -444,11 +438,9 @@ class QuarantineMediaTestCase(unittest.HomeserverTestCase):
access_token=admin_user_tok, access_token=admin_user_tok,
) )
self.pump(1.0) self.pump(1.0)
self.assertEqual(200, int(channel.result["code"]), msg=channel.result["body"]) self.assertEqual(HTTPStatus.OK, channel.code, msg=channel.json_body)
self.assertEqual( self.assertEqual(
json.loads(channel.result["body"].decode("utf-8")), channel.json_body, {"num_quarantined": 1}, "Expected 1 quarantined item"
{"num_quarantined": 1},
"Expected 1 quarantined item",
) )
# Attempt to access each piece of media, the first should fail, the # Attempt to access each piece of media, the first should fail, the
@ -467,10 +459,10 @@ class QuarantineMediaTestCase(unittest.HomeserverTestCase):
# Shouldn't be quarantined # Shouldn't be quarantined
self.assertEqual( self.assertEqual(
200, HTTPStatus.OK,
int(channel.code), channel.code,
msg=( msg=(
"Expected to receive a 200 on accessing not-quarantined media: %s" "Expected to receive a HTTPStatus.OK on accessing not-quarantined media: %s"
% server_and_media_id_2 % server_and_media_id_2
), ),
) )
@ -499,7 +491,7 @@ class PurgeHistoryTestCase(unittest.HomeserverTestCase):
def test_purge_history(self): def test_purge_history(self):
""" """
Simple test of purge history API. Simple test of purge history API.
Test only that is is possible to call, get status 200 and purge_id. Test only that is is possible to call, get status HTTPStatus.OK and purge_id.
""" """
channel = self.make_request( channel = self.make_request(
@ -509,7 +501,7 @@ class PurgeHistoryTestCase(unittest.HomeserverTestCase):
access_token=self.admin_user_tok, access_token=self.admin_user_tok,
) )
self.assertEqual(200, channel.code, msg=channel.json_body) self.assertEqual(HTTPStatus.OK, channel.code, msg=channel.json_body)
self.assertIn("purge_id", channel.json_body) self.assertIn("purge_id", channel.json_body)
purge_id = channel.json_body["purge_id"] purge_id = channel.json_body["purge_id"]
@ -520,5 +512,5 @@ class PurgeHistoryTestCase(unittest.HomeserverTestCase):
access_token=self.admin_user_tok, access_token=self.admin_user_tok,
) )
self.assertEqual(200, channel.code, msg=channel.json_body) self.assertEqual(HTTPStatus.OK, channel.code, msg=channel.json_body)
self.assertEqual("complete", channel.json_body["status"]) self.assertEqual("complete", channel.json_body["status"])

View file

@ -46,7 +46,7 @@ class BackgroundUpdatesTestCase(unittest.HomeserverTestCase):
) )
def test_requester_is_no_admin(self, method: str, url: str): def test_requester_is_no_admin(self, method: str, url: str):
""" """
If the user is not a server admin, an error 403 is returned. If the user is not a server admin, an error HTTPStatus.FORBIDDEN is returned.
""" """
self.register_user("user", "pass", admin=False) self.register_user("user", "pass", admin=False)

View file

@ -13,6 +13,7 @@
# limitations under the License. # limitations under the License.
import urllib.parse import urllib.parse
from http import HTTPStatus
from parameterized import parameterized from parameterized import parameterized
@ -53,7 +54,11 @@ class DeviceRestTestCase(unittest.HomeserverTestCase):
""" """
channel = self.make_request(method, self.url, b"{}") channel = self.make_request(method, self.url, b"{}")
self.assertEqual(401, int(channel.result["code"]), msg=channel.result["body"]) self.assertEqual(
HTTPStatus.UNAUTHORIZED,
channel.code,
msg=channel.json_body,
)
self.assertEqual(Codes.MISSING_TOKEN, channel.json_body["errcode"]) self.assertEqual(Codes.MISSING_TOKEN, channel.json_body["errcode"])
@parameterized.expand(["GET", "PUT", "DELETE"]) @parameterized.expand(["GET", "PUT", "DELETE"])
@ -67,13 +72,17 @@ class DeviceRestTestCase(unittest.HomeserverTestCase):
access_token=self.other_user_token, access_token=self.other_user_token,
) )
self.assertEqual(403, int(channel.result["code"]), msg=channel.result["body"]) self.assertEqual(
HTTPStatus.FORBIDDEN,
channel.code,
msg=channel.json_body,
)
self.assertEqual(Codes.FORBIDDEN, channel.json_body["errcode"]) self.assertEqual(Codes.FORBIDDEN, channel.json_body["errcode"])
@parameterized.expand(["GET", "PUT", "DELETE"]) @parameterized.expand(["GET", "PUT", "DELETE"])
def test_user_does_not_exist(self, method: str): def test_user_does_not_exist(self, method: str):
""" """
Tests that a lookup for a user that does not exist returns a 404 Tests that a lookup for a user that does not exist returns a HTTPStatus.NOT_FOUND
""" """
url = ( url = (
"/_synapse/admin/v2/users/@unknown_person:test/devices/%s" "/_synapse/admin/v2/users/@unknown_person:test/devices/%s"
@ -86,13 +95,13 @@ class DeviceRestTestCase(unittest.HomeserverTestCase):
access_token=self.admin_user_tok, access_token=self.admin_user_tok,
) )
self.assertEqual(404, channel.code, msg=channel.json_body) self.assertEqual(HTTPStatus.NOT_FOUND, channel.code, msg=channel.json_body)
self.assertEqual(Codes.NOT_FOUND, channel.json_body["errcode"]) self.assertEqual(Codes.NOT_FOUND, channel.json_body["errcode"])
@parameterized.expand(["GET", "PUT", "DELETE"]) @parameterized.expand(["GET", "PUT", "DELETE"])
def test_user_is_not_local(self, method: str): def test_user_is_not_local(self, method: str):
""" """
Tests that a lookup for a user that is not a local returns a 400 Tests that a lookup for a user that is not a local returns a HTTPStatus.BAD_REQUEST
""" """
url = ( url = (
"/_synapse/admin/v2/users/@unknown_person:unknown_domain/devices/%s" "/_synapse/admin/v2/users/@unknown_person:unknown_domain/devices/%s"
@ -105,12 +114,12 @@ class DeviceRestTestCase(unittest.HomeserverTestCase):
access_token=self.admin_user_tok, access_token=self.admin_user_tok,
) )
self.assertEqual(400, channel.code, msg=channel.json_body) self.assertEqual(HTTPStatus.BAD_REQUEST, channel.code, msg=channel.json_body)
self.assertEqual("Can only lookup local users", channel.json_body["error"]) self.assertEqual("Can only lookup local users", channel.json_body["error"])
def test_unknown_device(self): def test_unknown_device(self):
""" """
Tests that a lookup for a device that does not exist returns either 404 or 200. Tests that a lookup for a device that does not exist returns either HTTPStatus.NOT_FOUND or HTTPStatus.OK.
""" """
url = "/_synapse/admin/v2/users/%s/devices/unknown_device" % urllib.parse.quote( url = "/_synapse/admin/v2/users/%s/devices/unknown_device" % urllib.parse.quote(
self.other_user self.other_user
@ -122,7 +131,7 @@ class DeviceRestTestCase(unittest.HomeserverTestCase):
access_token=self.admin_user_tok, access_token=self.admin_user_tok,
) )
self.assertEqual(404, channel.code, msg=channel.json_body) self.assertEqual(HTTPStatus.NOT_FOUND, channel.code, msg=channel.json_body)
self.assertEqual(Codes.NOT_FOUND, channel.json_body["errcode"]) self.assertEqual(Codes.NOT_FOUND, channel.json_body["errcode"])
channel = self.make_request( channel = self.make_request(
@ -131,7 +140,7 @@ class DeviceRestTestCase(unittest.HomeserverTestCase):
access_token=self.admin_user_tok, access_token=self.admin_user_tok,
) )
self.assertEqual(200, channel.code, msg=channel.json_body) self.assertEqual(HTTPStatus.OK, channel.code, msg=channel.json_body)
channel = self.make_request( channel = self.make_request(
"DELETE", "DELETE",
@ -139,8 +148,8 @@ class DeviceRestTestCase(unittest.HomeserverTestCase):
access_token=self.admin_user_tok, access_token=self.admin_user_tok,
) )
# Delete unknown device returns status 200 # Delete unknown device returns status HTTPStatus.OK
self.assertEqual(200, channel.code, msg=channel.json_body) 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):
""" """
@ -167,7 +176,7 @@ class DeviceRestTestCase(unittest.HomeserverTestCase):
content=update, content=update,
) )
self.assertEqual(400, channel.code, msg=channel.json_body) self.assertEqual(HTTPStatus.BAD_REQUEST, channel.code, msg=channel.json_body)
self.assertEqual(Codes.TOO_LARGE, channel.json_body["errcode"]) self.assertEqual(Codes.TOO_LARGE, channel.json_body["errcode"])
# Ensure the display name was not updated. # Ensure the display name was not updated.
@ -177,12 +186,12 @@ class DeviceRestTestCase(unittest.HomeserverTestCase):
access_token=self.admin_user_tok, access_token=self.admin_user_tok,
) )
self.assertEqual(200, channel.code, msg=channel.json_body) self.assertEqual(HTTPStatus.OK, channel.code, msg=channel.json_body)
self.assertEqual("new display", channel.json_body["display_name"]) self.assertEqual("new display", channel.json_body["display_name"])
def test_update_no_display_name(self): def test_update_no_display_name(self):
""" """
Tests that a update for a device without JSON returns a 200 Tests that a update for a device without JSON returns a HTTPStatus.OK
""" """
# Set iniital display name. # Set iniital display name.
update = {"display_name": "new display"} update = {"display_name": "new display"}
@ -198,7 +207,7 @@ class DeviceRestTestCase(unittest.HomeserverTestCase):
access_token=self.admin_user_tok, access_token=self.admin_user_tok,
) )
self.assertEqual(200, channel.code, msg=channel.json_body) self.assertEqual(HTTPStatus.OK, channel.code, msg=channel.json_body)
# Ensure the display name was not updated. # Ensure the display name was not updated.
channel = self.make_request( channel = self.make_request(
@ -207,7 +216,7 @@ class DeviceRestTestCase(unittest.HomeserverTestCase):
access_token=self.admin_user_tok, access_token=self.admin_user_tok,
) )
self.assertEqual(200, channel.code, msg=channel.json_body) self.assertEqual(HTTPStatus.OK, channel.code, msg=channel.json_body)
self.assertEqual("new display", channel.json_body["display_name"]) self.assertEqual("new display", channel.json_body["display_name"])
def test_update_display_name(self): def test_update_display_name(self):
@ -222,7 +231,7 @@ class DeviceRestTestCase(unittest.HomeserverTestCase):
content={"display_name": "new displayname"}, content={"display_name": "new displayname"},
) )
self.assertEqual(200, channel.code, msg=channel.json_body) self.assertEqual(HTTPStatus.OK, channel.code, msg=channel.json_body)
# Check new display_name # Check new display_name
channel = self.make_request( channel = self.make_request(
@ -231,7 +240,7 @@ class DeviceRestTestCase(unittest.HomeserverTestCase):
access_token=self.admin_user_tok, access_token=self.admin_user_tok,
) )
self.assertEqual(200, channel.code, msg=channel.json_body) self.assertEqual(HTTPStatus.OK, channel.code, msg=channel.json_body)
self.assertEqual("new displayname", channel.json_body["display_name"]) self.assertEqual("new displayname", channel.json_body["display_name"])
def test_get_device(self): def test_get_device(self):
@ -244,7 +253,7 @@ class DeviceRestTestCase(unittest.HomeserverTestCase):
access_token=self.admin_user_tok, access_token=self.admin_user_tok,
) )
self.assertEqual(200, channel.code, msg=channel.json_body) self.assertEqual(HTTPStatus.OK, channel.code, msg=channel.json_body)
self.assertEqual(self.other_user, channel.json_body["user_id"]) self.assertEqual(self.other_user, channel.json_body["user_id"])
# Check that all fields are available # Check that all fields are available
self.assertIn("user_id", channel.json_body) self.assertIn("user_id", channel.json_body)
@ -269,7 +278,7 @@ class DeviceRestTestCase(unittest.HomeserverTestCase):
access_token=self.admin_user_tok, access_token=self.admin_user_tok,
) )
self.assertEqual(200, channel.code, msg=channel.json_body) self.assertEqual(HTTPStatus.OK, channel.code, msg=channel.json_body)
# Ensure that the number of devices is decreased # Ensure that the number of devices is decreased
res = self.get_success(self.handler.get_devices_by_user(self.other_user)) res = self.get_success(self.handler.get_devices_by_user(self.other_user))
@ -299,7 +308,11 @@ class DevicesRestTestCase(unittest.HomeserverTestCase):
""" """
channel = self.make_request("GET", self.url, b"{}") channel = self.make_request("GET", self.url, b"{}")
self.assertEqual(401, int(channel.result["code"]), msg=channel.result["body"]) self.assertEqual(
HTTPStatus.UNAUTHORIZED,
channel.code,
msg=channel.json_body,
)
self.assertEqual(Codes.MISSING_TOKEN, channel.json_body["errcode"]) self.assertEqual(Codes.MISSING_TOKEN, channel.json_body["errcode"])
def test_requester_is_no_admin(self): def test_requester_is_no_admin(self):
@ -314,12 +327,16 @@ class DevicesRestTestCase(unittest.HomeserverTestCase):
access_token=other_user_token, access_token=other_user_token,
) )
self.assertEqual(403, int(channel.result["code"]), msg=channel.result["body"]) self.assertEqual(
HTTPStatus.FORBIDDEN,
channel.code,
msg=channel.json_body,
)
self.assertEqual(Codes.FORBIDDEN, channel.json_body["errcode"]) self.assertEqual(Codes.FORBIDDEN, channel.json_body["errcode"])
def test_user_does_not_exist(self): def test_user_does_not_exist(self):
""" """
Tests that a lookup for a user that does not exist returns a 404 Tests that a lookup for a user that does not exist returns a HTTPStatus.NOT_FOUND
""" """
url = "/_synapse/admin/v2/users/@unknown_person:test/devices" url = "/_synapse/admin/v2/users/@unknown_person:test/devices"
channel = self.make_request( channel = self.make_request(
@ -328,12 +345,12 @@ class DevicesRestTestCase(unittest.HomeserverTestCase):
access_token=self.admin_user_tok, access_token=self.admin_user_tok,
) )
self.assertEqual(404, channel.code, msg=channel.json_body) self.assertEqual(HTTPStatus.NOT_FOUND, channel.code, msg=channel.json_body)
self.assertEqual(Codes.NOT_FOUND, channel.json_body["errcode"]) self.assertEqual(Codes.NOT_FOUND, channel.json_body["errcode"])
def test_user_is_not_local(self): def test_user_is_not_local(self):
""" """
Tests that a lookup for a user that is not a local returns a 400 Tests that a lookup for a user that is not a local returns a HTTPStatus.BAD_REQUEST
""" """
url = "/_synapse/admin/v2/users/@unknown_person:unknown_domain/devices" url = "/_synapse/admin/v2/users/@unknown_person:unknown_domain/devices"
@ -343,7 +360,7 @@ class DevicesRestTestCase(unittest.HomeserverTestCase):
access_token=self.admin_user_tok, access_token=self.admin_user_tok,
) )
self.assertEqual(400, channel.code, msg=channel.json_body) self.assertEqual(HTTPStatus.BAD_REQUEST, channel.code, msg=channel.json_body)
self.assertEqual("Can only lookup local users", channel.json_body["error"]) 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):
@ -359,7 +376,7 @@ class DevicesRestTestCase(unittest.HomeserverTestCase):
access_token=self.admin_user_tok, access_token=self.admin_user_tok,
) )
self.assertEqual(200, channel.code, msg=channel.json_body) self.assertEqual(HTTPStatus.OK, channel.code, msg=channel.json_body)
self.assertEqual(0, channel.json_body["total"]) self.assertEqual(0, channel.json_body["total"])
self.assertEqual(0, len(channel.json_body["devices"])) self.assertEqual(0, len(channel.json_body["devices"]))
@ -379,7 +396,7 @@ class DevicesRestTestCase(unittest.HomeserverTestCase):
access_token=self.admin_user_tok, access_token=self.admin_user_tok,
) )
self.assertEqual(200, channel.code, msg=channel.json_body) self.assertEqual(HTTPStatus.OK, channel.code, msg=channel.json_body)
self.assertEqual(number_devices, channel.json_body["total"]) self.assertEqual(number_devices, channel.json_body["total"])
self.assertEqual(number_devices, len(channel.json_body["devices"])) self.assertEqual(number_devices, len(channel.json_body["devices"]))
self.assertEqual(self.other_user, channel.json_body["devices"][0]["user_id"]) self.assertEqual(self.other_user, channel.json_body["devices"][0]["user_id"])
@ -417,7 +434,11 @@ class DeleteDevicesRestTestCase(unittest.HomeserverTestCase):
""" """
channel = self.make_request("POST", self.url, b"{}") channel = self.make_request("POST", self.url, b"{}")
self.assertEqual(401, int(channel.result["code"]), msg=channel.result["body"]) self.assertEqual(
HTTPStatus.UNAUTHORIZED,
channel.code,
msg=channel.json_body,
)
self.assertEqual(Codes.MISSING_TOKEN, channel.json_body["errcode"]) self.assertEqual(Codes.MISSING_TOKEN, channel.json_body["errcode"])
def test_requester_is_no_admin(self): def test_requester_is_no_admin(self):
@ -432,12 +453,16 @@ class DeleteDevicesRestTestCase(unittest.HomeserverTestCase):
access_token=other_user_token, access_token=other_user_token,
) )
self.assertEqual(403, int(channel.result["code"]), msg=channel.result["body"]) self.assertEqual(
HTTPStatus.FORBIDDEN,
channel.code,
msg=channel.json_body,
)
self.assertEqual(Codes.FORBIDDEN, channel.json_body["errcode"]) self.assertEqual(Codes.FORBIDDEN, channel.json_body["errcode"])
def test_user_does_not_exist(self): def test_user_does_not_exist(self):
""" """
Tests that a lookup for a user that does not exist returns a 404 Tests that a lookup for a user that does not exist returns a HTTPStatus.NOT_FOUND
""" """
url = "/_synapse/admin/v2/users/@unknown_person:test/delete_devices" url = "/_synapse/admin/v2/users/@unknown_person:test/delete_devices"
channel = self.make_request( channel = self.make_request(
@ -446,12 +471,12 @@ class DeleteDevicesRestTestCase(unittest.HomeserverTestCase):
access_token=self.admin_user_tok, access_token=self.admin_user_tok,
) )
self.assertEqual(404, channel.code, msg=channel.json_body) self.assertEqual(HTTPStatus.NOT_FOUND, channel.code, msg=channel.json_body)
self.assertEqual(Codes.NOT_FOUND, channel.json_body["errcode"]) self.assertEqual(Codes.NOT_FOUND, channel.json_body["errcode"])
def test_user_is_not_local(self): def test_user_is_not_local(self):
""" """
Tests that a lookup for a user that is not a local returns a 400 Tests that a lookup for a user that is not a local returns a HTTPStatus.BAD_REQUEST
""" """
url = "/_synapse/admin/v2/users/@unknown_person:unknown_domain/delete_devices" url = "/_synapse/admin/v2/users/@unknown_person:unknown_domain/delete_devices"
@ -461,12 +486,12 @@ class DeleteDevicesRestTestCase(unittest.HomeserverTestCase):
access_token=self.admin_user_tok, access_token=self.admin_user_tok,
) )
self.assertEqual(400, channel.code, msg=channel.json_body) self.assertEqual(HTTPStatus.BAD_REQUEST, channel.code, msg=channel.json_body)
self.assertEqual("Can only lookup local users", channel.json_body["error"]) self.assertEqual("Can only lookup local users", channel.json_body["error"])
def test_unknown_devices(self): def test_unknown_devices(self):
""" """
Tests that a remove of a device that does not exist returns 200. Tests that a remove of a device that does not exist returns HTTPStatus.OK.
""" """
channel = self.make_request( channel = self.make_request(
"POST", "POST",
@ -475,8 +500,8 @@ class DeleteDevicesRestTestCase(unittest.HomeserverTestCase):
content={"devices": ["unknown_device1", "unknown_device2"]}, content={"devices": ["unknown_device1", "unknown_device2"]},
) )
# Delete unknown devices returns status 200 # Delete unknown devices returns status HTTPStatus.OK
self.assertEqual(200, channel.code, msg=channel.json_body) self.assertEqual(HTTPStatus.OK, channel.code, msg=channel.json_body)
def test_delete_devices(self): def test_delete_devices(self):
""" """
@ -505,7 +530,7 @@ class DeleteDevicesRestTestCase(unittest.HomeserverTestCase):
content={"devices": device_ids}, content={"devices": device_ids},
) )
self.assertEqual(200, channel.code, msg=channel.json_body) self.assertEqual(HTTPStatus.OK, channel.code, msg=channel.json_body)
res = self.get_success(self.handler.get_devices_by_user(self.other_user)) res = self.get_success(self.handler.get_devices_by_user(self.other_user))
self.assertEqual(0, len(res)) self.assertEqual(0, len(res))

View file

@ -12,7 +12,7 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
import json from http import HTTPStatus
import synapse.rest.admin import synapse.rest.admin
from synapse.api.errors import Codes from synapse.api.errors import Codes
@ -76,12 +76,16 @@ class EventReportsTestCase(unittest.HomeserverTestCase):
""" """
channel = self.make_request("GET", self.url, b"{}") channel = self.make_request("GET", self.url, b"{}")
self.assertEqual(401, int(channel.result["code"]), msg=channel.result["body"]) self.assertEqual(
HTTPStatus.UNAUTHORIZED,
channel.code,
msg=channel.json_body,
)
self.assertEqual(Codes.MISSING_TOKEN, channel.json_body["errcode"]) self.assertEqual(Codes.MISSING_TOKEN, channel.json_body["errcode"])
def test_requester_is_no_admin(self): def test_requester_is_no_admin(self):
""" """
If the user is not a server admin, an error 403 is returned. If the user is not a server admin, an error HTTPStatus.FORBIDDEN is returned.
""" """
channel = self.make_request( channel = self.make_request(
@ -90,7 +94,11 @@ class EventReportsTestCase(unittest.HomeserverTestCase):
access_token=self.other_user_tok, access_token=self.other_user_tok,
) )
self.assertEqual(403, int(channel.result["code"]), msg=channel.result["body"]) self.assertEqual(
HTTPStatus.FORBIDDEN,
channel.code,
msg=channel.json_body,
)
self.assertEqual(Codes.FORBIDDEN, channel.json_body["errcode"]) self.assertEqual(Codes.FORBIDDEN, channel.json_body["errcode"])
def test_default_success(self): def test_default_success(self):
@ -104,7 +112,7 @@ class EventReportsTestCase(unittest.HomeserverTestCase):
access_token=self.admin_user_tok, access_token=self.admin_user_tok,
) )
self.assertEqual(200, int(channel.result["code"]), msg=channel.result["body"]) self.assertEqual(HTTPStatus.OK, channel.code, msg=channel.json_body)
self.assertEqual(channel.json_body["total"], 20) self.assertEqual(channel.json_body["total"], 20)
self.assertEqual(len(channel.json_body["event_reports"]), 20) self.assertEqual(len(channel.json_body["event_reports"]), 20)
self.assertNotIn("next_token", channel.json_body) self.assertNotIn("next_token", channel.json_body)
@ -121,7 +129,7 @@ class EventReportsTestCase(unittest.HomeserverTestCase):
access_token=self.admin_user_tok, access_token=self.admin_user_tok,
) )
self.assertEqual(200, int(channel.result["code"]), msg=channel.result["body"]) self.assertEqual(HTTPStatus.OK, channel.code, msg=channel.json_body)
self.assertEqual(channel.json_body["total"], 20) self.assertEqual(channel.json_body["total"], 20)
self.assertEqual(len(channel.json_body["event_reports"]), 5) self.assertEqual(len(channel.json_body["event_reports"]), 5)
self.assertEqual(channel.json_body["next_token"], 5) self.assertEqual(channel.json_body["next_token"], 5)
@ -138,7 +146,7 @@ class EventReportsTestCase(unittest.HomeserverTestCase):
access_token=self.admin_user_tok, access_token=self.admin_user_tok,
) )
self.assertEqual(200, int(channel.result["code"]), msg=channel.result["body"]) self.assertEqual(HTTPStatus.OK, channel.code, msg=channel.json_body)
self.assertEqual(channel.json_body["total"], 20) self.assertEqual(channel.json_body["total"], 20)
self.assertEqual(len(channel.json_body["event_reports"]), 15) self.assertEqual(len(channel.json_body["event_reports"]), 15)
self.assertNotIn("next_token", channel.json_body) self.assertNotIn("next_token", channel.json_body)
@ -155,7 +163,7 @@ class EventReportsTestCase(unittest.HomeserverTestCase):
access_token=self.admin_user_tok, access_token=self.admin_user_tok,
) )
self.assertEqual(200, int(channel.result["code"]), msg=channel.result["body"]) self.assertEqual(HTTPStatus.OK, channel.code, msg=channel.json_body)
self.assertEqual(channel.json_body["total"], 20) self.assertEqual(channel.json_body["total"], 20)
self.assertEqual(channel.json_body["next_token"], 15) self.assertEqual(channel.json_body["next_token"], 15)
self.assertEqual(len(channel.json_body["event_reports"]), 10) self.assertEqual(len(channel.json_body["event_reports"]), 10)
@ -172,7 +180,7 @@ class EventReportsTestCase(unittest.HomeserverTestCase):
access_token=self.admin_user_tok, access_token=self.admin_user_tok,
) )
self.assertEqual(200, int(channel.result["code"]), msg=channel.result["body"]) self.assertEqual(HTTPStatus.OK, channel.code, msg=channel.json_body)
self.assertEqual(channel.json_body["total"], 10) self.assertEqual(channel.json_body["total"], 10)
self.assertEqual(len(channel.json_body["event_reports"]), 10) self.assertEqual(len(channel.json_body["event_reports"]), 10)
self.assertNotIn("next_token", channel.json_body) self.assertNotIn("next_token", channel.json_body)
@ -192,7 +200,7 @@ class EventReportsTestCase(unittest.HomeserverTestCase):
access_token=self.admin_user_tok, access_token=self.admin_user_tok,
) )
self.assertEqual(200, int(channel.result["code"]), msg=channel.result["body"]) self.assertEqual(HTTPStatus.OK, channel.code, msg=channel.json_body)
self.assertEqual(channel.json_body["total"], 10) self.assertEqual(channel.json_body["total"], 10)
self.assertEqual(len(channel.json_body["event_reports"]), 10) self.assertEqual(len(channel.json_body["event_reports"]), 10)
self.assertNotIn("next_token", channel.json_body) self.assertNotIn("next_token", channel.json_body)
@ -212,7 +220,7 @@ class EventReportsTestCase(unittest.HomeserverTestCase):
access_token=self.admin_user_tok, access_token=self.admin_user_tok,
) )
self.assertEqual(200, int(channel.result["code"]), msg=channel.result["body"]) self.assertEqual(HTTPStatus.OK, channel.code, msg=channel.json_body)
self.assertEqual(channel.json_body["total"], 5) self.assertEqual(channel.json_body["total"], 5)
self.assertEqual(len(channel.json_body["event_reports"]), 5) self.assertEqual(len(channel.json_body["event_reports"]), 5)
self.assertNotIn("next_token", channel.json_body) self.assertNotIn("next_token", channel.json_body)
@ -234,7 +242,7 @@ class EventReportsTestCase(unittest.HomeserverTestCase):
access_token=self.admin_user_tok, access_token=self.admin_user_tok,
) )
self.assertEqual(200, int(channel.result["code"]), msg=channel.result["body"]) self.assertEqual(HTTPStatus.OK, channel.code, msg=channel.json_body)
self.assertEqual(channel.json_body["total"], 20) self.assertEqual(channel.json_body["total"], 20)
self.assertEqual(len(channel.json_body["event_reports"]), 20) self.assertEqual(len(channel.json_body["event_reports"]), 20)
report = 1 report = 1
@ -252,7 +260,7 @@ class EventReportsTestCase(unittest.HomeserverTestCase):
access_token=self.admin_user_tok, access_token=self.admin_user_tok,
) )
self.assertEqual(200, int(channel.result["code"]), msg=channel.result["body"]) self.assertEqual(HTTPStatus.OK, channel.code, msg=channel.json_body)
self.assertEqual(channel.json_body["total"], 20) self.assertEqual(channel.json_body["total"], 20)
self.assertEqual(len(channel.json_body["event_reports"]), 20) self.assertEqual(len(channel.json_body["event_reports"]), 20)
report = 1 report = 1
@ -265,7 +273,7 @@ class EventReportsTestCase(unittest.HomeserverTestCase):
def test_invalid_search_order(self): def test_invalid_search_order(self):
""" """
Testing that a invalid search order returns a 400 Testing that a invalid search order returns a HTTPStatus.BAD_REQUEST
""" """
channel = self.make_request( channel = self.make_request(
@ -274,13 +282,17 @@ class EventReportsTestCase(unittest.HomeserverTestCase):
access_token=self.admin_user_tok, access_token=self.admin_user_tok,
) )
self.assertEqual(400, int(channel.result["code"]), msg=channel.result["body"]) self.assertEqual(
HTTPStatus.BAD_REQUEST,
channel.code,
msg=channel.json_body,
)
self.assertEqual(Codes.INVALID_PARAM, channel.json_body["errcode"]) self.assertEqual(Codes.INVALID_PARAM, channel.json_body["errcode"])
self.assertEqual("Unknown direction: bar", channel.json_body["error"]) self.assertEqual("Unknown direction: bar", channel.json_body["error"])
def test_limit_is_negative(self): def test_limit_is_negative(self):
""" """
Testing that a negative limit parameter returns a 400 Testing that a negative limit parameter returns a HTTPStatus.BAD_REQUEST
""" """
channel = self.make_request( channel = self.make_request(
@ -289,12 +301,16 @@ class EventReportsTestCase(unittest.HomeserverTestCase):
access_token=self.admin_user_tok, access_token=self.admin_user_tok,
) )
self.assertEqual(400, int(channel.result["code"]), msg=channel.result["body"]) self.assertEqual(
HTTPStatus.BAD_REQUEST,
channel.code,
msg=channel.json_body,
)
self.assertEqual(Codes.INVALID_PARAM, channel.json_body["errcode"]) self.assertEqual(Codes.INVALID_PARAM, channel.json_body["errcode"])
def test_from_is_negative(self): def test_from_is_negative(self):
""" """
Testing that a negative from parameter returns a 400 Testing that a negative from parameter returns a HTTPStatus.BAD_REQUEST
""" """
channel = self.make_request( channel = self.make_request(
@ -303,7 +319,11 @@ class EventReportsTestCase(unittest.HomeserverTestCase):
access_token=self.admin_user_tok, access_token=self.admin_user_tok,
) )
self.assertEqual(400, int(channel.result["code"]), msg=channel.result["body"]) self.assertEqual(
HTTPStatus.BAD_REQUEST,
channel.code,
msg=channel.json_body,
)
self.assertEqual(Codes.INVALID_PARAM, channel.json_body["errcode"]) self.assertEqual(Codes.INVALID_PARAM, channel.json_body["errcode"])
def test_next_token(self): def test_next_token(self):
@ -319,7 +339,7 @@ class EventReportsTestCase(unittest.HomeserverTestCase):
access_token=self.admin_user_tok, access_token=self.admin_user_tok,
) )
self.assertEqual(200, int(channel.result["code"]), msg=channel.result["body"]) self.assertEqual(HTTPStatus.OK, channel.code, msg=channel.json_body)
self.assertEqual(channel.json_body["total"], 20) self.assertEqual(channel.json_body["total"], 20)
self.assertEqual(len(channel.json_body["event_reports"]), 20) self.assertEqual(len(channel.json_body["event_reports"]), 20)
self.assertNotIn("next_token", channel.json_body) self.assertNotIn("next_token", channel.json_body)
@ -332,7 +352,7 @@ class EventReportsTestCase(unittest.HomeserverTestCase):
access_token=self.admin_user_tok, access_token=self.admin_user_tok,
) )
self.assertEqual(200, int(channel.result["code"]), msg=channel.result["body"]) self.assertEqual(HTTPStatus.OK, channel.code, msg=channel.json_body)
self.assertEqual(channel.json_body["total"], 20) self.assertEqual(channel.json_body["total"], 20)
self.assertEqual(len(channel.json_body["event_reports"]), 20) self.assertEqual(len(channel.json_body["event_reports"]), 20)
self.assertNotIn("next_token", channel.json_body) self.assertNotIn("next_token", channel.json_body)
@ -345,7 +365,7 @@ class EventReportsTestCase(unittest.HomeserverTestCase):
access_token=self.admin_user_tok, access_token=self.admin_user_tok,
) )
self.assertEqual(200, int(channel.result["code"]), msg=channel.result["body"]) self.assertEqual(HTTPStatus.OK, channel.code, msg=channel.json_body)
self.assertEqual(channel.json_body["total"], 20) self.assertEqual(channel.json_body["total"], 20)
self.assertEqual(len(channel.json_body["event_reports"]), 19) self.assertEqual(len(channel.json_body["event_reports"]), 19)
self.assertEqual(channel.json_body["next_token"], 19) self.assertEqual(channel.json_body["next_token"], 19)
@ -359,7 +379,7 @@ class EventReportsTestCase(unittest.HomeserverTestCase):
access_token=self.admin_user_tok, access_token=self.admin_user_tok,
) )
self.assertEqual(200, int(channel.result["code"]), msg=channel.result["body"]) self.assertEqual(HTTPStatus.OK, channel.code, msg=channel.json_body)
self.assertEqual(channel.json_body["total"], 20) self.assertEqual(channel.json_body["total"], 20)
self.assertEqual(len(channel.json_body["event_reports"]), 1) self.assertEqual(len(channel.json_body["event_reports"]), 1)
self.assertNotIn("next_token", channel.json_body) self.assertNotIn("next_token", channel.json_body)
@ -372,10 +392,10 @@ class EventReportsTestCase(unittest.HomeserverTestCase):
channel = self.make_request( channel = self.make_request(
"POST", "POST",
"rooms/%s/report/%s" % (room_id, event_id), "rooms/%s/report/%s" % (room_id, event_id),
json.dumps({"score": -100, "reason": "this makes me sad"}), {"score": -100, "reason": "this makes me sad"},
access_token=user_tok, access_token=user_tok,
) )
self.assertEqual(200, int(channel.result["code"]), msg=channel.result["body"]) self.assertEqual(HTTPStatus.OK, channel.code, msg=channel.json_body)
def _create_event_and_report_without_parameters(self, room_id, user_tok): def _create_event_and_report_without_parameters(self, room_id, user_tok):
"""Create and report an event, but omit reason and score""" """Create and report an event, but omit reason and score"""
@ -385,10 +405,10 @@ class EventReportsTestCase(unittest.HomeserverTestCase):
channel = self.make_request( channel = self.make_request(
"POST", "POST",
"rooms/%s/report/%s" % (room_id, event_id), "rooms/%s/report/%s" % (room_id, event_id),
json.dumps({}), {},
access_token=user_tok, access_token=user_tok,
) )
self.assertEqual(200, int(channel.result["code"]), msg=channel.result["body"]) self.assertEqual(HTTPStatus.OK, channel.code, msg=channel.json_body)
def _check_fields(self, content): def _check_fields(self, content):
"""Checks that all attributes are present in an event report""" """Checks that all attributes are present in an event report"""
@ -439,12 +459,16 @@ class EventReportDetailTestCase(unittest.HomeserverTestCase):
""" """
channel = self.make_request("GET", self.url, b"{}") channel = self.make_request("GET", self.url, b"{}")
self.assertEqual(401, int(channel.result["code"]), msg=channel.result["body"]) self.assertEqual(
HTTPStatus.UNAUTHORIZED,
channel.code,
msg=channel.json_body,
)
self.assertEqual(Codes.MISSING_TOKEN, channel.json_body["errcode"]) self.assertEqual(Codes.MISSING_TOKEN, channel.json_body["errcode"])
def test_requester_is_no_admin(self): def test_requester_is_no_admin(self):
""" """
If the user is not a server admin, an error 403 is returned. If the user is not a server admin, an error HTTPStatus.FORBIDDEN is returned.
""" """
channel = self.make_request( channel = self.make_request(
@ -453,7 +477,11 @@ class EventReportDetailTestCase(unittest.HomeserverTestCase):
access_token=self.other_user_tok, access_token=self.other_user_tok,
) )
self.assertEqual(403, int(channel.result["code"]), msg=channel.result["body"]) self.assertEqual(
HTTPStatus.FORBIDDEN,
channel.code,
msg=channel.json_body,
)
self.assertEqual(Codes.FORBIDDEN, channel.json_body["errcode"]) self.assertEqual(Codes.FORBIDDEN, channel.json_body["errcode"])
def test_default_success(self): def test_default_success(self):
@ -467,12 +495,12 @@ class EventReportDetailTestCase(unittest.HomeserverTestCase):
access_token=self.admin_user_tok, access_token=self.admin_user_tok,
) )
self.assertEqual(200, int(channel.result["code"]), msg=channel.result["body"]) self.assertEqual(HTTPStatus.OK, channel.code, msg=channel.json_body)
self._check_fields(channel.json_body) self._check_fields(channel.json_body)
def test_invalid_report_id(self): def test_invalid_report_id(self):
""" """
Testing that an invalid `report_id` returns a 400. Testing that an invalid `report_id` returns a HTTPStatus.BAD_REQUEST.
""" """
# `report_id` is negative # `report_id` is negative
@ -482,7 +510,11 @@ class EventReportDetailTestCase(unittest.HomeserverTestCase):
access_token=self.admin_user_tok, access_token=self.admin_user_tok,
) )
self.assertEqual(400, int(channel.result["code"]), msg=channel.result["body"]) self.assertEqual(
HTTPStatus.BAD_REQUEST,
channel.code,
msg=channel.json_body,
)
self.assertEqual(Codes.INVALID_PARAM, channel.json_body["errcode"]) self.assertEqual(Codes.INVALID_PARAM, channel.json_body["errcode"])
self.assertEqual( self.assertEqual(
"The report_id parameter must be a string representing a positive integer.", "The report_id parameter must be a string representing a positive integer.",
@ -496,7 +528,11 @@ class EventReportDetailTestCase(unittest.HomeserverTestCase):
access_token=self.admin_user_tok, access_token=self.admin_user_tok,
) )
self.assertEqual(400, int(channel.result["code"]), msg=channel.result["body"]) self.assertEqual(
HTTPStatus.BAD_REQUEST,
channel.code,
msg=channel.json_body,
)
self.assertEqual(Codes.INVALID_PARAM, channel.json_body["errcode"]) self.assertEqual(Codes.INVALID_PARAM, channel.json_body["errcode"])
self.assertEqual( self.assertEqual(
"The report_id parameter must be a string representing a positive integer.", "The report_id parameter must be a string representing a positive integer.",
@ -510,7 +546,11 @@ class EventReportDetailTestCase(unittest.HomeserverTestCase):
access_token=self.admin_user_tok, access_token=self.admin_user_tok,
) )
self.assertEqual(400, int(channel.result["code"]), msg=channel.result["body"]) self.assertEqual(
HTTPStatus.BAD_REQUEST,
channel.code,
msg=channel.json_body,
)
self.assertEqual(Codes.INVALID_PARAM, channel.json_body["errcode"]) self.assertEqual(Codes.INVALID_PARAM, channel.json_body["errcode"])
self.assertEqual( self.assertEqual(
"The report_id parameter must be a string representing a positive integer.", "The report_id parameter must be a string representing a positive integer.",
@ -519,7 +559,7 @@ class EventReportDetailTestCase(unittest.HomeserverTestCase):
def test_report_id_not_found(self): def test_report_id_not_found(self):
""" """
Testing that a not existing `report_id` returns a 404. Testing that a not existing `report_id` returns a HTTPStatus.NOT_FOUND.
""" """
channel = self.make_request( channel = self.make_request(
@ -528,7 +568,11 @@ class EventReportDetailTestCase(unittest.HomeserverTestCase):
access_token=self.admin_user_tok, access_token=self.admin_user_tok,
) )
self.assertEqual(404, int(channel.result["code"]), msg=channel.result["body"]) self.assertEqual(
HTTPStatus.NOT_FOUND,
channel.code,
msg=channel.json_body,
)
self.assertEqual(Codes.NOT_FOUND, channel.json_body["errcode"]) self.assertEqual(Codes.NOT_FOUND, channel.json_body["errcode"])
self.assertEqual("Event report not found", channel.json_body["error"]) self.assertEqual("Event report not found", channel.json_body["error"])
@ -540,10 +584,10 @@ class EventReportDetailTestCase(unittest.HomeserverTestCase):
channel = self.make_request( channel = self.make_request(
"POST", "POST",
"rooms/%s/report/%s" % (room_id, event_id), "rooms/%s/report/%s" % (room_id, event_id),
json.dumps({"score": -100, "reason": "this makes me sad"}), {"score": -100, "reason": "this makes me sad"},
access_token=user_tok, access_token=user_tok,
) )
self.assertEqual(200, int(channel.result["code"]), msg=channel.result["body"]) self.assertEqual(HTTPStatus.OK, channel.code, msg=channel.json_body)
def _check_fields(self, content): def _check_fields(self, content):
"""Checks that all attributes are present in a event report""" """Checks that all attributes are present in a event report"""

View file

@ -13,8 +13,8 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
import json
import os import os
from http import HTTPStatus
from parameterized import parameterized from parameterized import parameterized
@ -56,7 +56,11 @@ class DeleteMediaByIDTestCase(unittest.HomeserverTestCase):
channel = self.make_request("DELETE", url, b"{}") channel = self.make_request("DELETE", url, b"{}")
self.assertEqual(401, int(channel.result["code"]), msg=channel.result["body"]) self.assertEqual(
HTTPStatus.UNAUTHORIZED,
channel.code,
msg=channel.json_body,
)
self.assertEqual(Codes.MISSING_TOKEN, channel.json_body["errcode"]) self.assertEqual(Codes.MISSING_TOKEN, channel.json_body["errcode"])
def test_requester_is_no_admin(self): def test_requester_is_no_admin(self):
@ -74,12 +78,16 @@ class DeleteMediaByIDTestCase(unittest.HomeserverTestCase):
access_token=self.other_user_token, access_token=self.other_user_token,
) )
self.assertEqual(403, int(channel.result["code"]), msg=channel.result["body"]) self.assertEqual(
HTTPStatus.FORBIDDEN,
channel.code,
msg=channel.json_body,
)
self.assertEqual(Codes.FORBIDDEN, channel.json_body["errcode"]) self.assertEqual(Codes.FORBIDDEN, channel.json_body["errcode"])
def test_media_does_not_exist(self): def test_media_does_not_exist(self):
""" """
Tests that a lookup for a media that does not exist returns a 404 Tests that a lookup for a media that does not exist returns a HTTPStatus.NOT_FOUND
""" """
url = "/_synapse/admin/v1/media/%s/%s" % (self.server_name, "12345") url = "/_synapse/admin/v1/media/%s/%s" % (self.server_name, "12345")
@ -89,12 +97,12 @@ class DeleteMediaByIDTestCase(unittest.HomeserverTestCase):
access_token=self.admin_user_tok, access_token=self.admin_user_tok,
) )
self.assertEqual(404, channel.code, msg=channel.json_body) self.assertEqual(HTTPStatus.NOT_FOUND, channel.code, msg=channel.json_body)
self.assertEqual(Codes.NOT_FOUND, channel.json_body["errcode"]) self.assertEqual(Codes.NOT_FOUND, channel.json_body["errcode"])
def test_media_is_not_local(self): def test_media_is_not_local(self):
""" """
Tests that a lookup for a media that is not a local returns a 400 Tests that a lookup for a media that is not a local returns a HTTPStatus.BAD_REQUEST
""" """
url = "/_synapse/admin/v1/media/%s/%s" % ("unknown_domain", "12345") url = "/_synapse/admin/v1/media/%s/%s" % ("unknown_domain", "12345")
@ -104,7 +112,7 @@ class DeleteMediaByIDTestCase(unittest.HomeserverTestCase):
access_token=self.admin_user_tok, access_token=self.admin_user_tok,
) )
self.assertEqual(400, channel.code, msg=channel.json_body) self.assertEqual(HTTPStatus.BAD_REQUEST, channel.code, msg=channel.json_body)
self.assertEqual("Can only delete local media", channel.json_body["error"]) self.assertEqual("Can only delete local media", channel.json_body["error"])
def test_delete_media(self): def test_delete_media(self):
@ -117,7 +125,10 @@ class DeleteMediaByIDTestCase(unittest.HomeserverTestCase):
# Upload some media into the room # Upload some media into the room
response = self.helper.upload_media( response = self.helper.upload_media(
upload_resource, SMALL_PNG, tok=self.admin_user_tok, expect_code=200 upload_resource,
SMALL_PNG,
tok=self.admin_user_tok,
expect_code=HTTPStatus.OK,
) )
# Extract media ID from the response # Extract media ID from the response
server_and_media_id = response["content_uri"][6:] # Cut off 'mxc://' server_and_media_id = response["content_uri"][6:] # Cut off 'mxc://'
@ -137,10 +148,11 @@ class DeleteMediaByIDTestCase(unittest.HomeserverTestCase):
# Should be successful # Should be successful
self.assertEqual( self.assertEqual(
200, HTTPStatus.OK,
channel.code, channel.code,
msg=( msg=(
"Expected to receive a 200 on accessing media: %s" % server_and_media_id "Expected to receive a HTTPStatus.OK on accessing media: %s"
% server_and_media_id
), ),
) )
@ -157,7 +169,7 @@ class DeleteMediaByIDTestCase(unittest.HomeserverTestCase):
access_token=self.admin_user_tok, access_token=self.admin_user_tok,
) )
self.assertEqual(200, channel.code, msg=channel.json_body) self.assertEqual(HTTPStatus.OK, channel.code, msg=channel.json_body)
self.assertEqual(1, channel.json_body["total"]) self.assertEqual(1, channel.json_body["total"])
self.assertEqual( self.assertEqual(
media_id, media_id,
@ -174,10 +186,10 @@ class DeleteMediaByIDTestCase(unittest.HomeserverTestCase):
access_token=self.admin_user_tok, access_token=self.admin_user_tok,
) )
self.assertEqual( self.assertEqual(
404, HTTPStatus.NOT_FOUND,
channel.code, channel.code,
msg=( msg=(
"Expected to receive a 404 on accessing deleted media: %s" "Expected to receive a HTTPStatus.NOT_FOUND on accessing deleted media: %s"
% server_and_media_id % server_and_media_id
), ),
) )
@ -216,7 +228,11 @@ class DeleteMediaByDateSizeTestCase(unittest.HomeserverTestCase):
channel = self.make_request("POST", self.url, b"{}") channel = self.make_request("POST", self.url, b"{}")
self.assertEqual(401, int(channel.result["code"]), msg=channel.result["body"]) self.assertEqual(
HTTPStatus.UNAUTHORIZED,
channel.code,
msg=channel.json_body,
)
self.assertEqual(Codes.MISSING_TOKEN, channel.json_body["errcode"]) self.assertEqual(Codes.MISSING_TOKEN, channel.json_body["errcode"])
def test_requester_is_no_admin(self): def test_requester_is_no_admin(self):
@ -232,12 +248,16 @@ class DeleteMediaByDateSizeTestCase(unittest.HomeserverTestCase):
access_token=self.other_user_token, access_token=self.other_user_token,
) )
self.assertEqual(403, int(channel.result["code"]), msg=channel.result["body"]) self.assertEqual(
HTTPStatus.FORBIDDEN,
channel.code,
msg=channel.json_body,
)
self.assertEqual(Codes.FORBIDDEN, channel.json_body["errcode"]) self.assertEqual(Codes.FORBIDDEN, channel.json_body["errcode"])
def test_media_is_not_local(self): def test_media_is_not_local(self):
""" """
Tests that a lookup for media that is not local returns a 400 Tests that a lookup for media that is not local returns a HTTPStatus.BAD_REQUEST
""" """
url = "/_synapse/admin/v1/media/%s/delete" % "unknown_domain" url = "/_synapse/admin/v1/media/%s/delete" % "unknown_domain"
@ -247,7 +267,7 @@ class DeleteMediaByDateSizeTestCase(unittest.HomeserverTestCase):
access_token=self.admin_user_tok, access_token=self.admin_user_tok,
) )
self.assertEqual(400, channel.code, msg=channel.json_body) self.assertEqual(HTTPStatus.BAD_REQUEST, channel.code, msg=channel.json_body)
self.assertEqual("Can only delete local media", channel.json_body["error"]) self.assertEqual("Can only delete local media", channel.json_body["error"])
def test_missing_parameter(self): def test_missing_parameter(self):
@ -260,7 +280,11 @@ class DeleteMediaByDateSizeTestCase(unittest.HomeserverTestCase):
access_token=self.admin_user_tok, access_token=self.admin_user_tok,
) )
self.assertEqual(400, int(channel.result["code"]), msg=channel.result["body"]) self.assertEqual(
HTTPStatus.BAD_REQUEST,
channel.code,
msg=channel.json_body,
)
self.assertEqual(Codes.MISSING_PARAM, channel.json_body["errcode"]) self.assertEqual(Codes.MISSING_PARAM, channel.json_body["errcode"])
self.assertEqual( self.assertEqual(
"Missing integer query parameter 'before_ts'", channel.json_body["error"] "Missing integer query parameter 'before_ts'", channel.json_body["error"]
@ -276,7 +300,11 @@ class DeleteMediaByDateSizeTestCase(unittest.HomeserverTestCase):
access_token=self.admin_user_tok, access_token=self.admin_user_tok,
) )
self.assertEqual(400, int(channel.result["code"]), msg=channel.result["body"]) self.assertEqual(
HTTPStatus.BAD_REQUEST,
channel.code,
msg=channel.json_body,
)
self.assertEqual(Codes.INVALID_PARAM, channel.json_body["errcode"]) self.assertEqual(Codes.INVALID_PARAM, channel.json_body["errcode"])
self.assertEqual( self.assertEqual(
"Query parameter before_ts must be a positive integer.", "Query parameter before_ts must be a positive integer.",
@ -289,7 +317,11 @@ class DeleteMediaByDateSizeTestCase(unittest.HomeserverTestCase):
access_token=self.admin_user_tok, access_token=self.admin_user_tok,
) )
self.assertEqual(400, int(channel.result["code"]), msg=channel.result["body"]) self.assertEqual(
HTTPStatus.BAD_REQUEST,
channel.code,
msg=channel.json_body,
)
self.assertEqual(Codes.INVALID_PARAM, channel.json_body["errcode"]) self.assertEqual(Codes.INVALID_PARAM, channel.json_body["errcode"])
self.assertEqual( self.assertEqual(
"Query parameter before_ts you provided is from the year 1970. " "Query parameter before_ts you provided is from the year 1970. "
@ -303,7 +335,11 @@ class DeleteMediaByDateSizeTestCase(unittest.HomeserverTestCase):
access_token=self.admin_user_tok, access_token=self.admin_user_tok,
) )
self.assertEqual(400, int(channel.result["code"]), msg=channel.result["body"]) self.assertEqual(
HTTPStatus.BAD_REQUEST,
channel.code,
msg=channel.json_body,
)
self.assertEqual(Codes.INVALID_PARAM, channel.json_body["errcode"]) self.assertEqual(Codes.INVALID_PARAM, channel.json_body["errcode"])
self.assertEqual( self.assertEqual(
"Query parameter size_gt must be a string representing a positive integer.", "Query parameter size_gt must be a string representing a positive integer.",
@ -316,7 +352,11 @@ class DeleteMediaByDateSizeTestCase(unittest.HomeserverTestCase):
access_token=self.admin_user_tok, access_token=self.admin_user_tok,
) )
self.assertEqual(400, int(channel.result["code"]), msg=channel.result["body"]) self.assertEqual(
HTTPStatus.BAD_REQUEST,
channel.code,
msg=channel.json_body,
)
self.assertEqual(Codes.UNKNOWN, channel.json_body["errcode"]) self.assertEqual(Codes.UNKNOWN, channel.json_body["errcode"])
self.assertEqual( self.assertEqual(
"Boolean query parameter 'keep_profiles' must be one of ['true', 'false']", "Boolean query parameter 'keep_profiles' must be one of ['true', 'false']",
@ -345,7 +385,7 @@ class DeleteMediaByDateSizeTestCase(unittest.HomeserverTestCase):
self.url + "?before_ts=" + str(now_ms), self.url + "?before_ts=" + str(now_ms),
access_token=self.admin_user_tok, access_token=self.admin_user_tok,
) )
self.assertEqual(200, channel.code, msg=channel.json_body) self.assertEqual(HTTPStatus.OK, channel.code, msg=channel.json_body)
self.assertEqual(1, channel.json_body["total"]) self.assertEqual(1, channel.json_body["total"])
self.assertEqual( self.assertEqual(
media_id, media_id,
@ -370,7 +410,7 @@ class DeleteMediaByDateSizeTestCase(unittest.HomeserverTestCase):
self.url + "?before_ts=" + str(now_ms), self.url + "?before_ts=" + str(now_ms),
access_token=self.admin_user_tok, access_token=self.admin_user_tok,
) )
self.assertEqual(200, channel.code, msg=channel.json_body) self.assertEqual(HTTPStatus.OK, channel.code, msg=channel.json_body)
self.assertEqual(0, channel.json_body["total"]) self.assertEqual(0, channel.json_body["total"])
self._access_media(server_and_media_id) self._access_media(server_and_media_id)
@ -382,7 +422,7 @@ class DeleteMediaByDateSizeTestCase(unittest.HomeserverTestCase):
self.url + "?before_ts=" + str(now_ms), self.url + "?before_ts=" + str(now_ms),
access_token=self.admin_user_tok, access_token=self.admin_user_tok,
) )
self.assertEqual(200, channel.code, msg=channel.json_body) self.assertEqual(HTTPStatus.OK, channel.code, msg=channel.json_body)
self.assertEqual(1, channel.json_body["total"]) self.assertEqual(1, channel.json_body["total"])
self.assertEqual( self.assertEqual(
server_and_media_id.split("/")[1], server_and_media_id.split("/")[1],
@ -406,7 +446,7 @@ class DeleteMediaByDateSizeTestCase(unittest.HomeserverTestCase):
self.url + "?before_ts=" + str(now_ms) + "&size_gt=67", self.url + "?before_ts=" + str(now_ms) + "&size_gt=67",
access_token=self.admin_user_tok, access_token=self.admin_user_tok,
) )
self.assertEqual(200, channel.code, msg=channel.json_body) self.assertEqual(HTTPStatus.OK, channel.code, msg=channel.json_body)
self.assertEqual(0, channel.json_body["total"]) self.assertEqual(0, channel.json_body["total"])
self._access_media(server_and_media_id) self._access_media(server_and_media_id)
@ -417,7 +457,7 @@ class DeleteMediaByDateSizeTestCase(unittest.HomeserverTestCase):
self.url + "?before_ts=" + str(now_ms) + "&size_gt=66", self.url + "?before_ts=" + str(now_ms) + "&size_gt=66",
access_token=self.admin_user_tok, access_token=self.admin_user_tok,
) )
self.assertEqual(200, channel.code, msg=channel.json_body) self.assertEqual(HTTPStatus.OK, channel.code, msg=channel.json_body)
self.assertEqual(1, channel.json_body["total"]) self.assertEqual(1, channel.json_body["total"])
self.assertEqual( self.assertEqual(
server_and_media_id.split("/")[1], server_and_media_id.split("/")[1],
@ -439,10 +479,10 @@ class DeleteMediaByDateSizeTestCase(unittest.HomeserverTestCase):
channel = self.make_request( channel = self.make_request(
"PUT", "PUT",
"/profile/%s/avatar_url" % (self.admin_user,), "/profile/%s/avatar_url" % (self.admin_user,),
content=json.dumps({"avatar_url": "mxc://%s" % (server_and_media_id,)}), content={"avatar_url": "mxc://%s" % (server_and_media_id,)},
access_token=self.admin_user_tok, access_token=self.admin_user_tok,
) )
self.assertEqual(200, channel.code, msg=channel.json_body) self.assertEqual(HTTPStatus.OK, channel.code, msg=channel.json_body)
now_ms = self.clock.time_msec() now_ms = self.clock.time_msec()
channel = self.make_request( channel = self.make_request(
@ -450,7 +490,7 @@ class DeleteMediaByDateSizeTestCase(unittest.HomeserverTestCase):
self.url + "?before_ts=" + str(now_ms) + "&keep_profiles=true", self.url + "?before_ts=" + str(now_ms) + "&keep_profiles=true",
access_token=self.admin_user_tok, access_token=self.admin_user_tok,
) )
self.assertEqual(200, channel.code, msg=channel.json_body) self.assertEqual(HTTPStatus.OK, channel.code, msg=channel.json_body)
self.assertEqual(0, channel.json_body["total"]) self.assertEqual(0, channel.json_body["total"])
self._access_media(server_and_media_id) self._access_media(server_and_media_id)
@ -461,7 +501,7 @@ class DeleteMediaByDateSizeTestCase(unittest.HomeserverTestCase):
self.url + "?before_ts=" + str(now_ms) + "&keep_profiles=false", self.url + "?before_ts=" + str(now_ms) + "&keep_profiles=false",
access_token=self.admin_user_tok, access_token=self.admin_user_tok,
) )
self.assertEqual(200, channel.code, msg=channel.json_body) self.assertEqual(HTTPStatus.OK, channel.code, msg=channel.json_body)
self.assertEqual(1, channel.json_body["total"]) self.assertEqual(1, channel.json_body["total"])
self.assertEqual( self.assertEqual(
server_and_media_id.split("/")[1], server_and_media_id.split("/")[1],
@ -484,10 +524,10 @@ class DeleteMediaByDateSizeTestCase(unittest.HomeserverTestCase):
channel = self.make_request( channel = self.make_request(
"PUT", "PUT",
"/rooms/%s/state/m.room.avatar" % (room_id,), "/rooms/%s/state/m.room.avatar" % (room_id,),
content=json.dumps({"url": "mxc://%s" % (server_and_media_id,)}), content={"url": "mxc://%s" % (server_and_media_id,)},
access_token=self.admin_user_tok, access_token=self.admin_user_tok,
) )
self.assertEqual(200, channel.code, msg=channel.json_body) self.assertEqual(HTTPStatus.OK, channel.code, msg=channel.json_body)
now_ms = self.clock.time_msec() now_ms = self.clock.time_msec()
channel = self.make_request( channel = self.make_request(
@ -495,7 +535,7 @@ class DeleteMediaByDateSizeTestCase(unittest.HomeserverTestCase):
self.url + "?before_ts=" + str(now_ms) + "&keep_profiles=true", self.url + "?before_ts=" + str(now_ms) + "&keep_profiles=true",
access_token=self.admin_user_tok, access_token=self.admin_user_tok,
) )
self.assertEqual(200, channel.code, msg=channel.json_body) self.assertEqual(HTTPStatus.OK, channel.code, msg=channel.json_body)
self.assertEqual(0, channel.json_body["total"]) self.assertEqual(0, channel.json_body["total"])
self._access_media(server_and_media_id) self._access_media(server_and_media_id)
@ -506,7 +546,7 @@ class DeleteMediaByDateSizeTestCase(unittest.HomeserverTestCase):
self.url + "?before_ts=" + str(now_ms) + "&keep_profiles=false", self.url + "?before_ts=" + str(now_ms) + "&keep_profiles=false",
access_token=self.admin_user_tok, access_token=self.admin_user_tok,
) )
self.assertEqual(200, channel.code, msg=channel.json_body) self.assertEqual(HTTPStatus.OK, channel.code, msg=channel.json_body)
self.assertEqual(1, channel.json_body["total"]) self.assertEqual(1, channel.json_body["total"])
self.assertEqual( self.assertEqual(
server_and_media_id.split("/")[1], server_and_media_id.split("/")[1],
@ -523,7 +563,10 @@ class DeleteMediaByDateSizeTestCase(unittest.HomeserverTestCase):
# Upload some media into the room # Upload some media into the room
response = self.helper.upload_media( response = self.helper.upload_media(
upload_resource, SMALL_PNG, tok=self.admin_user_tok, expect_code=200 upload_resource,
SMALL_PNG,
tok=self.admin_user_tok,
expect_code=HTTPStatus.OK,
) )
# Extract media ID from the response # Extract media ID from the response
server_and_media_id = response["content_uri"][6:] # Cut off 'mxc://' server_and_media_id = response["content_uri"][6:] # Cut off 'mxc://'
@ -554,10 +597,10 @@ class DeleteMediaByDateSizeTestCase(unittest.HomeserverTestCase):
if expect_success: if expect_success:
self.assertEqual( self.assertEqual(
200, HTTPStatus.OK,
channel.code, channel.code,
msg=( msg=(
"Expected to receive a 200 on accessing media: %s" "Expected to receive a HTTPStatus.OK on accessing media: %s"
% server_and_media_id % server_and_media_id
), ),
) )
@ -565,10 +608,10 @@ class DeleteMediaByDateSizeTestCase(unittest.HomeserverTestCase):
self.assertTrue(os.path.exists(local_path)) self.assertTrue(os.path.exists(local_path))
else: else:
self.assertEqual( self.assertEqual(
404, HTTPStatus.NOT_FOUND,
channel.code, channel.code,
msg=( msg=(
"Expected to receive a 404 on accessing deleted media: %s" "Expected to receive a HTTPStatus.NOT_FOUND on accessing deleted media: %s"
% (server_and_media_id) % (server_and_media_id)
), ),
) )
@ -597,7 +640,10 @@ class QuarantineMediaByIDTestCase(unittest.HomeserverTestCase):
# Upload some media into the room # Upload some media into the room
response = self.helper.upload_media( response = self.helper.upload_media(
upload_resource, SMALL_PNG, tok=self.admin_user_tok, expect_code=200 upload_resource,
SMALL_PNG,
tok=self.admin_user_tok,
expect_code=HTTPStatus.OK,
) )
# Extract media ID from the response # Extract media ID from the response
server_and_media_id = response["content_uri"][6:] # Cut off 'mxc://' server_and_media_id = response["content_uri"][6:] # Cut off 'mxc://'
@ -617,7 +663,11 @@ class QuarantineMediaByIDTestCase(unittest.HomeserverTestCase):
b"{}", b"{}",
) )
self.assertEqual(401, int(channel.result["code"]), msg=channel.result["body"]) self.assertEqual(
HTTPStatus.UNAUTHORIZED,
channel.code,
msg=channel.json_body,
)
self.assertEqual(Codes.MISSING_TOKEN, channel.json_body["errcode"]) self.assertEqual(Codes.MISSING_TOKEN, channel.json_body["errcode"])
@parameterized.expand(["quarantine", "unquarantine"]) @parameterized.expand(["quarantine", "unquarantine"])
@ -634,7 +684,11 @@ class QuarantineMediaByIDTestCase(unittest.HomeserverTestCase):
access_token=self.other_user_token, access_token=self.other_user_token,
) )
self.assertEqual(403, int(channel.result["code"]), msg=channel.result["body"]) self.assertEqual(
HTTPStatus.FORBIDDEN,
channel.code,
msg=channel.json_body,
)
self.assertEqual(Codes.FORBIDDEN, channel.json_body["errcode"]) self.assertEqual(Codes.FORBIDDEN, channel.json_body["errcode"])
def test_quarantine_media(self): def test_quarantine_media(self):
@ -652,7 +706,7 @@ class QuarantineMediaByIDTestCase(unittest.HomeserverTestCase):
access_token=self.admin_user_tok, access_token=self.admin_user_tok,
) )
self.assertEqual(200, channel.code, msg=channel.json_body) self.assertEqual(HTTPStatus.OK, channel.code, msg=channel.json_body)
self.assertFalse(channel.json_body) self.assertFalse(channel.json_body)
media_info = self.get_success(self.store.get_local_media(self.media_id)) media_info = self.get_success(self.store.get_local_media(self.media_id))
@ -665,7 +719,7 @@ class QuarantineMediaByIDTestCase(unittest.HomeserverTestCase):
access_token=self.admin_user_tok, access_token=self.admin_user_tok,
) )
self.assertEqual(200, channel.code, msg=channel.json_body) self.assertEqual(HTTPStatus.OK, channel.code, msg=channel.json_body)
self.assertFalse(channel.json_body) self.assertFalse(channel.json_body)
media_info = self.get_success(self.store.get_local_media(self.media_id)) media_info = self.get_success(self.store.get_local_media(self.media_id))
@ -690,7 +744,7 @@ class QuarantineMediaByIDTestCase(unittest.HomeserverTestCase):
access_token=self.admin_user_tok, access_token=self.admin_user_tok,
) )
self.assertEqual(200, channel.code, msg=channel.json_body) self.assertEqual(HTTPStatus.OK, channel.code, msg=channel.json_body)
self.assertFalse(channel.json_body) self.assertFalse(channel.json_body)
# verify that is not in quarantine # verify that is not in quarantine
@ -718,7 +772,10 @@ class ProtectMediaByIDTestCase(unittest.HomeserverTestCase):
# Upload some media into the room # Upload some media into the room
response = self.helper.upload_media( response = self.helper.upload_media(
upload_resource, SMALL_PNG, tok=self.admin_user_tok, expect_code=200 upload_resource,
SMALL_PNG,
tok=self.admin_user_tok,
expect_code=HTTPStatus.OK,
) )
# Extract media ID from the response # Extract media ID from the response
server_and_media_id = response["content_uri"][6:] # Cut off 'mxc://' server_and_media_id = response["content_uri"][6:] # Cut off 'mxc://'
@ -734,7 +791,11 @@ class ProtectMediaByIDTestCase(unittest.HomeserverTestCase):
channel = self.make_request("POST", self.url % (action, self.media_id), b"{}") channel = self.make_request("POST", self.url % (action, self.media_id), b"{}")
self.assertEqual(401, int(channel.result["code"]), msg=channel.result["body"]) self.assertEqual(
HTTPStatus.UNAUTHORIZED,
channel.code,
msg=channel.json_body,
)
self.assertEqual(Codes.MISSING_TOKEN, channel.json_body["errcode"]) self.assertEqual(Codes.MISSING_TOKEN, channel.json_body["errcode"])
@parameterized.expand(["protect", "unprotect"]) @parameterized.expand(["protect", "unprotect"])
@ -751,7 +812,11 @@ class ProtectMediaByIDTestCase(unittest.HomeserverTestCase):
access_token=self.other_user_token, access_token=self.other_user_token,
) )
self.assertEqual(403, int(channel.result["code"]), msg=channel.result["body"]) self.assertEqual(
HTTPStatus.FORBIDDEN,
channel.code,
msg=channel.json_body,
)
self.assertEqual(Codes.FORBIDDEN, channel.json_body["errcode"]) self.assertEqual(Codes.FORBIDDEN, channel.json_body["errcode"])
def test_protect_media(self): def test_protect_media(self):
@ -769,7 +834,7 @@ class ProtectMediaByIDTestCase(unittest.HomeserverTestCase):
access_token=self.admin_user_tok, access_token=self.admin_user_tok,
) )
self.assertEqual(200, channel.code, msg=channel.json_body) self.assertEqual(HTTPStatus.OK, channel.code, msg=channel.json_body)
self.assertFalse(channel.json_body) self.assertFalse(channel.json_body)
media_info = self.get_success(self.store.get_local_media(self.media_id)) media_info = self.get_success(self.store.get_local_media(self.media_id))
@ -782,7 +847,7 @@ class ProtectMediaByIDTestCase(unittest.HomeserverTestCase):
access_token=self.admin_user_tok, access_token=self.admin_user_tok,
) )
self.assertEqual(200, channel.code, msg=channel.json_body) self.assertEqual(HTTPStatus.OK, channel.code, msg=channel.json_body)
self.assertFalse(channel.json_body) self.assertFalse(channel.json_body)
media_info = self.get_success(self.store.get_local_media(self.media_id)) media_info = self.get_success(self.store.get_local_media(self.media_id))
@ -816,7 +881,11 @@ class PurgeMediaCacheTestCase(unittest.HomeserverTestCase):
channel = self.make_request("POST", self.url, b"{}") channel = self.make_request("POST", self.url, b"{}")
self.assertEqual(401, int(channel.result["code"]), msg=channel.result["body"]) self.assertEqual(
HTTPStatus.UNAUTHORIZED,
channel.code,
msg=channel.json_body,
)
self.assertEqual(Codes.MISSING_TOKEN, channel.json_body["errcode"]) self.assertEqual(Codes.MISSING_TOKEN, channel.json_body["errcode"])
def test_requester_is_not_admin(self): def test_requester_is_not_admin(self):
@ -832,7 +901,11 @@ class PurgeMediaCacheTestCase(unittest.HomeserverTestCase):
access_token=self.other_user_token, access_token=self.other_user_token,
) )
self.assertEqual(403, int(channel.result["code"]), msg=channel.result["body"]) self.assertEqual(
HTTPStatus.FORBIDDEN,
channel.code,
msg=channel.json_body,
)
self.assertEqual(Codes.FORBIDDEN, channel.json_body["errcode"]) self.assertEqual(Codes.FORBIDDEN, channel.json_body["errcode"])
def test_invalid_parameter(self): def test_invalid_parameter(self):
@ -845,7 +918,11 @@ class PurgeMediaCacheTestCase(unittest.HomeserverTestCase):
access_token=self.admin_user_tok, access_token=self.admin_user_tok,
) )
self.assertEqual(400, int(channel.result["code"]), msg=channel.result["body"]) self.assertEqual(
HTTPStatus.BAD_REQUEST,
channel.code,
msg=channel.json_body,
)
self.assertEqual(Codes.INVALID_PARAM, channel.json_body["errcode"]) self.assertEqual(Codes.INVALID_PARAM, channel.json_body["errcode"])
self.assertEqual( self.assertEqual(
"Query parameter before_ts must be a positive integer.", "Query parameter before_ts must be a positive integer.",
@ -858,7 +935,11 @@ class PurgeMediaCacheTestCase(unittest.HomeserverTestCase):
access_token=self.admin_user_tok, access_token=self.admin_user_tok,
) )
self.assertEqual(400, int(channel.result["code"]), msg=channel.result["body"]) self.assertEqual(
HTTPStatus.BAD_REQUEST,
channel.code,
msg=channel.json_body,
)
self.assertEqual(Codes.INVALID_PARAM, channel.json_body["errcode"]) self.assertEqual(Codes.INVALID_PARAM, channel.json_body["errcode"])
self.assertEqual( self.assertEqual(
"Query parameter before_ts you provided is from the year 1970. " "Query parameter before_ts you provided is from the year 1970. "

View file

@ -14,6 +14,7 @@
import random import random
import string import string
from http import HTTPStatus
import synapse.rest.admin import synapse.rest.admin
from synapse.api.errors import Codes from synapse.api.errors import Codes
@ -63,7 +64,11 @@ class ManageRegistrationTokensTestCase(unittest.HomeserverTestCase):
def test_create_no_auth(self): def test_create_no_auth(self):
"""Try to create a token without authentication.""" """Try to create a token without authentication."""
channel = self.make_request("POST", self.url + "/new", {}) channel = self.make_request("POST", self.url + "/new", {})
self.assertEqual(401, int(channel.result["code"]), msg=channel.result["body"]) self.assertEqual(
HTTPStatus.UNAUTHORIZED,
channel.code,
msg=channel.json_body,
)
self.assertEqual(Codes.MISSING_TOKEN, channel.json_body["errcode"]) self.assertEqual(Codes.MISSING_TOKEN, channel.json_body["errcode"])
def test_create_requester_not_admin(self): def test_create_requester_not_admin(self):
@ -74,7 +79,11 @@ class ManageRegistrationTokensTestCase(unittest.HomeserverTestCase):
{}, {},
access_token=self.other_user_tok, access_token=self.other_user_tok,
) )
self.assertEqual(403, int(channel.result["code"]), msg=channel.result["body"]) self.assertEqual(
HTTPStatus.FORBIDDEN,
channel.code,
msg=channel.json_body,
)
self.assertEqual(Codes.FORBIDDEN, channel.json_body["errcode"]) self.assertEqual(Codes.FORBIDDEN, channel.json_body["errcode"])
def test_create_using_defaults(self): def test_create_using_defaults(self):
@ -86,7 +95,7 @@ class ManageRegistrationTokensTestCase(unittest.HomeserverTestCase):
access_token=self.admin_user_tok, access_token=self.admin_user_tok,
) )
self.assertEqual(200, int(channel.result["code"]), msg=channel.result["body"]) self.assertEqual(HTTPStatus.OK, channel.code, msg=channel.json_body)
self.assertEqual(len(channel.json_body["token"]), 16) self.assertEqual(len(channel.json_body["token"]), 16)
self.assertIsNone(channel.json_body["uses_allowed"]) self.assertIsNone(channel.json_body["uses_allowed"])
self.assertIsNone(channel.json_body["expiry_time"]) self.assertIsNone(channel.json_body["expiry_time"])
@ -110,7 +119,7 @@ class ManageRegistrationTokensTestCase(unittest.HomeserverTestCase):
access_token=self.admin_user_tok, access_token=self.admin_user_tok,
) )
self.assertEqual(200, int(channel.result["code"]), msg=channel.result["body"]) self.assertEqual(HTTPStatus.OK, channel.code, msg=channel.json_body)
self.assertEqual(channel.json_body["token"], token) self.assertEqual(channel.json_body["token"], token)
self.assertEqual(channel.json_body["uses_allowed"], 1) self.assertEqual(channel.json_body["uses_allowed"], 1)
self.assertEqual(channel.json_body["expiry_time"], data["expiry_time"]) self.assertEqual(channel.json_body["expiry_time"], data["expiry_time"])
@ -131,7 +140,7 @@ class ManageRegistrationTokensTestCase(unittest.HomeserverTestCase):
access_token=self.admin_user_tok, access_token=self.admin_user_tok,
) )
self.assertEqual(200, int(channel.result["code"]), msg=channel.result["body"]) self.assertEqual(HTTPStatus.OK, channel.code, msg=channel.json_body)
self.assertEqual(len(channel.json_body["token"]), 16) self.assertEqual(len(channel.json_body["token"]), 16)
self.assertIsNone(channel.json_body["uses_allowed"]) self.assertIsNone(channel.json_body["uses_allowed"])
self.assertIsNone(channel.json_body["expiry_time"]) self.assertIsNone(channel.json_body["expiry_time"])
@ -149,7 +158,11 @@ class ManageRegistrationTokensTestCase(unittest.HomeserverTestCase):
access_token=self.admin_user_tok, access_token=self.admin_user_tok,
) )
self.assertEqual(400, int(channel.result["code"]), msg=channel.result["body"]) self.assertEqual(
HTTPStatus.BAD_REQUEST,
channel.code,
msg=channel.json_body,
)
self.assertEqual(channel.json_body["errcode"], Codes.INVALID_PARAM) self.assertEqual(channel.json_body["errcode"], Codes.INVALID_PARAM)
def test_create_token_invalid_chars(self): def test_create_token_invalid_chars(self):
@ -165,7 +178,11 @@ class ManageRegistrationTokensTestCase(unittest.HomeserverTestCase):
access_token=self.admin_user_tok, access_token=self.admin_user_tok,
) )
self.assertEqual(400, int(channel.result["code"]), msg=channel.result["body"]) self.assertEqual(
HTTPStatus.BAD_REQUEST,
channel.code,
msg=channel.json_body,
)
self.assertEqual(channel.json_body["errcode"], Codes.INVALID_PARAM) self.assertEqual(channel.json_body["errcode"], Codes.INVALID_PARAM)
def test_create_token_already_exists(self): def test_create_token_already_exists(self):
@ -180,7 +197,7 @@ class ManageRegistrationTokensTestCase(unittest.HomeserverTestCase):
data, data,
access_token=self.admin_user_tok, access_token=self.admin_user_tok,
) )
self.assertEqual(200, int(channel1.result["code"]), msg=channel1.result["body"]) self.assertEqual(HTTPStatus.OK, channel1.code, msg=channel1.json_body)
channel2 = self.make_request( channel2 = self.make_request(
"POST", "POST",
@ -188,7 +205,7 @@ class ManageRegistrationTokensTestCase(unittest.HomeserverTestCase):
data, data,
access_token=self.admin_user_tok, access_token=self.admin_user_tok,
) )
self.assertEqual(400, int(channel2.result["code"]), msg=channel2.result["body"]) self.assertEqual(HTTPStatus.BAD_REQUEST, channel2.code, msg=channel2.json_body)
self.assertEqual(channel2.json_body["errcode"], Codes.INVALID_PARAM) self.assertEqual(channel2.json_body["errcode"], Codes.INVALID_PARAM)
def test_create_unable_to_generate_token(self): def test_create_unable_to_generate_token(self):
@ -220,7 +237,7 @@ class ManageRegistrationTokensTestCase(unittest.HomeserverTestCase):
{"length": 1}, {"length": 1},
access_token=self.admin_user_tok, access_token=self.admin_user_tok,
) )
self.assertEqual(500, int(channel.result["code"]), msg=channel.result["body"]) self.assertEqual(500, channel.code, msg=channel.json_body)
def test_create_uses_allowed(self): def test_create_uses_allowed(self):
"""Check you can only create a token with good values for uses_allowed.""" """Check you can only create a token with good values for uses_allowed."""
@ -231,7 +248,7 @@ class ManageRegistrationTokensTestCase(unittest.HomeserverTestCase):
{"uses_allowed": 0}, {"uses_allowed": 0},
access_token=self.admin_user_tok, access_token=self.admin_user_tok,
) )
self.assertEqual(200, int(channel.result["code"]), msg=channel.result["body"]) self.assertEqual(HTTPStatus.OK, channel.code, msg=channel.json_body)
self.assertEqual(channel.json_body["uses_allowed"], 0) self.assertEqual(channel.json_body["uses_allowed"], 0)
# Should fail with negative integer # Should fail with negative integer
@ -241,7 +258,11 @@ class ManageRegistrationTokensTestCase(unittest.HomeserverTestCase):
{"uses_allowed": -5}, {"uses_allowed": -5},
access_token=self.admin_user_tok, access_token=self.admin_user_tok,
) )
self.assertEqual(400, int(channel.result["code"]), msg=channel.result["body"]) self.assertEqual(
HTTPStatus.BAD_REQUEST,
channel.code,
msg=channel.json_body,
)
self.assertEqual(channel.json_body["errcode"], Codes.INVALID_PARAM) self.assertEqual(channel.json_body["errcode"], Codes.INVALID_PARAM)
# Should fail with float # Should fail with float
@ -251,7 +272,11 @@ class ManageRegistrationTokensTestCase(unittest.HomeserverTestCase):
{"uses_allowed": 1.5}, {"uses_allowed": 1.5},
access_token=self.admin_user_tok, access_token=self.admin_user_tok,
) )
self.assertEqual(400, int(channel.result["code"]), msg=channel.result["body"]) self.assertEqual(
HTTPStatus.BAD_REQUEST,
channel.code,
msg=channel.json_body,
)
self.assertEqual(channel.json_body["errcode"], Codes.INVALID_PARAM) self.assertEqual(channel.json_body["errcode"], Codes.INVALID_PARAM)
def test_create_expiry_time(self): def test_create_expiry_time(self):
@ -263,7 +288,11 @@ class ManageRegistrationTokensTestCase(unittest.HomeserverTestCase):
{"expiry_time": self.clock.time_msec() - 10000}, {"expiry_time": self.clock.time_msec() - 10000},
access_token=self.admin_user_tok, access_token=self.admin_user_tok,
) )
self.assertEqual(400, int(channel.result["code"]), msg=channel.result["body"]) self.assertEqual(
HTTPStatus.BAD_REQUEST,
channel.code,
msg=channel.json_body,
)
self.assertEqual(channel.json_body["errcode"], Codes.INVALID_PARAM) self.assertEqual(channel.json_body["errcode"], Codes.INVALID_PARAM)
# Should fail with float # Should fail with float
@ -273,7 +302,11 @@ class ManageRegistrationTokensTestCase(unittest.HomeserverTestCase):
{"expiry_time": self.clock.time_msec() + 1000000.5}, {"expiry_time": self.clock.time_msec() + 1000000.5},
access_token=self.admin_user_tok, access_token=self.admin_user_tok,
) )
self.assertEqual(400, int(channel.result["code"]), msg=channel.result["body"]) self.assertEqual(
HTTPStatus.BAD_REQUEST,
channel.code,
msg=channel.json_body,
)
self.assertEqual(channel.json_body["errcode"], Codes.INVALID_PARAM) self.assertEqual(channel.json_body["errcode"], Codes.INVALID_PARAM)
def test_create_length(self): def test_create_length(self):
@ -285,7 +318,7 @@ class ManageRegistrationTokensTestCase(unittest.HomeserverTestCase):
{"length": 64}, {"length": 64},
access_token=self.admin_user_tok, access_token=self.admin_user_tok,
) )
self.assertEqual(200, int(channel.result["code"]), msg=channel.result["body"]) self.assertEqual(HTTPStatus.OK, channel.code, msg=channel.json_body)
self.assertEqual(len(channel.json_body["token"]), 64) self.assertEqual(len(channel.json_body["token"]), 64)
# Should fail with 0 # Should fail with 0
@ -295,7 +328,11 @@ class ManageRegistrationTokensTestCase(unittest.HomeserverTestCase):
{"length": 0}, {"length": 0},
access_token=self.admin_user_tok, access_token=self.admin_user_tok,
) )
self.assertEqual(400, int(channel.result["code"]), msg=channel.result["body"]) self.assertEqual(
HTTPStatus.BAD_REQUEST,
channel.code,
msg=channel.json_body,
)
self.assertEqual(channel.json_body["errcode"], Codes.INVALID_PARAM) self.assertEqual(channel.json_body["errcode"], Codes.INVALID_PARAM)
# Should fail with a negative integer # Should fail with a negative integer
@ -305,7 +342,11 @@ class ManageRegistrationTokensTestCase(unittest.HomeserverTestCase):
{"length": -5}, {"length": -5},
access_token=self.admin_user_tok, access_token=self.admin_user_tok,
) )
self.assertEqual(400, int(channel.result["code"]), msg=channel.result["body"]) self.assertEqual(
HTTPStatus.BAD_REQUEST,
channel.code,
msg=channel.json_body,
)
self.assertEqual(channel.json_body["errcode"], Codes.INVALID_PARAM) self.assertEqual(channel.json_body["errcode"], Codes.INVALID_PARAM)
# Should fail with a float # Should fail with a float
@ -315,7 +356,11 @@ class ManageRegistrationTokensTestCase(unittest.HomeserverTestCase):
{"length": 8.5}, {"length": 8.5},
access_token=self.admin_user_tok, access_token=self.admin_user_tok,
) )
self.assertEqual(400, int(channel.result["code"]), msg=channel.result["body"]) self.assertEqual(
HTTPStatus.BAD_REQUEST,
channel.code,
msg=channel.json_body,
)
self.assertEqual(channel.json_body["errcode"], Codes.INVALID_PARAM) self.assertEqual(channel.json_body["errcode"], Codes.INVALID_PARAM)
# Should fail with 65 # Should fail with 65
@ -325,7 +370,11 @@ class ManageRegistrationTokensTestCase(unittest.HomeserverTestCase):
{"length": 65}, {"length": 65},
access_token=self.admin_user_tok, access_token=self.admin_user_tok,
) )
self.assertEqual(400, int(channel.result["code"]), msg=channel.result["body"]) self.assertEqual(
HTTPStatus.BAD_REQUEST,
channel.code,
msg=channel.json_body,
)
self.assertEqual(channel.json_body["errcode"], Codes.INVALID_PARAM) self.assertEqual(channel.json_body["errcode"], Codes.INVALID_PARAM)
# UPDATING # UPDATING
@ -337,7 +386,11 @@ class ManageRegistrationTokensTestCase(unittest.HomeserverTestCase):
self.url + "/1234", # Token doesn't exist but that doesn't matter self.url + "/1234", # Token doesn't exist but that doesn't matter
{}, {},
) )
self.assertEqual(401, int(channel.result["code"]), msg=channel.result["body"]) self.assertEqual(
HTTPStatus.UNAUTHORIZED,
channel.code,
msg=channel.json_body,
)
self.assertEqual(Codes.MISSING_TOKEN, channel.json_body["errcode"]) self.assertEqual(Codes.MISSING_TOKEN, channel.json_body["errcode"])
def test_update_requester_not_admin(self): def test_update_requester_not_admin(self):
@ -348,7 +401,11 @@ class ManageRegistrationTokensTestCase(unittest.HomeserverTestCase):
{}, {},
access_token=self.other_user_tok, access_token=self.other_user_tok,
) )
self.assertEqual(403, int(channel.result["code"]), msg=channel.result["body"]) self.assertEqual(
HTTPStatus.FORBIDDEN,
channel.code,
msg=channel.json_body,
)
self.assertEqual(Codes.FORBIDDEN, channel.json_body["errcode"]) self.assertEqual(Codes.FORBIDDEN, channel.json_body["errcode"])
def test_update_non_existent(self): def test_update_non_existent(self):
@ -360,7 +417,11 @@ class ManageRegistrationTokensTestCase(unittest.HomeserverTestCase):
access_token=self.admin_user_tok, access_token=self.admin_user_tok,
) )
self.assertEqual(404, int(channel.result["code"]), msg=channel.result["body"]) self.assertEqual(
HTTPStatus.NOT_FOUND,
channel.code,
msg=channel.json_body,
)
self.assertEqual(channel.json_body["errcode"], Codes.NOT_FOUND) self.assertEqual(channel.json_body["errcode"], Codes.NOT_FOUND)
def test_update_uses_allowed(self): def test_update_uses_allowed(self):
@ -375,7 +436,7 @@ class ManageRegistrationTokensTestCase(unittest.HomeserverTestCase):
{"uses_allowed": 1}, {"uses_allowed": 1},
access_token=self.admin_user_tok, access_token=self.admin_user_tok,
) )
self.assertEqual(200, int(channel.result["code"]), msg=channel.result["body"]) self.assertEqual(HTTPStatus.OK, channel.code, msg=channel.json_body)
self.assertEqual(channel.json_body["uses_allowed"], 1) self.assertEqual(channel.json_body["uses_allowed"], 1)
self.assertIsNone(channel.json_body["expiry_time"]) self.assertIsNone(channel.json_body["expiry_time"])
@ -386,7 +447,7 @@ class ManageRegistrationTokensTestCase(unittest.HomeserverTestCase):
{"uses_allowed": 0}, {"uses_allowed": 0},
access_token=self.admin_user_tok, access_token=self.admin_user_tok,
) )
self.assertEqual(200, int(channel.result["code"]), msg=channel.result["body"]) self.assertEqual(HTTPStatus.OK, channel.code, msg=channel.json_body)
self.assertEqual(channel.json_body["uses_allowed"], 0) self.assertEqual(channel.json_body["uses_allowed"], 0)
self.assertIsNone(channel.json_body["expiry_time"]) self.assertIsNone(channel.json_body["expiry_time"])
@ -397,7 +458,7 @@ class ManageRegistrationTokensTestCase(unittest.HomeserverTestCase):
{"uses_allowed": None}, {"uses_allowed": None},
access_token=self.admin_user_tok, access_token=self.admin_user_tok,
) )
self.assertEqual(200, int(channel.result["code"]), msg=channel.result["body"]) self.assertEqual(HTTPStatus.OK, channel.code, msg=channel.json_body)
self.assertIsNone(channel.json_body["uses_allowed"]) self.assertIsNone(channel.json_body["uses_allowed"])
self.assertIsNone(channel.json_body["expiry_time"]) self.assertIsNone(channel.json_body["expiry_time"])
@ -408,7 +469,11 @@ class ManageRegistrationTokensTestCase(unittest.HomeserverTestCase):
{"uses_allowed": 1.5}, {"uses_allowed": 1.5},
access_token=self.admin_user_tok, access_token=self.admin_user_tok,
) )
self.assertEqual(400, int(channel.result["code"]), msg=channel.result["body"]) self.assertEqual(
HTTPStatus.BAD_REQUEST,
channel.code,
msg=channel.json_body,
)
self.assertEqual(channel.json_body["errcode"], Codes.INVALID_PARAM) self.assertEqual(channel.json_body["errcode"], Codes.INVALID_PARAM)
# Should fail with a negative integer # Should fail with a negative integer
@ -418,7 +483,11 @@ class ManageRegistrationTokensTestCase(unittest.HomeserverTestCase):
{"uses_allowed": -5}, {"uses_allowed": -5},
access_token=self.admin_user_tok, access_token=self.admin_user_tok,
) )
self.assertEqual(400, int(channel.result["code"]), msg=channel.result["body"]) self.assertEqual(
HTTPStatus.BAD_REQUEST,
channel.code,
msg=channel.json_body,
)
self.assertEqual(channel.json_body["errcode"], Codes.INVALID_PARAM) self.assertEqual(channel.json_body["errcode"], Codes.INVALID_PARAM)
def test_update_expiry_time(self): def test_update_expiry_time(self):
@ -434,7 +503,7 @@ class ManageRegistrationTokensTestCase(unittest.HomeserverTestCase):
{"expiry_time": new_expiry_time}, {"expiry_time": new_expiry_time},
access_token=self.admin_user_tok, access_token=self.admin_user_tok,
) )
self.assertEqual(200, int(channel.result["code"]), msg=channel.result["body"]) self.assertEqual(HTTPStatus.OK, channel.code, msg=channel.json_body)
self.assertEqual(channel.json_body["expiry_time"], new_expiry_time) self.assertEqual(channel.json_body["expiry_time"], new_expiry_time)
self.assertIsNone(channel.json_body["uses_allowed"]) self.assertIsNone(channel.json_body["uses_allowed"])
@ -445,7 +514,7 @@ class ManageRegistrationTokensTestCase(unittest.HomeserverTestCase):
{"expiry_time": None}, {"expiry_time": None},
access_token=self.admin_user_tok, access_token=self.admin_user_tok,
) )
self.assertEqual(200, int(channel.result["code"]), msg=channel.result["body"]) self.assertEqual(HTTPStatus.OK, channel.code, msg=channel.json_body)
self.assertIsNone(channel.json_body["expiry_time"]) self.assertIsNone(channel.json_body["expiry_time"])
self.assertIsNone(channel.json_body["uses_allowed"]) self.assertIsNone(channel.json_body["uses_allowed"])
@ -457,7 +526,11 @@ class ManageRegistrationTokensTestCase(unittest.HomeserverTestCase):
{"expiry_time": past_time}, {"expiry_time": past_time},
access_token=self.admin_user_tok, access_token=self.admin_user_tok,
) )
self.assertEqual(400, int(channel.result["code"]), msg=channel.result["body"]) self.assertEqual(
HTTPStatus.BAD_REQUEST,
channel.code,
msg=channel.json_body,
)
self.assertEqual(channel.json_body["errcode"], Codes.INVALID_PARAM) self.assertEqual(channel.json_body["errcode"], Codes.INVALID_PARAM)
# Should fail a float # Should fail a float
@ -467,7 +540,11 @@ class ManageRegistrationTokensTestCase(unittest.HomeserverTestCase):
{"expiry_time": new_expiry_time + 0.5}, {"expiry_time": new_expiry_time + 0.5},
access_token=self.admin_user_tok, access_token=self.admin_user_tok,
) )
self.assertEqual(400, int(channel.result["code"]), msg=channel.result["body"]) self.assertEqual(
HTTPStatus.BAD_REQUEST,
channel.code,
msg=channel.json_body,
)
self.assertEqual(channel.json_body["errcode"], Codes.INVALID_PARAM) self.assertEqual(channel.json_body["errcode"], Codes.INVALID_PARAM)
def test_update_both(self): def test_update_both(self):
@ -488,7 +565,7 @@ class ManageRegistrationTokensTestCase(unittest.HomeserverTestCase):
access_token=self.admin_user_tok, access_token=self.admin_user_tok,
) )
self.assertEqual(200, int(channel.result["code"]), msg=channel.result["body"]) self.assertEqual(HTTPStatus.OK, channel.code, msg=channel.json_body)
self.assertEqual(channel.json_body["uses_allowed"], 1) self.assertEqual(channel.json_body["uses_allowed"], 1)
self.assertEqual(channel.json_body["expiry_time"], new_expiry_time) self.assertEqual(channel.json_body["expiry_time"], new_expiry_time)
@ -509,7 +586,11 @@ class ManageRegistrationTokensTestCase(unittest.HomeserverTestCase):
access_token=self.admin_user_tok, access_token=self.admin_user_tok,
) )
self.assertEqual(400, int(channel.result["code"]), msg=channel.result["body"]) self.assertEqual(
HTTPStatus.BAD_REQUEST,
channel.code,
msg=channel.json_body,
)
self.assertEqual(channel.json_body["errcode"], Codes.INVALID_PARAM) self.assertEqual(channel.json_body["errcode"], Codes.INVALID_PARAM)
# DELETING # DELETING
@ -521,7 +602,11 @@ class ManageRegistrationTokensTestCase(unittest.HomeserverTestCase):
self.url + "/1234", # Token doesn't exist but that doesn't matter self.url + "/1234", # Token doesn't exist but that doesn't matter
{}, {},
) )
self.assertEqual(401, int(channel.result["code"]), msg=channel.result["body"]) self.assertEqual(
HTTPStatus.UNAUTHORIZED,
channel.code,
msg=channel.json_body,
)
self.assertEqual(Codes.MISSING_TOKEN, channel.json_body["errcode"]) self.assertEqual(Codes.MISSING_TOKEN, channel.json_body["errcode"])
def test_delete_requester_not_admin(self): def test_delete_requester_not_admin(self):
@ -532,7 +617,11 @@ class ManageRegistrationTokensTestCase(unittest.HomeserverTestCase):
{}, {},
access_token=self.other_user_tok, access_token=self.other_user_tok,
) )
self.assertEqual(403, int(channel.result["code"]), msg=channel.result["body"]) self.assertEqual(
HTTPStatus.FORBIDDEN,
channel.code,
msg=channel.json_body,
)
self.assertEqual(Codes.FORBIDDEN, channel.json_body["errcode"]) self.assertEqual(Codes.FORBIDDEN, channel.json_body["errcode"])
def test_delete_non_existent(self): def test_delete_non_existent(self):
@ -544,7 +633,11 @@ class ManageRegistrationTokensTestCase(unittest.HomeserverTestCase):
access_token=self.admin_user_tok, access_token=self.admin_user_tok,
) )
self.assertEqual(404, int(channel.result["code"]), msg=channel.result["body"]) self.assertEqual(
HTTPStatus.NOT_FOUND,
channel.code,
msg=channel.json_body,
)
self.assertEqual(channel.json_body["errcode"], Codes.NOT_FOUND) self.assertEqual(channel.json_body["errcode"], Codes.NOT_FOUND)
def test_delete(self): def test_delete(self):
@ -559,7 +652,7 @@ class ManageRegistrationTokensTestCase(unittest.HomeserverTestCase):
access_token=self.admin_user_tok, access_token=self.admin_user_tok,
) )
self.assertEqual(200, int(channel.result["code"]), msg=channel.result["body"]) self.assertEqual(HTTPStatus.OK, channel.code, msg=channel.json_body)
# GETTING ONE # GETTING ONE
@ -570,7 +663,11 @@ class ManageRegistrationTokensTestCase(unittest.HomeserverTestCase):
self.url + "/1234", # Token doesn't exist but that doesn't matter self.url + "/1234", # Token doesn't exist but that doesn't matter
{}, {},
) )
self.assertEqual(401, int(channel.result["code"]), msg=channel.result["body"]) self.assertEqual(
HTTPStatus.UNAUTHORIZED,
channel.code,
msg=channel.json_body,
)
self.assertEqual(Codes.MISSING_TOKEN, channel.json_body["errcode"]) self.assertEqual(Codes.MISSING_TOKEN, channel.json_body["errcode"])
def test_get_requester_not_admin(self): def test_get_requester_not_admin(self):
@ -581,7 +678,11 @@ class ManageRegistrationTokensTestCase(unittest.HomeserverTestCase):
{}, {},
access_token=self.other_user_tok, access_token=self.other_user_tok,
) )
self.assertEqual(403, int(channel.result["code"]), msg=channel.result["body"]) self.assertEqual(
HTTPStatus.FORBIDDEN,
channel.code,
msg=channel.json_body,
)
self.assertEqual(Codes.FORBIDDEN, channel.json_body["errcode"]) self.assertEqual(Codes.FORBIDDEN, channel.json_body["errcode"])
def test_get_non_existent(self): def test_get_non_existent(self):
@ -593,7 +694,11 @@ class ManageRegistrationTokensTestCase(unittest.HomeserverTestCase):
access_token=self.admin_user_tok, access_token=self.admin_user_tok,
) )
self.assertEqual(404, int(channel.result["code"]), msg=channel.result["body"]) self.assertEqual(
HTTPStatus.NOT_FOUND,
channel.code,
msg=channel.json_body,
)
self.assertEqual(channel.json_body["errcode"], Codes.NOT_FOUND) self.assertEqual(channel.json_body["errcode"], Codes.NOT_FOUND)
def test_get(self): def test_get(self):
@ -608,7 +713,7 @@ class ManageRegistrationTokensTestCase(unittest.HomeserverTestCase):
access_token=self.admin_user_tok, access_token=self.admin_user_tok,
) )
self.assertEqual(200, int(channel.result["code"]), msg=channel.result["body"]) self.assertEqual(HTTPStatus.OK, channel.code, msg=channel.json_body)
self.assertEqual(channel.json_body["token"], token) self.assertEqual(channel.json_body["token"], token)
self.assertIsNone(channel.json_body["uses_allowed"]) self.assertIsNone(channel.json_body["uses_allowed"])
self.assertIsNone(channel.json_body["expiry_time"]) self.assertIsNone(channel.json_body["expiry_time"])
@ -620,7 +725,11 @@ class ManageRegistrationTokensTestCase(unittest.HomeserverTestCase):
def test_list_no_auth(self): def test_list_no_auth(self):
"""Try to list tokens without authentication.""" """Try to list tokens without authentication."""
channel = self.make_request("GET", self.url, {}) channel = self.make_request("GET", self.url, {})
self.assertEqual(401, int(channel.result["code"]), msg=channel.result["body"]) self.assertEqual(
HTTPStatus.UNAUTHORIZED,
channel.code,
msg=channel.json_body,
)
self.assertEqual(Codes.MISSING_TOKEN, channel.json_body["errcode"]) self.assertEqual(Codes.MISSING_TOKEN, channel.json_body["errcode"])
def test_list_requester_not_admin(self): def test_list_requester_not_admin(self):
@ -631,7 +740,11 @@ class ManageRegistrationTokensTestCase(unittest.HomeserverTestCase):
{}, {},
access_token=self.other_user_tok, access_token=self.other_user_tok,
) )
self.assertEqual(403, int(channel.result["code"]), msg=channel.result["body"]) self.assertEqual(
HTTPStatus.FORBIDDEN,
channel.code,
msg=channel.json_body,
)
self.assertEqual(Codes.FORBIDDEN, channel.json_body["errcode"]) self.assertEqual(Codes.FORBIDDEN, channel.json_body["errcode"])
def test_list_all(self): def test_list_all(self):
@ -646,7 +759,7 @@ class ManageRegistrationTokensTestCase(unittest.HomeserverTestCase):
access_token=self.admin_user_tok, access_token=self.admin_user_tok,
) )
self.assertEqual(200, int(channel.result["code"]), msg=channel.result["body"]) self.assertEqual(HTTPStatus.OK, channel.code, msg=channel.json_body)
self.assertEqual(len(channel.json_body["registration_tokens"]), 1) self.assertEqual(len(channel.json_body["registration_tokens"]), 1)
token_info = channel.json_body["registration_tokens"][0] token_info = channel.json_body["registration_tokens"][0]
self.assertEqual(token_info["token"], token) self.assertEqual(token_info["token"], token)
@ -664,7 +777,11 @@ class ManageRegistrationTokensTestCase(unittest.HomeserverTestCase):
access_token=self.admin_user_tok, access_token=self.admin_user_tok,
) )
self.assertEqual(400, int(channel.result["code"]), msg=channel.result["body"]) self.assertEqual(
HTTPStatus.BAD_REQUEST,
channel.code,
msg=channel.json_body,
)
def _test_list_query_parameter(self, valid: str): def _test_list_query_parameter(self, valid: str):
"""Helper used to test both valid=true and valid=false.""" """Helper used to test both valid=true and valid=false."""
@ -696,7 +813,7 @@ class ManageRegistrationTokensTestCase(unittest.HomeserverTestCase):
access_token=self.admin_user_tok, access_token=self.admin_user_tok,
) )
self.assertEqual(200, int(channel.result["code"]), msg=channel.result["body"]) self.assertEqual(HTTPStatus.OK, channel.code, msg=channel.json_body)
self.assertEqual(len(channel.json_body["registration_tokens"]), 2) self.assertEqual(len(channel.json_body["registration_tokens"]), 2)
token_info_1 = channel.json_body["registration_tokens"][0] token_info_1 = channel.json_body["registration_tokens"][0]
token_info_2 = channel.json_body["registration_tokens"][1] token_info_2 = channel.json_body["registration_tokens"][1]

View file

@ -66,7 +66,7 @@ class DeleteRoomTestCase(unittest.HomeserverTestCase):
def test_requester_is_no_admin(self): def test_requester_is_no_admin(self):
""" """
If the user is not a server admin, an error 403 is returned. If the user is not a server admin, an error HTTPStatus.FORBIDDEN is returned.
""" """
channel = self.make_request( channel = self.make_request(
@ -76,12 +76,12 @@ class DeleteRoomTestCase(unittest.HomeserverTestCase):
access_token=self.other_user_tok, access_token=self.other_user_tok,
) )
self.assertEqual(403, channel.code, msg=channel.json_body) self.assertEqual(HTTPStatus.FORBIDDEN, channel.code, msg=channel.json_body)
self.assertEqual(Codes.FORBIDDEN, channel.json_body["errcode"]) self.assertEqual(Codes.FORBIDDEN, channel.json_body["errcode"])
def test_room_does_not_exist(self): def test_room_does_not_exist(self):
""" """
Check that unknown rooms/server return error 404. Check that unknown rooms/server return error HTTPStatus.NOT_FOUND.
""" """
url = "/_synapse/admin/v1/rooms/%s" % "!unknown:test" url = "/_synapse/admin/v1/rooms/%s" % "!unknown:test"
@ -92,12 +92,12 @@ class DeleteRoomTestCase(unittest.HomeserverTestCase):
access_token=self.admin_user_tok, access_token=self.admin_user_tok,
) )
self.assertEqual(404, channel.code, msg=channel.json_body) self.assertEqual(HTTPStatus.NOT_FOUND, channel.code, msg=channel.json_body)
self.assertEqual(Codes.NOT_FOUND, channel.json_body["errcode"]) self.assertEqual(Codes.NOT_FOUND, channel.json_body["errcode"])
def test_room_is_not_valid(self): def test_room_is_not_valid(self):
""" """
Check that invalid room names, return an error 400. Check that invalid room names, return an error HTTPStatus.BAD_REQUEST.
""" """
url = "/_synapse/admin/v1/rooms/%s" % "invalidroom" url = "/_synapse/admin/v1/rooms/%s" % "invalidroom"
@ -108,7 +108,7 @@ class DeleteRoomTestCase(unittest.HomeserverTestCase):
access_token=self.admin_user_tok, access_token=self.admin_user_tok,
) )
self.assertEqual(400, channel.code, msg=channel.json_body) self.assertEqual(HTTPStatus.BAD_REQUEST, channel.code, msg=channel.json_body)
self.assertEqual( self.assertEqual(
"invalidroom is not a legal room ID", "invalidroom is not a legal room ID",
channel.json_body["error"], channel.json_body["error"],
@ -127,7 +127,7 @@ class DeleteRoomTestCase(unittest.HomeserverTestCase):
access_token=self.admin_user_tok, access_token=self.admin_user_tok,
) )
self.assertEqual(200, channel.code, msg=channel.json_body) self.assertEqual(HTTPStatus.OK, channel.code, msg=channel.json_body)
self.assertIn("new_room_id", channel.json_body) self.assertIn("new_room_id", channel.json_body)
self.assertIn("kicked_users", channel.json_body) self.assertIn("kicked_users", channel.json_body)
self.assertIn("failed_to_kick_users", channel.json_body) self.assertIn("failed_to_kick_users", channel.json_body)
@ -146,7 +146,7 @@ class DeleteRoomTestCase(unittest.HomeserverTestCase):
access_token=self.admin_user_tok, access_token=self.admin_user_tok,
) )
self.assertEqual(400, channel.code, msg=channel.json_body) self.assertEqual(HTTPStatus.BAD_REQUEST, channel.code, msg=channel.json_body)
self.assertEqual( self.assertEqual(
"User must be our own: @not:exist.bla", "User must be our own: @not:exist.bla",
channel.json_body["error"], channel.json_body["error"],
@ -165,7 +165,7 @@ class DeleteRoomTestCase(unittest.HomeserverTestCase):
access_token=self.admin_user_tok, access_token=self.admin_user_tok,
) )
self.assertEqual(400, channel.code, msg=channel.json_body) self.assertEqual(HTTPStatus.BAD_REQUEST, channel.code, msg=channel.json_body)
self.assertEqual(Codes.BAD_JSON, channel.json_body["errcode"]) self.assertEqual(Codes.BAD_JSON, channel.json_body["errcode"])
def test_purge_is_not_bool(self): def test_purge_is_not_bool(self):
@ -181,7 +181,7 @@ class DeleteRoomTestCase(unittest.HomeserverTestCase):
access_token=self.admin_user_tok, access_token=self.admin_user_tok,
) )
self.assertEqual(400, channel.code, msg=channel.json_body) self.assertEqual(HTTPStatus.BAD_REQUEST, channel.code, msg=channel.json_body)
self.assertEqual(Codes.BAD_JSON, channel.json_body["errcode"]) self.assertEqual(Codes.BAD_JSON, channel.json_body["errcode"])
def test_purge_room_and_block(self): def test_purge_room_and_block(self):
@ -207,7 +207,7 @@ class DeleteRoomTestCase(unittest.HomeserverTestCase):
access_token=self.admin_user_tok, access_token=self.admin_user_tok,
) )
self.assertEqual(200, channel.code, msg=channel.json_body) self.assertEqual(HTTPStatus.OK, channel.code, msg=channel.json_body)
self.assertEqual(None, channel.json_body["new_room_id"]) self.assertEqual(None, channel.json_body["new_room_id"])
self.assertEqual(self.other_user, channel.json_body["kicked_users"][0]) self.assertEqual(self.other_user, channel.json_body["kicked_users"][0])
self.assertIn("failed_to_kick_users", channel.json_body) self.assertIn("failed_to_kick_users", channel.json_body)
@ -240,7 +240,7 @@ class DeleteRoomTestCase(unittest.HomeserverTestCase):
access_token=self.admin_user_tok, access_token=self.admin_user_tok,
) )
self.assertEqual(200, channel.code, msg=channel.json_body) self.assertEqual(HTTPStatus.OK, channel.code, msg=channel.json_body)
self.assertEqual(None, channel.json_body["new_room_id"]) self.assertEqual(None, channel.json_body["new_room_id"])
self.assertEqual(self.other_user, channel.json_body["kicked_users"][0]) self.assertEqual(self.other_user, channel.json_body["kicked_users"][0])
self.assertIn("failed_to_kick_users", channel.json_body) self.assertIn("failed_to_kick_users", channel.json_body)
@ -274,7 +274,7 @@ class DeleteRoomTestCase(unittest.HomeserverTestCase):
access_token=self.admin_user_tok, access_token=self.admin_user_tok,
) )
self.assertEqual(200, channel.code, msg=channel.json_body) self.assertEqual(HTTPStatus.OK, channel.code, msg=channel.json_body)
self.assertEqual(None, channel.json_body["new_room_id"]) self.assertEqual(None, channel.json_body["new_room_id"])
self.assertEqual(self.other_user, channel.json_body["kicked_users"][0]) self.assertEqual(self.other_user, channel.json_body["kicked_users"][0])
self.assertIn("failed_to_kick_users", channel.json_body) self.assertIn("failed_to_kick_users", channel.json_body)
@ -305,9 +305,7 @@ class DeleteRoomTestCase(unittest.HomeserverTestCase):
) )
# The room is now blocked. # The room is now blocked.
self.assertEqual( self.assertEqual(HTTPStatus.OK, channel.code, msg=channel.json_body)
HTTPStatus.OK, int(channel.result["code"]), msg=channel.result["body"]
)
self._is_blocked(room_id) self._is_blocked(room_id)
def test_shutdown_room_consent(self): def test_shutdown_room_consent(self):
@ -327,7 +325,10 @@ class DeleteRoomTestCase(unittest.HomeserverTestCase):
# Assert that the user is getting consent error # Assert that the user is getting consent error
self.helper.send( self.helper.send(
self.room_id, body="foo", tok=self.other_user_tok, expect_code=403 self.room_id,
body="foo",
tok=self.other_user_tok,
expect_code=HTTPStatus.FORBIDDEN,
) )
# Test that room is not purged # Test that room is not purged
@ -345,7 +346,7 @@ class DeleteRoomTestCase(unittest.HomeserverTestCase):
access_token=self.admin_user_tok, access_token=self.admin_user_tok,
) )
self.assertEqual(200, channel.code, msg=channel.json_body) self.assertEqual(HTTPStatus.OK, channel.code, msg=channel.json_body)
self.assertEqual(self.other_user, channel.json_body["kicked_users"][0]) self.assertEqual(self.other_user, channel.json_body["kicked_users"][0])
self.assertIn("new_room_id", channel.json_body) self.assertIn("new_room_id", channel.json_body)
self.assertIn("failed_to_kick_users", channel.json_body) self.assertIn("failed_to_kick_users", channel.json_body)
@ -374,7 +375,7 @@ class DeleteRoomTestCase(unittest.HomeserverTestCase):
json.dumps({"history_visibility": "world_readable"}), json.dumps({"history_visibility": "world_readable"}),
access_token=self.other_user_tok, access_token=self.other_user_tok,
) )
self.assertEqual(200, channel.code, msg=channel.json_body) self.assertEqual(HTTPStatus.OK, channel.code, msg=channel.json_body)
# Test that room is not purged # Test that room is not purged
with self.assertRaises(AssertionError): with self.assertRaises(AssertionError):
@ -391,7 +392,7 @@ class DeleteRoomTestCase(unittest.HomeserverTestCase):
access_token=self.admin_user_tok, access_token=self.admin_user_tok,
) )
self.assertEqual(200, channel.code, msg=channel.json_body) self.assertEqual(HTTPStatus.OK, channel.code, msg=channel.json_body)
self.assertEqual(self.other_user, channel.json_body["kicked_users"][0]) self.assertEqual(self.other_user, channel.json_body["kicked_users"][0])
self.assertIn("new_room_id", channel.json_body) self.assertIn("new_room_id", channel.json_body)
self.assertIn("failed_to_kick_users", channel.json_body) self.assertIn("failed_to_kick_users", channel.json_body)
@ -406,7 +407,7 @@ class DeleteRoomTestCase(unittest.HomeserverTestCase):
self._has_no_members(self.room_id) self._has_no_members(self.room_id)
# Assert we can no longer peek into the room # Assert we can no longer peek into the room
self._assert_peek(self.room_id, expect_code=403) self._assert_peek(self.room_id, expect_code=HTTPStatus.FORBIDDEN)
def _is_blocked(self, room_id, expect=True): def _is_blocked(self, room_id, expect=True):
"""Assert that the room is blocked or not""" """Assert that the room is blocked or not"""
@ -502,7 +503,7 @@ class DeleteRoomV2TestCase(unittest.HomeserverTestCase):
) )
def test_requester_is_no_admin(self, method: str, url: str): def test_requester_is_no_admin(self, method: str, url: str):
""" """
If the user is not a server admin, an error 403 is returned. If the user is not a server admin, an error HTTPStatus.FORBIDDEN is returned.
""" """
channel = self.make_request( channel = self.make_request(
@ -524,7 +525,7 @@ class DeleteRoomV2TestCase(unittest.HomeserverTestCase):
) )
def test_room_does_not_exist(self, method: str, url: str): def test_room_does_not_exist(self, method: str, url: str):
""" """
Check that unknown rooms/server return error 404. Check that unknown rooms/server return error HTTPStatus.NOT_FOUND.
""" """
channel = self.make_request( channel = self.make_request(
@ -545,7 +546,7 @@ class DeleteRoomV2TestCase(unittest.HomeserverTestCase):
) )
def test_room_is_not_valid(self, method: str, url: str): def test_room_is_not_valid(self, method: str, url: str):
""" """
Check that invalid room names, return an error 400. Check that invalid room names, return an error HTTPStatus.BAD_REQUEST.
""" """
channel = self.make_request( channel = self.make_request(
@ -854,7 +855,10 @@ class DeleteRoomV2TestCase(unittest.HomeserverTestCase):
# Assert that the user is getting consent error # Assert that the user is getting consent error
self.helper.send( self.helper.send(
self.room_id, body="foo", tok=self.other_user_tok, expect_code=403 self.room_id,
body="foo",
tok=self.other_user_tok,
expect_code=HTTPStatus.FORBIDDEN,
) )
# Test that room is not purged # Test that room is not purged
@ -951,7 +955,7 @@ class DeleteRoomV2TestCase(unittest.HomeserverTestCase):
self._has_no_members(self.room_id) self._has_no_members(self.room_id)
# Assert we can no longer peek into the room # Assert we can no longer peek into the room
self._assert_peek(self.room_id, expect_code=403) self._assert_peek(self.room_id, expect_code=HTTPStatus.FORBIDDEN)
def _is_blocked(self, room_id: str, expect: bool = True) -> None: def _is_blocked(self, room_id: str, expect: bool = True) -> None:
"""Assert that the room is blocked or not""" """Assert that the room is blocked or not"""
@ -1094,7 +1098,7 @@ class RoomTestCase(unittest.HomeserverTestCase):
) )
# Check request completed successfully # Check request completed successfully
self.assertEqual(200, channel.code, msg=channel.json_body) self.assertEqual(HTTPStatus.OK, channel.code, msg=channel.json_body)
# Check that response json body contains a "rooms" key # Check that response json body contains a "rooms" key
self.assertTrue( self.assertTrue(
@ -1178,7 +1182,7 @@ class RoomTestCase(unittest.HomeserverTestCase):
url.encode("ascii"), url.encode("ascii"),
access_token=self.admin_user_tok, access_token=self.admin_user_tok,
) )
self.assertEqual(200, channel.code, msg=channel.json_body) self.assertEqual(HTTPStatus.OK, channel.code, msg=channel.json_body)
self.assertTrue("rooms" in channel.json_body) self.assertTrue("rooms" in channel.json_body)
for r in channel.json_body["rooms"]: for r in channel.json_body["rooms"]:
@ -1218,7 +1222,7 @@ class RoomTestCase(unittest.HomeserverTestCase):
url.encode("ascii"), url.encode("ascii"),
access_token=self.admin_user_tok, access_token=self.admin_user_tok,
) )
self.assertEqual(200, channel.code, msg=channel.json_body) self.assertEqual(HTTPStatus.OK, channel.code, msg=channel.json_body)
def test_correct_room_attributes(self): def test_correct_room_attributes(self):
"""Test the correct attributes for a room are returned""" """Test the correct attributes for a room are returned"""
@ -1241,7 +1245,7 @@ class RoomTestCase(unittest.HomeserverTestCase):
{"room_id": room_id}, {"room_id": room_id},
access_token=self.admin_user_tok, access_token=self.admin_user_tok,
) )
self.assertEqual(200, channel.code, msg=channel.json_body) self.assertEqual(HTTPStatus.OK, channel.code, msg=channel.json_body)
# Set this new alias as the canonical alias for this room # Set this new alias as the canonical alias for this room
self.helper.send_state( self.helper.send_state(
@ -1273,7 +1277,7 @@ class RoomTestCase(unittest.HomeserverTestCase):
url.encode("ascii"), url.encode("ascii"),
access_token=self.admin_user_tok, access_token=self.admin_user_tok,
) )
self.assertEqual(200, channel.code, msg=channel.json_body) self.assertEqual(HTTPStatus.OK, channel.code, msg=channel.json_body)
# Check that rooms were returned # Check that rooms were returned
self.assertTrue("rooms" in channel.json_body) self.assertTrue("rooms" in channel.json_body)
@ -1328,7 +1332,7 @@ class RoomTestCase(unittest.HomeserverTestCase):
url.encode("ascii"), url.encode("ascii"),
access_token=self.admin_user_tok, access_token=self.admin_user_tok,
) )
self.assertEqual(200, channel.code, msg=channel.json_body) self.assertEqual(HTTPStatus.OK, channel.code, msg=channel.json_body)
# Check that rooms were returned # Check that rooms were returned
self.assertTrue("rooms" in channel.json_body) self.assertTrue("rooms" in channel.json_body)
@ -1467,7 +1471,7 @@ class RoomTestCase(unittest.HomeserverTestCase):
def _search_test( def _search_test(
expected_room_id: Optional[str], expected_room_id: Optional[str],
search_term: str, search_term: str,
expected_http_code: int = 200, expected_http_code: int = HTTPStatus.OK,
): ):
"""Search for a room and check that the returned room's id is a match """Search for a room and check that the returned room's id is a match
@ -1485,7 +1489,7 @@ class RoomTestCase(unittest.HomeserverTestCase):
) )
self.assertEqual(expected_http_code, channel.code, msg=channel.json_body) self.assertEqual(expected_http_code, channel.code, msg=channel.json_body)
if expected_http_code != 200: if expected_http_code != HTTPStatus.OK:
return return
# Check that rooms were returned # Check that rooms were returned
@ -1528,7 +1532,7 @@ class RoomTestCase(unittest.HomeserverTestCase):
_search_test(None, "foo") _search_test(None, "foo")
_search_test(None, "bar") _search_test(None, "bar")
_search_test(None, "", expected_http_code=400) _search_test(None, "", expected_http_code=HTTPStatus.BAD_REQUEST)
# Test that the whole room id returns the room # Test that the whole room id returns the room
_search_test(room_id_1, room_id_1) _search_test(room_id_1, room_id_1)
@ -1565,7 +1569,7 @@ class RoomTestCase(unittest.HomeserverTestCase):
url.encode("ascii"), url.encode("ascii"),
access_token=self.admin_user_tok, access_token=self.admin_user_tok,
) )
self.assertEqual(200, channel.code, msg=channel.json_body) self.assertEqual(HTTPStatus.OK, channel.code, msg=channel.json_body)
self.assertEqual(room_id, channel.json_body.get("rooms")[0].get("room_id")) self.assertEqual(room_id, channel.json_body.get("rooms")[0].get("room_id"))
self.assertEqual("ж", channel.json_body.get("rooms")[0].get("name")) self.assertEqual("ж", channel.json_body.get("rooms")[0].get("name"))
@ -1598,7 +1602,7 @@ class RoomTestCase(unittest.HomeserverTestCase):
url.encode("ascii"), url.encode("ascii"),
access_token=self.admin_user_tok, access_token=self.admin_user_tok,
) )
self.assertEqual(200, channel.code, msg=channel.json_body) self.assertEqual(HTTPStatus.OK, channel.code, msg=channel.json_body)
self.assertIn("room_id", channel.json_body) self.assertIn("room_id", channel.json_body)
self.assertIn("name", channel.json_body) self.assertIn("name", channel.json_body)
@ -1630,7 +1634,7 @@ class RoomTestCase(unittest.HomeserverTestCase):
url.encode("ascii"), url.encode("ascii"),
access_token=self.admin_user_tok, access_token=self.admin_user_tok,
) )
self.assertEqual(200, channel.code, msg=channel.json_body) self.assertEqual(HTTPStatus.OK, channel.code, msg=channel.json_body)
self.assertEqual(1, channel.json_body["joined_local_devices"]) self.assertEqual(1, channel.json_body["joined_local_devices"])
# Have another user join the room # Have another user join the room
@ -1644,7 +1648,7 @@ class RoomTestCase(unittest.HomeserverTestCase):
url.encode("ascii"), url.encode("ascii"),
access_token=self.admin_user_tok, access_token=self.admin_user_tok,
) )
self.assertEqual(200, channel.code, msg=channel.json_body) self.assertEqual(HTTPStatus.OK, channel.code, msg=channel.json_body)
self.assertEqual(2, channel.json_body["joined_local_devices"]) self.assertEqual(2, channel.json_body["joined_local_devices"])
# leave room # leave room
@ -1656,7 +1660,7 @@ class RoomTestCase(unittest.HomeserverTestCase):
url.encode("ascii"), url.encode("ascii"),
access_token=self.admin_user_tok, access_token=self.admin_user_tok,
) )
self.assertEqual(200, channel.code, msg=channel.json_body) self.assertEqual(HTTPStatus.OK, channel.code, msg=channel.json_body)
self.assertEqual(0, channel.json_body["joined_local_devices"]) self.assertEqual(0, channel.json_body["joined_local_devices"])
def test_room_members(self): def test_room_members(self):
@ -1687,7 +1691,7 @@ class RoomTestCase(unittest.HomeserverTestCase):
url.encode("ascii"), url.encode("ascii"),
access_token=self.admin_user_tok, access_token=self.admin_user_tok,
) )
self.assertEqual(200, channel.code, msg=channel.json_body) self.assertEqual(HTTPStatus.OK, channel.code, msg=channel.json_body)
self.assertCountEqual( self.assertCountEqual(
["@admin:test", "@foo:test", "@bar:test"], channel.json_body["members"] ["@admin:test", "@foo:test", "@bar:test"], channel.json_body["members"]
@ -1700,7 +1704,7 @@ class RoomTestCase(unittest.HomeserverTestCase):
url.encode("ascii"), url.encode("ascii"),
access_token=self.admin_user_tok, access_token=self.admin_user_tok,
) )
self.assertEqual(200, channel.code, msg=channel.json_body) self.assertEqual(HTTPStatus.OK, channel.code, msg=channel.json_body)
self.assertCountEqual( self.assertCountEqual(
["@admin:test", "@bar:test", "@foobar:test"], channel.json_body["members"] ["@admin:test", "@bar:test", "@foobar:test"], channel.json_body["members"]
@ -1718,7 +1722,7 @@ class RoomTestCase(unittest.HomeserverTestCase):
url.encode("ascii"), url.encode("ascii"),
access_token=self.admin_user_tok, access_token=self.admin_user_tok,
) )
self.assertEqual(200, channel.code, msg=channel.json_body) self.assertEqual(HTTPStatus.OK, channel.code, msg=channel.json_body)
self.assertIn("state", channel.json_body) self.assertIn("state", channel.json_body)
# testing that the state events match is painful and not done here. We assume that # testing that the state events match is painful and not done here. We assume that
# the create_room already does the right thing, so no need to verify that we got # the create_room already does the right thing, so no need to verify that we got
@ -1733,7 +1737,7 @@ class RoomTestCase(unittest.HomeserverTestCase):
{"room_id": room_id}, {"room_id": room_id},
access_token=admin_user_tok, access_token=admin_user_tok,
) )
self.assertEqual(200, channel.code, msg=channel.json_body) self.assertEqual(HTTPStatus.OK, channel.code, msg=channel.json_body)
# Set this new alias as the canonical alias for this room # Set this new alias as the canonical alias for this room
self.helper.send_state( self.helper.send_state(
@ -1776,7 +1780,7 @@ class JoinAliasRoomTestCase(unittest.HomeserverTestCase):
def test_requester_is_no_admin(self): def test_requester_is_no_admin(self):
""" """
If the user is not a server admin, an error 403 is returned. If the user is not a server admin, an error HTTPStatus.FORBIDDEN is returned.
""" """
body = json.dumps({"user_id": self.second_user_id}) body = json.dumps({"user_id": self.second_user_id})
@ -1787,7 +1791,7 @@ class JoinAliasRoomTestCase(unittest.HomeserverTestCase):
access_token=self.second_tok, access_token=self.second_tok,
) )
self.assertEqual(403, channel.code, msg=channel.json_body) self.assertEqual(HTTPStatus.FORBIDDEN, channel.code, msg=channel.json_body)
self.assertEqual(Codes.FORBIDDEN, channel.json_body["errcode"]) self.assertEqual(Codes.FORBIDDEN, channel.json_body["errcode"])
def test_invalid_parameter(self): def test_invalid_parameter(self):
@ -1803,12 +1807,12 @@ class JoinAliasRoomTestCase(unittest.HomeserverTestCase):
access_token=self.admin_user_tok, access_token=self.admin_user_tok,
) )
self.assertEqual(400, channel.code, msg=channel.json_body) self.assertEqual(HTTPStatus.BAD_REQUEST, channel.code, msg=channel.json_body)
self.assertEqual(Codes.MISSING_PARAM, channel.json_body["errcode"]) self.assertEqual(Codes.MISSING_PARAM, channel.json_body["errcode"])
def test_local_user_does_not_exist(self): def test_local_user_does_not_exist(self):
""" """
Tests that a lookup for a user that does not exist returns a 404 Tests that a lookup for a user that does not exist returns a HTTPStatus.NOT_FOUND
""" """
body = json.dumps({"user_id": "@unknown:test"}) body = json.dumps({"user_id": "@unknown:test"})
@ -1819,7 +1823,7 @@ class JoinAliasRoomTestCase(unittest.HomeserverTestCase):
access_token=self.admin_user_tok, access_token=self.admin_user_tok,
) )
self.assertEqual(404, channel.code, msg=channel.json_body) self.assertEqual(HTTPStatus.NOT_FOUND, channel.code, msg=channel.json_body)
self.assertEqual(Codes.NOT_FOUND, channel.json_body["errcode"]) self.assertEqual(Codes.NOT_FOUND, channel.json_body["errcode"])
def test_remote_user(self): def test_remote_user(self):
@ -1835,7 +1839,7 @@ class JoinAliasRoomTestCase(unittest.HomeserverTestCase):
access_token=self.admin_user_tok, access_token=self.admin_user_tok,
) )
self.assertEqual(400, channel.code, msg=channel.json_body) self.assertEqual(HTTPStatus.BAD_REQUEST, channel.code, msg=channel.json_body)
self.assertEqual( self.assertEqual(
"This endpoint can only be used with local users", "This endpoint can only be used with local users",
channel.json_body["error"], channel.json_body["error"],
@ -1843,7 +1847,7 @@ class JoinAliasRoomTestCase(unittest.HomeserverTestCase):
def test_room_does_not_exist(self): def test_room_does_not_exist(self):
""" """
Check that unknown rooms/server return error 404. Check that unknown rooms/server return error HTTPStatus.NOT_FOUND.
""" """
body = json.dumps({"user_id": self.second_user_id}) body = json.dumps({"user_id": self.second_user_id})
url = "/_synapse/admin/v1/join/!unknown:test" url = "/_synapse/admin/v1/join/!unknown:test"
@ -1855,12 +1859,12 @@ class JoinAliasRoomTestCase(unittest.HomeserverTestCase):
access_token=self.admin_user_tok, access_token=self.admin_user_tok,
) )
self.assertEqual(404, channel.code, msg=channel.json_body) self.assertEqual(HTTPStatus.NOT_FOUND, channel.code, msg=channel.json_body)
self.assertEqual("No known servers", channel.json_body["error"]) self.assertEqual("No known servers", channel.json_body["error"])
def test_room_is_not_valid(self): def test_room_is_not_valid(self):
""" """
Check that invalid room names, return an error 400. Check that invalid room names, return an error HTTPStatus.BAD_REQUEST.
""" """
body = json.dumps({"user_id": self.second_user_id}) body = json.dumps({"user_id": self.second_user_id})
url = "/_synapse/admin/v1/join/invalidroom" url = "/_synapse/admin/v1/join/invalidroom"
@ -1872,7 +1876,7 @@ class JoinAliasRoomTestCase(unittest.HomeserverTestCase):
access_token=self.admin_user_tok, access_token=self.admin_user_tok,
) )
self.assertEqual(400, channel.code, msg=channel.json_body) self.assertEqual(HTTPStatus.BAD_REQUEST, channel.code, msg=channel.json_body)
self.assertEqual( self.assertEqual(
"invalidroom was not legal room ID or room alias", "invalidroom was not legal room ID or room alias",
channel.json_body["error"], channel.json_body["error"],
@ -1891,7 +1895,7 @@ class JoinAliasRoomTestCase(unittest.HomeserverTestCase):
access_token=self.admin_user_tok, access_token=self.admin_user_tok,
) )
self.assertEqual(200, channel.code, msg=channel.json_body) self.assertEqual(HTTPStatus.OK, channel.code, msg=channel.json_body)
self.assertEqual(self.public_room_id, channel.json_body["room_id"]) self.assertEqual(self.public_room_id, channel.json_body["room_id"])
# Validate if user is a member of the room # Validate if user is a member of the room
@ -1901,7 +1905,7 @@ class JoinAliasRoomTestCase(unittest.HomeserverTestCase):
"/_matrix/client/r0/joined_rooms", "/_matrix/client/r0/joined_rooms",
access_token=self.second_tok, access_token=self.second_tok,
) )
self.assertEquals(200, channel.code, msg=channel.json_body) self.assertEquals(HTTPStatus.OK, channel.code, msg=channel.json_body)
self.assertEqual(self.public_room_id, channel.json_body["joined_rooms"][0]) self.assertEqual(self.public_room_id, channel.json_body["joined_rooms"][0])
def test_join_private_room_if_not_member(self): def test_join_private_room_if_not_member(self):
@ -1922,7 +1926,7 @@ class JoinAliasRoomTestCase(unittest.HomeserverTestCase):
access_token=self.admin_user_tok, access_token=self.admin_user_tok,
) )
self.assertEqual(403, channel.code, msg=channel.json_body) self.assertEqual(HTTPStatus.FORBIDDEN, channel.code, msg=channel.json_body)
self.assertEqual(Codes.FORBIDDEN, channel.json_body["errcode"]) self.assertEqual(Codes.FORBIDDEN, channel.json_body["errcode"])
def test_join_private_room_if_member(self): def test_join_private_room_if_member(self):
@ -1950,7 +1954,7 @@ class JoinAliasRoomTestCase(unittest.HomeserverTestCase):
"/_matrix/client/r0/joined_rooms", "/_matrix/client/r0/joined_rooms",
access_token=self.admin_user_tok, access_token=self.admin_user_tok,
) )
self.assertEquals(200, channel.code, msg=channel.json_body) self.assertEquals(HTTPStatus.OK, channel.code, msg=channel.json_body)
self.assertEqual(private_room_id, channel.json_body["joined_rooms"][0]) self.assertEqual(private_room_id, channel.json_body["joined_rooms"][0])
# Join user to room. # Join user to room.
@ -1964,7 +1968,7 @@ class JoinAliasRoomTestCase(unittest.HomeserverTestCase):
content=body, content=body,
access_token=self.admin_user_tok, access_token=self.admin_user_tok,
) )
self.assertEqual(200, channel.code, msg=channel.json_body) self.assertEqual(HTTPStatus.OK, channel.code, msg=channel.json_body)
self.assertEqual(private_room_id, channel.json_body["room_id"]) self.assertEqual(private_room_id, channel.json_body["room_id"])
# Validate if user is a member of the room # Validate if user is a member of the room
@ -1974,7 +1978,7 @@ class JoinAliasRoomTestCase(unittest.HomeserverTestCase):
"/_matrix/client/r0/joined_rooms", "/_matrix/client/r0/joined_rooms",
access_token=self.second_tok, access_token=self.second_tok,
) )
self.assertEquals(200, channel.code, msg=channel.json_body) self.assertEquals(HTTPStatus.OK, channel.code, msg=channel.json_body)
self.assertEqual(private_room_id, channel.json_body["joined_rooms"][0]) self.assertEqual(private_room_id, channel.json_body["joined_rooms"][0])
def test_join_private_room_if_owner(self): def test_join_private_room_if_owner(self):
@ -1995,7 +1999,7 @@ class JoinAliasRoomTestCase(unittest.HomeserverTestCase):
access_token=self.admin_user_tok, access_token=self.admin_user_tok,
) )
self.assertEqual(200, channel.code, msg=channel.json_body) self.assertEqual(HTTPStatus.OK, channel.code, msg=channel.json_body)
self.assertEqual(private_room_id, channel.json_body["room_id"]) self.assertEqual(private_room_id, channel.json_body["room_id"])
# Validate if user is a member of the room # Validate if user is a member of the room
@ -2005,7 +2009,7 @@ class JoinAliasRoomTestCase(unittest.HomeserverTestCase):
"/_matrix/client/r0/joined_rooms", "/_matrix/client/r0/joined_rooms",
access_token=self.second_tok, access_token=self.second_tok,
) )
self.assertEquals(200, channel.code, msg=channel.json_body) self.assertEquals(HTTPStatus.OK, channel.code, msg=channel.json_body)
self.assertEqual(private_room_id, channel.json_body["joined_rooms"][0]) self.assertEqual(private_room_id, channel.json_body["joined_rooms"][0])
def test_context_as_non_admin(self): def test_context_as_non_admin(self):
@ -2039,7 +2043,7 @@ class JoinAliasRoomTestCase(unittest.HomeserverTestCase):
% (room_id, events[midway]["event_id"]), % (room_id, events[midway]["event_id"]),
access_token=tok, access_token=tok,
) )
self.assertEquals(403, channel.code, msg=channel.json_body) self.assertEquals(HTTPStatus.FORBIDDEN, channel.code, msg=channel.json_body)
self.assertEqual(Codes.FORBIDDEN, channel.json_body["errcode"]) self.assertEqual(Codes.FORBIDDEN, channel.json_body["errcode"])
def test_context_as_admin(self): def test_context_as_admin(self):
@ -2069,7 +2073,7 @@ class JoinAliasRoomTestCase(unittest.HomeserverTestCase):
% (room_id, events[midway]["event_id"]), % (room_id, events[midway]["event_id"]),
access_token=self.admin_user_tok, access_token=self.admin_user_tok,
) )
self.assertEquals(200, channel.code, msg=channel.json_body) self.assertEquals(HTTPStatus.OK, channel.code, msg=channel.json_body)
self.assertEquals( self.assertEquals(
channel.json_body["event"]["event_id"], events[midway]["event_id"] channel.json_body["event"]["event_id"], events[midway]["event_id"]
) )
@ -2128,7 +2132,7 @@ class MakeRoomAdminTestCase(unittest.HomeserverTestCase):
access_token=self.admin_user_tok, access_token=self.admin_user_tok,
) )
self.assertEqual(200, channel.code, msg=channel.json_body) self.assertEqual(HTTPStatus.OK, channel.code, msg=channel.json_body)
# Now we test that we can join the room and ban a user. # Now we test that we can join the room and ban a user.
self.helper.join(room_id, self.admin_user, tok=self.admin_user_tok) self.helper.join(room_id, self.admin_user, tok=self.admin_user_tok)
@ -2155,7 +2159,7 @@ class MakeRoomAdminTestCase(unittest.HomeserverTestCase):
access_token=self.admin_user_tok, access_token=self.admin_user_tok,
) )
self.assertEqual(200, channel.code, msg=channel.json_body) self.assertEqual(HTTPStatus.OK, channel.code, msg=channel.json_body)
# Now we test that we can join the room (we should have received an # Now we test that we can join the room (we should have received an
# invite) and can ban a user. # invite) and can ban a user.
@ -2181,7 +2185,7 @@ class MakeRoomAdminTestCase(unittest.HomeserverTestCase):
access_token=self.admin_user_tok, access_token=self.admin_user_tok,
) )
self.assertEqual(200, channel.code, msg=channel.json_body) self.assertEqual(HTTPStatus.OK, channel.code, msg=channel.json_body)
# Now we test that we can join the room and ban a user. # Now we test that we can join the room and ban a user.
self.helper.join(room_id, self.second_user_id, tok=self.second_tok) self.helper.join(room_id, self.second_user_id, tok=self.second_tok)
@ -2215,11 +2219,11 @@ class MakeRoomAdminTestCase(unittest.HomeserverTestCase):
access_token=self.admin_user_tok, access_token=self.admin_user_tok,
) )
# We expect this to fail with a 400 as there are no room admins. # We expect this to fail with a HTTPStatus.BAD_REQUEST as there are no room admins.
# #
# (Note we assert the error message to ensure that it's not denied for # (Note we assert the error message to ensure that it's not denied for
# some other reason) # some other reason)
self.assertEqual(400, channel.code, msg=channel.json_body) self.assertEqual(HTTPStatus.BAD_REQUEST, channel.code, msg=channel.json_body)
self.assertEqual( self.assertEqual(
channel.json_body["error"], channel.json_body["error"],
"No local admin user in room with power to update power levels.", "No local admin user in room with power to update power levels.",
@ -2249,7 +2253,7 @@ class BlockRoomTestCase(unittest.HomeserverTestCase):
@parameterized.expand([("PUT",), ("GET",)]) @parameterized.expand([("PUT",), ("GET",)])
def test_requester_is_no_admin(self, method: str): def test_requester_is_no_admin(self, method: str):
"""If the user is not a server admin, an error 403 is returned.""" """If the user is not a server admin, an error HTTPStatus.FORBIDDEN is returned."""
channel = self.make_request( channel = self.make_request(
method, method,
@ -2263,7 +2267,7 @@ class BlockRoomTestCase(unittest.HomeserverTestCase):
@parameterized.expand([("PUT",), ("GET",)]) @parameterized.expand([("PUT",), ("GET",)])
def test_room_is_not_valid(self, method: str): def test_room_is_not_valid(self, method: str):
"""Check that invalid room names, return an error 400.""" """Check that invalid room names, return an error HTTPStatus.BAD_REQUEST."""
channel = self.make_request( channel = self.make_request(
method, method,

View file

@ -12,6 +12,7 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
from http import HTTPStatus
from typing import List from typing import List
import synapse.rest.admin import synapse.rest.admin
@ -52,7 +53,11 @@ class ServerNoticeTestCase(unittest.HomeserverTestCase):
"""Try to send a server notice without authentication.""" """Try to send a server notice without authentication."""
channel = self.make_request("POST", self.url) channel = self.make_request("POST", self.url)
self.assertEqual(401, int(channel.result["code"]), msg=channel.result["body"]) self.assertEqual(
HTTPStatus.UNAUTHORIZED,
channel.code,
msg=channel.json_body,
)
self.assertEqual(Codes.MISSING_TOKEN, channel.json_body["errcode"]) self.assertEqual(Codes.MISSING_TOKEN, channel.json_body["errcode"])
def test_requester_is_no_admin(self): def test_requester_is_no_admin(self):
@ -63,12 +68,16 @@ class ServerNoticeTestCase(unittest.HomeserverTestCase):
access_token=self.other_user_token, access_token=self.other_user_token,
) )
self.assertEqual(403, int(channel.result["code"]), msg=channel.result["body"]) self.assertEqual(
HTTPStatus.FORBIDDEN,
channel.code,
msg=channel.json_body,
)
self.assertEqual(Codes.FORBIDDEN, channel.json_body["errcode"]) self.assertEqual(Codes.FORBIDDEN, channel.json_body["errcode"])
@override_config({"server_notices": {"system_mxid_localpart": "notices"}}) @override_config({"server_notices": {"system_mxid_localpart": "notices"}})
def test_user_does_not_exist(self): def test_user_does_not_exist(self):
"""Tests that a lookup for a user that does not exist returns a 404""" """Tests that a lookup for a user that does not exist returns a HTTPStatus.NOT_FOUND"""
channel = self.make_request( channel = self.make_request(
"POST", "POST",
self.url, self.url,
@ -76,13 +85,13 @@ class ServerNoticeTestCase(unittest.HomeserverTestCase):
content={"user_id": "@unknown_person:test", "content": ""}, content={"user_id": "@unknown_person:test", "content": ""},
) )
self.assertEqual(404, channel.code, msg=channel.json_body) self.assertEqual(HTTPStatus.NOT_FOUND, channel.code, msg=channel.json_body)
self.assertEqual(Codes.NOT_FOUND, channel.json_body["errcode"]) self.assertEqual(Codes.NOT_FOUND, channel.json_body["errcode"])
@override_config({"server_notices": {"system_mxid_localpart": "notices"}}) @override_config({"server_notices": {"system_mxid_localpart": "notices"}})
def test_user_is_not_local(self): def test_user_is_not_local(self):
""" """
Tests that a lookup for a user that is not a local returns a 400 Tests that a lookup for a user that is not a local returns a HTTPStatus.BAD_REQUEST
""" """
channel = self.make_request( channel = self.make_request(
"POST", "POST",
@ -94,7 +103,7 @@ class ServerNoticeTestCase(unittest.HomeserverTestCase):
}, },
) )
self.assertEqual(400, channel.code, msg=channel.json_body) self.assertEqual(HTTPStatus.BAD_REQUEST, channel.code, msg=channel.json_body)
self.assertEqual( self.assertEqual(
"Server notices can only be sent to local users", channel.json_body["error"] "Server notices can only be sent to local users", channel.json_body["error"]
) )
@ -110,7 +119,7 @@ class ServerNoticeTestCase(unittest.HomeserverTestCase):
access_token=self.admin_user_tok, access_token=self.admin_user_tok,
) )
self.assertEqual(400, channel.code, msg=channel.json_body) self.assertEqual(HTTPStatus.BAD_REQUEST, channel.code, msg=channel.json_body)
self.assertEqual(Codes.NOT_JSON, channel.json_body["errcode"]) self.assertEqual(Codes.NOT_JSON, channel.json_body["errcode"])
# no content # no content
@ -121,7 +130,7 @@ class ServerNoticeTestCase(unittest.HomeserverTestCase):
content={"user_id": self.other_user}, content={"user_id": self.other_user},
) )
self.assertEqual(400, channel.code, msg=channel.json_body) self.assertEqual(HTTPStatus.BAD_REQUEST, channel.code, msg=channel.json_body)
self.assertEqual(Codes.MISSING_PARAM, channel.json_body["errcode"]) self.assertEqual(Codes.MISSING_PARAM, channel.json_body["errcode"])
# no body # no body
@ -132,7 +141,7 @@ class ServerNoticeTestCase(unittest.HomeserverTestCase):
content={"user_id": self.other_user, "content": ""}, content={"user_id": self.other_user, "content": ""},
) )
self.assertEqual(400, channel.code, msg=channel.json_body) self.assertEqual(HTTPStatus.BAD_REQUEST, channel.code, msg=channel.json_body)
self.assertEqual(Codes.UNKNOWN, channel.json_body["errcode"]) self.assertEqual(Codes.UNKNOWN, channel.json_body["errcode"])
self.assertEqual("'body' not in content", channel.json_body["error"]) self.assertEqual("'body' not in content", channel.json_body["error"])
@ -144,7 +153,7 @@ class ServerNoticeTestCase(unittest.HomeserverTestCase):
content={"user_id": self.other_user, "content": {"body": ""}}, content={"user_id": self.other_user, "content": {"body": ""}},
) )
self.assertEqual(400, channel.code, msg=channel.json_body) self.assertEqual(HTTPStatus.BAD_REQUEST, channel.code, msg=channel.json_body)
self.assertEqual(Codes.UNKNOWN, channel.json_body["errcode"]) self.assertEqual(Codes.UNKNOWN, channel.json_body["errcode"])
self.assertEqual("'msgtype' not in content", channel.json_body["error"]) self.assertEqual("'msgtype' not in content", channel.json_body["error"])
@ -160,7 +169,7 @@ class ServerNoticeTestCase(unittest.HomeserverTestCase):
}, },
) )
self.assertEqual(400, channel.code, msg=channel.json_body) self.assertEqual(HTTPStatus.BAD_REQUEST, channel.code, msg=channel.json_body)
self.assertEqual(Codes.UNKNOWN, channel.json_body["errcode"]) self.assertEqual(Codes.UNKNOWN, channel.json_body["errcode"])
self.assertEqual( self.assertEqual(
"Server notices are not enabled on this server", channel.json_body["error"] "Server notices are not enabled on this server", channel.json_body["error"]
@ -185,7 +194,7 @@ class ServerNoticeTestCase(unittest.HomeserverTestCase):
"content": {"msgtype": "m.text", "body": "test msg one"}, "content": {"msgtype": "m.text", "body": "test msg one"},
}, },
) )
self.assertEqual(200, channel.code, msg=channel.json_body) self.assertEqual(HTTPStatus.OK, channel.code, msg=channel.json_body)
# user has one invite # user has one invite
invited_rooms = self._check_invite_and_join_status(self.other_user, 1, 0) invited_rooms = self._check_invite_and_join_status(self.other_user, 1, 0)
@ -216,7 +225,7 @@ class ServerNoticeTestCase(unittest.HomeserverTestCase):
"content": {"msgtype": "m.text", "body": "test msg two"}, "content": {"msgtype": "m.text", "body": "test msg two"},
}, },
) )
self.assertEqual(200, channel.code, msg=channel.json_body) self.assertEqual(HTTPStatus.OK, channel.code, msg=channel.json_body)
# user has no new invites or memberships # user has no new invites or memberships
self._check_invite_and_join_status(self.other_user, 0, 1) self._check_invite_and_join_status(self.other_user, 0, 1)
@ -250,7 +259,7 @@ class ServerNoticeTestCase(unittest.HomeserverTestCase):
"content": {"msgtype": "m.text", "body": "test msg one"}, "content": {"msgtype": "m.text", "body": "test msg one"},
}, },
) )
self.assertEqual(200, channel.code, msg=channel.json_body) self.assertEqual(HTTPStatus.OK, channel.code, msg=channel.json_body)
# user has one invite # user has one invite
invited_rooms = self._check_invite_and_join_status(self.other_user, 1, 0) invited_rooms = self._check_invite_and_join_status(self.other_user, 1, 0)
@ -293,7 +302,7 @@ class ServerNoticeTestCase(unittest.HomeserverTestCase):
"content": {"msgtype": "m.text", "body": "test msg two"}, "content": {"msgtype": "m.text", "body": "test msg two"},
}, },
) )
self.assertEqual(200, channel.code, msg=channel.json_body) self.assertEqual(HTTPStatus.OK, channel.code, msg=channel.json_body)
# user has one invite # user has one invite
invited_rooms = self._check_invite_and_join_status(self.other_user, 1, 0) invited_rooms = self._check_invite_and_join_status(self.other_user, 1, 0)
@ -333,7 +342,7 @@ class ServerNoticeTestCase(unittest.HomeserverTestCase):
"content": {"msgtype": "m.text", "body": "test msg one"}, "content": {"msgtype": "m.text", "body": "test msg one"},
}, },
) )
self.assertEqual(200, channel.code, msg=channel.json_body) self.assertEqual(HTTPStatus.OK, channel.code, msg=channel.json_body)
# user has one invite # user has one invite
invited_rooms = self._check_invite_and_join_status(self.other_user, 1, 0) invited_rooms = self._check_invite_and_join_status(self.other_user, 1, 0)
@ -382,7 +391,7 @@ class ServerNoticeTestCase(unittest.HomeserverTestCase):
"content": {"msgtype": "m.text", "body": "test msg two"}, "content": {"msgtype": "m.text", "body": "test msg two"},
}, },
) )
self.assertEqual(200, channel.code, msg=channel.json_body) self.assertEqual(HTTPStatus.OK, channel.code, msg=channel.json_body)
# user has one invite # user has one invite
invited_rooms = self._check_invite_and_join_status(self.other_user, 1, 0) invited_rooms = self._check_invite_and_join_status(self.other_user, 1, 0)
@ -440,7 +449,7 @@ class ServerNoticeTestCase(unittest.HomeserverTestCase):
channel = self.make_request( channel = self.make_request(
"GET", "/_matrix/client/r0/sync", access_token=token "GET", "/_matrix/client/r0/sync", access_token=token
) )
self.assertEqual(channel.code, 200) self.assertEqual(channel.code, HTTPStatus.OK)
# Get the messages # Get the messages
room = channel.json_body["rooms"]["join"][room_id] room = channel.json_body["rooms"]["join"][room_id]

View file

@ -13,7 +13,7 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
import json from http import HTTPStatus
from typing import Any, Dict, List, Optional from typing import Any, Dict, List, Optional
import synapse.rest.admin import synapse.rest.admin
@ -47,21 +47,29 @@ class UserMediaStatisticsTestCase(unittest.HomeserverTestCase):
""" """
channel = self.make_request("GET", self.url, b"{}") channel = self.make_request("GET", self.url, b"{}")
self.assertEqual(401, int(channel.result["code"]), msg=channel.result["body"]) self.assertEqual(
HTTPStatus.UNAUTHORIZED,
channel.code,
msg=channel.json_body,
)
self.assertEqual(Codes.MISSING_TOKEN, channel.json_body["errcode"]) self.assertEqual(Codes.MISSING_TOKEN, channel.json_body["errcode"])
def test_requester_is_no_admin(self): def test_requester_is_no_admin(self):
""" """
If the user is not a server admin, an error 403 is returned. If the user is not a server admin, an error HTTPStatus.FORBIDDEN is returned.
""" """
channel = self.make_request( channel = self.make_request(
"GET", "GET",
self.url, self.url,
json.dumps({}), {},
access_token=self.other_user_tok, access_token=self.other_user_tok,
) )
self.assertEqual(403, int(channel.result["code"]), msg=channel.result["body"]) self.assertEqual(
HTTPStatus.FORBIDDEN,
channel.code,
msg=channel.json_body,
)
self.assertEqual(Codes.FORBIDDEN, channel.json_body["errcode"]) self.assertEqual(Codes.FORBIDDEN, channel.json_body["errcode"])
def test_invalid_parameter(self): def test_invalid_parameter(self):
@ -75,7 +83,11 @@ class UserMediaStatisticsTestCase(unittest.HomeserverTestCase):
access_token=self.admin_user_tok, access_token=self.admin_user_tok,
) )
self.assertEqual(400, int(channel.result["code"]), msg=channel.result["body"]) self.assertEqual(
HTTPStatus.BAD_REQUEST,
channel.code,
msg=channel.json_body,
)
self.assertEqual(Codes.INVALID_PARAM, channel.json_body["errcode"]) self.assertEqual(Codes.INVALID_PARAM, channel.json_body["errcode"])
# negative from # negative from
@ -85,7 +97,11 @@ class UserMediaStatisticsTestCase(unittest.HomeserverTestCase):
access_token=self.admin_user_tok, access_token=self.admin_user_tok,
) )
self.assertEqual(400, int(channel.result["code"]), msg=channel.result["body"]) self.assertEqual(
HTTPStatus.BAD_REQUEST,
channel.code,
msg=channel.json_body,
)
self.assertEqual(Codes.INVALID_PARAM, channel.json_body["errcode"]) self.assertEqual(Codes.INVALID_PARAM, channel.json_body["errcode"])
# negative limit # negative limit
@ -95,7 +111,11 @@ class UserMediaStatisticsTestCase(unittest.HomeserverTestCase):
access_token=self.admin_user_tok, access_token=self.admin_user_tok,
) )
self.assertEqual(400, int(channel.result["code"]), msg=channel.result["body"]) self.assertEqual(
HTTPStatus.BAD_REQUEST,
channel.code,
msg=channel.json_body,
)
self.assertEqual(Codes.INVALID_PARAM, channel.json_body["errcode"]) self.assertEqual(Codes.INVALID_PARAM, channel.json_body["errcode"])
# negative from_ts # negative from_ts
@ -105,7 +125,11 @@ class UserMediaStatisticsTestCase(unittest.HomeserverTestCase):
access_token=self.admin_user_tok, access_token=self.admin_user_tok,
) )
self.assertEqual(400, int(channel.result["code"]), msg=channel.result["body"]) self.assertEqual(
HTTPStatus.BAD_REQUEST,
channel.code,
msg=channel.json_body,
)
self.assertEqual(Codes.INVALID_PARAM, channel.json_body["errcode"]) self.assertEqual(Codes.INVALID_PARAM, channel.json_body["errcode"])
# negative until_ts # negative until_ts
@ -115,7 +139,11 @@ class UserMediaStatisticsTestCase(unittest.HomeserverTestCase):
access_token=self.admin_user_tok, access_token=self.admin_user_tok,
) )
self.assertEqual(400, int(channel.result["code"]), msg=channel.result["body"]) self.assertEqual(
HTTPStatus.BAD_REQUEST,
channel.code,
msg=channel.json_body,
)
self.assertEqual(Codes.INVALID_PARAM, channel.json_body["errcode"]) self.assertEqual(Codes.INVALID_PARAM, channel.json_body["errcode"])
# until_ts smaller from_ts # until_ts smaller from_ts
@ -125,7 +153,11 @@ class UserMediaStatisticsTestCase(unittest.HomeserverTestCase):
access_token=self.admin_user_tok, access_token=self.admin_user_tok,
) )
self.assertEqual(400, int(channel.result["code"]), msg=channel.result["body"]) self.assertEqual(
HTTPStatus.BAD_REQUEST,
channel.code,
msg=channel.json_body,
)
self.assertEqual(Codes.INVALID_PARAM, channel.json_body["errcode"]) self.assertEqual(Codes.INVALID_PARAM, channel.json_body["errcode"])
# empty search term # empty search term
@ -135,7 +167,11 @@ class UserMediaStatisticsTestCase(unittest.HomeserverTestCase):
access_token=self.admin_user_tok, access_token=self.admin_user_tok,
) )
self.assertEqual(400, int(channel.result["code"]), msg=channel.result["body"]) self.assertEqual(
HTTPStatus.BAD_REQUEST,
channel.code,
msg=channel.json_body,
)
self.assertEqual(Codes.INVALID_PARAM, channel.json_body["errcode"]) self.assertEqual(Codes.INVALID_PARAM, channel.json_body["errcode"])
# invalid search order # invalid search order
@ -145,7 +181,11 @@ class UserMediaStatisticsTestCase(unittest.HomeserverTestCase):
access_token=self.admin_user_tok, access_token=self.admin_user_tok,
) )
self.assertEqual(400, int(channel.result["code"]), msg=channel.result["body"]) self.assertEqual(
HTTPStatus.BAD_REQUEST,
channel.code,
msg=channel.json_body,
)
self.assertEqual(Codes.INVALID_PARAM, channel.json_body["errcode"]) self.assertEqual(Codes.INVALID_PARAM, channel.json_body["errcode"])
def test_limit(self): def test_limit(self):
@ -160,7 +200,7 @@ class UserMediaStatisticsTestCase(unittest.HomeserverTestCase):
access_token=self.admin_user_tok, access_token=self.admin_user_tok,
) )
self.assertEqual(200, int(channel.result["code"]), msg=channel.result["body"]) self.assertEqual(HTTPStatus.OK, channel.code, msg=channel.json_body)
self.assertEqual(channel.json_body["total"], 10) self.assertEqual(channel.json_body["total"], 10)
self.assertEqual(len(channel.json_body["users"]), 5) self.assertEqual(len(channel.json_body["users"]), 5)
self.assertEqual(channel.json_body["next_token"], 5) self.assertEqual(channel.json_body["next_token"], 5)
@ -178,7 +218,7 @@ class UserMediaStatisticsTestCase(unittest.HomeserverTestCase):
access_token=self.admin_user_tok, access_token=self.admin_user_tok,
) )
self.assertEqual(200, int(channel.result["code"]), msg=channel.result["body"]) self.assertEqual(HTTPStatus.OK, channel.code, msg=channel.json_body)
self.assertEqual(channel.json_body["total"], 20) self.assertEqual(channel.json_body["total"], 20)
self.assertEqual(len(channel.json_body["users"]), 15) self.assertEqual(len(channel.json_body["users"]), 15)
self.assertNotIn("next_token", channel.json_body) self.assertNotIn("next_token", channel.json_body)
@ -196,7 +236,7 @@ class UserMediaStatisticsTestCase(unittest.HomeserverTestCase):
access_token=self.admin_user_tok, access_token=self.admin_user_tok,
) )
self.assertEqual(200, int(channel.result["code"]), msg=channel.result["body"]) self.assertEqual(HTTPStatus.OK, channel.code, msg=channel.json_body)
self.assertEqual(channel.json_body["total"], 20) self.assertEqual(channel.json_body["total"], 20)
self.assertEqual(channel.json_body["next_token"], 15) self.assertEqual(channel.json_body["next_token"], 15)
self.assertEqual(len(channel.json_body["users"]), 10) self.assertEqual(len(channel.json_body["users"]), 10)
@ -218,7 +258,7 @@ class UserMediaStatisticsTestCase(unittest.HomeserverTestCase):
access_token=self.admin_user_tok, access_token=self.admin_user_tok,
) )
self.assertEqual(200, int(channel.result["code"]), msg=channel.result["body"]) self.assertEqual(HTTPStatus.OK, channel.code, msg=channel.json_body)
self.assertEqual(channel.json_body["total"], number_users) self.assertEqual(channel.json_body["total"], number_users)
self.assertEqual(len(channel.json_body["users"]), number_users) self.assertEqual(len(channel.json_body["users"]), number_users)
self.assertNotIn("next_token", channel.json_body) self.assertNotIn("next_token", channel.json_body)
@ -231,7 +271,7 @@ class UserMediaStatisticsTestCase(unittest.HomeserverTestCase):
access_token=self.admin_user_tok, access_token=self.admin_user_tok,
) )
self.assertEqual(200, int(channel.result["code"]), msg=channel.result["body"]) self.assertEqual(HTTPStatus.OK, channel.code, msg=channel.json_body)
self.assertEqual(channel.json_body["total"], number_users) self.assertEqual(channel.json_body["total"], number_users)
self.assertEqual(len(channel.json_body["users"]), number_users) self.assertEqual(len(channel.json_body["users"]), number_users)
self.assertNotIn("next_token", channel.json_body) self.assertNotIn("next_token", channel.json_body)
@ -244,7 +284,7 @@ class UserMediaStatisticsTestCase(unittest.HomeserverTestCase):
access_token=self.admin_user_tok, access_token=self.admin_user_tok,
) )
self.assertEqual(200, int(channel.result["code"]), msg=channel.result["body"]) self.assertEqual(HTTPStatus.OK, channel.code, msg=channel.json_body)
self.assertEqual(channel.json_body["total"], number_users) self.assertEqual(channel.json_body["total"], number_users)
self.assertEqual(len(channel.json_body["users"]), 19) self.assertEqual(len(channel.json_body["users"]), 19)
self.assertEqual(channel.json_body["next_token"], 19) self.assertEqual(channel.json_body["next_token"], 19)
@ -257,7 +297,7 @@ class UserMediaStatisticsTestCase(unittest.HomeserverTestCase):
access_token=self.admin_user_tok, access_token=self.admin_user_tok,
) )
self.assertEqual(200, int(channel.result["code"]), msg=channel.result["body"]) self.assertEqual(HTTPStatus.OK, channel.code, msg=channel.json_body)
self.assertEqual(channel.json_body["total"], number_users) self.assertEqual(channel.json_body["total"], number_users)
self.assertEqual(len(channel.json_body["users"]), 1) self.assertEqual(len(channel.json_body["users"]), 1)
self.assertNotIn("next_token", channel.json_body) self.assertNotIn("next_token", channel.json_body)
@ -274,7 +314,7 @@ class UserMediaStatisticsTestCase(unittest.HomeserverTestCase):
access_token=self.admin_user_tok, access_token=self.admin_user_tok,
) )
self.assertEqual(200, channel.code, msg=channel.json_body) self.assertEqual(HTTPStatus.OK, channel.code, msg=channel.json_body)
self.assertEqual(0, channel.json_body["total"]) self.assertEqual(0, channel.json_body["total"])
self.assertEqual(0, len(channel.json_body["users"])) self.assertEqual(0, len(channel.json_body["users"]))
@ -371,7 +411,7 @@ class UserMediaStatisticsTestCase(unittest.HomeserverTestCase):
self.url, self.url,
access_token=self.admin_user_tok, access_token=self.admin_user_tok,
) )
self.assertEqual(200, int(channel.result["code"]), msg=channel.result["body"]) self.assertEqual(HTTPStatus.OK, channel.code, msg=channel.json_body)
self.assertEqual(channel.json_body["users"][0]["media_count"], 3) self.assertEqual(channel.json_body["users"][0]["media_count"], 3)
# filter media starting at `ts1` after creating first media # filter media starting at `ts1` after creating first media
@ -381,7 +421,7 @@ class UserMediaStatisticsTestCase(unittest.HomeserverTestCase):
self.url + "?from_ts=%s" % (ts1,), self.url + "?from_ts=%s" % (ts1,),
access_token=self.admin_user_tok, access_token=self.admin_user_tok,
) )
self.assertEqual(200, int(channel.result["code"]), msg=channel.result["body"]) self.assertEqual(HTTPStatus.OK, channel.code, msg=channel.json_body)
self.assertEqual(channel.json_body["total"], 0) self.assertEqual(channel.json_body["total"], 0)
self._create_media(self.other_user_tok, 3) self._create_media(self.other_user_tok, 3)
@ -396,7 +436,7 @@ class UserMediaStatisticsTestCase(unittest.HomeserverTestCase):
self.url + "?from_ts=%s&until_ts=%s" % (ts1, ts2), self.url + "?from_ts=%s&until_ts=%s" % (ts1, ts2),
access_token=self.admin_user_tok, access_token=self.admin_user_tok,
) )
self.assertEqual(200, int(channel.result["code"]), msg=channel.result["body"]) self.assertEqual(HTTPStatus.OK, channel.code, msg=channel.json_body)
self.assertEqual(channel.json_body["users"][0]["media_count"], 3) self.assertEqual(channel.json_body["users"][0]["media_count"], 3)
# filter media until `ts2` and earlier # filter media until `ts2` and earlier
@ -405,7 +445,7 @@ class UserMediaStatisticsTestCase(unittest.HomeserverTestCase):
self.url + "?until_ts=%s" % (ts2,), self.url + "?until_ts=%s" % (ts2,),
access_token=self.admin_user_tok, access_token=self.admin_user_tok,
) )
self.assertEqual(200, int(channel.result["code"]), msg=channel.result["body"]) self.assertEqual(HTTPStatus.OK, channel.code, msg=channel.json_body)
self.assertEqual(channel.json_body["users"][0]["media_count"], 6) self.assertEqual(channel.json_body["users"][0]["media_count"], 6)
def test_search_term(self): def test_search_term(self):
@ -417,7 +457,7 @@ class UserMediaStatisticsTestCase(unittest.HomeserverTestCase):
self.url, self.url,
access_token=self.admin_user_tok, access_token=self.admin_user_tok,
) )
self.assertEqual(200, int(channel.result["code"]), msg=channel.result["body"]) self.assertEqual(HTTPStatus.OK, channel.code, msg=channel.json_body)
self.assertEqual(channel.json_body["total"], 20) self.assertEqual(channel.json_body["total"], 20)
# filter user 1 and 10-19 by `user_id` # filter user 1 and 10-19 by `user_id`
@ -426,7 +466,7 @@ class UserMediaStatisticsTestCase(unittest.HomeserverTestCase):
self.url + "?search_term=foo_user_1", self.url + "?search_term=foo_user_1",
access_token=self.admin_user_tok, access_token=self.admin_user_tok,
) )
self.assertEqual(200, int(channel.result["code"]), msg=channel.result["body"]) self.assertEqual(HTTPStatus.OK, channel.code, msg=channel.json_body)
self.assertEqual(channel.json_body["total"], 11) self.assertEqual(channel.json_body["total"], 11)
# filter on this user in `displayname` # filter on this user in `displayname`
@ -435,7 +475,7 @@ class UserMediaStatisticsTestCase(unittest.HomeserverTestCase):
self.url + "?search_term=bar_user_10", self.url + "?search_term=bar_user_10",
access_token=self.admin_user_tok, access_token=self.admin_user_tok,
) )
self.assertEqual(200, int(channel.result["code"]), msg=channel.result["body"]) self.assertEqual(HTTPStatus.OK, channel.code, msg=channel.json_body)
self.assertEqual(channel.json_body["users"][0]["displayname"], "bar_user_10") self.assertEqual(channel.json_body["users"][0]["displayname"], "bar_user_10")
self.assertEqual(channel.json_body["total"], 1) self.assertEqual(channel.json_body["total"], 1)
@ -445,7 +485,7 @@ class UserMediaStatisticsTestCase(unittest.HomeserverTestCase):
self.url + "?search_term=foobar", self.url + "?search_term=foobar",
access_token=self.admin_user_tok, access_token=self.admin_user_tok,
) )
self.assertEqual(200, int(channel.result["code"]), msg=channel.result["body"]) self.assertEqual(HTTPStatus.OK, channel.code, msg=channel.json_body)
self.assertEqual(channel.json_body["total"], 0) self.assertEqual(channel.json_body["total"], 0)
def _create_users_with_media(self, number_users: int, media_per_user: int): def _create_users_with_media(self, number_users: int, media_per_user: int):
@ -471,7 +511,7 @@ class UserMediaStatisticsTestCase(unittest.HomeserverTestCase):
for _ in range(number_media): for _ in range(number_media):
# Upload some media into the room # Upload some media into the room
self.helper.upload_media( self.helper.upload_media(
upload_resource, SMALL_PNG, tok=user_token, expect_code=200 upload_resource, SMALL_PNG, tok=user_token, expect_code=HTTPStatus.OK
) )
def _check_fields(self, content: List[Dict[str, Any]]): def _check_fields(self, content: List[Dict[str, Any]]):
@ -505,7 +545,7 @@ class UserMediaStatisticsTestCase(unittest.HomeserverTestCase):
url.encode("ascii"), url.encode("ascii"),
access_token=self.admin_user_tok, access_token=self.admin_user_tok,
) )
self.assertEqual(200, channel.code, msg=channel.json_body) self.assertEqual(HTTPStatus.OK, channel.code, msg=channel.json_body)
self.assertEqual(channel.json_body["total"], len(expected_user_list)) self.assertEqual(channel.json_body["total"], len(expected_user_list))
returned_order = [row["user_id"] for row in channel.json_body["users"]] returned_order = [row["user_id"] for row in channel.json_body["users"]]

File diff suppressed because it is too large Load diff

View file

@ -12,6 +12,8 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
from http import HTTPStatus
import synapse.rest.admin import synapse.rest.admin
from synapse.api.errors import Codes, SynapseError from synapse.api.errors import Codes, SynapseError
from synapse.rest.client import login from synapse.rest.client import login
@ -33,30 +35,38 @@ class UsernameAvailableTestCase(unittest.HomeserverTestCase):
async def check_username(username): async def check_username(username):
if username == "allowed": if username == "allowed":
return True return True
raise SynapseError(400, "User ID already taken.", errcode=Codes.USER_IN_USE) raise SynapseError(
HTTPStatus.BAD_REQUEST,
"User ID already taken.",
errcode=Codes.USER_IN_USE,
)
handler = self.hs.get_registration_handler() handler = self.hs.get_registration_handler()
handler.check_username = check_username handler.check_username = check_username
def test_username_available(self): def test_username_available(self):
""" """
The endpoint should return a 200 response if the username does not exist The endpoint should return a HTTPStatus.OK response if the username does not exist
""" """
url = "%s?username=%s" % (self.url, "allowed") url = "%s?username=%s" % (self.url, "allowed")
channel = self.make_request("GET", url, None, self.admin_user_tok) channel = self.make_request("GET", url, None, self.admin_user_tok)
self.assertEqual(200, int(channel.result["code"]), msg=channel.result["body"]) self.assertEqual(HTTPStatus.OK, channel.code, msg=channel.json_body)
self.assertTrue(channel.json_body["available"]) self.assertTrue(channel.json_body["available"])
def test_username_unavailable(self): def test_username_unavailable(self):
""" """
The endpoint should return a 200 response if the username does not exist The endpoint should return a HTTPStatus.OK response if the username does not exist
""" """
url = "%s?username=%s" % (self.url, "disallowed") url = "%s?username=%s" % (self.url, "disallowed")
channel = self.make_request("GET", url, None, self.admin_user_tok) channel = self.make_request("GET", url, None, self.admin_user_tok)
self.assertEqual(400, int(channel.result["code"]), msg=channel.result["body"]) self.assertEqual(
HTTPStatus.BAD_REQUEST,
channel.code,
msg=channel.json_body,
)
self.assertEqual(channel.json_body["errcode"], "M_USER_IN_USE") self.assertEqual(channel.json_body["errcode"], "M_USER_IN_USE")
self.assertEqual(channel.json_body["error"], "User ID already taken.") self.assertEqual(channel.json_body["error"], "User ID already taken.")