Example #1
0
def setup_app(app):
    with app.app_context():
        db.drop_all()
        db.create_all()
        reindex_all(recreate=True)

        ctx = app.test_request_context()
        ctx.push()

        user_count = User.query.filter_by(email='*****@*****.**').count()
        if user_count == 0:
            user = User(email=TEST_EMAIL, password='******', active=True)
            admin_role = Role(name='admin')
            coordinator_role = Role(name='coordinator')

            user.roles.append(admin_role)
            user.roles.append(coordinator_role)

            db.session.add(admin_role)
            db.session.add(coordinator_role)
            db.session.add(user)
            db.session.commit()

        yield app
        ctx.pop()
Example #2
0
def app(request):
    """Flask application fixture for E2E/integration/selenium tests.
    Overrides the `app` fixture found in `../conftest.py`. Tests/files in this
    folder and subfolders will see this variant of the `app` fixture.
    """
    app = create_app()
    app.config.update(
        dict(TESTING=True,
             TEST_RUNNER="celery.contrib.test_runner.CeleryTestSuiteRunner",
             CELERY_ALWAYS_EAGER=True,
             CELERY_RESULT_BACKEND="cache",
             CELERY_CACHE_BACKEND="memory",
             MAIL_SUPPRESS_SEND=True,
             CELERY_EAGER_PROPAGATES_EXCEPTIONS=True,
             SQLALCHEMY_DATABASE_URI=os.environ.get(
                 'SQLALCHEMY_DATABASE_URI',
                 'postgresql+psycopg2://hepdata:hepdata@localhost/hepdata_test'
             )))

    with app.app_context():
        if not database_exists(str(db.engine.url)):
            create_database(str(db.engine.url))
        db.create_all()

    with app.app_context():
        db.drop_all()
        db.create_all()
        reindex_all(recreate=True)

        ctx = app.test_request_context()
        ctx.push()

        user_count = User.query.filter_by(email='*****@*****.**').count()
        if user_count == 0:
            user = User(email='*****@*****.**',
                        password='******',
                        active=True)
            admin_role = Role(name='admin')
            coordinator_role = Role(name='coordinator')

            user.roles.append(admin_role)
            user.roles.append(coordinator_role)

            db.session.add(admin_role)
            db.session.add(coordinator_role)
            db.session.add(user)
            db.session.commit()

        load_default_data(app)

    def teardown():
        with app.app_context():
            db.drop_all()
            ctx.pop()

    request.addfinalizer(teardown)

    return app
Example #3
0
def reindex():
    if has_role(current_user, 'admin'):
        reindex_all(recreate=True)
        push_data_keywords()
        admin_idx = AdminIndexer()
        admin_idx.reindex(recreate=True)
        return jsonify({"success": True})
    else:
        return jsonify({"success": False,
                        'message': "You don't have sufficient privileges to "
                                   "perform this action."})
Example #4
0
def test_tables(app, live_server, env_browser):
    """E2E test to tables in a record."""
    browser = env_browser

    # Import record with non-default table names
    import_default_data(app, [{'hepdata_id': 'ins1206352'}])

    try:
        browser.get(flask.url_for('hepdata_theme.index', _external=True))
        assert (flask.url_for('hepdata_theme.index', _external=True)
                in browser.current_url)

        latest_item = browser.find_element_by_css_selector(
            '.latest-record .title')
        actions = ActionChains(browser)
        actions.move_to_element(latest_item).perform()
        latest_item.click()

        # Check current table name
        assert (browser.find_element_by_id('table_name').text ==
                'Figure 8 panel (a)')

        # Check switching tables works as expected
        new_table = browser.find_elements_by_css_selector(
            '#table-list li h4')[2]
        assert (new_table.text == "Figure 8 panel (c)")
        new_table.click()
        _check_table_links(browser, "Figure 8 panel (c)")

        # Get link to table from table page
        table_link = browser.find_element_by_css_selector('#data_link_container button') \
            .get_attribute('data-clipboard-text')
        assert (table_link.endswith('table=Figure%208%20panel%20(c)'))
        _check_table_links(browser, "Figure 8 panel (c)", url=table_link)

        # Check a link to a table name with spaces removed
        short_table_link = table_link.replace('%20', '')
        _check_table_links(browser, "Figure 8 panel (c)", url=short_table_link)

        # Check a link to an invalid table
        invalid_table_link = table_link.replace('Figure%208%20panel%20(c)',
                                                'NotARealTable')
        _check_table_links(browser,
                           "Figure 8 panel (a)",
                           url=invalid_table_link)

    finally:
        # Delete record and reindex so added record doesn't affect other tests
        submission = get_latest_hepsubmission(inspire_id='1206352')
        unload_submission(submission.publication_recid)
        reindex_all(recreate=True)
