Skip to content

Commit

Permalink
global: fix tests from pallets family upgrade
Browse files Browse the repository at this point in the history
Co-authored-by: Zacharias Zacharodimos <[email protected]>
  • Loading branch information
Pablo Panero and zzacharo committed May 16, 2022
1 parent 55a5880 commit 2b0869b
Show file tree
Hide file tree
Showing 12 changed files with 60 additions and 55 deletions.
7 changes: 5 additions & 2 deletions invenio_oauthclient/handlers/rest.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
# -*- coding: utf-8 -*-
#
# This file is part of Invenio.
# Copyright (C) 2015-2018 CERN.
# Copyright (C) 2015-2022 CERN.
#
# Invenio is free software; you can redistribute it and/or modify it
# under the terms of the MIT License; see LICENSE file for more details.

"""Handlers for customizing oauthclient endpoints."""

import json
from functools import partial, wraps

import json
from flask import abort, current_app, jsonify, make_response, redirect, \
render_template, request, url_for
from invenio_db import db
Expand Down Expand Up @@ -244,7 +246,8 @@ def signup_handler(remote, *args, **kwargs):
remote.name]
try:
form = create_csrf_disabled_registrationform(remote)
form = fill_form(form, request.json or {})
json_data = {} if not request.data else json.loads(request.data)
form = fill_form(form, json_data)
next_url = base_signup_handler(remote, form, *args, **kwargs)
if form.is_submitted():
response_payload = dict(
Expand Down
24 changes: 22 additions & 2 deletions run-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,31 @@ set -o nounset
cleanup() {
eval "$(docker-services-cli down --env)"
}
trap cleanup EXIT
# Check for arguments
# Note: "-k" would clash with "pytest"
keep_services=0
pytest_args=()
for arg in $@; do
# from the CLI args, filter out some known values and forward the rest to "pytest"
# note: we don't use "getopts" here b/c of some limitations (e.g. long options),
# which means that we can't combine short options (e.g. "./run-tests -Kk pattern")
case ${arg} in
-K|--keep-services)
keep_services=1
;;
*)
pytest_args+=( ${arg} )
;;
esac
done

if [[ ${keep_services} -eq 0 ]]; then
trap cleanup EXIT
fi

python -m check_manifest --ignore ".*-requirements.txt"
python -m sphinx.cmd.build -qnN docs docs/_build/html
eval "$(docker-services-cli up --db ${DB:-postgresql} --env)"
python -m pytest
python -m pytest ${pytest_args[@]+"${pytest_args[@]}"}
tests_exit_code=$?
exit "$tests_exit_code"
10 changes: 5 additions & 5 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,14 @@
'uritemplate.py>=0.2.0,<2.0',
],
'mysql': [
'invenio-db[mysql,versioning]>=1.0.11',
'invenio-db[mysql,versioning]>=1.0.14',
],
'orcid': [],
'postgresql': [
'invenio-db[postgresql,versioning]>=1.0.11',
'invenio-db[postgresql,versioning]>=1.0.14',
],
'sqlite': [
'invenio-db[versioning]>=1.0.11',
'invenio-db[versioning]>=1.0.14',
],
'tests': tests_require,
}
Expand All @@ -63,8 +63,8 @@
install_requires = [
'blinker>=1.4',
'Flask-OAuthlib>=0.9.6',
'invenio-accounts>=2.0.0.dev2',
'invenio-base>=1.2.5',
'invenio-accounts>=2.0.0.dev10',
'invenio-base>=1.2.11',
'invenio-i18n>=1.3.1',
'invenio-mail>=1.0.2',
'invenio-theme>=1.3.12',
Expand Down
4 changes: 2 additions & 2 deletions tests/test_contrib_cern.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ def test_account_setup(app, example_cern, models_fixture):
remote_app='cern', code='test',
state=get_state('cern')))
assert resp.status_code == 302
assert resp.location == ('http://localhost/account/settings/'
assert resp.location == ('/account/settings/'
'linkedaccounts/')
assert len(g.identity.provides) == 7

Expand Down Expand Up @@ -182,7 +182,7 @@ def test_authorized_reject(app):
error_description='User denied access',
state=get_state('cern')))
assert resp.status_code in (301, 302)
assert resp.location == 'http://localhost/'
assert resp.location == '/'
# Check message flash
assert session['_flashes'][0][0] == 'info'

