From 5afbb5d80fd9bdaddc047286de40db7be856929e Mon Sep 17 00:00:00 2001 From: Matej Dujava Date: Mon, 23 Mar 2026 19:43:47 +0100 Subject: [PATCH] Fix billing tests to pass if default provider id != 2 --- testsuite/billing.py | 16 ++++++++-------- testsuite/tests/ui/billing/braintree/conftest.py | 5 +++-- .../ui/billing/braintree/test_invoice_payment.py | 6 +++--- testsuite/tests/ui/billing/stripe/conftest.py | 5 +++-- 4 files changed, 17 insertions(+), 15 deletions(-) diff --git a/testsuite/billing.py b/testsuite/billing.py index 9d1b9d7f8..fbf6053a8 100644 --- a/testsuite/billing.py +++ b/testsuite/billing.py @@ -12,10 +12,11 @@ class Stripe: """API for Stripe""" - def __init__(self, api_key): + def __init__(self, api_key, provider_account_id): # Due to fact that we can set up only one Stripe per 3scale and we use same api_key everytime this # is not disruptive even if it looks like it is. stripe.api_key = api_key + self.provider_account_id = provider_account_id @staticmethod @backoff.on_predicate(backoff.fibo, lambda x: x == [], max_tries=10, jitter=None) @@ -23,9 +24,8 @@ def read_charge(customer): """Retrieves the details of the charge""" return stripe.Charge.search(query=f"customer:'{customer['id']}'").data - @staticmethod @backoff.on_exception(backoff.expo, IndexError, max_tries=4, jitter=None) - def read_customer_by_account(account): + def read_customer_by_account(self, account): """ Read Stripe customer. Different 3scale deployments can have customers with the same id, which is reflected to the @@ -51,7 +51,7 @@ def assert_payment(self, invoice, account): class Braintree: """API for braintree""" - def __init__(self, merchant_id, public_key, private_key): + def __init__(self, merchant_id, public_key, private_key, provider_account_id): self.gateway = braintree.BraintreeGateway( braintree.Configuration( environment=braintree.Environment.Sandbox, @@ -60,6 +60,7 @@ def __init__(self, merchant_id, public_key, private_key): private_key=private_key, ) ) + self.provider_account_id = provider_account_id @backoff.on_exception(backoff.fibo, (ServiceUnavailableError, RequestTimeoutError), max_tries=8, jitter=None) def get_customer_transactions(self, account): @@ -76,10 +77,9 @@ def merchant_currency(self): merchant_accounts = list(self.gateway.merchant_account.all().merchant_accounts.items) return [x for x in merchant_accounts if x.default is True][0].currency_iso_code - @staticmethod - def customer_id(account): - """Returns Braintree customer id. It is in a form `3scale-2-{account_id}-1`""" - return f"3scale-2-{account.entity_id}-1" + def customer_id(self, account): + """Returns Braintree customer id. It is in a form `3scale-{provider_id}-{account_id}-1`""" + return f"3scale-{self.provider_account_id}-{account.entity_id}-1" @staticmethod def _assert_transaction(invoice, transaction): diff --git a/testsuite/tests/ui/billing/braintree/conftest.py b/testsuite/tests/ui/billing/braintree/conftest.py index 469c1293b..daa8d7ad9 100644 --- a/testsuite/tests/ui/billing/braintree/conftest.py +++ b/testsuite/tests/ui/billing/braintree/conftest.py @@ -24,13 +24,14 @@ def require_braintree_patch(openshift): @pytest.fixture(scope="session") -def braintree(testconfig): +def braintree(testconfig, threescale): """Braintree API""" braintree_credentials = testconfig["braintree"] merchant_id = braintree_credentials["merchant_id"] public_key = braintree_credentials["public_key"] private_key = braintree_credentials["private_key"] - return Braintree(merchant_id, public_key, private_key) + provider_account_id = threescale.provider_accounts.fetch().entity_id + return Braintree(merchant_id, public_key, private_key, provider_account_id) @pytest.fixture(scope="module", autouse=True) diff --git a/testsuite/tests/ui/billing/braintree/test_invoice_payment.py b/testsuite/tests/ui/billing/braintree/test_invoice_payment.py index cbf07310a..56c6af0b6 100644 --- a/testsuite/tests/ui/billing/braintree/test_invoice_payment.py +++ b/testsuite/tests/ui/billing/braintree/test_invoice_payment.py @@ -15,7 +15,7 @@ def card_setup(custom_card): def normalize_url(url): """Invoice url need to be changed from internal to external form""" - for rep in (("3scale-admin", "3scale"), ("/api/", "/admin/account/")): + for rep in (("-admin.", "."), ("/api/", "/admin/account/")): url = url.replace(*rep) return url @@ -28,11 +28,11 @@ def test_no_sca_ui_invoice(braintree, ui_invoice, account): assert invoice_view.state_field.text == "State Paid" -def test_mail_completed_payment(invoice, mailhog_client): +def test_mail_completed_payment(provider_account, invoice, mailhog_client): """Tests mail notification about successful payment""" invoice.charge() mailhog_client.assert_message_received( - subject="Provider Name API - Payment completed", + subject=f"{provider_account['org_name']} API - Payment completed", content=f"successfully completed your monthly payment for our service of USD {invoice.entity['cost']}0.\r\n\r\n" f"Your invoice is available online at:\r\n\r\n{normalize_url(invoice.url)}", ) diff --git a/testsuite/tests/ui/billing/stripe/conftest.py b/testsuite/tests/ui/billing/stripe/conftest.py index d1a0d3247..7f6b34911 100644 --- a/testsuite/tests/ui/billing/stripe/conftest.py +++ b/testsuite/tests/ui/billing/stripe/conftest.py @@ -20,9 +20,10 @@ def gateway_setup(custom_admin_login, navigator, testconfig): @pytest.fixture(scope="session") -def stripe(testconfig): +def stripe(testconfig, threescale): """Stripe API""" - return Stripe(testconfig["stripe"]["api_key"]) + provider_account_id = threescale.provider_accounts.fetch().entity_id + return Stripe(testconfig["stripe"]["api_key"], provider_account_id) @pytest.fixture(scope="module")