From b4015b3f719cb6b7561450c8cf38c440fdd027b4 Mon Sep 17 00:00:00 2001 From: Cristiano Betta Date: Wed, 4 Nov 2026 15:02:20 +0000 Subject: [PATCH 1/2] test: delete e2e merchant accounts after each run Calls core-api's DELETE /merchant-accounts/{id} (gr4vy/core-api#9999) directly, since it is not in the public spec. Failures are logged and never fail the run. Co-Authored-By: Claude Opus 5.5 --- tests/backoffice/test_merchant_accounts.py | 7 +++- tests/conftest.py | 9 +++-- tests/utils/environment.py | 43 ++++++++++++++++++---- 3 files changed, 46 insertions(+), 13 deletions(-) diff --git a/tests/backoffice/test_merchant_accounts.py b/tests/backoffice/test_merchant_accounts.py index c88b1431..d81015c9 100644 --- a/tests/backoffice/test_merchant_accounts.py +++ b/tests/backoffice/test_merchant_accounts.py @@ -5,16 +5,19 @@ import secrets from utils import MISSING_ID, reach -from utils.environment import create_client +from utils.environment import create_client, delete_merchant_account -def test_crud_with_partial_update(merchant): +def test_crud_with_partial_update(merchant, request): # Merchant-account operations are account-level, not merchant-scoped, so # drive them with an unscoped (admin) client. sdk = create_client(merchant.private_key) account_id = secrets.token_hex(8) created = sdk.merchant_accounts.create(id=account_id, display_name="Original") + request.addfinalizer( + lambda: delete_merchant_account(merchant.private_key, account_id) + ) assert created.id == account_id fetched = sdk.merchant_accounts.get(merchant_account_id=account_id) diff --git a/tests/conftest.py b/tests/conftest.py index a7e68b1e..0f6bec8c 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -7,6 +7,7 @@ import os import sys +from typing import Iterator import pytest @@ -17,10 +18,12 @@ sys.path.insert(0, os.path.dirname(__file__)) # pylint: disable=wrong-import-position -from utils.environment import setup_merchant +from utils.environment import delete_merchant_account, setup_merchant from utils.merchant import TestMerchant @pytest.fixture(scope="module", name="merchant") -def merchant_fixture() -> TestMerchant: - return setup_merchant() +def merchant_fixture() -> Iterator[TestMerchant]: + merchant = setup_merchant() + yield merchant + delete_merchant_account(merchant.private_key, merchant.merchant_account_id) diff --git a/tests/utils/environment.py b/tests/utils/environment.py index 425ef05d..1f1da565 100644 --- a/tests/utils/environment.py +++ b/tests/utils/environment.py @@ -57,6 +57,29 @@ def create_client(private_key: str, merchant_account_id: Optional[str] = None) - ) +def delete_merchant_account(private_key: str, merchant_account_id: str) -> None: + """Delete a merchant account a suite created; log and carry on if it fails. + + The delete endpoint is not in the public spec, so it is called directly. + """ + try: + response = httpx.delete( + f"{API_BASE_URL}/merchant-accounts/{merchant_account_id}", + headers={"Authorization": f"Bearer {auth.get_token(private_key)}"}, + timeout=httpx.Timeout(30.0, connect=10.0), + ) + if response.status_code not in (204, 404): + _logger.warning( + "Failed to delete merchant account %s: HTTP %s", + merchant_account_id, + response.status_code, + ) + except Exception: # pylint: disable=broad-exception-caught + _logger.warning( + "Failed to delete merchant account %s", merchant_account_id, exc_info=True + ) + + def setup_merchant() -> TestMerchant: """Provision an isolated merchant account + mock-card service. @@ -71,14 +94,18 @@ def setup_merchant() -> TestMerchant: ) client = create_client(private_key, merchant_account_id) - client.payment_services.create( - display_name="Payment service", - payment_service_definition_id="mock-card", - fields=[{"key": "merchant_id", "value": "test"}], - accepted_currencies=["USD"], - accepted_countries=["US"], - active=True, - ) + try: + client.payment_services.create( + display_name="Payment service", + payment_service_definition_id="mock-card", + fields=[{"key": "merchant_id", "value": "test"}], + accepted_currencies=["USD"], + accepted_countries=["US"], + active=True, + ) + except Exception: + delete_merchant_account(private_key, merchant_account_id) + raise return TestMerchant( client=client, From 6e4b3fe7001a5c31a24d797711a59af272ed37f0 Mon Sep 17 00:00:00 2001 From: Cristiano Betta Date: Wed, 4 Nov 2026 15:44:57 +0000 Subject: [PATCH 2/2] test: arm e2e merchant-account cleanup before the create call A create whose response is lost still leaves the account on the server; deleting an id that was never created returns 404, which cleanup already treats as done. Co-Authored-By: Claude Opus 5.5 --- tests/backoffice/test_merchant_accounts.py | 2 +- tests/utils/environment.py | 7 +++---- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/tests/backoffice/test_merchant_accounts.py b/tests/backoffice/test_merchant_accounts.py index d81015c9..6c7a742d 100644 --- a/tests/backoffice/test_merchant_accounts.py +++ b/tests/backoffice/test_merchant_accounts.py @@ -14,10 +14,10 @@ def test_crud_with_partial_update(merchant, request): sdk = create_client(merchant.private_key) account_id = secrets.token_hex(8) - created = sdk.merchant_accounts.create(id=account_id, display_name="Original") request.addfinalizer( lambda: delete_merchant_account(merchant.private_key, account_id) ) + created = sdk.merchant_accounts.create(id=account_id, display_name="Original") assert created.id == account_id fetched = sdk.merchant_accounts.get(merchant_account_id=account_id) diff --git a/tests/utils/environment.py b/tests/utils/environment.py index 1f1da565..9a355016 100644 --- a/tests/utils/environment.py +++ b/tests/utils/environment.py @@ -89,12 +89,11 @@ def setup_merchant() -> TestMerchant: admin = create_client(private_key) merchant_account_id = secrets.token_hex(8) - admin.merchant_accounts.create( - id=merchant_account_id, display_name=merchant_account_id - ) - client = create_client(private_key, merchant_account_id) try: + admin.merchant_accounts.create( + id=merchant_account_id, display_name=merchant_account_id + ) client.payment_services.create( display_name="Payment service", payment_service_definition_id="mock-card",