Skip to content

Commit

Permalink
Merge pull request #363 from mollie/342-testmode-as-querystring-and-body
Browse files Browse the repository at this point in the history
Add testmode to data or params based on HTTP method
  • Loading branch information
geertjanvdenbosch authored Sep 3, 2024
2 parents 69ccca6 + 530b862 commit b28da78
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 6 deletions.
30 changes: 24 additions & 6 deletions mollie/api/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,13 +163,17 @@ def _format_request_data(
path: str,
data: Optional[Dict[str, Any]],
params: Optional[Dict[str, Any]],
http_method: str,
) -> Tuple[str, str, Optional[Dict[str, Any]]]:
if path.startswith(f"{self.api_endpoint}/{self.api_version}"):
url = path
else:
url = f"{self.api_endpoint}/{self.api_version}/{path}"

payload = ""

data, params = self._get_testmode(data, params, http_method)

if data is not None:
try:
payload = json.dumps(data)
Expand All @@ -178,10 +182,6 @@ def _format_request_data(

if params is None:
params = {}
if self.testmode and "testmode" not in params:
if not (self.api_key.startswith("access_") or hasattr(self, "_oauth_client")):
raise RequestSetupError("Configuring testmode only works with access_token or OAuth authorization")
params["testmode"] = "true"

querystring = generate_querystring(params)
if querystring:
Expand All @@ -190,6 +190,24 @@ def _format_request_data(

return url, payload, params

def _get_testmode(self, data, params, http_method):
if self.testmode or (params and "testmode" in params):
if not (self.api_key.startswith("access_") or hasattr(self, "_oauth_client")):
raise RequestSetupError("Configuring testmode only works with access_token or OAuth authorization")

# Add to params if we're dealing with a GET request, for any other request add to data.
# If testmode is passed in the params, we're always overriding self.testmode. If
# self.testmode is True, simply pass in "true".
if http_method == "GET":
params["testmode"] = params.get("testmode") or "true"
elif not data or "testmode" not in data:
data["testmode"] = params.get("testmode") or "true"

# Delete from the params since it's not a valid parameter when the request is not GET
params.pop("testmode", None)

return data, params

def _perform_http_call_apikey(
self,
http_method: str,
Expand All @@ -206,7 +224,7 @@ def _perform_http_call_apikey(
self._client.verify = True
self._setup_retry()

url, payload, params = self._format_request_data(path, data, params)
url, payload, params = self._format_request_data(path, data, params, http_method)
try:
headers = {
"Accept": "application/json",
Expand Down Expand Up @@ -240,7 +258,7 @@ def _perform_http_call_oauth(
params: Optional[Dict[str, Any]] = None,
idempotency_key: str = "",
) -> requests.Response:
url, payload, params = self._format_request_data(path, data, params)
url, payload, params = self._format_request_data(path, data, params, http_method)
try:
headers = {
"Accept": "application/json",
Expand Down
16 changes: 16 additions & 0 deletions tests/test_api_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -530,3 +530,19 @@ def test_update_customer_bad_request(client, response):
)

assert exc.value.idempotency_key == "test_idempotency_key"


@pytest.mark.parametrize(
"testmode,params,http_method,expected",
[
(True, {}, "GET", ({}, {"testmode": "true"})),
(True, {}, "POST", ({"testmode": "true"}, {})),
(False, {"testmode": "true"}, "GET", ({}, {"testmode": "true"})),
(False, {"testmode": "true"}, "POST", ({"testmode": "true"}, {})),
(False, {"invalid": "something"}, "POST", ({}, {"invalid": "something"})),
],
)
def test__get_testmode_sets_data_or_params_correctly(oauth_client, testmode, params, http_method, expected):
oauth_client.testmode = testmode
return_value = oauth_client._get_testmode({}, params, http_method)
assert return_value == expected

0 comments on commit b28da78

Please sign in to comment.