Skip to content

Commit

Permalink
fix tests for oauth
Browse files Browse the repository at this point in the history
  • Loading branch information
zzacharo committed May 15, 2020
1 parent 69b5187 commit 20dbf79
Show file tree
Hide file tree
Showing 22 changed files with 458 additions and 258 deletions.
6 changes: 3 additions & 3 deletions examples/cern_app_rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@

from invenio_oauthclient import InvenioOAuthClientREST
from invenio_oauthclient.contrib import cern
from invenio_oauthclient.views.client import blueprint as blueprint_client
from invenio_oauthclient.views.client import rest_blueprint as blueprint_client

from invenio_oauthclient._compat import monkey_patch_werkzeug # noqa isort:skip
monkey_patch_werkzeug() # noqa isort:skip
Expand All @@ -116,7 +116,7 @@

app.config.update(
SQLALCHEMY_DATABASE_URI=os.environ.get(
'SQLALCHEMY_DATABASE_URI', 'sqlite:///cern_app.db'
'SQLALCHEMY_DATABASE_URI', 'sqlite:///cern_app_rest.db'
),
OAUTHCLIENT_REST_REMOTE_APPS=dict(
cern=cern.REMOTE_REST_APP
Expand Down Expand Up @@ -151,7 +151,7 @@ def index():
def cern():
"""Home page: try to print user email or redirect to login with cern."""
if not current_user.is_authenticated:
return redirect(url_for('invenio_oauthclient.login',
return redirect(url_for('invenio_oauthclient.rest_login',
remote_app='cern'))

return 'hello {}'.format(current_user.email)
6 changes: 3 additions & 3 deletions examples/github_app_rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@

from invenio_oauthclient import InvenioOAuthClientREST
from invenio_oauthclient.contrib import github
from invenio_oauthclient.views.client import blueprint as blueprint_client
from invenio_oauthclient.views.client import rest_blueprint as blueprint_client

from invenio_oauthclient._compat import monkey_patch_werkzeug # noqa isort:skip
monkey_patch_werkzeug() # noqa isort:skip
Expand All @@ -106,7 +106,7 @@

app.config.update(
SQLALCHEMY_DATABASE_URI=os.environ.get(
'SQLALCHEMY_DATABASE_URI', 'sqlite:///github_app.db'
'SQLALCHEMY_DATABASE_URI', 'sqlite:///github_app_rest.db'
),
OAUTHCLIENT_REST_REMOTE_APPS=dict(
github=github.REMOTE_REST_APP,
Expand Down Expand Up @@ -146,6 +146,6 @@ def index():
def github():
"""Try to print user email or redirect to login with github."""
if not current_user.is_authenticated:
return redirect(url_for('invenio_oauthclient.login',
return redirect(url_for('invenio_oauthclient.rest_login',
remote_app='github'))
return 'hello {}'.format(current_user.email)
6 changes: 3 additions & 3 deletions examples/globus_app_rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@

from invenio_oauthclient import InvenioOAuthClientREST
from invenio_oauthclient.contrib import globus
from invenio_oauthclient.views.client import blueprint as blueprint_client
from invenio_oauthclient.views.client import rest_blueprint as blueprint_client

from invenio_oauthclient._compat import monkey_patch_werkzeug # noqa isort:skip
monkey_patch_werkzeug() # noqa isort:skip
Expand All @@ -97,7 +97,7 @@

app.config.update(
SQLALCHEMY_DATABASE_URI=os.environ.get(
'SQLALCHEMY_DATABASE_URI', 'sqlite:///globus_app.db'
'SQLALCHEMY_DATABASE_URI', 'sqlite:///globus_app_rest.db'
),
OAUTHCLIENT_REST_REMOTE_APPS=dict(
globus=globus.REMOTE_REST_APP,
Expand Down Expand Up @@ -137,6 +137,6 @@ def index():
def globus():
"""Try to print user email or redirect to login with globus."""
if not current_user.is_authenticated:
return redirect(url_for('invenio_oauthclient.login',
return redirect(url_for('invenio_oauthclient.rest_login',
remote_app='globus'))
return 'hello {}'.format(current_user.email)
6 changes: 3 additions & 3 deletions examples/orcid_app_rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@

from invenio_oauthclient import InvenioOAuthClientREST
from invenio_oauthclient.contrib import orcid
from invenio_oauthclient.views.client import blueprint as blueprint_client
from invenio_oauthclient.views.client import rest_blueprint as blueprint_client

from invenio_oauthclient._compat import monkey_patch_werkzeug # noqa isort:skip
monkey_patch_werkzeug() # noqa isort:skip
Expand All @@ -109,7 +109,7 @@
app.config.update(
SQLALCHEMY_ECHO=False,
SQLALCHEMY_DATABASE_URI=os.environ.get(
'SQLALCHEMY_DATABASE_URI', 'sqlite:///orcid_app.db'
'SQLALCHEMY_DATABASE_URI', 'sqlite:///orcid_app_rest.db'
),
OAUTHCLIENT_REST_REMOTE_APPS=dict(
orcid=orcid.REMOTE_SANDBOX_REST_APP,
Expand Down Expand Up @@ -148,6 +148,6 @@ def index():
def orcid():
"""Try to print user email or redirect to login with orcid."""
if not current_user.is_authenticated:
return redirect(url_for('invenio_oauthclient.login',
return redirect(url_for('invenio_oauthclient.rest_login',
remote_app='orcid'))
return 'hello {}'.format(current_user.email)
2 changes: 1 addition & 1 deletion invenio_oauthclient/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,4 +257,4 @@ class CustomOAuthRemoteApp(OAuthRemoteApp):
"""Configuration of default error redirect URL."""

OAUTHCLIENT_REST_DEFAULT_RESPONSE_HANDLER = None
"""Default REST response handler."""
"""Default REST response handler."""
5 changes: 2 additions & 3 deletions invenio_oauthclient/contrib/cern.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@
view='invenio_oauthclient.handlers.rest:signup_handler',
),
response_handler=(
'invenio_oauthclient.handlers.rest:default_response_handler'
'invenio_oauthclient.handlers.rest:default_remote_response_handler'
),
authorized_redirect_url='/',
disconnect_redirect_url='/',
Expand Down Expand Up @@ -422,8 +422,7 @@ def account_info(remote, resp):
return _account_info(remote, resp)
except OAuthCERNRejectedAccountError as e:
current_app.logger.warning(e.message, exc_info=True)
flash(_('CERN account not allowed.'),
category='danger')
flash(_('CERN account not allowed.'), category='danger')
return redirect('/')


Expand Down
4 changes: 2 additions & 2 deletions invenio_oauthclient/contrib/github.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@

from invenio_oauthclient.errors import OAuthResponseError
from invenio_oauthclient.handlers import authorized_signup_handler, \
oauth_resp_remote_error_handler
oauth_error_handler
from invenio_oauthclient.handlers.rest import response_handler
from invenio_oauthclient.models import RemoteAccount
from invenio_oauthclient.utils import oauth_link_external_id, \
Expand Down Expand Up @@ -123,7 +123,7 @@
view='invenio_oauthclient.handlers.rest:signup_handler',
),
response_handler=(
'invenio_oauthclient.handlers.rest:default_response_handler'
'invenio_oauthclient.handlers.rest:default_remote_response_handler'
),
authorized_redirect_url='/',
disconnect_redirect_url='/',
Expand Down
2 changes: 1 addition & 1 deletion invenio_oauthclient/contrib/globus.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@
view='invenio_oauthclient.handlers.rest:signup_handler',
),
response_handler=(
'invenio_oauthclient.handlers.rest:default_response_handler'
'invenio_oauthclient.handlers.rest:default_remote_response_handler'
),
authorized_redirect_url='/',
disconnect_redirect_url='/',
Expand Down
2 changes: 1 addition & 1 deletion invenio_oauthclient/contrib/orcid.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@
view='invenio_oauthclient.handlers.rest:signup_handler',
),
response_handler=(
'invenio_oauthclient.handlers.rest:default_response_handler'
'invenio_oauthclient.handlers.rest:default_remote_response_handler'
),
authorized_redirect_url='/',
disconnect_redirect_url='/',
Expand Down
2 changes: 1 addition & 1 deletion invenio_oauthclient/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,4 +106,4 @@ def __init__(self, location):


class OAuthRemoteNotFound(Exception):
"""Define exception for remote app not found."""
"""Define exception for remote app not found."""
36 changes: 19 additions & 17 deletions invenio_oauthclient/ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@

from __future__ import absolute_import, print_function

from functools import partial

from flask_login import user_logged_out

from . import config, handlers
Expand All @@ -36,7 +34,7 @@ def __init__(self, app, remote_app_config_key, default_disconnect_handler,
self.handlers = {}
self.disconnect_handlers = {}
self.signup_handlers = {}
self.response_handler = {}
self.remote_app_response_handler = {}

# Connect signal to remove access tokens on logout
user_logged_out.connect(handlers.oauth_logout_handler)
Expand All @@ -56,6 +54,9 @@ def __init__(self, app, remote_app_config_key, default_disconnect_handler,
def dummy_handler(remote, *args, **kargs):
pass

self.default_response_handler = default_response_handler or \
dummy_handler

for remote_app, conf in app.config[
remote_app_config_key].items():
# Prevent double creation problems
Expand All @@ -81,7 +82,8 @@ def dummy_handler(remote, *args, **kargs):
# Register authorized handler
self.handlers[remote_app] = handlers.authorized_handler(
handlers.make_handler(
conf.get('authorized_handler', default_authorized_handler),
conf.get(
'authorized_handler', default_authorized_handler),
remote
),
remote.authorized_response
Expand All @@ -93,16 +95,14 @@ def dummy_handler(remote, *args, **kargs):
remote,
with_response=False,
)

self.remote_app_response_handler[
remote_app] = handlers.make_handler(
conf.get('response_handler',
default_response_handler or dummy_handler),
remote,
with_response=False
)
remote_app] = obj_or_import_string(
conf.get(
'response_handler',
default_remote_app_response_handler or dummy_handler))

# Register sign-up handlers

signup_handler = conf.get('signup_handler', dict())
account_info_handler = handlers.make_handler(
signup_handler.get('info', dummy_handler),
Expand Down Expand Up @@ -179,16 +179,18 @@ def __init__(self, app=None):

def init_app(self, app):
"""Flask application initialization."""

_default_response_handler = obj_or_import_string(
app.config.get("OAUTHCLIENT_REST_DEFAULT_RESPONSE_HANDLER"),
handlers.rest.default_response_handler
)
_default_remote_handler = handlers.rest.default_remote_response_handler
state = _OAuthClientState(
app, 'OAUTHCLIENT_REST_REMOTE_APPS',
handlers.rest.disconnect_handler,
handlers.rest.authorized_default_handler,
handlers.rest.default_response_handler,
)
self.default_response_handler = obj_or_import_string(
app.config.get("OAUTHCLIENT_REST_DEFAULT_RESPONSE_HANDLER"),
partial(handlers.rest.default_response_handler, None)
default_remote_app_response_handler=_default_remote_handler,
default_response_handler=_default_response_handler
)

app.extensions['invenio-oauthclient'] = state
return state
3 changes: 2 additions & 1 deletion invenio_oauthclient/handlers/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
from .utils import get_session_next_url, response_token_setter, token_getter, \
token_session_key, token_setter


#
# Handlers
#
Expand Down Expand Up @@ -132,7 +133,7 @@ def base_disconnect_handler(remote, *args, **kwargs):
:returns: Redirect response.
"""
if not current_user.is_authenticated:
raise OAuthClientUnAuthorized()
raise OAuthClientUnAuthorized()

with db.session.begin_nested():
account = RemoteAccount.get(
Expand Down
Loading

0 comments on commit 20dbf79

Please sign in to comment.