From df3d7dc84b2ec973dcb1315f8cf266ede2215539 Mon Sep 17 00:00:00 2001 From: Pedro Crespo-Valero <32402063+pcrespov@users.noreply.github.com> Date: Mon, 11 Dec 2023 11:22:15 +0100 Subject: [PATCH] fixes tests --- .../src/servicelib/rabbitmq/_rpc_router.py | 2 +- .../api/test__one_time_payment_workflows.py | 2 +- .../api/test__payment_method_workflows.py | 2 +- services/payments/tests/unit/conftest.py | 32 +++++++++++++++---- .../unit/test_db_payments_methods_repo.py | 1 + .../test_db_payments_transactions_repo.py | 1 + .../tests/unit/test_db_payments_users_repo.py | 1 + .../payments/tests/unit/test_rpc_payments.py | 22 ++++++++++--- .../tests/unit/test_rpc_payments_methods.py | 6 ++-- .../test_services_auto_recharge_listener.py | 4 +-- .../tests/unit/test_services_payments.py | 2 +- .../unit/test_services_payments_gateway.py | 2 +- .../test_services_resource_usage_tracker.py | 2 +- 13 files changed, 57 insertions(+), 22 deletions(-) diff --git a/packages/service-library/src/servicelib/rabbitmq/_rpc_router.py b/packages/service-library/src/servicelib/rabbitmq/_rpc_router.py index d0c4a7c41ad4..15239012682d 100644 --- a/packages/service-library/src/servicelib/rabbitmq/_rpc_router.py +++ b/packages/service-library/src/servicelib/rabbitmq/_rpc_router.py @@ -43,7 +43,7 @@ async def wrapper(*args, **kwargs): # NOTE: we do not return internal exceptions over RPC raise RPCServerError( method_name=func.__name__, - exc_type=f"{type(exc)}", + exc_type=f"{exc.__class__.__module__}.{exc.__class__.__name__}", msg=f"{exc}", ) from None diff --git a/services/payments/tests/unit/api/test__one_time_payment_workflows.py b/services/payments/tests/unit/api/test__one_time_payment_workflows.py index f4697b7ab3d6..f5ede941309b 100644 --- a/services/payments/tests/unit/api/test__one_time_payment_workflows.py +++ b/services/payments/tests/unit/api/test__one_time_payment_workflows.py @@ -59,11 +59,11 @@ def app_environment( ) async def test_successful_one_time_payment_workflow( is_pdb_enabled: bool, + mock_payments_gateway_service_or_none: MockRouter | None, app: FastAPI, client: httpx.AsyncClient, faker: Faker, rpc_client: RabbitMQRPCClient, - mock_payments_gateway_service_or_none: MockRouter | None, wallet_id: WalletID, wallet_name: IDStr, user_id: UserID, diff --git a/services/payments/tests/unit/api/test__payment_method_workflows.py b/services/payments/tests/unit/api/test__payment_method_workflows.py index c1a60bc5555c..0dc06c92e1c6 100644 --- a/services/payments/tests/unit/api/test__payment_method_workflows.py +++ b/services/payments/tests/unit/api/test__payment_method_workflows.py @@ -62,11 +62,11 @@ def app_environment( ) async def test_successful_create_payment_method_workflow( is_pdb_enabled: bool, + mock_payments_gateway_service_or_none: MockRouter | None, app: FastAPI, client: httpx.AsyncClient, faker: Faker, rpc_client: RabbitMQRPCClient, - mock_payments_gateway_service_or_none: MockRouter | None, wallet_id: WalletID, wallet_name: IDStr, user_id: UserID, diff --git a/services/payments/tests/unit/conftest.py b/services/payments/tests/unit/conftest.py index acb0220c0e00..0cb200f451ee 100644 --- a/services/payments/tests/unit/conftest.py +++ b/services/payments/tests/unit/conftest.py @@ -59,22 +59,40 @@ def disable_rabbitmq_and_rpc_setup(mocker: MockerFixture) -> Callable: def _(): # The following services are affected if rabbitmq is not in place - mocker.patch("simcore_service_payments.core.application.setup_notifier") - mocker.patch("simcore_service_payments.core.application.setup_socketio") - mocker.patch("simcore_service_payments.core.application.setup_rabbitmq") - mocker.patch("simcore_service_payments.core.application.setup_rpc_api_routes") mocker.patch( - "simcore_service_payments.core.application.setup_auto_recharge_listener" + "simcore_service_payments.core.application.setup_notifier", autospec=True + ) + mocker.patch( + "simcore_service_payments.core.application.setup_socketio", autospec=True + ) + mocker.patch( + "simcore_service_payments.core.application.setup_rabbitmq", autospec=True + ) + mocker.patch( + "simcore_service_payments.core.application.setup_rpc_api_routes", + autospec=True, + ) + mocker.patch( + "simcore_service_payments.core.application.setup_auto_recharge_listener", + autospec=True, ) return _ @pytest.fixture -def with_disabled_rabbitmq_and_rpc(disable_rabbitmq_and_rpc_setup: Callable): +def with_disabled_rabbitmq_and_rpc(disable_rabbitmq_and_rpc_setup: Callable) -> None: disable_rabbitmq_and_rpc_setup() +@pytest.fixture +def with_disabled_gateway(mocker: MockerFixture) -> None: + mocker.patch( + "simcore_service_payments.core.application.setup_payments_gateway", + autospec=True, + ) + + @pytest.fixture async def rpc_client( faker: Faker, rabbitmq_rpc_client: Callable[[str], Awaitable[RabbitMQRPCClient]] @@ -470,7 +488,7 @@ def mock_resource_usage_tracker_service_api_base( @pytest.fixture -def mock_resoruce_usage_tracker_service_api( +def mock_resource_usage_tracker_service_api( faker: Faker, mock_resource_usage_tracker_service_api_base: MockRouter, rut_service_openapi_specs: dict[str, Any], diff --git a/services/payments/tests/unit/test_db_payments_methods_repo.py b/services/payments/tests/unit/test_db_payments_methods_repo.py index 76166c5d0be8..e762c49a57cc 100644 --- a/services/payments/tests/unit/test_db_payments_methods_repo.py +++ b/services/payments/tests/unit/test_db_payments_methods_repo.py @@ -26,6 +26,7 @@ def app_environment( app_environment: EnvVarsDict, postgres_env_vars_dict: EnvVarsDict, with_disabled_rabbitmq_and_rpc: None, + with_disabled_gateway: None, wait_for_postgres_ready_and_db_migrated: None, ): # set environs diff --git a/services/payments/tests/unit/test_db_payments_transactions_repo.py b/services/payments/tests/unit/test_db_payments_transactions_repo.py index ccab0fed110a..47ea81014253 100644 --- a/services/payments/tests/unit/test_db_payments_transactions_repo.py +++ b/services/payments/tests/unit/test_db_payments_transactions_repo.py @@ -31,6 +31,7 @@ def app_environment( app_environment: EnvVarsDict, postgres_env_vars_dict: EnvVarsDict, with_disabled_rabbitmq_and_rpc: None, + with_disabled_gateway: None, wait_for_postgres_ready_and_db_migrated: None, ): # set environs diff --git a/services/payments/tests/unit/test_db_payments_users_repo.py b/services/payments/tests/unit/test_db_payments_users_repo.py index 820871050037..122420c598eb 100644 --- a/services/payments/tests/unit/test_db_payments_users_repo.py +++ b/services/payments/tests/unit/test_db_payments_users_repo.py @@ -32,6 +32,7 @@ def app_environment( app_environment: EnvVarsDict, postgres_env_vars_dict: EnvVarsDict, with_disabled_rabbitmq_and_rpc: None, + with_disabled_gateway: None, wait_for_postgres_ready_and_db_migrated: None, ): # set environs diff --git a/services/payments/tests/unit/test_rpc_payments.py b/services/payments/tests/unit/test_rpc_payments.py index 7e2872c2ffdf..fcf9e3b4e3dc 100644 --- a/services/payments/tests/unit/test_rpc_payments.py +++ b/services/payments/tests/unit/test_rpc_payments.py @@ -13,6 +13,7 @@ from models_library.api_schemas_webserver.wallets import WalletPaymentInitiated from models_library.rabbitmq_basic_types import RPCMethodName from pydantic import parse_obj_as +from pytest_mock import MockerFixture from pytest_simcore.helpers.typing_env import EnvVarsDict from pytest_simcore.helpers.utils_envs import setenvs_from_dict from respx import MockRouter @@ -68,7 +69,17 @@ def init_payment_kwargs(faker: Faker) -> dict[str, Any]: } +@pytest.fixture +def _disable_startup(mocker: MockerFixture): + mocker.patch( + "simcore_service_payments.services.payments_gateway._create_start_policy", + return_value=lambda: print("on-startup"), + ) + + async def test_rpc_init_payment_fail( + is_pdb_enabled: bool, + _disable_startup, app: FastAPI, rpc_client: RabbitMQRPCClient, init_payment_kwargs: dict[str, Any], @@ -81,18 +92,20 @@ async def test_rpc_init_payment_fail( PAYMENTS_RPC_NAMESPACE, parse_obj_as(RPCMethodName, "init_payment"), **init_payment_kwargs, + timeout_s=None if is_pdb_enabled else 5, ) - exc = exc_info.value + exc: RPCServerError = exc_info.value assert exc.exc_type == f"{httpx.ConnectError}" assert exc.method_name == "init_payment" assert exc.msg async def test_webserver_one_time_payment_workflow( + is_pdb_enabled: bool, + mock_payments_gateway_service_or_none: MockRouter | None, app: FastAPI, rpc_client: RabbitMQRPCClient, - mock_payments_gateway_service_or_none: MockRouter | None, init_payment_kwargs: dict[str, Any], payments_clean_db: None, ): @@ -115,7 +128,7 @@ async def test_webserver_one_time_payment_workflow( payment_id=result.payment_id, user_id=init_payment_kwargs["user_id"], wallet_id=init_payment_kwargs["wallet_id"], - timeout_s=20, # for tests + timeout_s=None if is_pdb_enabled else 5, ) assert result is None @@ -125,6 +138,7 @@ async def test_webserver_one_time_payment_workflow( async def test_cancel_invalid_payment_id( + is_pdb_enabled: bool, rpc_client: RabbitMQRPCClient, mock_payments_gateway_service_or_none: MockRouter | None, init_payment_kwargs: dict[str, Any], @@ -140,7 +154,7 @@ async def test_cancel_invalid_payment_id( payment_id=invalid_payment_id, user_id=init_payment_kwargs["user_id"], wallet_id=init_payment_kwargs["wallet_id"], - timeout_s=20, # for tests + timeout_s=None if is_pdb_enabled else 5, ) error = exc_info.value diff --git a/services/payments/tests/unit/test_rpc_payments_methods.py b/services/payments/tests/unit/test_rpc_payments_methods.py index d1a0f5ce7463..0de94e3b7213 100644 --- a/services/payments/tests/unit/test_rpc_payments_methods.py +++ b/services/payments/tests/unit/test_rpc_payments_methods.py @@ -119,9 +119,9 @@ async def test_webserver_init_and_cancel_payment_method_workflow( async def test_webserver_crud_payment_method_workflow( is_pdb_enabled: bool, + mock_payments_gateway_service_or_none: MockRouter | None, app: FastAPI, rpc_client: RabbitMQRPCClient, - mock_payments_gateway_service_or_none: MockRouter | None, user_id: UserID, user_name: IDStr, user_email: EmailStr, @@ -200,10 +200,10 @@ async def test_webserver_crud_payment_method_workflow( async def test_webserver_pay_with_payment_method_workflow( is_pdb_enabled: bool, + mock_resource_usage_tracker_service_api: None, + mock_payments_gateway_service_or_none: MockRouter | None, app: FastAPI, rpc_client: RabbitMQRPCClient, - mock_resoruce_usage_tracker_service_api: None, - mock_payments_gateway_service_or_none: MockRouter | None, faker: Faker, product_name: ProductName, user_id: UserID, diff --git a/services/payments/tests/unit/test_services_auto_recharge_listener.py b/services/payments/tests/unit/test_services_auto_recharge_listener.py index bc54d8297e48..c6069fb5603b 100644 --- a/services/payments/tests/unit/test_services_auto_recharge_listener.py +++ b/services/payments/tests/unit/test_services_auto_recharge_listener.py @@ -228,7 +228,7 @@ async def test_process_message__whole_autorecharge_flow_success( mocked_pay_with_payment_method: mock.AsyncMock, mock_rpc_server: RabbitMQRPCClient, mock_rpc_client: RabbitMQRPCClient, - mock_resoruce_usage_tracker_service_api: MockRouter, + mock_resource_usage_tracker_service_api: MockRouter, postgres_db: sa.engine.Engine, ): publisher = create_rabbitmq_client("publisher") @@ -241,7 +241,7 @@ async def test_process_message__whole_autorecharge_flow_success( assert row.wallet_id == wallet_id assert row.state == PaymentTransactionState.SUCCESS assert row.comment == "Payment generated by auto recharge" - assert len(mock_resoruce_usage_tracker_service_api.calls) == 1 + assert len(mock_resource_usage_tracker_service_api.calls) == 1 @pytest.mark.parametrize( diff --git a/services/payments/tests/unit/test_services_payments.py b/services/payments/tests/unit/test_services_payments.py index 5790b5f53fac..514941f7c4cc 100644 --- a/services/payments/tests/unit/test_services_payments.py +++ b/services/payments/tests/unit/test_services_payments.py @@ -59,12 +59,12 @@ def app_environment( async def test_fails_to_pay_with_payment_method_without_funds( + mock_payments_gateway_service_or_none: MockRouter | None, app: FastAPI, create_fake_payment_method_in_db: Callable[ [PaymentMethodID, WalletID, UserID], Awaitable[PaymentsMethodsDB] ], no_funds_payment_method_id: PaymentMethodID, - mock_payments_gateway_service_or_none: MockRouter | None, wallet_id: WalletID, wallet_name: IDStr, user_id: UserID, diff --git a/services/payments/tests/unit/test_services_payments_gateway.py b/services/payments/tests/unit/test_services_payments_gateway.py index 8d525e155109..268d2486b230 100644 --- a/services/payments/tests/unit/test_services_payments_gateway.py +++ b/services/payments/tests/unit/test_services_payments_gateway.py @@ -89,9 +89,9 @@ def amount_dollars(request: pytest.FixtureRequest) -> float: "https://github.com/ITISFoundation/osparc-simcore/pull/4715" ) async def test_one_time_payment_workflow( + mock_payments_gateway_service_or_none: MockRouter | None, app: FastAPI, faker: Faker, - mock_payments_gateway_service_or_none: MockRouter | None, amount_dollars: float, ): payment_gateway_api = PaymentsGatewayApi.get_from_app_state(app) diff --git a/services/payments/tests/unit/test_services_resource_usage_tracker.py b/services/payments/tests/unit/test_services_resource_usage_tracker.py index 868cdf656613..cc3b255c6191 100644 --- a/services/payments/tests/unit/test_services_resource_usage_tracker.py +++ b/services/payments/tests/unit/test_services_resource_usage_tracker.py @@ -56,7 +56,7 @@ def app( async def test_add_credits_to_wallet( - app: FastAPI, faker: Faker, mock_resoruce_usage_tracker_service_api: MockRouter + app: FastAPI, faker: Faker, mock_resource_usage_tracker_service_api: MockRouter ): # test rut_api = ResourceUsageTrackerApi.get_from_app_state(app)