1010import pytest
1111from aiohttp .test_utils import TestClient
1212from common_library .users_enums import UserStatus
13+ from models_library .products import ProductName
1314from pytest_simcore .helpers .webserver_login import UserInfoDict
1415from servicelib .aiohttp import status
1516from simcore_service_webserver .login ._login_repository_legacy import (
1617 ActionLiteralStr ,
1718 AsyncpgStorage ,
1819 ConfirmationTokenDict ,
1920)
21+ from simcore_service_webserver .users import _users_service
22+ from simcore_service_webserver .wallets import _api as _wallets_service
23+ from simcore_service_webserver .wallets import _db as _wallets_repository
2024
2125CreateTokenCallable : TypeAlias = Callable [
22- [int , ActionLiteralStr ], Coroutine [Any , Any , ConfirmationTokenDict ]
26+ [int , ActionLiteralStr , str | None ], Coroutine [Any , Any , ConfirmationTokenDict ]
2327]
2428
2529
@@ -28,9 +32,9 @@ async def create_valid_confirmation_token(db: AsyncpgStorage) -> CreateTokenCall
2832 """Fixture to create a valid confirmation token for a given action."""
2933
3034 async def _create_token (
31- user_id : int , action : ActionLiteralStr
35+ user_id : int , action : ActionLiteralStr , data : str | None = None
3236 ) -> ConfirmationTokenDict :
33- return await db .create_confirmation (user_id = user_id , action = action )
37+ return await db .create_confirmation (user_id = user_id , action = action , data = data )
3438
3539 return _create_token
3640
@@ -39,10 +43,13 @@ async def test_confirm_registration(
3943 client : TestClient ,
4044 create_valid_confirmation_token : CreateTokenCallable ,
4145 unconfirmed_user : UserInfoDict ,
46+ product_name : ProductName ,
4247):
4348 assert unconfirmed_user ["status" ] == UserStatus .CONFIRMATION_PENDING
4449 target_user_id = unconfirmed_user ["id" ]
45- confirmation = await create_valid_confirmation_token (target_user_id , "REGISTRATION" )
50+ confirmation = await create_valid_confirmation_token (
51+ target_user_id , "REGISTRATION" , None
52+ )
4653 code = confirmation ["code" ]
4754
4855 # clicks link to confirm registration
@@ -54,6 +61,24 @@ async def test_confirm_registration(
5461 assert response .history [0 ].status == status .HTTP_302_FOUND
5562 assert response .history [0 ].headers ["Location" ].endswith ("/#?registered=true" )
5663
64+ # checks _handle_confirm_registration updated status
65+ assert client .app
66+ user = await _users_service .get_user (client .app , user_id = unconfirmed_user ["id" ])
67+ assert user ["status" ] == UserStatus .ACTIVE
68+
69+ # checks that the user has one wallet created (via SIGNAL_ON_USER_CONFIRMATION)
70+ wallets = await _wallets_service .list_wallets_for_user (
71+ client .app , user_id = unconfirmed_user ["id" ], product_name = product_name
72+ )
73+ assert len (wallets ) == 1
74+
75+ # delete to allow teardown
76+ await _wallets_repository .delete_wallet (
77+ client .app ,
78+ wallet_id = wallets [0 ].wallet_id ,
79+ product_name = product_name ,
80+ )
81+
5782
5883async def test_confirm_change_email (
5984 client : TestClient ,
@@ -63,17 +88,19 @@ async def test_confirm_change_email(
6388 assert registered_user ["status" ] == UserStatus .ACTIVE
6489
6590 user_id = registered_user ["id" ]
66- confirmation = await create_valid_confirmation_token (user_id , "CHANGE_EMAIL" )
91+ confirmation = await create_valid_confirmation_token (
92+ user_id , "CHANGE_EMAIL" , "new_" + registered_user ["email" ]
93+ )
6794 code = confirmation ["code" ]
6895
6996 # clicks link to confirm registration
7097 response = await client .get (f"/v0/auth/confirmation/{ code } " )
7198 assert response .status == status .HTTP_200_OK
7299
73- # checks redirection to front-end, which will prompt and then finalize change-email
74- assert len ( response . history ) == 1
75- assert response . history [ 0 ]. status == status . HTTP_302_FOUND
76- assert response . history [ 0 ]. headers [ "Location" ]. endswith ( "/#?registered=true" )
100+ # checks _handle_confirm_registration updated status
101+ assert client . app
102+ user = await _users_service . get_user ( client . app , user_id = registered_user [ "id" ])
103+ assert user [ "email" ] == "new_" + registered_user [ "email" ]
77104
78105
79106async def test_confirm_reset_password (
@@ -82,12 +109,22 @@ async def test_confirm_reset_password(
82109 registered_user : UserInfoDict ,
83110):
84111 user_id = registered_user ["id" ]
85- confirmation = await create_valid_confirmation_token (user_id , "RESET_PASSWORD" )
112+ confirmation = await create_valid_confirmation_token (
113+ user_id , "RESET_PASSWORD" , None
114+ )
86115 code = confirmation ["code" ]
87116
88117 response = await client .get (f"/v0/auth/confirmation/{ code } " )
89- assert response .status == status .HTTP_302_FOUND
90- assert response .headers ["Location" ].endswith (f"reset-password?code={ code } " )
118+ assert response .status == status .HTTP_200_OK
119+
120+ # checks redirection
121+ assert len (response .history ) == 1
122+ assert response .history [0 ].status == status .HTTP_302_FOUND
123+ assert (
124+ response .history [0 ]
125+ .headers ["Location" ]
126+ .endswith (f"/#reset-password?code={ code } " )
127+ )
91128
92129
93130async def test_handler_exception_logging (
@@ -96,7 +133,7 @@ async def test_handler_exception_logging(
96133 registered_user : UserInfoDict ,
97134):
98135 user_id = registered_user ["id" ]
99- confirmation = await create_valid_confirmation_token (user_id , "REGISTRATION" )
136+ confirmation = await create_valid_confirmation_token (user_id , "REGISTRATION" , None )
100137 code = confirmation ["code" ]
101138
102139 with patch (
@@ -107,12 +144,12 @@ async def test_handler_exception_logging(
107144 "simcore_service_webserver.login._controller.confirmation_rest._logger.exception"
108145 ) as mock_logger :
109146 response = await client .get (f"/v0/auth/confirmation/{ code } " )
110- assert response .status == 503
147+ assert response .status == status .HTTP_200_OK
148+
149+ # checks redirection
150+ assert len (response .history ) == 1
151+ assert response .history [0 ].status == status .HTTP_302_FOUND
152+ assert "/#/error?message=" in response .history [0 ].headers ["Location" ]
153+
111154 mock_handler .assert_called_once ()
112- mock_logger .assert_called_once_with (
113- user_error_msg = "Sorry, we cannot confirm your REGISTRATION."
114- "Please try again in a few moments." ,
115- error = mock_handler .side_effect ,
116- error_code = mock_handler .side_effect ,
117- tip = "Failed during email_confirmation" ,
118- )
155+ mock_logger .assert_called_once ()
0 commit comments