12
12
# See the License for the specific language governing permissions and
13
13
# limitations under the License.
14
14
15
- import json
16
15
import urllib .parse
17
16
17
+ from parameterized import parameterized
18
+
18
19
import synapse .rest .admin
19
20
from synapse .api .errors import Codes
20
21
from synapse .rest .client import login
@@ -45,57 +46,32 @@ def prepare(self, reactor, clock, hs):
45
46
self .other_user_device_id ,
46
47
)
47
48
48
- def test_no_auth (self ):
49
+ @parameterized .expand (["GET" , "PUT" , "DELETE" ])
50
+ def test_no_auth (self , method : str ):
49
51
"""
50
52
Try to get a device of an user without authentication.
51
53
"""
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"{}" )
63
55
64
56
self .assertEqual (401 , int (channel .result ["code" ]), msg = channel .result ["body" ])
65
57
self .assertEqual (Codes .MISSING_TOKEN , channel .json_body ["errcode" ])
66
58
67
- def test_requester_is_no_admin (self ):
59
+ @parameterized .expand (["GET" , "PUT" , "DELETE" ])
60
+ def test_requester_is_no_admin (self , method : str ):
68
61
"""
69
62
If the user is not a server admin, an error is returned.
70
63
"""
71
64
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 ,
91
66
self .url ,
92
67
access_token = self .other_user_token ,
93
68
)
94
69
95
70
self .assertEqual (403 , int (channel .result ["code" ]), msg = channel .result ["body" ])
96
71
self .assertEqual (Codes .FORBIDDEN , channel .json_body ["errcode" ])
97
72
98
- def test_user_does_not_exist (self ):
73
+ @parameterized .expand (["GET" , "PUT" , "DELETE" ])
74
+ def test_user_does_not_exist (self , method : str ):
99
75
"""
100
76
Tests that a lookup for a user that does not exist returns a 404
101
77
"""
@@ -105,33 +81,16 @@ def test_user_does_not_exist(self):
105
81
)
106
82
107
83
channel = self .make_request (
108
- "GET" ,
84
+ method ,
109
85
url ,
110
86
access_token = self .admin_user_tok ,
111
87
)
112
88
113
89
self .assertEqual (404 , channel .code , msg = channel .json_body )
114
90
self .assertEqual (Codes .NOT_FOUND , channel .json_body ["errcode" ])
115
91
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 ):
135
94
"""
136
95
Tests that a lookup for a user that is not a local returns a 400
137
96
"""
@@ -141,25 +100,7 @@ def test_user_is_not_local(self):
141
100
)
142
101
143
102
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 ,
163
104
url ,
164
105
access_token = self .admin_user_tok ,
165
106
)
@@ -219,12 +160,11 @@ def test_update_device_too_long_display_name(self):
219
160
* (synapse .handlers .device .MAX_DEVICE_DISPLAY_NAME_LEN + 1 )
220
161
}
221
162
222
- body = json .dumps (update )
223
163
channel = self .make_request (
224
164
"PUT" ,
225
165
self .url ,
226
166
access_token = self .admin_user_tok ,
227
- content = body . encode ( encoding = "utf_8" ) ,
167
+ content = update ,
228
168
)
229
169
230
170
self .assertEqual (400 , channel .code , msg = channel .json_body )
@@ -275,12 +215,11 @@ def test_update_display_name(self):
275
215
Tests a normal successful update of display name
276
216
"""
277
217
# Set new display_name
278
- body = json .dumps ({"display_name" : "new displayname" })
279
218
channel = self .make_request (
280
219
"PUT" ,
281
220
self .url ,
282
221
access_token = self .admin_user_tok ,
283
- content = body . encode ( encoding = "utf_8" ) ,
222
+ content = { "display_name" : "new displayname" } ,
284
223
)
285
224
286
225
self .assertEqual (200 , channel .code , msg = channel .json_body )
@@ -529,12 +468,11 @@ def test_unknown_devices(self):
529
468
"""
530
469
Tests that a remove of a device that does not exist returns 200.
531
470
"""
532
- body = json .dumps ({"devices" : ["unknown_device1" , "unknown_device2" ]})
533
471
channel = self .make_request (
534
472
"POST" ,
535
473
self .url ,
536
474
access_token = self .admin_user_tok ,
537
- content = body . encode ( encoding = "utf_8" ) ,
475
+ content = { "devices" : [ "unknown_device1" , "unknown_device2" ]} ,
538
476
)
539
477
540
478
# Delete unknown devices returns status 200
@@ -560,12 +498,11 @@ def test_delete_devices(self):
560
498
device_ids .append (str (d ["device_id" ]))
561
499
562
500
# Delete devices
563
- body = json .dumps ({"devices" : device_ids })
564
501
channel = self .make_request (
565
502
"POST" ,
566
503
self .url ,
567
504
access_token = self .admin_user_tok ,
568
- content = body . encode ( encoding = "utf_8" ) ,
505
+ content = { "devices" : device_ids } ,
569
506
)
570
507
571
508
self .assertEqual (200 , channel .code , msg = channel .json_body )
0 commit comments