Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

utils: fixes missing args from redirect target #186

Merged
merged 1 commit into from
Feb 26, 2019
Merged
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
8 changes: 6 additions & 2 deletions invenio_oauthclient/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from invenio_db import db
from invenio_db.utils import rebuild_encrypted_properties
from sqlalchemy.exc import IntegrityError
from uritools import urisplit
from uritools import uricompose, urisplit
from werkzeug.local import LocalProxy
from werkzeug.utils import import_string
from wtforms.fields.core import FormField
Expand Down Expand Up @@ -157,7 +157,11 @@ def get_safe_redirect_target(arg='next'):
if redirect_uri.host in allowed_hosts:
return target
elif redirect_uri.path:
return redirect_uri.path
return uricompose(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice this uricompose, I have learnt something! :)
👍

path=redirect_uri.path,
query=redirect_uri.query,
fragment=redirect_uri.fragment
)
return None


Expand Down
1 change: 1 addition & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ def base_app(request):
SQLALCHEMY_TRACK_MODIFICATIONS=False,
SECURITY_PASSWORD_HASH='plaintext',
SECURITY_PASSWORD_SCHEMES=['plaintext'],
APP_ALLOWED_HOSTS=['localhost']
)
FlaskMenu(base_app)
Babel(base_app)
Expand Down
20 changes: 18 additions & 2 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@
import pytest
from flask_security.confirmable import _security
from invenio_db import db
from six.moves.urllib.parse import quote_plus

from invenio_oauthclient.errors import AlreadyLinkedError
from invenio_oauthclient.models import RemoteAccount, RemoteToken
from invenio_oauthclient.utils import _get_external_id, \
create_csrf_disabled_registrationform, create_registrationform, \
fill_form, oauth_authenticate, oauth_get_user, oauth_link_external_id, \
oauth_unlink_external_id, obj_or_import_string, rebuild_access_tokens
fill_form, get_safe_redirect_target, oauth_authenticate, oauth_get_user, \
oauth_link_external_id, oauth_unlink_external_id, obj_or_import_string, \
rebuild_access_tokens


def test_utilities(models_fixture):
Expand Down Expand Up @@ -175,6 +177,20 @@ def test_registrationform_userprofile_disable_csrf(app_with_userprofiles_csrf,
_assert_no_csrf_token(filled_form)


@pytest.mark.parametrize("test_input,expected", [
('https://invenio.org/search?page=1&q=&keywords=taxonomy&keywords=animali',
'/search?page=1&q=&keywords=taxonomy&keywords=animali'),
('/search?page=1&size=20',
'/search?page=1&size=20'),
('https://localhost/search?page=1',
'https://localhost/search?page=1'),
])
def test_get_safe_redirect_target(app, test_input, expected):
with app.test_request_context(
'/?next={0}'.format(quote_plus(test_input))):
assert get_safe_redirect_target() == expected


def _assert_csrf_token(form):
"""Assert that the field `csrf_token` exists in the form."""
assert 'csrf_token' in form
Expand Down