Skip to content

Commit

Permalink
Simplify tests for the device admin rest API. (matrix-org#10664)
Browse files Browse the repository at this point in the history
By replacing duplicated code with parameterized tests and
avoiding unnecessary dumping of JSON data.
  • Loading branch information
dklimpel authored Aug 20, 2021
1 parent 7862d70 commit f499dc3
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 81 deletions.
1 change: 1 addition & 0 deletions changelog.d/10664.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Simplify tests for device admin rest API.
99 changes: 18 additions & 81 deletions tests/rest/admin/test_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import json
import urllib.parse

from parameterized import parameterized

import synapse.rest.admin
from synapse.api.errors import Codes
from synapse.rest.client import login
Expand Down Expand Up @@ -45,57 +46,32 @@ def prepare(self, reactor, clock, hs):
self.other_user_device_id,
)

def test_no_auth(self):
@parameterized.expand(["GET", "PUT", "DELETE"])
def test_no_auth(self, method: str):
"""
Try to get a device of an user without authentication.
"""
channel = self.make_request("GET", self.url, b"{}")

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

channel = self.make_request("PUT", self.url, b"{}")

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

channel = self.make_request("DELETE", self.url, b"{}")
channel = self.make_request(method, self.url, b"{}")

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

def test_requester_is_no_admin(self):
@parameterized.expand(["GET", "PUT", "DELETE"])
def test_requester_is_no_admin(self, method: str):
"""
If the user is not a server admin, an error is returned.
"""
channel = self.make_request(
"GET",
self.url,
access_token=self.other_user_token,
)

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

channel = self.make_request(
"PUT",
self.url,
access_token=self.other_user_token,
)

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

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

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

def test_user_does_not_exist(self):
@parameterized.expand(["GET", "PUT", "DELETE"])
def test_user_does_not_exist(self, method: str):
"""
Tests that a lookup for a user that does not exist returns a 404
"""
Expand All @@ -105,33 +81,16 @@ def test_user_does_not_exist(self):
)

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

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

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

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

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

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

def test_user_is_not_local(self):
@parameterized.expand(["GET", "PUT", "DELETE"])
def test_user_is_not_local(self, method: str):
"""
Tests that a lookup for a user that is not a local returns a 400
"""
Expand All @@ -141,25 +100,7 @@ def test_user_is_not_local(self):
)

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

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

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

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

channel = self.make_request(
"DELETE",
method,
url,
access_token=self.admin_user_tok,
)
Expand Down Expand Up @@ -219,12 +160,11 @@ def test_update_device_too_long_display_name(self):
* (synapse.handlers.device.MAX_DEVICE_DISPLAY_NAME_LEN + 1)
}

body = json.dumps(update)
channel = self.make_request(
"PUT",
self.url,
access_token=self.admin_user_tok,
content=body.encode(encoding="utf_8"),
content=update,
)

self.assertEqual(400, channel.code, msg=channel.json_body)
Expand Down Expand Up @@ -275,12 +215,11 @@ def test_update_display_name(self):
Tests a normal successful update of display name
"""
# Set new display_name
body = json.dumps({"display_name": "new displayname"})
channel = self.make_request(
"PUT",
self.url,
access_token=self.admin_user_tok,
content=body.encode(encoding="utf_8"),
content={"display_name": "new displayname"},
)

self.assertEqual(200, channel.code, msg=channel.json_body)
Expand Down Expand Up @@ -529,12 +468,11 @@ def test_unknown_devices(self):
"""
Tests that a remove of a device that does not exist returns 200.
"""
body = json.dumps({"devices": ["unknown_device1", "unknown_device2"]})
channel = self.make_request(
"POST",
self.url,
access_token=self.admin_user_tok,
content=body.encode(encoding="utf_8"),
content={"devices": ["unknown_device1", "unknown_device2"]},
)

# Delete unknown devices returns status 200
Expand All @@ -560,12 +498,11 @@ def test_delete_devices(self):
device_ids.append(str(d["device_id"]))

# Delete devices
body = json.dumps({"devices": device_ids})
channel = self.make_request(
"POST",
self.url,
access_token=self.admin_user_tok,
content=body.encode(encoding="utf_8"),
content={"devices": device_ids},
)

self.assertEqual(200, channel.code, msg=channel.json_body)
Expand Down

0 comments on commit f499dc3

Please sign in to comment.