Skip to content

Commit

Permalink
add invenio-userprofiles to tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Frust, Tobias (FWCC) - 111645 committed Sep 28, 2017
1 parent 8846cdd commit 4046b4d
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 1 deletion.
25 changes: 24 additions & 1 deletion tests/conftest.py
Expand Up @@ -30,7 +30,8 @@
from flask_menu import Menu as FlaskMenu
from invenio_accounts import InvenioAccounts
from invenio_db import InvenioDB, db
from invenio_userprofiles import UserProfile
from invenio_userprofiles import InvenioUserProfiles, UserProfile
from invenio_userprofiles.views import blueprint_ui_init
from sqlalchemy_utils.functions import (create_database, database_exists,
drop_database)

Expand Down Expand Up @@ -104,6 +105,17 @@ def app(base_app):
return base_app


@pytest.fixture
def userprofiles_app(app):
"""Configure userprofiles module."""
app.config.update(
USERPROFILES_EXTEND_SECURITY_FORMS=True,
)
InvenioUserProfiles(app)
app.register_blueprint(blueprint_ui_init)
return app


@pytest.fixture
def models_fixture(app):
"""Flask app with example data used to test models."""
Expand Down Expand Up @@ -155,6 +167,17 @@ def views_fixture(base_app):
return base_app


@pytest.fixture
def userprofiles_fixture(views_fixture):
"""Fixture with userprofiles module."""
views_fixture.config.update(
USERPROFILES_EXTEND_SECURITY_FORMS=True,
)
InvenioUserProfiles(views_fixture)
views_fixture.register_blueprint(blueprint_ui_init)
return views_fixture


@pytest.fixture
def user(userprofiles_app):
"""Create users."""
Expand Down
66 changes: 66 additions & 0 deletions tests/test_views.py
Expand Up @@ -279,6 +279,72 @@ def test_valid_authorized(views_fixture):
assert resp.status_code == 400


def test_valid_authorized_userprofiles(userprofiles_fixture):
"""Test authorized signup handler with userprofiles enabled."""
app = userprofiles_fixture
with app.test_client() as client:
_authorized_valid_config(app)
resp = client.post(
url_for('shibboleth_authenticator.authorized', remote_app='idp'),
data=dict(SAMLResponse=_load_file('valid.xml.base64'))
)
assert resp.status_code == 302
assert current_user.email == 'smartin@yaco.es'
assert current_user.is_authenticated

_authorized_valid_config(app)
resp = client.post(
url_for('shibboleth_authenticator.authorized', remote_app='idp'),
data=dict(SAMLResponse=_load_file('expired.xml.base64'))
)
assert resp.status_code == 403
assert not current_user.is_authenticated

from shibboleth_authenticator.views import serializer

# test valid request with next parameter
next_url = '/test/redirect'
state = serializer.dumps({
'app': 'idp',
'sid': _create_identifier(),
'next': next_url,
})
resp = client.post(
url_for('shibboleth_authenticator.authorized', remote_app='idp'),
data=dict(
SAMLResponse=_load_file('valid.xml.base64'),
RelayState=state,
)
)
check_redirect_location(resp, lambda x: x.endswith(next_url))
assert current_user.email == 'smartin@yaco.es'
assert current_user.is_authenticated

# test invalid state token
state = serializer.dumps({
'app': 'idp',
'sid': 'invalid',
'next': next_url,
})
resp = client.post(
url_for('shibboleth_authenticator.authorized', remote_app='idp'),
data=dict(
SAMLResponse=_load_file('valid.xml.base64'),
RelayState=state,
)
)
assert resp.status_code == 400

resp = client.post(
url_for('shibboleth_authenticator.authorized', remote_app='idp'),
data=dict(
SAMLResponse=_load_file('valid.xml.base64'),
RelayState='',
)
)
assert resp.status_code == 400


@mock.patch('shibboleth_authenticator.views.len')
def test_metadata_fail(mock_len, views_fixture):
"""Test failing metadata view."""
Expand Down

0 comments on commit 4046b4d

Please sign in to comment.