11from datetime import timedelta
22
33from aiohttp .test_utils import make_mocked_request
4- from aiohttp .web import Application
4+ from aiohttp .web import Application , Response
55from pytest_simcore .helpers .webserver_users import UserInfoDict
6- from simcore_service_webserver .login import _confirmation_service , _confirmation_web
7- from simcore_service_webserver .login ._login_repository_legacy import AsyncpgStorage
8- from simcore_service_webserver .login .settings import LoginOptions
6+ from simcore_service_webserver .login import _confirmation_web
7+ from simcore_service_webserver .login ._confirmation_service import ConfirmationService
98
109
1110async def test_confirmation_token_workflow (
12- db : AsyncpgStorage , login_options : LoginOptions , registered_user : UserInfoDict
11+ confirmation_service : ConfirmationService ,
12+ registered_user : UserInfoDict ,
1313):
1414 # Step 1: Create a new confirmation token
1515 user_id = registered_user ["id" ]
1616 action = "RESET_PASSWORD"
17- confirmation = await _confirmation_service .get_or_create_confirmation_without_data (
18- login_options , db , user_id = user_id , action = action
17+ confirmation = await confirmation_service .get_or_create_confirmation_without_data (
18+ user_id = user_id , action = action
1919 )
2020
2121 assert confirmation is not None
22- assert confirmation [ " user_id" ] == user_id
23- assert confirmation [ " action" ] == action
22+ assert confirmation . user_id == user_id
23+ assert confirmation . action == action
2424
2525 # Step 2: Check that the token is not expired
26- assert not _confirmation_service .is_confirmation_expired (
27- login_options , confirmation
28- )
26+ assert not confirmation_service .is_confirmation_expired (confirmation )
2927
3028 # Step 3: Validate the confirmation code
31- code = confirmation ["code" ]
32- validated_confirmation = await _confirmation_service .validate_confirmation_code (
33- code , db , login_options
34- )
29+ code = confirmation .code
30+ validated_confirmation = await confirmation_service .validate_confirmation_code (code )
3531
3632 assert validated_confirmation is not None
37- assert validated_confirmation [ " code" ] == code
38- assert validated_confirmation [ " user_id" ] == user_id
39- assert validated_confirmation [ " action" ] == action
33+ assert validated_confirmation . code == code
34+ assert validated_confirmation . user_id == user_id
35+ assert validated_confirmation . action == action
4036
4137 # Step 4: Create confirmation link
4238 app = Application ()
39+
40+ async def mock_handler (request ):
41+ return Response ()
42+
4343 app .router .add_get (
44- "/auth/confirmation/{code}" , lambda request : None , name = "auth_confirmation"
44+ "/auth/confirmation/{code}" , mock_handler , name = "auth_confirmation"
4545 )
4646 request = make_mocked_request (
4747 "GET" ,
@@ -52,74 +52,63 @@ async def test_confirmation_token_workflow(
5252
5353 # Create confirmation link
5454 confirmation_link = _confirmation_web .make_confirmation_link (
55- request , confirmation [ " code" ]
55+ request , confirmation . code
5656 )
5757
5858 # Assertions
5959 assert confirmation_link .startswith ("https://example.com/auth/confirmation/" )
60- assert confirmation [ " code" ] in confirmation_link
60+ assert confirmation . code in confirmation_link
6161
6262
6363async def test_expired_confirmation_token (
64- db : AsyncpgStorage , login_options : LoginOptions , registered_user : UserInfoDict
64+ confirmation_service : ConfirmationService ,
65+ registered_user : UserInfoDict ,
6566):
6667 user_id = registered_user ["id" ]
6768 action = "CHANGE_EMAIL"
6869
6970 # Create a brand new confirmation token
70- confirmation_1 = (
71- await _confirmation_service .get_or_create_confirmation_without_data (
72- login_options , db , user_id = user_id , action = action
73- )
71+ confirmation_1 = await confirmation_service .get_or_create_confirmation_without_data (
72+ user_id = user_id , action = action
7473 )
7574
7675 assert confirmation_1 is not None
77- assert confirmation_1 [ " user_id" ] == user_id
78- assert confirmation_1 [ " action" ] == action
76+ assert confirmation_1 . user_id == user_id
77+ assert confirmation_1 . action == action
7978
8079 # Check that the token is not expired
81- assert not _confirmation_service .is_confirmation_expired (
82- login_options , confirmation_1
83- )
84- assert _confirmation_service .get_expiration_date (login_options , confirmation_1 )
80+ assert not confirmation_service .is_confirmation_expired (confirmation_1 )
81+ assert confirmation_service .get_expiration_date (confirmation_1 )
8582
86- confirmation_2 = (
87- await _confirmation_service .get_or_create_confirmation_without_data (
88- login_options , db , user_id = user_id , action = action
89- )
83+ confirmation_2 = await confirmation_service .get_or_create_confirmation_without_data (
84+ user_id = user_id , action = action
9085 )
9186
9287 assert confirmation_2 == confirmation_1
9388
9489 # Enforce ALL EXPIRED
95- login_options .CHANGE_EMAIL_CONFIRMATION_LIFETIME = 0
96- assert login_options .get_confirmation_lifetime (action ) == timedelta (seconds = 0 )
90+ confirmation_service .options .CHANGE_EMAIL_CONFIRMATION_LIFETIME = 0
91+ assert confirmation_service .options .get_confirmation_lifetime (action ) == timedelta (
92+ seconds = 0
93+ )
9794
98- confirmation_3 = (
99- await _confirmation_service .get_or_create_confirmation_without_data (
100- login_options , db , user_id = user_id , action = action
101- )
95+ confirmation_3 = await confirmation_service .get_or_create_confirmation_without_data (
96+ user_id = user_id , action = action
10297 )
10398
10499 # when expired, it gets renewed
105100 assert confirmation_3 != confirmation_1
106101
107102 # now all have expired
108103 assert (
109- await _confirmation_service .validate_confirmation_code (
110- confirmation_1 ["code" ], db , login_options
111- )
104+ await confirmation_service .validate_confirmation_code (confirmation_1 .code )
112105 is None
113106 )
114107 assert (
115- await _confirmation_service .validate_confirmation_code (
116- confirmation_2 ["code" ], db , login_options
117- )
108+ await confirmation_service .validate_confirmation_code (confirmation_2 .code )
118109 is None
119110 )
120111 assert (
121- await _confirmation_service .validate_confirmation_code (
122- confirmation_3 ["code" ], db , login_options
123- )
112+ await confirmation_service .validate_confirmation_code (confirmation_3 .code )
124113 is None
125114 )
0 commit comments