17
17
from typing import Any , Type , Union
18
18
from unittest .mock import Mock
19
19
20
- from twisted .internet import defer
21
-
22
20
import synapse
23
21
from synapse .api .constants import LoginType
24
22
from synapse .api .errors import Codes
@@ -190,7 +188,7 @@ def password_only_auth_provider_login_test_body(self):
190
188
self .assertEqual (flows , [{"type" : "m.login.password" }] + ADDITIONAL_LOGIN_FLOWS )
191
189
192
190
# check_password must return an awaitable
193
- mock_password_provider .check_password .return_value = defer . succeed (True )
191
+ mock_password_provider .check_password .return_value = make_awaitable (True )
194
192
channel = self ._send_password_login ("u" , "p" )
195
193
self .assertEqual (channel .code , 200 , channel .result )
196
194
self .assertEqual ("@u:test" , channel .json_body ["user_id" ])
@@ -226,13 +224,13 @@ def password_only_auth_provider_ui_auth_test_body(self):
226
224
self .get_success (module_api .register_user ("u" ))
227
225
228
226
# log in twice, to get two devices
229
- mock_password_provider .check_password .return_value = defer . succeed (True )
227
+ mock_password_provider .check_password .return_value = make_awaitable (True )
230
228
tok1 = self .login ("u" , "p" )
231
229
self .login ("u" , "p" , device_id = "dev2" )
232
230
mock_password_provider .reset_mock ()
233
231
234
232
# have the auth provider deny the request to start with
235
- mock_password_provider .check_password .return_value = defer . succeed (False )
233
+ mock_password_provider .check_password .return_value = make_awaitable (False )
236
234
237
235
# make the initial request which returns a 401
238
236
session = self ._start_delete_device_session (tok1 , "dev2" )
@@ -246,7 +244,7 @@ def password_only_auth_provider_ui_auth_test_body(self):
246
244
mock_password_provider .reset_mock ()
247
245
248
246
# Finally, check the request goes through when we allow it
249
- mock_password_provider .check_password .return_value = defer . succeed (True )
247
+ mock_password_provider .check_password .return_value = make_awaitable (True )
250
248
channel = self ._authed_delete_device (tok1 , "dev2" , session , "u" , "p" )
251
249
self .assertEqual (channel .code , 200 )
252
250
mock_password_provider .check_password .assert_called_once_with ("@u:test" , "p" )
@@ -260,7 +258,7 @@ def local_user_fallback_login_test_body(self):
260
258
self .register_user ("localuser" , "localpass" )
261
259
262
260
# check_password must return an awaitable
263
- mock_password_provider .check_password .return_value = defer . succeed (False )
261
+ mock_password_provider .check_password .return_value = make_awaitable (False )
264
262
channel = self ._send_password_login ("u" , "p" )
265
263
self .assertEqual (channel .code , 403 , channel .result )
266
264
@@ -277,7 +275,7 @@ def local_user_fallback_ui_auth_test_body(self):
277
275
self .register_user ("localuser" , "localpass" )
278
276
279
277
# have the auth provider deny the request
280
- mock_password_provider .check_password .return_value = defer . succeed (False )
278
+ mock_password_provider .check_password .return_value = make_awaitable (False )
281
279
282
280
# log in twice, to get two devices
283
281
tok1 = self .login ("localuser" , "localpass" )
@@ -320,7 +318,7 @@ def no_local_user_fallback_login_test_body(self):
320
318
self .register_user ("localuser" , "localpass" )
321
319
322
320
# check_password must return an awaitable
323
- mock_password_provider .check_password .return_value = defer . succeed (False )
321
+ mock_password_provider .check_password .return_value = make_awaitable (False )
324
322
channel = self ._send_password_login ("localuser" , "localpass" )
325
323
self .assertEqual (channel .code , 403 )
326
324
self .assertEqual (channel .json_body ["errcode" ], "M_FORBIDDEN" )
@@ -342,7 +340,7 @@ def no_local_user_fallback_ui_auth_test_body(self):
342
340
self .register_user ("localuser" , "localpass" )
343
341
344
342
# allow login via the auth provider
345
- mock_password_provider .check_password .return_value = defer . succeed (True )
343
+ mock_password_provider .check_password .return_value = make_awaitable (True )
346
344
347
345
# log in twice, to get two devices
348
346
tok1 = self .login ("localuser" , "p" )
@@ -359,7 +357,7 @@ def no_local_user_fallback_ui_auth_test_body(self):
359
357
mock_password_provider .check_password .assert_not_called ()
360
358
361
359
# now try deleting with the local password
362
- mock_password_provider .check_password .return_value = defer . succeed (False )
360
+ mock_password_provider .check_password .return_value = make_awaitable (False )
363
361
channel = self ._authed_delete_device (
364
362
tok1 , "dev2" , session , "localuser" , "localpass"
365
363
)
@@ -413,7 +411,7 @@ def custom_auth_provider_login_test_body(self):
413
411
self .assertEqual (channel .code , 400 , channel .result )
414
412
mock_password_provider .check_auth .assert_not_called ()
415
413
416
- mock_password_provider .check_auth .return_value = defer . succeed (
414
+ mock_password_provider .check_auth .return_value = make_awaitable (
417
415
("@user:bz" , None )
418
416
)
419
417
channel = self ._send_login ("test.login_type" , "u" , test_field = "y" )
@@ -427,7 +425,7 @@ def custom_auth_provider_login_test_body(self):
427
425
# try a weird username. Again, it's unclear what we *expect* to happen
428
426
# in these cases, but at least we can guard against the API changing
429
427
# unexpectedly
430
- mock_password_provider .check_auth .return_value = defer . succeed (
428
+ mock_password_provider .check_auth .return_value = make_awaitable (
431
429
("@ MALFORMED! :bz" , None )
432
430
)
433
431
channel = self ._send_login ("test.login_type" , " USER🙂NAME " , test_field = " abc " )
@@ -477,7 +475,7 @@ def custom_auth_provider_ui_auth_test_body(self):
477
475
mock_password_provider .reset_mock ()
478
476
479
477
# right params, but authing as the wrong user
480
- mock_password_provider .check_auth .return_value = defer . succeed (
478
+ mock_password_provider .check_auth .return_value = make_awaitable (
481
479
("@user:bz" , None )
482
480
)
483
481
body ["auth" ]["test_field" ] = "foo"
@@ -490,7 +488,7 @@ def custom_auth_provider_ui_auth_test_body(self):
490
488
mock_password_provider .reset_mock ()
491
489
492
490
# and finally, succeed
493
- mock_password_provider .check_auth .return_value = defer . succeed (
491
+ mock_password_provider .check_auth .return_value = make_awaitable (
494
492
("@localuser:test" , None )
495
493
)
496
494
channel = self ._delete_device (tok1 , "dev2" , body )
@@ -508,9 +506,9 @@ def test_custom_auth_provider_callback(self):
508
506
self .custom_auth_provider_callback_test_body ()
509
507
510
508
def custom_auth_provider_callback_test_body (self ):
511
- callback = Mock (return_value = defer . succeed (None ))
509
+ callback = Mock (return_value = make_awaitable (None ))
512
510
513
- mock_password_provider .check_auth .return_value = defer . succeed (
511
+ mock_password_provider .check_auth .return_value = make_awaitable (
514
512
("@user:bz" , callback )
515
513
)
516
514
channel = self ._send_login ("test.login_type" , "u" , test_field = "y" )
@@ -646,7 +644,7 @@ def password_custom_auth_password_disabled_ui_auth_test_body(self):
646
644
login is disabled"""
647
645
# register the user and log in twice via the test login type to get two devices,
648
646
self .register_user ("localuser" , "localpass" )
649
- mock_password_provider .check_auth .return_value = defer . succeed (
647
+ mock_password_provider .check_auth .return_value = make_awaitable (
650
648
("@localuser:test" , None )
651
649
)
652
650
channel = self ._send_login ("test.login_type" , "localuser" , test_field = "" )
0 commit comments