Skip to content

Commit f499dc3

Browse files
authored
Simplify tests for the device admin rest API. (matrix-org#10664)
By replacing duplicated code with parameterized tests and avoiding unnecessary dumping of JSON data.
1 parent 7862d70 commit f499dc3

File tree

2 files changed

+19
-81
lines changed

2 files changed

+19
-81
lines changed

changelog.d/10664.misc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Simplify tests for device admin rest API.

tests/rest/admin/test_device.py

Lines changed: 18 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,10 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
import json
1615
import urllib.parse
1716

17+
from parameterized import parameterized
18+
1819
import synapse.rest.admin
1920
from synapse.api.errors import Codes
2021
from synapse.rest.client import login
@@ -45,57 +46,32 @@ def prepare(self, reactor, clock, hs):
4546
self.other_user_device_id,
4647
)
4748

48-
def test_no_auth(self):
49+
@parameterized.expand(["GET", "PUT", "DELETE"])
50+
def test_no_auth(self, method: str):
4951
"""
5052
Try to get a device of an user without authentication.
5153
"""
52-
channel = self.make_request("GET", self.url, b"{}")
53-
54-
self.assertEqual(401, int(channel.result["code"]), msg=channel.result["body"])
55-
self.assertEqual(Codes.MISSING_TOKEN, channel.json_body["errcode"])
56-
57-
channel = self.make_request("PUT", self.url, b"{}")
58-
59-
self.assertEqual(401, int(channel.result["code"]), msg=channel.result["body"])
60-
self.assertEqual(Codes.MISSING_TOKEN, channel.json_body["errcode"])
61-
62-
channel = self.make_request("DELETE", self.url, b"{}")
54+
channel = self.make_request(method, self.url, b"{}")
6355

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

67-
def test_requester_is_no_admin(self):
59+
@parameterized.expand(["GET", "PUT", "DELETE"])
60+
def test_requester_is_no_admin(self, method: str):
6861
"""
6962
If the user is not a server admin, an error is returned.
7063
"""
7164
channel = self.make_request(
72-
"GET",
73-
self.url,
74-
access_token=self.other_user_token,
75-
)
76-
77-
self.assertEqual(403, int(channel.result["code"]), msg=channel.result["body"])
78-
self.assertEqual(Codes.FORBIDDEN, channel.json_body["errcode"])
79-
80-
channel = self.make_request(
81-
"PUT",
82-
self.url,
83-
access_token=self.other_user_token,
84-
)
85-
86-
self.assertEqual(403, int(channel.result["code"]), msg=channel.result["body"])
87-
self.assertEqual(Codes.FORBIDDEN, channel.json_body["errcode"])
88-
89-
channel = self.make_request(
90-
"DELETE",
65+
method,
9166
self.url,
9267
access_token=self.other_user_token,
9368
)
9469

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

98-
def test_user_does_not_exist(self):
73+
@parameterized.expand(["GET", "PUT", "DELETE"])
74+
def test_user_does_not_exist(self, method: str):
9975
"""
10076
Tests that a lookup for a user that does not exist returns a 404
10177
"""
@@ -105,33 +81,16 @@ def test_user_does_not_exist(self):
10581
)
10682

10783
channel = self.make_request(
108-
"GET",
84+
method,
10985
url,
11086
access_token=self.admin_user_tok,
11187
)
11288

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

116-
channel = self.make_request(
117-
"PUT",
118-
url,
119-
access_token=self.admin_user_tok,
120-
)
121-
122-
self.assertEqual(404, channel.code, msg=channel.json_body)
123-
self.assertEqual(Codes.NOT_FOUND, channel.json_body["errcode"])
124-
125-
channel = self.make_request(
126-
"DELETE",
127-
url,
128-
access_token=self.admin_user_tok,
129-
)
130-
131-
self.assertEqual(404, channel.code, msg=channel.json_body)
132-
self.assertEqual(Codes.NOT_FOUND, channel.json_body["errcode"])
133-
134-
def test_user_is_not_local(self):
92+
@parameterized.expand(["GET", "PUT", "DELETE"])
93+
def test_user_is_not_local(self, method: str):
13594
"""
13695
Tests that a lookup for a user that is not a local returns a 400
13796
"""
@@ -141,25 +100,7 @@ def test_user_is_not_local(self):
141100
)
142101

143102
channel = self.make_request(
144-
"GET",
145-
url,
146-
access_token=self.admin_user_tok,
147-
)
148-
149-
self.assertEqual(400, channel.code, msg=channel.json_body)
150-
self.assertEqual("Can only lookup local users", channel.json_body["error"])
151-
152-
channel = self.make_request(
153-
"PUT",
154-
url,
155-
access_token=self.admin_user_tok,
156-
)
157-
158-
self.assertEqual(400, channel.code, msg=channel.json_body)
159-
self.assertEqual("Can only lookup local users", channel.json_body["error"])
160-
161-
channel = self.make_request(
162-
"DELETE",
103+
method,
163104
url,
164105
access_token=self.admin_user_tok,
165106
)
@@ -219,12 +160,11 @@ def test_update_device_too_long_display_name(self):
219160
* (synapse.handlers.device.MAX_DEVICE_DISPLAY_NAME_LEN + 1)
220161
}
221162

222-
body = json.dumps(update)
223163
channel = self.make_request(
224164
"PUT",
225165
self.url,
226166
access_token=self.admin_user_tok,
227-
content=body.encode(encoding="utf_8"),
167+
content=update,
228168
)
229169

230170
self.assertEqual(400, channel.code, msg=channel.json_body)
@@ -275,12 +215,11 @@ def test_update_display_name(self):
275215
Tests a normal successful update of display name
276216
"""
277217
# Set new display_name
278-
body = json.dumps({"display_name": "new displayname"})
279218
channel = self.make_request(
280219
"PUT",
281220
self.url,
282221
access_token=self.admin_user_tok,
283-
content=body.encode(encoding="utf_8"),
222+
content={"display_name": "new displayname"},
284223
)
285224

286225
self.assertEqual(200, channel.code, msg=channel.json_body)
@@ -529,12 +468,11 @@ def test_unknown_devices(self):
529468
"""
530469
Tests that a remove of a device that does not exist returns 200.
531470
"""
532-
body = json.dumps({"devices": ["unknown_device1", "unknown_device2"]})
533471
channel = self.make_request(
534472
"POST",
535473
self.url,
536474
access_token=self.admin_user_tok,
537-
content=body.encode(encoding="utf_8"),
475+
content={"devices": ["unknown_device1", "unknown_device2"]},
538476
)
539477

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

562500
# Delete devices
563-
body = json.dumps({"devices": device_ids})
564501
channel = self.make_request(
565502
"POST",
566503
self.url,
567504
access_token=self.admin_user_tok,
568-
content=body.encode(encoding="utf_8"),
505+
content={"devices": device_ids},
569506
)
570507

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

0 commit comments

Comments
 (0)