Skip to content

Commit e0d553a

Browse files
Merge pull request #331 from mollie/330-identifier-regular-expression-syntax
Refactor regular expressions that extract identifiers from URLs
2 parents 156ee60 + 3e9f660 commit e0d553a

File tree

5 files changed

+53
-3
lines changed

5 files changed

+53
-3
lines changed

mollie/api/objects/chargeback.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ def settlement_id(self):
4949
if not url:
5050
return None
5151

52-
match = re.findall(r"/settlements/(stl_\w+)$", url)
52+
match = re.findall(r"/settlements/(stl_[^/]+)/?$", url)
5353
if match:
5454
return match[0]
5555

mollie/api/objects/subscription.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,8 @@ def customer_id(self):
120120
option is to extract it from the link.
121121
"""
122122
url = self._get_link("customer")
123-
matches = re.findall(r"/customers/(cst_\w+)", url)
123+
124+
matches = re.findall(r"/customers/(cst_[^/]+)/?$", url)
124125
if matches:
125126
return matches[0]
126127

mollie/api/resources/base.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ def perform_api_call(
4444
params: Optional[Dict[str, Any]] = None,
4545
idempotency_key: str = "",
4646
) -> Dict[str, Any]:
47-
4847
resp = self.client.perform_http_call(http_method, path, data, params, idempotency_key)
4948
if "application/hal+json" in resp.headers.get("Content-Type", ""):
5049
# set the content type according to the media type definition

tests/test_customer_subscriptions.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ def test_get_customer_subscription(client, response):
3535

3636
customer = client.customers.get(CUSTOMER_ID)
3737
subscription = customer.subscriptions.get(SUBSCRIPTION_ID)
38+
assert isinstance(subscription, Subscription)
3839
assert subscription.resource == "subscription"
3940
assert subscription.id == SUBSCRIPTION_ID
4041
assert subscription.mode == "live"
@@ -53,6 +54,7 @@ def test_get_customer_subscription(client, response):
5354
assert subscription.canceled_at is None
5455
assert subscription.metadata == {"order_id": 1337}
5556
assert subscription.application_fee is None
57+
assert subscription.customer_id == CUSTOMER_ID
5658
assert subscription.is_active() is True
5759
assert subscription.is_suspended() is False
5860
assert subscription.is_pending() is False
@@ -84,6 +86,29 @@ def test_customer_subscription_get_related_customer(client, response):
8486
assert related_customer.id == CUSTOMER_ID == customer.id
8587

8688

89+
@pytest.mark.parametrize(
90+
"url_customer_id, actual_customer_id",
91+
[
92+
(CUSTOMER_ID, CUSTOMER_ID),
93+
("cst_8.mqcHMN4U", "cst_8.mqcHMN4U"),
94+
("cst_8.mqcHMN4U/", "cst_8.mqcHMN4U"), # trailing slah in URL
95+
],
96+
)
97+
def test_customer_subscription_get_related_customer_id_syntax(url_customer_id, actual_customer_id, client):
98+
data = {
99+
"resource": "subscription",
100+
"id": "sub_rVKGtNd6s3",
101+
"_links": {
102+
"customer": {
103+
"href": f"https://api.mollie.com/v2/customers/{url_customer_id}",
104+
"type": "application/hal+json",
105+
},
106+
},
107+
}
108+
subscription = Subscription(data, client)
109+
assert subscription.customer_id == actual_customer_id
110+
111+
87112
def test_customer_subscription_get_related_profile(client, response):
88113
response.get(f"https://api.mollie.com/v2/customers/{CUSTOMER_ID}", "customer_single")
89114
response.get(

tests/test_payment_chargebacks.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ def test_get_payment_chargeback(client, response):
3737
assert chargeback.reason is None
3838
assert chargeback.reversed_at == "2018-03-14T17:00:55.0Z"
3939
assert chargeback.payment_id == PAYMENT_ID
40+
assert chargeback.settlement_id == SETTLEMENT_ID
4041

4142

4243
def test_get_payment_chargeback_invalid_id(client, response):
@@ -69,3 +70,27 @@ def test_payment_chargeback_get_related_settlement(client, response):
6970
related_settlement = chargeback.get_settlement()
7071
assert isinstance(related_settlement, Settlement)
7172
assert related_settlement.id == SETTLEMENT_ID
73+
74+
75+
@pytest.mark.parametrize(
76+
"url_settlement_id, actual_settlement_id",
77+
[
78+
(SETTLEMENT_ID, SETTLEMENT_ID),
79+
("stl_j.k30akdN", "stl_j.k30akdN"),
80+
("stl_j.k30akdN/", "stl_j.k30akdN"), # trailing slash in URL
81+
],
82+
)
83+
def test_payment_chargeback_get_related_settlement_id_syntax(url_settlement_id, actual_settlement_id, client):
84+
"""Identifiers may have unexpected syntax."""
85+
data = {
86+
"resource": "chargeback",
87+
"id": "chb_n9z0tp",
88+
"_links": {
89+
"settlement": {
90+
"href": f"https://api.mollie.com/v2/settlements/{url_settlement_id}",
91+
"type": "application/hal+json",
92+
},
93+
},
94+
}
95+
chargeback = Chargeback(data, client)
96+
assert chargeback.settlement_id == actual_settlement_id

0 commit comments

Comments
 (0)