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_convert_response_returns_list_of_contracts(self): json_listings = { "productListings": get_fixture("product-listings"), "products": get_fixture("products"), } session = Session( response=Response( status_code=200, content=json_listings, ) ) client = make_client(session, convert_response=True) response = client.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.assertIsInstance(response, Dict) for listing in response.values(): self.assertIsInstance(listing, Listing) self.assertEqual(session.request_kwargs, expected_args)
def test_convert_response_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"), } ], }, ) ) client = make_client(session, convert_response=True) response = client.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.assertIsInstance(response, List) for item in response: self.assertIsInstance(item, User) self.assertEqual(session.request_kwargs, expected_args)
def test_success(self): json_listings = { "productListings": get_fixture("product-listings"), "products": get_fixture("products"), } session = Session( response=Response(status_code=200, content=json_listings) ) client = make_client(session) response = client.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(response, json_listings) 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"), } ], }, ) ) client = make_client(session) response = client.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(response, [get_fixture("user")]) self.assertEqual(session.request_kwargs, expected_args)
def test_convert_response_returns_list_of_accounts(self): json_accounts = get_fixture("accounts") session = Session( Response( status_code=200, content={"accounts": json_accounts}, ) ) client = make_client(session, convert_response=True) response = client.get_accounts() expected_args = { "headers": {"Authorization": "Macaroon secret-token"}, "json": None, "method": "get", "params": None, "url": "https://1.2.3.4/v1/accounts", } self.assertIsInstance(response, List) for item in response: self.assertIsInstance(item, Account) self.assertEqual(session.request_kwargs, expected_args)
def test_parse_contracts(self): raw_contracts = get_fixture("contracts") parsed_contracts = parse_contracts(raw_contracts=raw_contracts) expectation = [ Contract( id="cAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpP", account_id="aAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpP", name="Product Name 2", product_id="product-id-2", entitlements=[ Entitlement( type="entitlement-type", support_level=None, enabled_by_default=True, ), ], items=[ ContractItem( 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", ), ], ) ] self.assertIsInstance(parsed_contracts, List) self.assertEqual(to_dict(expectation), to_dict(parsed_contracts))
def test_success(self): json_account = get_fixture("account") session = Session( response=Response( status_code=200, content=json_account, ) ) client = make_client(session) response = client.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(response, json_account) self.assertEqual(session.request_kwargs, expected_args)
def test_success(self): json_customer_info = get_fixture("customer-info") session = Session( response=Response( status_code=200, content=json_customer_info, ) ) client = make_client(session) response = client.get_customer_info(account_id="aAaBbCcDdEeFfGg") expected_args = { "headers": {"Authorization": "Macaroon secret-token"}, "json": None, "method": "get", "params": None, "url": ( "https://1.2.3.4/v1" "/accounts/aAaBbCcDdEeFfGg/customer-info/stripe" ), } self.assertEqual(response, json_customer_info) self.assertEqual(session.request_kwargs, expected_args)
def test_convert_response_returns_list_of_subscriptions(self): json_subscriptions = get_fixture("subscriptions") session = Session( response=Response( status_code=200, content={"subscriptions": json_subscriptions}, ) ) client = make_client(session, convert_response=True) response = client.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.assertIsInstance(response, List) for item in response: self.assertIsInstance(item, Subscription) self.assertEqual(session.request_kwargs, expected_args)
def test_success(self): json_subscriptions = get_fixture("subscriptions") session = Session( response=Response( status_code=200, content={"subscriptions": json_subscriptions}, ) ) client = make_client(session) response = client.get_account_subscriptions( account_id="aABbCcdD", 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" "/marketplace/canonical-ua/subscriptions" ), } self.assertEqual(response, json_subscriptions) self.assertEqual(session.request_kwargs, expected_args)
def test_convert_response_returns_list_of_contracts(self): json_contracts = get_fixture("contracts") session = Session( response=Response( status_code=200, content={"contracts": json_contracts}, ) ) client = make_client(session, convert_response=True) response = client.get_account_contracts("aAbBcCdD") expected_args = { "headers": {"Authorization": "Macaroon secret-token"}, "json": None, "method": "get", "params": None, "url": ( "https://1.2.3.4/v1/accounts/aAbBcCdD/contracts" "?productTags=ua&productTags=classic&productTags=pro" ), } self.assertIsInstance(response, List) for item in response: self.assertIsInstance(item, Contract) self.assertEqual(session.request_kwargs, expected_args)
def test_success(self): json_ensure_account = get_fixture("ensured-account") session = Session( response=Response( status_code=200, content=json_ensure_account, ) ) client = make_client(session) response = client.ensure_purchase_account( email="email@url", account_name="Joe Doe", payment_method_id="pm_abcdef", country="GB", ) expected_args = { "headers": {"Authorization": "Macaroon secret-token"}, "json": { "address": {"country": "GB"}, "defaultPaymentMethod": {"Id": "pm_abcdef"}, "email": "email@url", "name": "Joe Doe", }, "method": "post", "params": None, "url": "https://1.2.3.4/v1/purchase-account", } self.assertEqual(response, json_ensure_account) self.assertEqual(session.request_kwargs, expected_args)
def test_success(self): json_subscription = get_fixture("subscription-auto-renewal") session = Session( response=Response( status_code=200, content=json_subscription, ) ) client = make_client(session) response = client.get_subscription_auto_renewal( subscription_id="sAaBbCcDdEeFfGg" ) expected_args = { "headers": {"Authorization": "Macaroon secret-token"}, "json": None, "method": "get", "params": None, "url": ( "https://1.2.3.4/v1" "/subscription/sAaBbCcDdEeFfGg/auto-renewal" ), } self.assertEqual(response, json_subscription) self.assertEqual(session.request_kwargs, expected_args)
def test_account_purchases_filters_none_values(self): json_purchases = get_fixture("purchases") session = Session( response=Response( status_code=200, content=json_purchases, ) ) client = make_client(session) response = client.get_account_purchases( account_id="aABbCcdD", filters=None, ) expected_args = { "headers": {"Authorization": "Macaroon secret-token"}, "json": None, "method": "get", "params": None, "url": "https://1.2.3.4/v1/accounts/aABbCcdD/purchases", } self.assertEqual(response, json_purchases.get("purchases")) 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( 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_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_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, ), Entitlement( type="entitlement-type-2", support_level=None, enabled_by_default=False, ), Entitlement( type="support", support_level="advanced", enabled_by_default=False, ), ] self.assertIsInstance(parsed_entitlements, List) self.assertEqual(to_dict(expectation), to_dict(parsed_entitlements))
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_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_account(self): raw_account = get_fixture("account") parsed_account = parse_account(raw_account=raw_account) expectation = Account( id="aAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpP", name="Account Name", ) self.assertIsInstance(parsed_account, Account) self.assertEqual(to_dict(expectation), to_dict(parsed_account))
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_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_success(self): json_account = get_fixture("account") session = Session( response=Response( status_code=200, content=json_account, ) ) client = make_client(session) address = { "city": "Lone Done", "country": "GB", "line1": "Road Street", "postal_code": "111000", "state": "", } tax_id = {"type": "eu_vat", "value": "GB 123 1234 14"} response = client.put_customer_info( account_id="aAaBbCcDdEeFfGg", payment_method_id="pm_abcdef", address=address, name="Joe Doe", tax_id=tax_id, ) expected_args = { "headers": {"Authorization": "Macaroon secret-token"}, "json": { "defaultPaymentMethod": {"Id": "pm_abcdef"}, "paymentMethodID": "pm_abcdef", "address": address, "name": "Joe Doe", "taxID": tax_id, }, "method": "put", "params": None, "url": ( "https://1.2.3.4/v1" "/accounts/aAaBbCcDdEeFfGg/customer-info/stripe" ), } self.assertEqual(response, json_account) self.assertEqual(session.request_kwargs, expected_args)
def test_parse_accounts(self): raw_accounts = get_fixture("accounts") parsed_accounts = parse_accounts(raw_accounts=raw_accounts) expectation = [ Account( id="a123AbBcCdDeEfFgGhHiIjJkKlLmMnNoOpP", name="Free", ), Account( id="aAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpP", name="Account Name", ), ] self.assertIsInstance(parsed_accounts, List) self.assertEqual(to_dict(expectation), to_dict(parsed_accounts))
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): json_purchase = get_fixture("purchase") session = Session( response=Response( status_code=200, content=json_purchase, ) ) client = make_client(session) purchase_request = { "accountID": "aAaBbCcDdEeFfGg", "purchaseItems": [ { "productListingID": "lAaBbCcDdEeFfGg", "metric": "active-machines", "value": 5, } ], "previousPurchaseID": "pAaBbCcDdEeFfGg", } response = client.preview_purchase_from_marketplace( marketplace="canonical-ua", purchase_request=purchase_request, ) expected_args = { "headers": {"Authorization": "Macaroon secret-token"}, "json": purchase_request, "method": "post", "params": None, "url": ( "https://1.2.3.4/v1" "/marketplace/canonical-ua/purchase/preview" ), } self.assertEqual(response, json_purchase) self.assertEqual(session.request_kwargs, expected_args)
def test_convert_response(self): json_account = get_fixture("account") session = Session( Response( status_code=200, content=json_account, ) ) client = make_client(session, convert_response=True) response = client.get_purchase_account() expected_args = { "headers": {"Authorization": "Macaroon secret-token"}, "json": None, "method": "get", "params": None, "url": "https://1.2.3.4/v1/purchase-account", } self.assertIsInstance(response, Account) self.assertEqual(session.request_kwargs, expected_args)
def test_success(self): json_purchase = get_fixture("purchase") session = Session( response=Response( status_code=200, content=json_purchase, ) ) client = make_client(session) response = client.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(response, json_purchase) self.assertEqual(session.request_kwargs, expected_args)
def test_parse_purchase_contract_items(self): raw_contract_items = [get_fixture("contract-item-shop")] parsed_contract_items = parse_contract_items( raw_items=raw_contract_items) expectation = [ ContractItem( contract_id="cAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpP2", created_at="2014-02-01T10:00:00Z", start_date="2014-03-01T10:00:00Z", end_date="2015-03-01T00:00:00Z", reason="purchase_made", value=1, product_listing_id="lAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpP2", purchase_id="pAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpP2", trial_id=None, ), ] self.assertIsInstance(parsed_contract_items, List) self.assertEqual(to_dict(expectation), to_dict(parsed_contract_items))