Skip to content

Commit 03afb86

Browse files
committed
building test
1 parent 13ec44c commit 03afb86

File tree

5 files changed

+70
-40
lines changed

5 files changed

+70
-40
lines changed

services/web/server/tests/unit/conftest.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,11 @@
1313

1414
import pytest
1515
import yaml
16+
from models_library.products import ProductName
1617
from pytest_mock import MockFixture, MockType
1718
from pytest_simcore.helpers.webserver_projects import empty_project_data
1819
from simcore_service_webserver.application_settings_utils import AppConfigDict
20+
from simcore_service_webserver.constants import FRONTEND_APP_DEFAULT
1921

2022
CURRENT_DIR = Path(sys.argv[0] if __name__ == "__main__" else __file__).resolve().parent
2123

@@ -86,3 +88,8 @@ def disabled_setup_garbage_collector(mocker: MockFixture) -> MockType:
8688
autospec=True,
8789
return_value=False,
8890
)
91+
92+
93+
@pytest.fixture(scope="session")
94+
def product_name() -> ProductName:
95+
return ProductName(FRONTEND_APP_DEFAULT)

services/web/server/tests/unit/isolated/products/conftest.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,10 @@
1313
from models_library.products import ProductName
1414
from pytest_simcore.helpers.faker_factories import random_product
1515
from simcore_postgres_database.models.products import products as products_table
16-
from simcore_service_webserver.constants import FRONTEND_APP_DEFAULT
1716
from sqlalchemy import String
1817
from sqlalchemy.dialects import postgresql
1918

2019

21-
@pytest.fixture(scope="session")
22-
def product_name() -> ProductName:
23-
return ProductName(FRONTEND_APP_DEFAULT)
24-
25-
2620
@pytest.fixture
2721
def product_db_server_defaults() -> dict[str, Any]:
2822
server_defaults = {}

services/web/server/tests/unit/with_dbs/03/login/test_login_controller_confirmation_rest.py

Lines changed: 58 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,20 @@
1010
import pytest
1111
from aiohttp.test_utils import TestClient
1212
from common_library.users_enums import UserStatus
13+
from models_library.products import ProductName
1314
from pytest_simcore.helpers.webserver_login import UserInfoDict
1415
from servicelib.aiohttp import status
1516
from 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

2125
CreateTokenCallable: 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

5883
async 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

79106
async 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

93130
async 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()

services/web/server/tests/unit/with_dbs/03/tags/test_tags.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -423,20 +423,17 @@ async def test_cannot_share_tag_with_everyone(
423423
assert error
424424

425425

426-
@pytest.fixture
427-
def product_name() -> str:
428-
return "osparc"
429-
430-
431426
@pytest.mark.parametrize(
432427
"user_role,expected_status",
433428
[
434429
(
435430
role,
436431
# granted only to:
437-
status.HTTP_403_FORBIDDEN
438-
if role < UserRole.TESTER
439-
else status.HTTP_201_CREATED,
432+
(
433+
status.HTTP_403_FORBIDDEN
434+
if role < UserRole.TESTER
435+
else status.HTTP_201_CREATED
436+
),
440437
)
441438
for role in UserRole
442439
if role >= UserRole.USER

services/web/server/tests/unit/with_dbs/04/folders/test_folders_repository.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,6 @@ def user_role():
1818
return UserRole.USER
1919

2020

21-
@pytest.fixture
22-
def product_name():
23-
return "osparc"
24-
25-
2621
async def test_batch_get_trashed_by_primary_gid(
2722
client: TestClient,
2823
logged_user: dict[str, Any],

0 commit comments

Comments
 (0)