Skip to content

Commit

Permalink
Allow client-wide configuration of the testmode variable
Browse files Browse the repository at this point in the history
  • Loading branch information
Tom Hendrikx committed Dec 27, 2022
1 parent c186b80 commit c609899
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
11 changes: 11 additions & 0 deletions mollie/api/client.py
Expand Up @@ -53,6 +53,7 @@ class Client(object):
client_id: str = ""
client_secret: str = ""
set_token: Callable[[dict], None]
testmode: bool = False

@staticmethod
def validate_api_endpoint(api_endpoint: str) -> str:
Expand Down Expand Up @@ -126,6 +127,9 @@ def set_access_token(self, access_token: str) -> None:
def set_timeout(self, timeout: Union[int, Tuple[int, int]]) -> None:
self.timeout = timeout

def set_testmode(self, testmode: bool) -> None:
self.testmode = testmode

def set_user_agent_component(self, key: str, value: str, sanitize: bool = True) -> None:
"""Add or replace new user-agent component strings.
Expand Down Expand Up @@ -167,6 +171,13 @@ def _format_request_data(
except TypeError as err:
raise RequestSetupError(f"Error encoding data into JSON: {err}.")

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:
url += "?" + querystring
Expand Down
43 changes: 43 additions & 0 deletions tests/test_api_client.py
Expand Up @@ -430,3 +430,46 @@ def test_unauthorized_oauth_client_should_return_authorization_url(mocker, respo
assert authorization_url.startswith(
client.OAUTH_AUTHORIZATION_URL
), "A client without initial token should return a correct authorization url"


def test_enable_testmode_globally_access_token(response):
mocked_request = response.get(
"https://api.mollie.com/v2/methods", "methods_list", match=[matchers.query_string_matcher("testmode=true")]
)

client = Client()
client.set_access_token("access_123")
client.set_testmode(True)

client.methods.list()
assert mocked_request.call_count == 1


def test_enable_testmode_globally_oauth(response, oauth_client):
mocked_request = response.get(
"https://api.mollie.com/v2/methods", "methods_list", match=[matchers.query_string_matcher("testmode=true")]
)

oauth_client.set_testmode(True)

oauth_client.methods.list()
assert mocked_request.call_count == 1


def test_override_testmode(response, oauth_client):
mocked_request = response.get(
"https://api.mollie.com/v2/methods", "methods_list", match=[matchers.query_string_matcher("testmode=false")]
)

oauth_client.set_testmode(True)

oauth_client.methods.list(testmode="false")
assert mocked_request.call_count == 1


def test_testmode_for_apikey_access_raises_error(client, response):
client.set_testmode(True)

with pytest.raises(RequestSetupError) as excinfo:
client.methods.list()
assert str(excinfo.value) == "Configuring testmode only works with access_token or OAuth authorization"

0 comments on commit c609899

Please sign in to comment.