Skip to content

Commit 7a42d2f

Browse files
author
Tom Hendrikx
committed
Provide correct CustomerSubscriptions object when the 'subscriptions' link is missing
Since the `CustomerSubscriptions` instance also provides access to creating new subscriptions, we need to initialize it correctly at all times.
1 parent aa78086 commit 7a42d2f

File tree

4 files changed

+63
-4
lines changed

4 files changed

+63
-4
lines changed

mollie/api/objects/customer.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,9 @@ def created_at(self):
4242

4343
@property
4444
def subscriptions(self):
45-
url = self._get_link("subscriptions")
46-
4745
from ..resources import CustomerSubscriptions
4846

49-
return CustomerSubscriptions(self.client, resource_path=url)
47+
return CustomerSubscriptions(self.client, resource_path=f"customers/{self.id}/subscriptions")
5048

5149
@property
5250
def mandates(self):
@@ -59,3 +57,8 @@ def payments(self):
5957
from ..resources import CustomerPayments
6058

6159
return CustomerPayments(self.client, self)
60+
61+
# Additional methods
62+
63+
def has_subscriptions(self):
64+
return self._get_link("subscriptions") is not None
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"resource": "customer",
3+
"id": "cst_8wmqcHMN4U",
4+
"mode": "test",
5+
"name": "Customer A",
6+
"email": "customer@example.org",
7+
"locale": "nl_NL",
8+
"metadata": null,
9+
"createdAt": "2018-04-06T13:23:21.0Z",
10+
"_links": {
11+
"self": {
12+
"href": "https://api.mollie.com/v2/customers/cst_8wmqcHMN4U",
13+
"type": "application/hal+json"
14+
},
15+
"mandates": null,
16+
"subscriptions": null,
17+
"payments": null,
18+
"documentation": {
19+
"href": "https://docs.mollie.com/reference/v2/customers-api/get-customer",
20+
"type": "text/html"
21+
}
22+
}
23+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"_embedded":{
3+
"subscriptions":[
4+
5+
]
6+
},
7+
"count":0,
8+
"_links":{
9+
"documentation":{
10+
"href":"https://docs.mollie.com/reference/v2/subscriptions-api/list-subscriptions",
11+
"type":"text/html"
12+
},
13+
"self":{
14+
"href":"https://api.mollie.com/v2/customers/cst_tJs32TE8qk/subscriptions?limit=50",
15+
"type":"application/hal+json"
16+
},
17+
"previous":null,
18+
"next":null
19+
}
20+
}

tests/test_customer_subscriptions.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,19 @@ def test_list_customer_subscriptions(client, response):
2626
assert_list_object(subscriptions, Subscription)
2727

2828

29+
def test_list_customer_subscriptions_when_unavailable(client, response):
30+
response.get(f"https://api.mollie.com/v2/customers/{CUSTOMER_ID}", "customer_single_no_links")
31+
response.get(
32+
f"https://api.mollie.com/v2/customers/{CUSTOMER_ID}/subscriptions",
33+
"customer_subscriptions_list_empty",
34+
)
35+
customer = client.customers.get(CUSTOMER_ID)
36+
assert customer.has_subscriptions() is False
37+
# Now that we know that there are no subscriptions, we are still going to get them
38+
subscriptions = customer.subscriptions.list()
39+
assert_list_object(subscriptions, Subscription, 0)
40+
41+
2942
def test_get_customer_subscription(client, response):
3043
response.get(f"https://api.mollie.com/v2/customers/{CUSTOMER_ID}", "customer_single")
3144
response.get(
@@ -142,7 +155,7 @@ def test_cancel_customer_subscription_invalid_id(client, response):
142155

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

148161
data = {

0 commit comments

Comments
 (0)