Example #5
0
def test_reindex_all(app, load_default_data, identifiers):
    index = app.config.get('ELASTICSEARCH_INDEX')
    # Delete the default index
    es.indices.delete(index=index)

    # Check we can't search
    with pytest.raises(NotFoundError, match=r"no such index "):
        es_api.search('', index=index)

    # Reindex, recreating the index
    es_api.reindex_all(index=index, recreate=True, synchronous=True)

    # Search should work again
    results = es_api.search('', index=index)
    assert (results['total'] == len(identifiers))
Example #6
0
def app(request):
    """Flask app fixture."""
    app = create_app()
    app.config.update(
        dict(TESTING=True,
             TEST_RUNNER="celery.contrib.test_runner.CeleryTestSuiteRunner",
             CELERY_ALWAYS_EAGER=True,
             CELERY_RESULT_BACKEND="cache",
             CELERY_CACHE_BACKEND="memory",
             MAIL_SUPPRESS_SEND=True,
             CELERY_EAGER_PROPAGATES_EXCEPTIONS=True,
             SQLALCHEMY_DATABASE_URI=os.environ.get(
                 'SQLALCHEMY_DATABASE_URI',
                 'postgresql+psycopg2://hepdata:hepdata@localhost/hepdata_test'
             )))

    with app.app_context():
        db.drop_all()
        db.create_all()
        reindex_all(recreate=True)

        ctx = app.test_request_context()
        ctx.push()

        user_count = User.query.filter_by(email='*****@*****.**').count()
        if user_count == 0:
            user = User(email=TEST_EMAIL, password='******', active=True)
            admin_role = Role(name='admin')
            coordinator_role = Role(name='coordinator')

            user.roles.append(admin_role)
            user.roles.append(coordinator_role)

            db.session.add(admin_role)
            db.session.add(coordinator_role)
            db.session.add(user)
            db.session.commit()

        yield app
        ctx.pop()
Example #7
0
def reindex(recreate, start, end, batch):
    reindex_all(recreate=recreate, start=start, end=end, batch=batch)
Example #8
0
def app(request):
    """Flask application fixture for E2E/integration/selenium tests.
    Overrides the `app` fixture found in `../conftest.py`. Tests/files in this
    folder and subfolders will see this variant of the `app` fixture.
    """
    app = create_app()
    test_db_host = app.config.get('TEST_DB_HOST', 'localhost')
    # Note that in GitHub Actions we add "TESTING=True" and
    # "APP_ENABLE_SECURE_HEADERS=False" to config_local.py as well,
    # to ensure that they're set before the app is initialised,
    # as changing them later doesn't have the desired effect.
    app.config.update(
        dict(TESTING=True,
             TEST_RUNNER="celery.contrib.test_runner.CeleryTestSuiteRunner",
             CELERY_TASK_ALWAYS_EAGER=True,
             CELERY_RESULT_BACKEND="cache",
             CELERY_CACHE_BACKEND="memory",
             CELERY_TASK_EAGER_PROPAGATES=True,
             ELASTICSEARCH_INDEX="hepdata-main-test",
             SUBMISSION_INDEX='hepdata-submission-test',
             AUTHOR_INDEX='hepdata-authors-test',
             SQLALCHEMY_DATABASE_URI=os.environ.get(
                 'SQLALCHEMY_DATABASE_URI',
                 'postgresql+psycopg2://hepdata:hepdata@' + test_db_host +
                 '/hepdata_test'),
             APP_ENABLE_SECURE_HEADERS=False))

    with app.app_context():
        if not database_exists(str(db.engine.url)):
            create_database(str(db.engine.url))
        db.create_all()

    with app.app_context():
        db.drop_all()
        db.create_all()
        reindex_all(recreate=True, synchronous=True)

        ctx = app.test_request_context()
        ctx.push()

        user_count = User.query.filter_by(email='*****@*****.**').count()
        if user_count == 0:
            user = User(email='*****@*****.**',
                        password='******',
                        active=True)
            admin_role = Role(name='admin')
            coordinator_role = Role(name='coordinator')

            user.roles.append(admin_role)
            user.roles.append(coordinator_role)

            db.session.add(admin_role)
            db.session.add(coordinator_role)
            db.session.add(user)
            db.session.commit()

        import_default_data(app, get_identifiers())

    def teardown():
        with app.app_context():
            db.engine.dispose()
            db.drop_all()
            ctx.pop()

    request.addfinalizer(teardown)

    return app