def test_success(self): session = Session(response=Response( status_code=200, content=get_fixture("account"), )) make_client(session).put_payment_method( account_id="aAaBbCcDdEeFfGg", payment_method_id="pm_abcdef", ) expected_args = { "headers": { "Authorization": "Macaroon secret-token" }, "json": { "defaultPaymentMethod": { "Id": "pm_abcdef" }, }, "method": "put", "params": None, "url": ("https://1.2.3.4/v1" "/accounts/aAaBbCcDdEeFfGg/customer-info/stripe"), } self.assertEqual(session.request_kwargs, expected_args)
def test_success(self): session = Session(response=Response( status_code=200, content={}, )) make_client(session).post_subscription_auto_renewal( subscription_id="sAaBbCcDdEeFfGg", should_auto_renew=True, ) expected_args = { "headers": { "Authorization": "Macaroon secret-token" }, "json": { "shouldAutoRenew": True }, "method": "post", "params": None, "url": ("https://1.2.3.4/v1" "/subscription/sAaBbCcDdEeFfGg/auto-renewal"), } self.assertEqual(session.request_kwargs, expected_args)
def test_success(self): session = Session(response=Response( status_code=200, content={}, )) make_client(session).post_stripe_invoice_id( tx_type="purchase", tx_id="pAaBbCcDdEeFfGg", invoice_id="in_aAbBbCcDdEe", ) expected_args = { "headers": { "Authorization": "Macaroon secret-token" }, "json": None, "method": "post", "params": None, "url": ("https://1.2.3.4/v1" "/purchase/pAaBbCcDdEeFfGg/payment/stripe/in_aAbBbCcDdEe"), } self.assertEqual(session.request_kwargs, expected_args)
def test_success(self): session = Session(response=Response( status_code=200, content=get_fixture("ensured-account"), )) make_client(session).ensure_purchase_account( email="email@url", account_name="Joe Doe", marketplace="canonical-ua", captcha_value="abcd1234", ) expected_args = { "headers": { "Authorization": "Macaroon secret-token" }, "json": { "email": "email@url", "accountName": "Joe Doe", "recaptchaToken": "abcd1234", }, "method": "post", "params": None, "url": "https://1.2.3.4/v1/marketplace/canonical-ua/account", } self.assertEqual(session.request_kwargs, expected_args)
def test_success(self): session = Session(response=Response( status_code=200, content={ "accountInfo": get_fixture("account"), "users": [{ "accountInfo": get_fixture("account"), "userInfo": get_fixture("user"), }], }, )) make_client(session).get_account_users(account_id="aAbBcCdD") expected_args = { "headers": { "Authorization": "Macaroon secret-token" }, "json": {}, "method": "get", "params": None, "url": "https://1.2.3.4/v1/accounts/aAbBcCdD/users", } self.assertEqual(session.request_kwargs, expected_args)
def test_errors(self): cases = [ (500, False, UAContractsAPIError), (500, True, UAContractsAPIErrorView), ] for code, is_for_view, expected_error in cases: session = Session(response=Response(status_code=code, content={})) client = make_client(session, is_for_view=is_for_view) with self.assertRaises(expected_error): client.put_anonymous_customer_info( account_id="aAaBbCcDdEeFfGg", address={ "city": "Lone Done", "country": "GB", "line1": "Road Street", "postal_code": "111000", "state": "", }, name="Joe Doe", tax_id={ "type": "eu_vat", "value": "GB 123 1234 14" }, )
def test_errors(self): cases = [ (401, False, UAContractsAPIError), (401, True, UAContractsAPIErrorView), (500, False, UAContractsAPIError), (500, True, UAContractsAPIErrorView), ] for code, is_for_view, expected_error in cases: session = Session(response=Response(status_code=code, content={})) client = make_client(session, is_for_view=is_for_view) with self.assertRaises(expected_error): client.preview_purchase_from_marketplace( marketplace="canonical-ua", purchase_request={ "accountID": "aAaBbCcDdEeFfGg", "purchaseItems": [{ "productListingID": "lAaBbCcDdEeFfGg", "metric": "active-machines", "value": 5, }], "previousPurchaseID": "pAaBbCcDdEeFfGg", }, )
def test_returns_list_of_accounts(self): session = Session( Response( status_code=200, content={"accounts": get_fixture("accounts")}, )) ua_contracts_api = make_client(session) advantage_mapper = AdvantageMapper(ua_contracts_api) response = advantage_mapper.get_accounts() self.assertIsInstance(response, List) for item in response: self.assertIsInstance(item, Account) expectation = [ Account( id="a123AbBcCdDeEfFgGhHiIjJkKlLmMnNoOpP", name="Free", role="admin", ), Account( id="aAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpP", name="Account Name", role="admin", ), ] self.assertEqual(to_dict(expectation), to_dict(response))
def test_account_subscriptions_filters(self): session = Session(response=Response( status_code=200, content={"subscriptions": get_fixture("subscriptions")}, )) make_client(session).get_account_subscriptions( account_id="aABbCcdD", marketplace="canonical-ua", filters="?status=active&period=monthly", ) expected_args = { "headers": { "Authorization": "Macaroon secret-token" }, "json": None, "method": "get", "params": None, "url": ("https://1.2.3.4/v1/accounts/aABbCcdD" "/marketplace/canonical-ua/subscriptions" "?status=active&period=monthly"), } self.assertEqual(session.request_kwargs, expected_args)
def test_success(self): session = Session(response=Response( status_code=200, content={ "productListings": get_fixture("product-listings"), "products": get_fixture("products"), }, )) make_client(session).get_product_listings(marketplace="canonical-ua") expected_args = { "headers": { "Authorization": "Macaroon secret-token" }, "json": None, "method": "get", "params": None, "url": ("https://1.2.3.4" "/v1/marketplace/canonical-ua/product-listings"), } self.assertEqual(session.request_kwargs, expected_args)
def test_errors(self): cases = [ (500, False, UAContractsAPIError), (500, True, UAContractsAPIErrorView), ] for code, is_for_view, expected_error in cases: session = Session(response=Response(status_code=code, content={})) client = make_client(session, is_for_view=is_for_view) with self.assertRaises(expected_error): client.post_purchase_calculate( marketplace="canonical-ua", request_body={ "country": "GB", "productItems": [ { "productListingID": "lAaBbCcDdEeFfHh", "value": 1, }, ], "hasTaxID": True, }, )
def test_success(self): session = Session(response=Response( status_code=200, content={}, )) make_client(session).put_contract_entitlements( contract_id="cAbBcCdD", entitlements_request={ "entitlements": [{ "type": "fips", "is_enabled": True }] }, ) expected_args = { "headers": { "Authorization": "Macaroon secret-token" }, "json": { "entitlements": [{ "type": "fips", "is_enabled": True }] }, "method": "put", "params": None, "url": "https://1.2.3.4/v1/contracts/cAbBcCdD/defaultEnablement", } self.assertEqual(session.request_kwargs, expected_args)
def test_success(self): session = Session(response=Response( status_code=200, content={}, )) make_client(session).put_account_user_role( account_id="aAbBcCdD", user_role_request={ "email": "*****@*****.**", "role": "admin", "nameHint": "Joe", }, ) expected_args = { "headers": { "Authorization": "Macaroon secret-token" }, "json": { "email": "*****@*****.**", "role": "admin", "nameHint": "Joe", }, "method": "put", "params": None, "url": "https://1.2.3.4/v1/accounts/aAbBcCdD/user-role", } self.assertEqual(session.request_kwargs, expected_args)
def test_account_purchases_filters(self): session = Session(response=Response( status_code=200, content=get_fixture("purchases"), )) make_client(session).get_account_purchases( account_id="aABbCcdD", filters="?marketplace=canonical-ua", ) expected_args = { "headers": { "Authorization": "Macaroon secret-token" }, "json": None, "method": "get", "params": None, "url": ("https://1.2.3.4/v1/accounts/aABbCcdD" "/purchases?marketplace=canonical-ua"), } self.assertEqual(session.request_kwargs, expected_args)
def test_returns_list_of_contracts(self): session = Session(response=Response( status_code=200, content=get_fixture("contract"), )) ua_contracts_api = make_client(session) advantage_mapper = AdvantageMapper(ua_contracts_api) response = advantage_mapper.get_contract( "cAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpP") self.assertIsInstance(response, Contract)
def test_returns_list_of_contracts(self): session = Session(response=Response( status_code=200, content={"contracts": get_fixture("contracts")}, )) ua_contracts_api = make_client(session) advantage_mapper = AdvantageMapper(ua_contracts_api) response = advantage_mapper.get_account_contracts("aAbBcCdD") self.assertIsInstance(response, List) for item in response: self.assertIsInstance(item, Contract)
def test_success(self): session = Session(response=Response( status_code=200, content=get_fixture("account"), )) make_client(session).put_customer_info( account_id="aAaBbCcDdEeFfGg", payment_method_id="pm_abcdef", address={ "city": "Lone Done", "country": "GB", "line1": "Road Street", "postal_code": "111000", "state": "", }, name="Joe Doe", tax_id={ "type": "eu_vat", "value": "GB 123 1234 14" }, ) expected_args = { "headers": { "Authorization": "Macaroon secret-token" }, "json": { "defaultPaymentMethod": { "Id": "pm_abcdef" }, "paymentMethodID": "pm_abcdef", "address": { "city": "Lone Done", "country": "GB", "line1": "Road Street", "postal_code": "111000", "state": "", }, "name": "Joe Doe", "taxID": { "type": "eu_vat", "value": "GB 123 1234 14" }, }, "method": "put", "params": None, "url": ("https://1.2.3.4/v1" "/accounts/aAaBbCcDdEeFfGg/customer-info/stripe"), } self.assertEqual(session.request_kwargs, expected_args)
def test_returns_account_offers(self): session = Session(response=Response( status_code=200, content=get_fixture("offers"), )) ua_contracts_api = make_client(session) advantage_mapper = AdvantageMapper(ua_contracts_api) response = advantage_mapper.get_account_offers("account_id") self.assertIsInstance(response, List) for item in response: self.assertIsInstance(item, Offer)
def test_errors(self): cases = [ (500, False, UAContractsAPIError), (500, True, UAContractsAPIErrorView), ] for code, is_for_view, expected_error in cases: session = Session(response=Response(status_code=code, content={})) client = make_client(session, is_for_view=is_for_view) with self.assertRaises(expected_error): client.get_account_users(account_id="aAbBcCdD")
def test_errors(self): cases = [ (500, False, UAContractsAPIError), (500, True, UAContractsAPIErrorView), ] for code, is_for_view, expected_error in cases: session = Session(response=Response(status_code=code, content={})) client = make_client(session, is_for_view=is_for_view) with self.assertRaises(expected_error): client.cancel_subscription(subscription_id="sAaBbCcDdEeFfGg")
def test_returns_account_purchases(self): session = Session(response=Response( status_code=200, content=get_fixture("purchases"), )) ua_contracts_api = make_client(session) advantage_mapper = AdvantageMapper(ua_contracts_api) response = advantage_mapper.get_account_purchases( account_id="aABbCcdD", ) self.assertIsInstance(response, List) for item in response: self.assertIsInstance(item, Purchase)
def test_returns_3ds_purchase(self): session = Session( Response( status_code=200, content=get_fixture("purchase-3ds"), )) ua_contracts_api = make_client(session) advantage_mapper = AdvantageMapper(ua_contracts_api) response = advantage_mapper.get_purchase("pAaBbCcDd") self.assertIsInstance(response, Purchase) expected_invoice = Invoice( reason="subscription_create", currency="usd", status="open", total=27000, id="in_aAaBbCcDd", items=[{ "currency": "usd", "description": ("1 machine x UA Infrastructure" " - Essential (Physical) (at $225.00 / year)"), "pro_rated_amount": 22500, "quantity": 1, }], tax_amount=4500, payment_status={ "pi_client_secret": "pi_aAbBcCdD", "status": "need_3ds_authorization", }, url="download.link", ) self.assertEqual(to_dict(expected_invoice), to_dict(response.invoice)) expected_purchase = Purchase( id="pAaBbCcDd", account_id="aAaBbCcDd", subscription_id="sAaBbCcDd", marketplace="canonical-ua", created_at="2022-02-23T13:46:05.871Z", status="processing", invoice=expected_invoice, items=[PurchaseItem( listing_id="lAaBbCcDd", value=1, )], ) self.assertEqual(to_dict(expected_purchase), to_dict(response))
def test_errors(self): cases = [ (401, False, UAContractsAPIError), (401, True, UAContractsAPIErrorView), (500, False, UAContractsAPIError), (500, True, UAContractsAPIErrorView), ] for code, is_for_view, expected_error in cases: session = Session(response=Response(status_code=code, content={})) client = make_client(session, is_for_view=is_for_view) with self.assertRaises(expected_error): client.get_product_listings(marketplace="canonical-ua")
def test_errors(self): cases = [ (401, False, UAContractsAPIError), (401, True, UAContractsAPIErrorView), (500, False, UAContractsAPIError), (500, True, UAContractsAPIErrorView), ] for code, is_for_view, expected_error in cases: session = Session(response=Response(status_code=code, content={})) client = make_client(session, is_for_view=is_for_view) with self.assertRaises(expected_error): client.get_purchase(purchase_id="pAaAbBbCcDdEe")
def test_errors(self): cases = [ (500, False, UAContractsAPIError), (500, True, UAContractsAPIErrorView), ] for code, is_for_view, expected_error in cases: session = Session(response=Response(status_code=code, content={})) client = make_client(session, is_for_view=is_for_view) with self.assertRaises(expected_error): client.put_payment_method( account_id="aAaBbCcDdEeFfGg", payment_method_id="pm_abcdef", )
def test_errors(self): cases = [ (401, False, UnauthorizedError), (401, True, UnauthorizedErrorView), (500, False, UAContractsAPIError), (500, True, UAContractsAPIErrorView), ] for code, is_for_view, expected_error in cases: session = Session(response=Response(status_code=code, content={})) client = make_client(session, is_for_view=is_for_view) with self.assertRaises(expected_error): client.get_account_subscriptions(account_id="aABbCcdD", marketplace="canonical-ua")
def test_errors(self): cases = [ (401, False, UAContractsAPIError), (401, True, UAContractsAPIErrorView), (404, False, UAContractsUserHasNoAccount), (404, True, UAContractsUserHasNoAccount), (500, False, UAContractsAPIError), (500, True, UAContractsAPIErrorView), ] for code, is_for_view, expected_error in cases: session = Session(response=Response(status_code=code, content={})) client = make_client(session, is_for_view=is_for_view) with self.assertRaises(expected_error): client.get_customer_info(account_id="aABbCcdD")
def test_returns_list_of_contracts(self): session = Session(response=Response( status_code=200, content={ "productListings": get_fixture("product-listings"), "products": get_fixture("products"), }, )) ua_contracts_api = make_client(session) advantage_mapper = AdvantageMapper(ua_contracts_api) response = advantage_mapper.get_product_listings( marketplace="canonical-ua") self.assertIsInstance(response, Dict) for listing in response.values(): self.assertIsInstance(listing, Listing)
def test_errors(self): cases = [ (500, False, UAContractsAPIError), (500, True, UAContractsAPIErrorView), ] for code, is_for_view, expected_error in cases: session = Session(response=Response(status_code=code, content={})) client = make_client(session, is_for_view=is_for_view) with self.assertRaises(expected_error): client.post_stripe_invoice_id( tx_type="purchase", tx_id="pAaBbCcDdEeFfGg", invoice_id="in_aAbBbCcDdEe", )
def test_returns_purchase(self): session = Session( Response( status_code=200, content=get_fixture("purchase"), )) ua_contracts_api = make_client(session) advantage_mapper = AdvantageMapper(ua_contracts_api) response = advantage_mapper.get_purchase("pAaBbCcDd") self.assertIsInstance(response, Purchase) expected_invoice = Invoice( reason="subscription_update", currency="usd", status="open", total=1200, id="in_abcdef", items=[{ "currency": "usd", "description": "Description", "pro_rated_amount": 1000, "quantity": 1, }], tax_amount=200, payment_status={ "error": "generic_decline", "status": "need_another_payment_method", }, url="invoice.url", ) self.assertEqual(to_dict(expected_invoice), to_dict(response.invoice)) expected_purchase = Purchase( id="pAaBbCcDd", account_id="aAaBbCcDdEeFfGg", subscription_id="sAaBbCcDdEeFfGg", marketplace="canonical-ua", created_at="2020-01-01T10:00:00Z", status="processing", invoice=expected_invoice, items=[PurchaseItem( listing_id="lAaBbCcDdEeFfGg", value=1, )], ) self.assertEqual(to_dict(expected_purchase), to_dict(response))