1313
1414
1515class TestAuth :
16+ def test_can_login (self , with_db , with_client , make_pro_subscriber , faker ):
17+ # Request can-login with a capitalized email
18+ capital_email = faker .email ().capitalize ()
19+ subscriber = make_pro_subscriber (email = capital_email )
20+
21+ assert subscriber .email != capital_email
22+ assert subscriber .email == capital_email .lower ()
23+
24+ with with_db () as db :
25+ # Check we've saved the email as lowercase
26+ subscriber_check = repo .subscriber .get_by_email (db , capital_email .lower ())
27+ assert subscriber_check is not None
28+ assert subscriber_check .email != capital_email
29+ assert subscriber_check .email == capital_email .lower ()
30+
31+ response = with_client .post ('/can-login' , json = {'email' : subscriber .email })
32+
33+ assert response .status_code == 200 , response .text
34+ data = response .json ()
35+ assert data is True
36+
1637 def test_me (self , with_db , with_client ):
1738 response = with_client .get ('/me' , headers = auth_headers )
1839 assert response .status_code == 200 , response .text
@@ -198,21 +219,14 @@ def test_fxa_with_allowlist_and_with_bad_invite_code(self, with_client, with_l10
198219199220 response = with_client .get (
200221 '/fxa_login' ,
201- params = {
202- 'email' : email ,
203- 'invite_code' : 'absolute nonsense!'
204- },
222+ params = {'email' : email , 'invite_code' : 'absolute nonsense!' },
205223 )
206224 assert response .status_code == 404 , response .text
207225 data = response .json ()
208226 assert data .get ('detail' ) == l10n ('invite-code-not-valid' )
209227
210228 def test_fxa_with_allowlist_and_with_used_invite_code (
211- self ,
212- with_client ,
213- with_l10n ,
214- make_invite ,
215- make_pro_subscriber
229+ self , with_client , with_l10n , make_invite , make_pro_subscriber
216230 ):
217231 os .environ ['AUTH_SCHEME' ] = 'fxa'
218232 os .environ ['FXA_ALLOW_LIST' ] = '@example.org'
@@ -223,10 +237,7 @@ def test_fxa_with_allowlist_and_with_used_invite_code(
223237224238 response = with_client .get (
225239 '/fxa_login' ,
226- params = {
227- 'email' : email ,
228- 'invite_code' : invite .code
229- },
240+ params = {'email' : email , 'invite_code' : invite .code },
230241 )
231242 assert response .status_code == 403 , response .text
232243 data = response .json ()
@@ -433,17 +444,12 @@ def test_fxa_token_success(self, make_basic_subscriber, with_client):
433444
434445 subscriber = make_basic_subscriber (
email = '[email protected] ' )
435446 access_token_expires = timedelta (minutes = float (10 ))
436- one_time_access_token = create_access_token (data = {
437- 'sub' : f'uid-{ subscriber .id } ' ,
438- 'jti' : secrets .token_urlsafe (16 )
439- }, expires_delta = access_token_expires )
447+ one_time_access_token = create_access_token (
448+ data = {'sub' : f'uid-{ subscriber .id } ' , 'jti' : secrets .token_urlsafe (16 )}, expires_delta = access_token_expires
449+ )
440450
441451 # Exchange the one-time token with a long-living token
442- response = with_client .post (
443- '/fxa-token' , headers = {
444- 'Authorization' : f'Bearer { one_time_access_token } '
445- }
446- )
452+ response = with_client .post ('/fxa-token' , headers = {'Authorization' : f'Bearer { one_time_access_token } ' })
447453
448454 assert response .status_code == 200 , response .text
449455
@@ -454,11 +460,7 @@ def test_fxa_token_success(self, make_basic_subscriber, with_client):
454460 assert data .get ('token_type' ) == 'bearer'
455461
456462 # Test it out!
457- response = with_client .get (
458- '/me' , headers = {
459- 'Authorization' : f'Bearer { access_token } '
460- }
461- )
463+ response = with_client .get ('/me' , headers = {'Authorization' : f'Bearer { access_token } ' })
462464
463465 assert response .status_code == 200 , response .text
464466 assert response .json ().get ('email' ) == subscriber .email
@@ -471,16 +473,15 @@ def test_fxa_token_failed_due_to_non_one_time_token(self, make_basic_subscriber,
471473
472474 subscriber = make_basic_subscriber (
email = '[email protected] ' )
473475 access_token_expires = timedelta (minutes = float (10 ))
474- regular_access_token = create_access_token (data = {
475- 'sub' : f'uid-{ subscriber .id } ' ,
476- }, expires_delta = access_token_expires )
477-
478- response = with_client .post (
479- '/fxa-token' , headers = {
480- 'Authorization' : f'Bearer { regular_access_token } '
481- }
476+ regular_access_token = create_access_token (
477+ data = {
478+ 'sub' : f'uid-{ subscriber .id } ' ,
479+ },
480+ expires_delta = access_token_expires ,
482481 )
483482
483+ response = with_client .post ('/fxa-token' , headers = {'Authorization' : f'Bearer { regular_access_token } ' })
484+
484485 assert response .status_code == 401 , response .text
485486
486487 def test_fxa_token_failed_due_to_empty_auth (self , make_basic_subscriber , with_client ):
@@ -489,9 +490,7 @@ def test_fxa_token_failed_due_to_empty_auth(self, make_basic_subscriber, with_cl
489490
490491 del with_client .app .dependency_overrides [auth .get_subscriber ]
491492
492- response = with_client .post (
493- '/fxa-token'
494- )
493+ response = with_client .post ('/fxa-token' )
495494
496495 assert response .status_code == 401 , response .text
497496
@@ -504,17 +503,12 @@ def test_fxa_token_failed_due_to_invalid_auth_scheme(self, with_client, make_bas
504503
505504 subscriber = make_basic_subscriber (
email = '[email protected] ' )
506505 access_token_expires = timedelta (minutes = float (10 ))
507- one_time_access_token = create_access_token (data = {
508- 'sub' : f'uid-{ subscriber .id } ' ,
509- 'jti' : secrets .token_urlsafe (16 )
510- }, expires_delta = access_token_expires )
506+ one_time_access_token = create_access_token (
507+ data = {'sub' : f'uid-{ subscriber .id } ' , 'jti' : secrets .token_urlsafe (16 )}, expires_delta = access_token_expires
508+ )
511509
512510 # Exchange the one-time token with a long-living token
513- response = with_client .post (
514- '/fxa-token' , headers = {
515- 'Authorization' : f'Bearer { one_time_access_token } '
516- }
517- )
511+ response = with_client .post ('/fxa-token' , headers = {'Authorization' : f'Bearer { one_time_access_token } ' })
518512 os .environ ['AUTH_SCHEME' ] = saved_scheme
519513 assert response .status_code == 405 , response .text
520514
@@ -530,17 +524,15 @@ def test_auth(self, with_db, with_client):
530524 assert len (ecs ) == 0
531525
532526 with patch ('appointment.controller.calendar.Tools.dns_caldav_lookup' ) as mock :
533- mock .return_value = " https://example.com" , 300
527+ mock .return_value = ' https://example.com' , 300
534528
535529 with patch ('appointment.controller.calendar.CalDavConnector.sync_calendars' ) as sync_mock :
536530 sync_mock .return_value = None
537531
538532 response = with_client .post (
539533 '/caldav/auth' ,
540- json = {
'user' :
'[email protected] ' ,
541- 'url' : 'example.com' ,
542- 'password' : 'test' },
543- headers = auth_headers
534+ json = {
'user' :
'[email protected] ' ,
'url' :
'example.com' ,
'password' :
'test' },
535+ headers = auth_headers ,
544536 )
545537
546538 mock .assert_called ()
@@ -559,19 +551,13 @@ def test_disconnect(self, with_db, with_client, make_external_connections, make_
559551 ec = make_external_connections (TEST_USER_ID , type = models .ExternalConnectionType .caldav , type_id = type_id )
560552 calendar = make_caldav_calendar (subscriber_id = TEST_USER_ID , user = username )
561553
562- response = with_client .post (
563- '/caldav/disconnect' , json = {'type_id' : ec .type_id },
564- headers = auth_headers
565- )
554+ response = with_client .post ('/caldav/disconnect' , json = {'type_id' : ec .type_id }, headers = auth_headers )
566555
567556 assert response .status_code == 200 , response .content
568557
569558 with with_db () as db :
570559 ecs = repo .external_connection .get_by_type (
571- db ,
572- TEST_USER_ID ,
573- models .ExternalConnectionType .caldav ,
574- type_id = type_id
560+ db , TEST_USER_ID , models .ExternalConnectionType .caldav , type_id = type_id
575561 )
576562 assert len (ecs ) == 0
577563
@@ -586,19 +572,13 @@ def test_disconnect(self, with_db, with_client, make_external_connections, make_
586572 ec = make_external_connections (TEST_USER_ID , type = models .ExternalConnectionType .google , type_id = type_id )
587573 calendar = make_google_calendar (subscriber_id = TEST_USER_ID )
588574
589- response = with_client .post (
590- '/google/disconnect' , json = {'type_id' : ec .type_id },
591- headers = auth_headers
592- )
575+ response = with_client .post ('/google/disconnect' , json = {'type_id' : ec .type_id }, headers = auth_headers )
593576
594577 assert response .status_code == 200 , response .content
595578
596579 with with_db () as db :
597580 ecs = repo .external_connection .get_by_type (
598- db ,
599- TEST_USER_ID ,
600- models .ExternalConnectionType .google ,
601- type_id = type_id
581+ db , TEST_USER_ID , models .ExternalConnectionType .google , type_id = type_id
602582 )
603583 assert len (ecs ) == 0
604584
0 commit comments