Skip to content

Commit

Permalink
Provide correct CustomerSubscriptions object when the 'subscriptions'…
Browse files Browse the repository at this point in the history
… link is missing

Since the `CustomerSubscriptions` instance also provides access to creating a new
subscription, we need initialize it correctly at all times.
  • Loading branch information
Tom Hendrikx committed Jun 22, 2023
1 parent 29afa85 commit 45f5b82
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 1 deletion.
7 changes: 7 additions & 0 deletions mollie/api/objects/customer.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ def created_at(self):
@property
def subscriptions(self):
url = self._get_link("subscriptions")
if not url:
url = f"customers/{self.id}/subscriptions"

from ..resources import CustomerSubscriptions

Expand All @@ -59,3 +61,8 @@ def payments(self):
from ..resources import CustomerPayments

return CustomerPayments(self.client, self)

# Additional methods

def has_subscriptions(self):
return self._get_link("subscriptions") is not None
23 changes: 23 additions & 0 deletions tests/responses/customer_single_no_links.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"resource": "customer",
"id": "cst_8wmqcHMN4U",
"mode": "test",
"name": "Customer A",
"email": "customer@example.org",
"locale": "nl_NL",
"metadata": null,
"createdAt": "2018-04-06T13:23:21.0Z",
"_links": {
"self": {
"href": "https://api.mollie.com/v2/customers/cst_8wmqcHMN4U",
"type": "application/hal+json"
},
"mandates": null,
"subscriptions": null,
"payments": null,
"documentation": {
"href": "https://docs.mollie.com/reference/v2/customers-api/get-customer",
"type": "text/html"
}
}
}
20 changes: 20 additions & 0 deletions tests/responses/customer_subscriptions_list_empty.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"_embedded":{
"subscriptions":[

]
},
"count":0,
"_links":{
"documentation":{
"href":"https://docs.mollie.com/reference/v2/subscriptions-api/list-subscriptions",
"type":"text/html"
},
"self":{
"href":"https://api.mollie.com/v2/customers/cst_tJs32TE8qk/subscriptions?limit=50",
"type":"application/hal+json"
},
"previous":null,
"next":null
}
}
15 changes: 14 additions & 1 deletion tests/test_customer_subscriptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,19 @@ def test_list_customer_subscriptions(client, response):
assert_list_object(subscriptions, Subscription)


def test_list_customer_subscriptions_when_unavailable(client, response):
response.get(f"https://api.mollie.com/v2/customers/{CUSTOMER_ID}", "customer_single_no_links")
response.get(
f"https://api.mollie.com/v2/customers/{CUSTOMER_ID}/subscriptions",
"customer_subscriptions_list_empty",
)
customer = client.customers.get(CUSTOMER_ID)
assert customer.has_subscriptions() is False
# Now that we know that there are no subscriptions, we are still going to get them
subscriptions = customer.subscriptions.list()
assert_list_object(subscriptions, Subscription, 0)


def test_get_customer_subscription(client, response):
response.get(f"https://api.mollie.com/v2/customers/{CUSTOMER_ID}", "customer_single")
response.get(
Expand Down Expand Up @@ -142,7 +155,7 @@ def test_cancel_customer_subscription_invalid_id(client, response):

def test_create_customer_subscription(client, response):
"""Create a subscription with customer object."""
response.get(f"https://api.mollie.com/v2/customers/{CUSTOMER_ID}", "customer_single")
response.get(f"https://api.mollie.com/v2/customers/{CUSTOMER_ID}", "customer_single_no_links")
response.post(f"https://api.mollie.com/v2/customers/{CUSTOMER_ID}/subscriptions", "subscription_single")

data = {
Expand Down

0 comments on commit 45f5b82

Please sign in to comment.