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_parse_listings(self): raw_products = get_fixture("products") raw_product_listings = get_fixture("product-listings") parsed_listings = parse_product_listings( raw_product_listings=raw_product_listings, raw_products=raw_products, ) expectation = { "lAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpP2": Listing( id="lAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpP2", name="product-id-2", marketplace="canonical-ua", status="active", product=Product( name="Product Name 2", id="product-id-2", ), price=1000, currency="USD", trial_days=None, period=None, ) } self.assertIsInstance(parsed_listings, Dict) self.assertEqual(to_dict(expectation), to_dict(parsed_listings))
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_parse_legacy_contract_items(self): raw_contract_items = [get_fixture("contract-item-legacy")] parsed_contract_items = parse_contract_items( raw_items=raw_contract_items) expectation = [ ContractItem( id=6, contract_id="cAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpP2", created_at="2014-01-01T10:00:00Z", start_date="2014-01-01T10:00:00Z", end_date="2015-01-01T00:00:00Z", reason="contract_created", value=5, product_listing_id=None, purchase_id=None, trial_id=None, renewal=Renewal( id="rAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpP2", contract_id="cAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpP2", actionable=True, start_date="2014-11-01T00:00:00Z", end_date="2015-01-01T00:00:00Z", status="pending", new_contract_start="2015-01-01T00:00:00Z", price=5000, currency="USD", ), ), ] self.assertIsInstance(parsed_contract_items, List) self.assertEqual(to_dict(expectation), to_dict(parsed_contract_items))
def test_parse_entitlements(self): raw_entitlements = get_fixture("entitlements") parsed_entitlements = parse_entitlements( raw_entitlements=raw_entitlements) expectation = [ Entitlement( type="entitlement-type", support_level=None, enabled_by_default=True, is_available=True, ), Entitlement( type="entitlement-type-2", support_level=None, enabled_by_default=False, is_available=True, ), Entitlement( type="support", support_level="advanced", enabled_by_default=False, is_available=True, ), ] self.assertIsInstance(parsed_entitlements, List) self.assertEqual(to_dict(expectation), to_dict(parsed_entitlements))
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_parse_subscriptions(self): raw_subscriptions = get_fixture("subscriptions") parsed_subscriptions = parse_subscriptions( raw_subscriptions=raw_subscriptions) expectation = [ Subscription( id="sAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpP", account_id="aAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpP", marketplace="canonical-ua", period="monthly", status="active", last_purchase_id="pAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpP", is_auto_renewing=True, items=[ SubscriptionItem( subscription_id="sAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpP", product_listing_id="lAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpP", value=3, ), ], started_with_trial=None, ), ] self.assertIsInstance(parsed_subscriptions, List) self.assertEqual(to_dict(expectation), to_dict(parsed_subscriptions))
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_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_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_parse_users(self): raw_users = get_fixture("users") parsed_users = parse_users(raw_users) joe = User( display_name="Joe Doe", name="joedoe2021", email="*****@*****.**", id="aAbBcCdD", last_login_at="2021-09-10T12:00:00Z", first_login_at="2021-09-10T12:00:00Z", verified=True, ) joe.set_user_role_on_account("admin") expectation = [ joe, User( display_name="Jane Doe", name="janedoe2021", email="*****@*****.**", id="aAbBcCdD2", last_login_at="2021-09-10T12:00:00Z", first_login_at="2021-09-10T12:00:00Z", verified=False, ), ] self.assertEqual(to_dict(parsed_users), to_dict(expectation))
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_parse_subscription(self): raw_subscription = get_fixture("subscription") parsed_subscription = parse_subscription( raw_subscription=raw_subscription) expectation = Subscription( id="sAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpP", account_id="aAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpP", marketplace="canonical-ua", period="yearly", status="active", last_purchase_id="pAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpP", pending_purchases=["pAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpP222"], items=[ SubscriptionItem( subscription_id="sAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpP", product_listing_id="lAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpP", value=3, ), ], started_with_trial=True, in_trial=True, ) self.assertIsInstance(parsed_subscription, Subscription) self.assertEqual(to_dict(expectation), to_dict(parsed_subscription))
def test_parse_offers(self): raw_offers = get_fixture("offers") parsed_offers = parse_offers(raw_offers) expectation = [ Offer( id="oOaAbBcCdDeEfFgG", account_id="aAbBcCdDeEfFgG", total=810000, actionable=True, created_at="2022-01-04T10:00:00Z", marketplace="canonical-ua", items=[ OfferItem( id="lAaBbCcDdEeFfGg", name="uai-advanced-desktop-oneoff", price=60000, allowance=2, ), OfferItem( id="lAaBbCcDdEeFfGg-2", name="uai-advanced-physical-oneoff", price=750000, allowance=5, ), ], ) ] self.assertIsInstance(parsed_offers, List) self.assertEqual(to_dict(expectation), to_dict(parsed_offers))
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_parse_product(self): raw_product = get_fixture("product") parsed_product = parse_product(raw_product=raw_product) expectation = Product( id="product-id", name="Product Name", ) self.assertIsInstance(parsed_product, Product) self.assertEqual(to_dict(expectation), to_dict(parsed_product))
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_returns_list_of_users(self): session = Session( Response( status_code=200, content={ "accountInfo": get_fixture("account"), "users": [{ "accountInfo": get_fixture("account"), "userInfo": get_fixture("user"), }], }, )) ua_contracts_api = make_client(session) advantage_mapper = AdvantageMapper(ua_contracts_api) response = advantage_mapper.get_account_users(account_id="aAbBcCdD") self.assertIsInstance(response, List) for item in response: self.assertIsInstance(item, User)
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_parse_contract(self): raw_contract = get_fixture("contract") parsed_contract = parse_contract(raw_contract=raw_contract) expectation = Contract( id="cAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpP", account_id="aAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpP", name="Product Name", product_id="product-id", entitlements=[ Entitlement( type="entitlement-type-2", support_level=None, enabled_by_default=False, ), Entitlement( type="support", support_level="advanced", enabled_by_default=False, ), ], number_of_active_machines=0, items=[ ContractItem( id=10, contract_id="cAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpP", created_at="2017-01-02T10:00:00Z", start_date="2017-01-02T10:00:00Z", end_date="2018-01-02T10:00:00Z", reason="trial_started", value=1, product_listing_id="lAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpP", purchase_id=None, trial_id="tAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpP", ), ContractItem( id=11, contract_id="cAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpP", created_at="2017-02-02T10:00:00Z", start_date="2017-02-02T10:00:00Z", end_date="2018-02-02T10:00:00Z", reason="purchase_made", value=1, product_listing_id="lAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpP", purchase_id="pAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpP", trial_id=None, ), ], ) self.assertIsInstance(parsed_contract, Contract) self.assertEqual(to_dict(expectation), to_dict(parsed_contract))
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_parse_user(self): raw_user = get_fixture("user") parsed_user = parse_user(raw_user) expectation = User( display_name="Joe Doe", name="joedoe2021", email="*****@*****.**", id="aAbBcCdD", last_login_at="2021-09-10T12:00:00Z", first_login_at="2021-09-10T12:00:00Z", verified=True, ) self.assertEqual(to_dict(parsed_user), to_dict(expectation))
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))
def test_parse_renewal(self): raw_renewal = get_fixture("renewal") parsed_renewal = parse_renewal(raw_renewal=raw_renewal) expectation = Renewal( id="rAaBbCcDdEeFf", contract_id="cAaBbCcDdEeFf", actionable=False, start_date="2020-11-01T00:00:00Z", end_date="2021-01-01T00:00:00Z", status="done", new_contract_start="2021-01-01T00:00:00Z", price=11000, currency="USD", ) self.assertEqual(to_dict(expectation), to_dict(parsed_renewal))
def test_success(self): session = Session(response=Response( status_code=200, content=get_fixture("purchase"), )) make_client(session).get_purchase(purchase_id="pAaAbBbCcDdEe") expected_args = { "headers": { "Authorization": "Macaroon secret-token" }, "json": None, "method": "get", "params": None, "url": "https://1.2.3.4/v1/purchase/pAaAbBbCcDdEe", } self.assertEqual(session.request_kwargs, expected_args)
def test_success(self): session = Session(response=Response( status_code=200, content=get_fixture("account"), )) make_client(session).get_purchase_account("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/account", } self.assertEqual(session.request_kwargs, expected_args)
def test_success(self): session = Session(response=Response( status_code=200, content=get_fixture("offers"), )) make_client(session).get_account_offers("account_id") expected_args = { "headers": { "Authorization": "Macaroon secret-token" }, "json": None, "method": "get", "params": None, "url": "https://1.2.3.4/v1/accounts/account_id/offers", } self.assertEqual(session.request_kwargs, expected_args)