Skip to content
This repository was archived by the owner on Apr 26, 2024. It is now read-only.

Commit 736e1df

Browse files
committed
Simplify tests slightly.
1 parent b987892 commit 736e1df

File tree

1 file changed

+15
-31
lines changed

1 file changed

+15
-31
lines changed

tests/rest/client/v2_alpha/test_auth.py

Lines changed: 15 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
# See the License for the specific language governing permissions and
1414
# limitations under the License.
1515

16-
from typing import List, Union
16+
from typing import Union
1717

1818
from twisted.internet.defer import succeed
1919

@@ -177,13 +177,8 @@ def create_resource_dict(self):
177177
def prepare(self, reactor, clock, hs):
178178
self.user_pass = "pass"
179179
self.user = self.register_user("test", self.user_pass)
180-
self.user_tok = self.login("test", self.user_pass)
181-
182-
def get_device_ids(self, access_token: str) -> List[str]:
183-
# Get the list of devices so one can be deleted.
184-
channel = self.make_request("GET", "devices", access_token=access_token,)
185-
self.assertEqual(channel.code, 200)
186-
return [d["device_id"] for d in channel.json_body["devices"]]
180+
self.device_id = "dev1"
181+
self.user_tok = self.login("test", self.user_pass, self.device_id)
187182

188183
def delete_device(
189184
self,
@@ -219,11 +214,9 @@ def test_ui_auth(self):
219214
"""
220215
Test user interactive authentication outside of registration.
221216
"""
222-
device_id = self.get_device_ids(self.user_tok)[0]
223-
224217
# Attempt to delete this device.
225218
# Returns a 401 as per the spec
226-
channel = self.delete_device(self.user_tok, device_id, 401)
219+
channel = self.delete_device(self.user_tok, self.device_id, 401)
227220

228221
# Grab the session
229222
session = channel.json_body["session"]
@@ -233,7 +226,7 @@ def test_ui_auth(self):
233226
# Make another request providing the UI auth flow.
234227
self.delete_device(
235228
self.user_tok,
236-
device_id,
229+
self.device_id,
237230
200,
238231
{
239232
"auth": {
@@ -252,14 +245,13 @@ def test_grandfathered_identifier(self):
252245
UIA - check that still works.
253246
"""
254247

255-
device_id = self.get_device_ids(self.user_tok)[0]
256-
channel = self.delete_device(self.user_tok, device_id, 401)
248+
channel = self.delete_device(self.user_tok, self.device_id, 401)
257249
session = channel.json_body["session"]
258250

259251
# Make another request providing the UI auth flow.
260252
self.delete_device(
261253
self.user_tok,
262-
device_id,
254+
self.device_id,
263255
200,
264256
{
265257
"auth": {
@@ -282,14 +274,11 @@ def test_can_change_body(self):
282274
session ID should be rejected.
283275
"""
284276
# Create a second login.
285-
self.login("test", self.user_pass)
286-
287-
device_ids = self.get_device_ids(self.user_tok)
288-
self.assertEqual(len(device_ids), 2)
277+
self.login("test", self.user_pass, "dev2")
289278

290279
# Attempt to delete the first device.
291280
# Returns a 401 as per the spec
292-
channel = self.delete_devices(401, {"devices": [device_ids[0]]})
281+
channel = self.delete_devices(401, {"devices": [self.device_id]})
293282

294283
# Grab the session
295284
session = channel.json_body["session"]
@@ -301,7 +290,7 @@ def test_can_change_body(self):
301290
self.delete_devices(
302291
200,
303292
{
304-
"devices": [device_ids[1]],
293+
"devices": ["dev2"],
305294
"auth": {
306295
"type": "m.login.password",
307296
"identifier": {"type": "m.id.user", "user": self.user},
@@ -316,14 +305,11 @@ def test_cannot_change_uri(self):
316305
The initial requested URI cannot be modified during the user interactive authentication session.
317306
"""
318307
# Create a second login.
319-
self.login("test", self.user_pass)
320-
321-
device_ids = self.get_device_ids(self.user_tok)
322-
self.assertEqual(len(device_ids), 2)
308+
self.login("test", self.user_pass, "dev2")
323309

324310
# Attempt to delete the first device.
325311
# Returns a 401 as per the spec
326-
channel = self.delete_device(self.user_tok, device_ids[0], 401)
312+
channel = self.delete_device(self.user_tok, self.device_id, 401)
327313

328314
# Grab the session
329315
session = channel.json_body["session"]
@@ -334,7 +320,7 @@ def test_cannot_change_uri(self):
334320
# second device. This results in an error.
335321
self.delete_device(
336322
self.user_tok,
337-
device_ids[1],
323+
"dev2",
338324
403,
339325
{
340326
"auth": {
@@ -361,8 +347,7 @@ def test_does_not_offer_password_for_sso_user(self):
361347
def test_does_not_offer_sso_for_password_user(self):
362348
# now call the device deletion API: we should get the option to auth with SSO
363349
# and not password.
364-
device_ids = self.get_device_ids(self.user_tok)
365-
channel = self.delete_device(self.user_tok, device_ids[0], 401)
350+
channel = self.delete_device(self.user_tok, self.device_id, 401)
366351

367352
flows = channel.json_body["flows"]
368353
self.assertEqual(flows, [{"stages": ["m.login.password"]}])
@@ -373,8 +358,7 @@ def test_offers_both_flows_for_upgraded_user(self):
373358
login_resp = self.helper.login_via_oidc(UserID.from_string(self.user).localpart)
374359
self.assertEqual(login_resp["user_id"], self.user)
375360

376-
device_ids = self.get_device_ids(self.user_tok)
377-
channel = self.delete_device(self.user_tok, device_ids[0], 401)
361+
channel = self.delete_device(self.user_tok, self.device_id, 401)
378362

379363
flows = channel.json_body["flows"]
380364
# we have no particular expectations of ordering here

0 commit comments

Comments
 (0)