diff --git a/tests/backoffice/test_merchant_accounts.py b/tests/backoffice/test_merchant_accounts.py index c88b1431..6c7a742d 100644 --- a/tests/backoffice/test_merchant_accounts.py +++ b/tests/backoffice/test_merchant_accounts.py @@ -5,15 +5,18 @@ 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) + 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 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..9a355016 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. @@ -66,19 +89,22 @@ 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) - 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: + 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", + 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,