示例#1
0
def test_instance_create_created_instance():
    """Test ``instance create`` command checking the resulting instance."""
    runner = CliRunner()

    # Missing arg
    result = runner.invoke(instance, ['create'])
    assert result.exit_code != 0

    # With arg
    with runner.isolated_filesystem():
        site_name = 'mysite2'
        result = runner.invoke(instance, ['create', site_name])
        assert result.exit_code == 0

        cwd = os.getcwd()
        path_to_folder = os.path.join(cwd, site_name)
        os.chdir(path_to_folder)

        if os.getenv('REQUIREMENTS') == 'devel':
            assert call(['pip', 'install', '-r',
                         'requirements-devel.txt']) == 0

        assert call(['pip', 'install', '-e', '.']) == 0
        assert pkg_resources.get_distribution(site_name)

        from invenio_app.factory import create_app
        app = create_app()
        with app.app_context():
            assert app.config.get('THEME_SITENAME') == site_name

        assert call(['invenio', '--help']) == 0
        assert call(['pip', 'uninstall', site_name, '-y']) == 0

        os.chdir(cwd)
示例#2
0
def test_migrate_secret_key():
    """Test cli command for SECRET_KEY change."""
    def _config_loader(app, **kwargs):
        app.config['CONFIG_LOADER'] = True
        app.config.update(kwargs)

    create_app = create_app_factory('test', config_loader=_config_loader)
    app = create_app(KWARGS_TEST=True)
    script_info = ScriptInfo(create_app=lambda info: app)

    # Check that CLI command fails when the SECRET_KEY is not set.
    with app.app_context():
        runner = CliRunner()
        result = runner.invoke(
            instance, ['migrate-secret-key', '--old-key', 'OLD_SECRET_KEY'],
            obj=script_info)
        assert result.exit_code == 1
        assert 'Error: SECRET_KEY is not set in the configuration.' in \
            result.output

    app.secret_key = "SECRET"
    with patch('pkg_resources.EntryPoint') as MockEntryPoint:
        # Test that the CLI command succeeds when the entrypoint does
        # return a function.
        entrypoint = MockEntryPoint('ep1', 'ep1')
        entrypoint.load.return_value = MagicMock()
        with patch('invenio_base.cli.iter_entry_points',
                   return_value=[entrypoint]):
            result = runner.invoke(
                instance,
                ['migrate-secret-key', '--old-key', 'OLD_SECRET_KEY'],
                obj=script_info)
            assert result.exit_code == 0
            assert entrypoint.load.called
            entrypoint.load.return_value.assert_called_with(
                old_key='OLD_SECRET_KEY')
            assert 'Successfully changed secret key.' in result.output

        # Test that the CLI command fails correctly when the entrypoint does
        # not return a function.
        entrypoint = MockEntryPoint('ep2', 'ep2')
        entrypoint.load.return_value = 'ep2'
        with patch('invenio_base.cli.iter_entry_points',
                   return_value=[entrypoint]):
            result = runner.invoke(
                instance,
                ['migrate-secret-key', '--old-key', 'OLD_SECRET_KEY'],
                obj=script_info)
            assert result.exit_code == -1
            assert entrypoint.load.called
            assert 'Failed to initialize entry point' in result.output
示例#3
0
def base_app(tmpdir_factory):
    """Flask application fixture."""
    tmpdir = tmpdir_factory.mktemp('data')
    sqlite = 'sqlite:///{filepath}'.format(
        filepath=tmpdir.join('unit-test.db'))

    app_ = create_app(
        DEBUG_TB_ENABLED=False,
        SQLALCHEMY_DATABASE_URI=os.environ.get('SQLALCHEMY_DATABASE_URI',
                                               sqlite),
        SECRET_KEY="CHANGE_ME",
        SECURITY_PASSWORD_SALT="CHANGE_ME",
        TESTING=True,
        DEBUG=True,
    )

    with app_.app_context():
        yield app_
示例#4
0
def base_app(tmpdir_factory):
    """Flask application fixture for E2E/integration/selenium tests."""
    tmpdir = tmpdir_factory.mktemp('data')
    sqlite = 'sqlite:///{filepath}'.format(filepath=tmpdir.join('test.db'))

    os.environ.update(
        APP_INSTANCE_PATH=os.environ.get('INSTANCE_PATH', str(tmpdir)))

    app = create_app(
        DEBUG_TB_ENABLED=False,
        SQLALCHEMY_DATABASE_URI=os.environ.get('SQLALCHEMY_DATABASE_URI',
                                               sqlite),
        SECRET_KEY="CHANGE_ME",
        SECURITY_PASSWORD_SALT="CHANGE_ME",
        TESTING=True,
        DEBUG=True,
    )

    with app.app_context():
        yield app