Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions tests/backoffice/test_merchant_accounts.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
9 changes: 6 additions & 3 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import os
import sys
from typing import Iterator

import pytest

Expand All @@ -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)
50 changes: 38 additions & 12 deletions tests/utils/environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand All @@ -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,
Expand Down
Loading