def test_oauth_client_will_refresh_token_automatically(mocker, oauth_token, response): """Initializing the client with an expired token will trigger a token refresh automatically.""" # expire the token: set expiration time in the past. oauth_token["expires_at"] = time.time() - 5 set_token_mock = mocker.Mock() client = Client() client.setup_oauth( client_id="client_id", client_secret="client_secret", redirect_uri="https://example.com/callback", scope=("organizations.read", ), token=oauth_token, set_token=set_token_mock, ) # setup two request mocks: the token refresh and the actual data request response.post("https://api.mollie.com/oauth2/tokens", "token_single") response.get("https://api.mollie.com/v2/organizations/me", "organization_current") organization = client.organizations.get('me') assert isinstance(organization, Organization), "Unexpected result from request." assert response.assert_all_requests_are_fired, "Not all expected requests have been performed." # verify handling of the new token set_token_mock.assert_called_once() args, kwargs = set_token_mock.call_args assert isinstance(args[0], dict), "set_token() did not receive a dictionary."
def oauth_client(oauth_token): """Setup a Mollie API client with initialized OAuth2 authentication.""" client_id = "app_nvQQ4mGHqprcfFFqpnmbOgUs" client_secret = "2Tuc4qk8U6kCA8qBV3Fb2wwceDDfeRebDQpbOgUs" redirect_uri = "https://example.com/callback" scope = ("organizations.read", ) def set_token(x): logger.info("Storing token: %s", x) client = Client() client.setup_oauth( client_id=client_id, client_secret=client_secret, redirect_uri=redirect_uri, scope=scope, token=oauth_token, set_token=set_token, ) assert client._oauth_client.authorized is True return client