Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions chartmogul/api/account.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,13 @@ class Account(Resource):
_path = "/account"

class _Schema(Schema):
id = fields.String(allow_none=True)
name = fields.String()
currency = fields.String()
time_zone = fields.String()
week_start_on = fields.String()
churn_recognition = fields.String(allow_none=True)
churn_when_zero_mrr = fields.String(allow_none=True)

@post_load
def make(self, data, **kwargs):
Expand Down
3 changes: 3 additions & 0 deletions chartmogul/api/invoice.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class _Schema(Schema):
account_code = fields.String(allow_none=True)
description = fields.String(allow_none=True)
event_order = fields.Int(allow_none=True)
errors = fields.Dict(allow_none=True)

@post_load
def make(self, data, **kwargs):
Expand Down Expand Up @@ -97,3 +98,5 @@ def all(cls, config, **kwargs):
"/data_sources{/data_source_uuid}/customers{/customer_uuid}/invoices",
)
Invoice.retrieve = Invoice._method("retrieve", "get", "/invoices{/uuid}")
Invoice.update_status = Invoice._method("modify", "patch", "/invoices{/uuid}")
Invoice.disable = Invoice._method("patch", "patch", "/invoices{/uuid}/disable")
44 changes: 44 additions & 0 deletions chartmogul/api/subscription_event.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,47 @@ def make(self, data, **kwargs):
SubscriptionEvent.modify_with_params = SubscriptionEvent._method(
"modify_with_params", "patch", "/subscription_events"
)


@classmethod
def _destroy(cls, config, **kwargs):
"""Accept flat params and wrap in subscription_event envelope for the API."""
data = kwargs.get("data", {})
if "subscription_event" not in data:
data = {"subscription_event": data}
return cls.destroy_with_params(config, data=data)


@classmethod
def _modify(cls, config, **kwargs):
"""Accept flat params and wrap in subscription_event envelope for the API."""
data = kwargs.get("data", {})
if "subscription_event" not in data:
data = {"subscription_event": data}
return cls.modify_with_params(config, data=data)


@classmethod
def _disable(cls, config, **kwargs):
"""Disable a subscription event by setting disabled to true."""
data = kwargs.get("data", {})
data["disabled"] = True
if "subscription_event" not in data:
data = {"subscription_event": data}
return cls.modify_with_params(config, data=data)


@classmethod
def _enable(cls, config, **kwargs):
"""Enable a subscription event by setting disabled to false."""
data = kwargs.get("data", {})
data["disabled"] = False
if "subscription_event" not in data:
data = {"subscription_event": data}
return cls.modify_with_params(config, data=data)


SubscriptionEvent.destroy = _destroy
SubscriptionEvent.modify = _modify
SubscriptionEvent.disable = _disable
SubscriptionEvent.enable = _enable
1 change: 1 addition & 0 deletions chartmogul/api/transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class _Schema(Schema):
result = fields.String()
amount_in_cents = fields.Int(allow_none=True)
transaction_fees_in_cents = fields.Int(allow_none=True)
errors = fields.Dict(allow_none=True)

@post_load
def make(self, data, **kwargs):
Expand Down
107 changes: 107 additions & 0 deletions test/api/test_account.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,24 @@
"week_start_on": "sunday",
}

jsonResponseWithId = {
"id": "acct_a1b2c3d4",
"name": "Example Test Company",
"currency": "EUR",
"time_zone": "Europe/Berlin",
"week_start_on": "sunday",
}

jsonResponseWithInclude = {
"id": "acct_a1b2c3d4",
"name": "Example Test Company",
"currency": "EUR",
"time_zone": "Europe/Berlin",
"week_start_on": "sunday",
"churn_recognition": "immediate",
"churn_when_zero_mrr": "ignore",
}


class AccountTestCase(unittest.TestCase):
"""
Expand All @@ -35,3 +53,92 @@ def test_retrieve(self, mock_requests):
self.assertEqual(account.currency, "EUR")
self.assertEqual(account.time_zone, "Europe/Berlin")
self.assertEqual(account.week_start_on, "sunday")

@requests_mock.mock()
def test_retrieve_with_id(self, mock_requests):
mock_requests.register_uri(
"GET",
"https://api.chartmogul.com/v1/account",
request_headers={"Authorization": "Basic dG9rZW46"},
status_code=200,
json=jsonResponseWithId,
)

config = Config("token")
account = Account.retrieve(config).get()
self.assertTrue(isinstance(account, Account))
self.assertEqual(account.id, "acct_a1b2c3d4")

@requests_mock.mock()
def test_retrieve_with_include(self, mock_requests):
mock_requests.register_uri(
"GET",
"https://api.chartmogul.com/v1/account?include=churn_recognition,churn_when_zero_mrr",
request_headers={"Authorization": "Basic dG9rZW46"},
headers={"Content-Type": "application/json"},
status_code=200,
json=jsonResponseWithInclude,
)

config = Config("token")
account = Account.retrieve(
config,
include="churn_recognition,churn_when_zero_mrr"
).get()
self.assertTrue(isinstance(account, Account))
self.assertEqual(account.id, "acct_a1b2c3d4")
self.assertEqual(account.churn_recognition, "immediate")
self.assertEqual(account.churn_when_zero_mrr, "ignore")
self.assertEqual(
mock_requests.last_request.qs,
{"include": ["churn_recognition,churn_when_zero_mrr"]},
)

@requests_mock.mock()
def test_retrieve_without_id_field(self, mock_requests):
"""Old API responses without id field should not break deserialization."""
mock_requests.register_uri(
"GET",
"https://api.chartmogul.com/v1/account",
request_headers={"Authorization": "Basic dG9rZW46"},
status_code=200,
json=jsonResponse,
)

config = Config("token")
account = Account.retrieve(config).get()
self.assertTrue(isinstance(account, Account))
self.assertFalse(hasattr(account, "id"))

@requests_mock.mock()
def test_retrieve_with_single_include(self, mock_requests):
singleIncludeResponse = {
"id": "acct_a1b2c3d4",
"name": "Example Test Company",
"currency": "EUR",
"time_zone": "Europe/Berlin",
"week_start_on": "sunday",
"churn_recognition": "immediate",
}

mock_requests.register_uri(
"GET",
"https://api.chartmogul.com/v1/account?include=churn_recognition",
request_headers={"Authorization": "Basic dG9rZW46"},
headers={"Content-Type": "application/json"},
status_code=200,
json=singleIncludeResponse,
)

config = Config("token")
account = Account.retrieve(
config,
include="churn_recognition"
).get()
self.assertTrue(isinstance(account, Account))
self.assertEqual(account.churn_recognition, "immediate")
self.assertFalse(hasattr(account, "churn_when_zero_mrr"))
self.assertEqual(
mock_requests.last_request.qs,
{"include": ["churn_recognition"]},
)
Loading
Loading