Exemple #1
0
def test_load_file(mocker):
    environ = dict()
    mod = 'ldap2pg.config.'
    mocker.patch(mod + 'os.environ', environ)
    ff = mocker.patch(mod + 'Configuration.find_filename')
    mocker.patch(mod + 'open', create=True)
    read = mocker.patch(mod + 'Configuration.read')
    warn = mocker.patch(mod + 'logger.warning')

    from ldap2pg.config import Configuration

    config = Configuration()

    ff.return_value = ['filename.yml', 0o0]
    read.return_value = dict(
        sync_map=[dict(role='alice')],
        # To trigger a warning.
        unknown_key=True,
        # Should not trigger a warning.
        privileges=dict(ro=['__connect__']),
    )
    # send one env var
    environ.update(dict(PGDSN=b'envdsn'))

    config.load(argv=['--verbose'])

    assert 'envdsn' == config['postgres']['dsn']
    maplist = config['sync_map']
    assert 1 == len(maplist)
    assert 'DEBUG' == config['verbosity']
    # logger.warning is called once for unknown_key, not for privileges.
    assert 1 == warn.call_count
Exemple #2
0
def test_load_badfiles(mocker):
    environ = dict()
    mocker.patch('ldap2pg.config.os.environ', environ)
    ff = mocker.patch('ldap2pg.config.Configuration.find_filename')
    merge = mocker.patch('ldap2pg.config.Configuration.merge')
    read = mocker.patch('ldap2pg.config.Configuration.read')

    from ldap2pg.config import (
        Configuration,
        ConfigurationError,
        UserError,
    )

    config = Configuration()

    # Invalid file
    ff.return_value = ['filename.yml', 0o0]
    merge.side_effect = ValueError()
    o = mocker.patch('ldap2pg.config.open', mocker.mock_open(), create=True)
    read.return_value = {}
    with pytest.raises(ConfigurationError):
        config.load(argv=[])

    # Not readable.
    o.side_effect = OSError("failed to open")
    with pytest.raises(UserError):
        config.load(argv=["--color"])
Exemple #3
0
def test_load_stdin(mocker):
    environ = dict()
    mocker.patch('ldap2pg.config.os.environ', environ)
    ff = mocker.patch('ldap2pg.config.Configuration.find_filename')
    mocker.patch('ldap2pg.config.open', create=True)
    read = mocker.patch('ldap2pg.config.Configuration.read')

    from ldap2pg.config import Configuration

    config = Configuration()

    ff.return_value = ['-', 0o400]
    read.return_value = dict(sync_map=[dict(role='alice')])

    config.load(argv=[])

    maplist = config['sync_map']
    assert 1 == len(maplist)
Exemple #4
0
def test_load_file(mocker):
    environ = dict()
    mocker.patch('ldap2pg.config.os.environ', environ)
    ff = mocker.patch('ldap2pg.config.Configuration.find_filename')
    mocker.patch('ldap2pg.config.open', create=True)
    read = mocker.patch('ldap2pg.config.Configuration.read')

    from ldap2pg.config import Configuration

    config = Configuration()

    ff.return_value = ['filename.yml', 0o0]
    read.return_value = dict(sync_map=[dict(role='alice')])
    # send one env var for LDAP bind
    environ.update(dict(LDAPPASSWORD=b'envpass'))

    config.load(argv=['--verbose'])

    assert 'envpass' == config['ldap']['password']
    maplist = config['sync_map']
    assert 1 == len(maplist)
    assert 'DEBUG' == config['verbosity']
Exemple #5
0
def test_load(mocker):
    environ = dict()
    mocker.patch('ldap2pg.config.os.environ', environ)
    ff = mocker.patch('ldap2pg.config.Configuration.find_filename')
    read = mocker.patch('ldap2pg.config.Configuration.read')
    o = mocker.patch('ldap2pg.config.open', create=True)

    from ldap2pg.config import (
        Configuration,
        ConfigurationError,
        NoConfigurationError,
        UserError,
    )

    config = Configuration()

    ff.side_effect = NoConfigurationError()
    # Missing sync_map
    with pytest.raises(ConfigurationError):
        config.load(argv=[])

    ff.side_effect = None
    # Find `filename.yml`
    ff.return_value = ['filename.yml', 0o0]

    # Not readable.
    o.side_effect = OSError("failed to open")
    with pytest.raises(UserError):
        config.load(argv=[])

    # Readable..
    o.side_effect = None
    # ...containing mapping
    read.return_value = dict(sync_map=dict(ldap=dict(), role=dict()))
    # send one env var for LDAP bind
    environ.update(dict(LDAP_BIND='envbind'))

    config.load(argv=['--verbose'])

    assert 'envbind' == config['ldap']['bind']
    assert 1 == len(config['sync_map'])
    assert 'ldap' in config['sync_map'][0]
    assert config['verbose'] is True
Exemple #6
0
def test_show_versions(mocker):
    from ldap2pg.config import Configuration

    config = Configuration()
    with pytest.raises(SystemExit):
        config.load(argv=['--version'])