Expand Down
4 changes: 2 additions & 2 deletions tests/test_contrib_cern_openid.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ def test_account_setup(app, example_cern_openid, models_fixture):
remote_app='cern_openid', code='test',
state=get_state('cern_openid')))
assert resp.status_code == 302
assert resp.location == ('http://localhost/account/settings/'
assert resp.location == ('/account/settings/'
'linkedaccounts/')
assert len(g.identity.provides) == 3

Expand Down Expand Up @@ -163,7 +163,7 @@ def test_authorized_reject(app):
error_description='User denied access',
state=get_state('cern_openid')))
assert resp.status_code in (301, 302)
assert resp.location == 'http://localhost/'
assert resp.location == '/'
# Check message flash
assert session['_flashes'][0][0] == 'info'

Expand Down
13 changes: 3 additions & 10 deletions tests/test_contrib_github.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,7 @@ def test_authorized_signup_valid_user(app, example_github):
remote_app='github', code='test',
state=_get_state()))
assert resp.status_code == 302
assert resp.location == ('http://localhost/account/settings/' +
'linkedaccounts/')
assert resp.location == '/account/settings/linkedaccounts/'

# Assert database state (Sign-up complete)
user = User.query.filter_by(email=example_email).one()
Expand Down Expand Up @@ -145,10 +144,7 @@ def test_authorized_signup_valid_user(app, example_github):
remote_app='github', code='test',
state=_get_state()))
assert resp.status_code == 302
assert resp.location == (
'http://localhost/' +
'account/settings/linkedaccounts/'
)
assert resp.location == '/account/settings/linkedaccounts/'

# check that exist only one account
user = User.query.filter_by(email=example_email).one()
Expand Down Expand Up @@ -181,7 +177,6 @@ def test_authorized_signup_username_already_exists(app, example_github, user):
state=_get_state()))
assert resp.status_code == 302
assert resp.location == (
'http://localhost' +
url_for('invenio_oauthclient.signup', remote_app='github')
)

Expand Down Expand Up @@ -246,9 +241,7 @@ def test_authorized_reject(app):
error_description='User denied access',
state=_get_state()))
assert resp.status_code in (301, 302)
assert resp.location == (
'http://localhost/'
)
assert resp.location == '/'
# Check message flash
assert session['_flashes'][0][0] == 'info'

Expand Down
1 change: 0 additions & 1 deletion tests/test_contrib_github_rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,6 @@ def test_authorized_signup_username_already_exists(
state=_get_state()))
assert resp.status_code == 302
assert resp.location == (
'http://localhost' +
url_for('invenio_oauthclient.rest_signup', remote_app='github')
)

Expand Down
12 changes: 3 additions & 9 deletions tests/test_contrib_globus.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,7 @@ def test_authorized_signup_valid_user(app, example_globus):
remote_app='globus', code='test',
state=_get_state()))
assert resp.status_code == 302
assert resp.location == ('http://localhost/account/settings/' +
'linkedaccounts/')
assert resp.location == '/account/settings/linkedaccounts/'

# Assert database state (Sign-up complete)
user = User.query.filter_by(email='[email protected]').one()
Expand Down Expand Up @@ -120,10 +119,7 @@ def test_authorized_signup_valid_user(app, example_globus):
remote_app='globus', code='test',
state=_get_state()))
assert resp.status_code == 302
assert resp.location == (
'http://localhost/' +
'account/settings/linkedaccounts/'
)
assert resp.location == '/account/settings/linkedaccounts/'

# check that exist only one account
user = User.query.filter_by(email='[email protected]').one()
Expand All @@ -140,9 +136,7 @@ def test_authorized_reject(app):
error_description='User denied access',
state=_get_state()))
assert resp.status_code in (301, 302)
assert resp.location == (
'http://localhost/'
)
assert resp.location == '/'
# Check message flash
assert session['_flashes'][0][0] == 'info'

