Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions src/pymonzo/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,6 @@
MONZO_CLIENT_ID_ENV = 'MONZO_CLIENT_ID'
MONZO_CLIENT_SECRET_ENV = 'MONZO_CLIENT_SECRET'

REDIRECT_URI = os.getenv(
'PYMONZO_REDIRECT_URI',
default='https://github.com/pawelad/pymonzo',
)

TOKEN_FILE_NAME = os.getenv(
'PYMONZO_TOKEN_FILE_NAME',
default='.pymonzo-token',
Expand Down
7 changes: 5 additions & 2 deletions src/pymonzo/monzo_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class MonzoAPI(CommonMixin):
_cached_pots = None

def __init__(self, access_token=None, client_id=None, client_secret=None,
auth_code=None):
auth_code=None, redirect_url="https://github.com/pawelad/pymonzo"):
"""
We need Monzo access token to work with the API, which we try to get
in multiple ways detailed below. Basically you need to either pass
Expand All @@ -59,6 +59,9 @@ def __init__(self, access_token=None, client_id=None, client_secret=None,
:param auth_code: your Monzo OAuth 2 auth code
:type auth_code: str
"""

self._redirect_url = redirect_url

# Lets get the access token from:
# a) explicitly passed 'access_token'
if access_token:
Expand Down Expand Up @@ -146,7 +149,7 @@ def _get_oauth_token(self):

oauth = OAuth2Session(
client_id=self._client_id,
redirect_uri=config.REDIRECT_URI,
redirect_uri=self._redirect_url,
)

token = oauth.fetch_token(
Expand Down
28 changes: 27 additions & 1 deletion tests/test_monzo_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ def test_class_initialization(self, monkeypatch, mocker):
client_id = 'explicit_client_id'
client_secret = 'explicit_client_secret'
auth_code = 'explicit_auth_code'
redirect_url = 'explicit_redirect_url'
monkeypatch.setenv(config.MONZO_ACCESS_TOKEN_ENV, 'env_access_token')
monkeypatch.setenv(config.MONZO_CLIENT_ID_ENV, 'env_client_id')
monkeypatch.setenv(config.MONZO_CLIENT_SECRET_ENV, 'env_client_secret')
Expand All @@ -71,9 +72,11 @@ def test_class_initialization(self, monkeypatch, mocker):
monzo = MonzoAPI(
access_token=access_token, client_id=client_id,
client_secret=client_secret, auth_code=auth_code,
redirect_url=redirect_url
)

assert monzo._access_token == 'explicit_access_token'
assert monzo._redirect_url == redirect_url
assert monzo._client_id is None
assert monzo._client_secret is None
assert monzo._auth_code is None
Expand Down Expand Up @@ -223,7 +226,30 @@ def test_class_get_oauth_token_method(self, mocker, mocked_monzo):

mocked_oauth2_session.assert_called_once_with(
client_id=mocked_monzo._client_id,
redirect_uri=config.REDIRECT_URI,
redirect_uri="https://github.com/pawelad/pymonzo",
)
mocked_fetch_token.assert_called_once_with(
token_url=urljoin(mocked_monzo.api_url, '/oauth2/token'),
code=mocked_monzo._auth_code,
client_secret=mocked_monzo._client_secret,
)

def test_class_get_oauth_token_custom_redirect_method(self, mocker, mocked_monzo):
"""Test class `_get_oauth_token` method"""
mocked_fetch_token = mocker.MagicMock()
mocked_oauth2_session = mocker.patch('pymonzo.monzo_api.OAuth2Session')
mocked_oauth2_session.return_value.fetch_token = mocked_fetch_token

redirect_url = "https://example.com/"
mocked_monzo._redirect_url = redirect_url

token = mocked_monzo._get_oauth_token()

assert token == mocked_fetch_token.return_value

mocked_oauth2_session.assert_called_once_with(
client_id=mocked_monzo._client_id,
redirect_uri=redirect_url,
)
mocked_fetch_token.assert_called_once_with(
token_url=urljoin(mocked_monzo.api_url, '/oauth2/token'),
Expand Down