Skip to content

Commit 9cf78dd

Browse files
committed
Fix billing tests to pass if default provider id != 2
(cherry picked from commit 162acb1)
1 parent 4133c27 commit 9cf78dd

4 files changed

Lines changed: 18 additions & 16 deletions

File tree

testsuite/billing.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,27 +12,27 @@
1212
class Stripe:
1313
"""API for Stripe"""
1414

15-
def __init__(self, api_key):
15+
def __init__(self, api_key, provider_account_id):
1616
# Due to fact that we can set up only one Stripe per 3scale and we use same api_key everytime this
1717
# is not disruptive even if it looks like it is.
1818
stripe.api_key = api_key
19+
self.provider_account_id = provider_account_id
1920

2021
@staticmethod
2122
@backoff.on_predicate(backoff.fibo, lambda x: x == [], max_tries=10, jitter=None)
2223
def read_charge(customer):
2324
"""Retrieves the details of the charge"""
2425
return stripe.Charge.search(query=f"customer:'{customer['id']}'").data
2526

26-
@staticmethod
2727
@backoff.on_exception(backoff.expo, IndexError, max_tries=4, jitter=None)
28-
def read_customer_by_account(account):
28+
def read_customer_by_account(self, account):
2929
"""
3030
Read Stripe customer.
3131
Different 3scale deployments can have customers with the same id, which is reflected to the
3232
`3scale_account_reference` Stripe Customer variable. This method reads just the last one.
3333
"""
3434
return stripe.Customer.search(
35-
query=f"metadata['3scale_account_reference']:'3scale-2-{str(account.entity_id)}'"
35+
query=f"metadata['3scale_account_reference']:'3scale-{self.provider_account_id}-{str(account.entity_id)}'"
3636
).data[0]
3737

3838
def assert_payment(self, invoice, account):
@@ -51,7 +51,7 @@ def assert_payment(self, invoice, account):
5151
class Braintree:
5252
"""API for braintree"""
5353

54-
def __init__(self, merchant_id, public_key, private_key):
54+
def __init__(self, merchant_id, public_key, private_key, provider_account_id):
5555
self.gateway = braintree.BraintreeGateway(
5656
braintree.Configuration(
5757
environment=braintree.Environment.Sandbox,
@@ -60,6 +60,7 @@ def __init__(self, merchant_id, public_key, private_key):
6060
private_key=private_key,
6161
)
6262
)
63+
self.provider_account_id = provider_account_id
6364

6465
@backoff.on_exception(backoff.fibo, (ServiceUnavailableError, RequestTimeoutError), max_tries=8, jitter=None)
6566
def get_customer_transactions(self, account):
@@ -76,10 +77,9 @@ def merchant_currency(self):
7677
merchant_accounts = list(self.gateway.merchant_account.all().merchant_accounts.items)
7778
return [x for x in merchant_accounts if x.default is True][0].currency_iso_code
7879

79-
@staticmethod
80-
def customer_id(account):
81-
"""Returns Braintree customer id. It is in a form `3scale-2-{account_id}-1`"""
82-
return f"3scale-2-{account.entity_id}-1"
80+
def customer_id(self, account):
81+
"""Returns Braintree customer id. It is in a form `3scale-{provider_id}-{account_id}-1`"""
82+
return f"3scale-{self.provider_account_id}-{account.entity_id}-1"
8383

8484
@staticmethod
8585
def _assert_transaction(invoice, transaction):

testsuite/tests/ui/billing/braintree/conftest.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,14 @@ def require_braintree_patch(openshift):
2424

2525

2626
@pytest.fixture(scope="session")
27-
def braintree(testconfig):
27+
def braintree(testconfig, threescale):
2828
"""Braintree API"""
2929
braintree_credentials = testconfig["braintree"]
3030
merchant_id = braintree_credentials["merchant_id"]
3131
public_key = braintree_credentials["public_key"]
3232
private_key = braintree_credentials["private_key"]
33-
return Braintree(merchant_id, public_key, private_key)
33+
provider_account_id = threescale.provider_accounts.fetch().entity_id
34+
return Braintree(merchant_id, public_key, private_key, provider_account_id)
3435

3536

3637
@pytest.fixture(scope="module", autouse=True)

testsuite/tests/ui/billing/braintree/test_invoice_payment.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ def card_setup(custom_card):
1515

1616
def normalize_url(url):
1717
"""Invoice url need to be changed from internal to external form"""
18-
for rep in (("3scale-admin", "3scale"), ("/api/", "/admin/account/")):
18+
for rep in (("-admin.", "."), ("/api/", "/admin/account/")):
1919
url = url.replace(*rep)
2020
return url
2121

@@ -28,11 +28,11 @@ def test_no_sca_ui_invoice(braintree, ui_invoice, account):
2828
assert invoice_view.state_field.text == "State Paid"
2929

3030

31-
def test_mail_completed_payment(invoice, mailhog_client):
31+
def test_mail_completed_payment(provider_account, invoice, mailhog_client):
3232
"""Tests mail notification about successful payment"""
3333
invoice.charge()
3434
mailhog_client.assert_message_received(
35-
subject="Provider Name API - Payment completed",
35+
subject=f"{provider_account['org_name']} API - Payment completed",
3636
content=f"successfully completed your monthly payment for our service of USD {invoice.entity['cost']}0.\r\n\r\n"
3737
f"Your invoice is available online at:\r\n\r\n{normalize_url(invoice.url)}",
3838
)

testsuite/tests/ui/billing/stripe/conftest.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,10 @@ def gateway_setup(custom_admin_login, navigator, testconfig):
2020

2121

2222
@pytest.fixture(scope="session")
23-
def stripe(testconfig):
23+
def stripe(testconfig, threescale):
2424
"""Stripe API"""
25-
return Stripe(testconfig["stripe"]["api_key"])
25+
provider_account_id = threescale.provider_accounts.fetch().entity_id
26+
return Stripe(testconfig["stripe"]["api_key"], provider_account_id)
2627

2728

2829
@pytest.fixture(scope="module")

0 commit comments

Comments
 (0)