Expand Down
9 changes: 4 additions & 5 deletions tests/test_contrib_keycloak.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,7 @@ def test_authorized_signup_valid_user(app_with_userprofiles,
# note: because we provided an e-mail address in 'info_handler',
# the user does not need to sign up
assert resp.status_code == 302
assert resp.location == ("http://localhost/"
"account/settings/linkedaccounts/")
assert resp.location == "/account/settings/linkedaccounts/"

# check that the user exists
user = User.query.filter_by(email=example_keycloak["email"]).one()
Expand Down Expand Up @@ -155,7 +154,8 @@ def test_authorized_signup_valid_user(app_with_userprofiles,
def test_authorized_reject(app, example_keycloak_token):
"""Test a rejected request."""
with app.test_client() as c:
c.get(url_for("invenio_oauthclient.login", remote_app="keycloak"))
breakpoint()
resp=c.get(url_for("invenio_oauthclient.login", remote_app="keycloak"))

resp = c.get(
url_for(
Expand All @@ -164,9 +164,8 @@ def test_authorized_reject(app, example_keycloak_token):
state=get_state("keycloak")
)
)

assert resp.status_code in (301, 302)
assert resp.location == "http://localhost/"
assert resp.location == "/"

# check message flash
assert session["_flashes"][0][0] == "info"
Expand Down
5 changes: 1 addition & 4 deletions tests/test_contrib_orcid.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ def test_authorized_signup(app_with_userprofiles, example_orcid, orcid_bio):
state=get_state('orcid')))
assert resp.status_code == 302
assert resp.location == (
'http://localhost' +
url_for('invenio_oauthclient.signup', remote_app='orcid')
)

Expand Down Expand Up @@ -171,9 +170,7 @@ def test_authorized_reject(app, example_orcid):
error_description='User denied access',
state=get_state('orcid')))
assert resp.status_code in (301, 302)
assert resp.location == (
'http://localhost/'
)
assert resp.location == '/'
# Check message flash
assert session['_flashes'][0][0] == 'info'

Expand Down
1 change: 0 additions & 1 deletion tests/test_contrib_orcid_rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ def test_authorized_signup(app_rest_with_userprofiles,
state=get_state('orcid')))
assert resp.status_code == 302
assert resp.location == (
'http://localhost' +
url_for('invenio_oauthclient.rest_signup', remote_app='orcid')
)

Expand Down
25 changes: 13 additions & 12 deletions tests/test_handlers_rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,41 +190,42 @@ def test_dummy_handler(remote_name, base_app):


@pytest.mark.parametrize('remote_name', REMOTE_APPS)
def test_response_handler(remote_name, app_rest):
def test_response_handler(remote_name, base_app):
"""Test response handler."""

def mock_response_handler(remote, url, payload):
return remote.name
# Force usage of dummy handlers
app_rest.config['OAUTHCLIENT_REST_REMOTE_APPS'][remote_name][
base_app.config['OAUTHCLIENT_REST_REMOTE_APPS'][remote_name][
'response_handler'] = mock_response_handler

# Initialize InvenioOAuth
FlaskOAuth(app_rest)
InvenioOAuthClientREST(app_rest)
app_rest.register_blueprint(rest_blueprint)
FlaskOAuth(base_app)
InvenioOAuthClientREST(base_app)
base_app.register_blueprint(rest_blueprint)

# Try to sign-up client
response = app_rest.test_client().get(
response = base_app.test_client().get(
url_for('invenio_oauthclient.rest_signup', remote_app=remote_name)
)
assert remote_name in str(response.data)


@pytest.mark.parametrize('remote', REMOTE_APPS, indirect=["remote"])
def test_response_handler_with_postmessage(remote, app_rest, models_fixture):
def test_response_handler_with_postmessage(remote, base_app):
"""Test response handler with postmessage."""

# Force usage of dummy handlers
app_rest.config['OAUTHCLIENT_REST_REMOTE_APPS'][remote.name][
base_app.config['OAUTHCLIENT_REST_REMOTE_APPS'][remote.name][
'response_handler'] = response_handler_postmessage

# Initialize InvenioOAuth
FlaskOAuth(app_rest)
InvenioOAuthClientREST(app_rest)
app_rest.register_blueprint(rest_blueprint)
FlaskOAuth(base_app)
InvenioOAuthClientREST(base_app)
# The `rest_blueprint` is already registered indirectly by the
# `remote` fixture

datastore = app_rest.extensions['invenio-accounts'].datastore
datastore = base_app.extensions['invenio-accounts'].datastore
existing_email = '[email protected]'
user = datastore.find_user(email=existing_email)
# Already authenticated
Expand Down

0 comments on commit 2b0869b

Please sign in to comment.