Esempio n. 1
0
def test_response_handler_with_postmessage(remote, app_rest, models_fixture):
    """Test response handler with postmessage."""

    # Force usage of dummy handlers
    app_rest.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)

    datastore = app_rest.extensions['invenio-accounts'].datastore
    existing_email = '*****@*****.**'
    user = datastore.find_user(email=existing_email)
    # Already authenticated
    login_user(user)

    assert current_user.is_authenticated

    response = signup_handler(remote)
    expected_message = "Successfully signed up."
    expected_status = "200"

    assert expected_message in response
    assert expected_status in response
    assert 'window.opener.postMessage' in response
Esempio n. 2
0
def test_response_handler(remote_name, app_rest):
    """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][
        'response_handler'] = mock_response_handler

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

    # Try to sign-up client
    response = app_rest.test_client().get(
        url_for('invenio_oauthclient.rest_signup', remote_app=remote_name))
    assert remote_name in str(response.data)
Esempio n. 3
0
def test_dummy_handler(remote_name, base_app):
    """Test dummy handler."""

    signup_handler = base_app.config['OAUTHCLIENT_REST_REMOTE_APPS'][
        remote_name]['signup_handler']

    # Force usage of dummy handlers
    base_app.config['OAUTHCLIENT_REST_REMOTE_APPS'][remote_name][
        'signup_handler'] = {}

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

    # Try to sign-up client
    base_app.test_client().get(
        url_for('invenio_oauthclient.rest_signup',
                remote_app=remote_name,
                next='/someurl/'))

    base_app.config['OAUTHCLIENT_REST_REMOTE_APPS'][remote_name][
        'signup_handler'] = signup_handler
Esempio n. 4
0
def test_authorized(base_app, params):
    """Test login."""
    handled = {}

    def test_authorized_handler(resp, remote, *args, **kwargs):
        """Save configuration."""
        handled['resp'] = resp
        handled['remote'] = remote
        handled['args'] = args
        handled['kwargs'] = kwargs
        return 'TEST'

    def test_invalid_authorized_handler(resp, remote, *args, **kwargs):
        """Set wrong configuration."""
        handled['resp'] = 1
        handled['remote'] = 1
        handled['args'] = 1
        handled['kwargs'] = 1

    base_app.config['OAUTHCLIENT_REST_REMOTE_APPS'].update(
        dict(
            test=dict(
                authorized_handler=test_authorized_handler,
                params=params('testid'),
                title='MyLinkedTestAccount',
            ),
            test_invalid=dict(
                authorized_handler=test_invalid_authorized_handler,
                params=params('test_invalidid'),
                title='Test Invalid',
            ),
            full=dict(
                params=params('fullid'),
                title='Full',
            ),
        ))

    FlaskOAuth(base_app)
    InvenioOAuthClientREST(base_app)
    base_app.register_blueprint(rest_blueprint)

    with base_app.test_client() as client:
        # Ensure remote apps have been loaded (due to before first
        # request)
        client.get(url_for(
            'invenio_oauthclient.rest_login', remote_app='test'))
        mock_response(base_app.extensions['oauthlib.client'], 'test')
        mock_response(base_app.extensions['oauthlib.client'], 'test_invalid')

        from invenio_oauthclient.views.client import serializer

        state = serializer.dumps({
            'app': 'test',
            'sid': _create_identifier(),
            'next': None,
        })

        resp = client.get(
            url_for(
                'invenio_oauthclient.rest_authorized',
                remote_app='test',
                code='test',
                state=state
            )
        )
        assert resp.data == b'TEST'
        assert handled['remote'].name == 'test'
        assert not handled['args']
        assert not handled['kwargs']
        assert handled['resp']['access_token'] == 'test_access_token'

        state = serializer.dumps({
            'app': 'test_invalid',
            'sid': _create_identifier(),
            'next': None,
        })

        # handler should return something
        # Flask>1.0 is throwing TypeError and Flask<1.0 ValueError
        with pytest.raises((ValueError, TypeError)):
            client.get(url_for(
                'invenio_oauthclient.rest_authorized',
                remote_app='test_invalid',
                code='test',
                state=state,
            ))
Esempio n. 5
0
    SECRET_KEY='TEST',
    SECURITY_PASSWORD_SALT='security-password-salt',
    SECURITY_LOGIN_WITHOUT_CONFIRMATION=False,
    USERPROFILES_EXTEND_SECURITY_FORMS=True,
    SQLALCHEMY_TRACK_MODIFICATIONS=False,
    APP_THEME=['semantic-ui'],
    THEME_ICONS={'semantic-ui': dict(link='linkify icon')})

Babel(app)
FlaskMenu(app)
Mail(app)
InvenioDB(app)
InvenioAccounts(app)
InvenioUserProfiles(app)
FlaskOAuth(app)
InvenioOAuthClientREST(app)

app.register_blueprint(blueprint_user)
app.register_blueprint(blueprint_client)
app.register_blueprint(blueprint_userprofile_api_init)
app.register_blueprint(blueprint_userprofile_ui_init)


@app.route('/')
def index():
    """Homepage."""
    return 'Home page (without any restrictions)'


@app.route('/orcid')
def orcid():
Esempio n. 6
0
def _init_app_rest(app_):
    """Init OAuth rest app."""
    FlaskOAuth(app_)
    InvenioOAuthClientREST(app_)
    app_.register_blueprint(rest_blueprint)
    return app_