Ejemplo n.º 1
0
def test_processor():
    from ldap2pg.config import Mapping

    m = Mapping('dry', processor=bool)
    v = m.process(default=True, file_config=dict(dry=0), environ=dict())

    assert v is False
Ejemplo n.º 2
0
def test_mapping():
    from ldap2pg.config import Mapping

    m = Mapping('my:option', env=None)
    assert 'my_option' == m.arg
    assert 'my:option' in repr(m)

    # Fallback to default
    v = m.process(default='defval', file_config=dict(), environ=dict())
    assert 'defval' == v

    # Read file
    v = m.process(
        default='defval',
        file_config=dict(my=dict(option='fileval')),
        environ=dict(),
    )
    assert 'fileval' == v

    # Ignore env
    v = m.process(
        default='defval',
        file_config=dict(my=dict(option='fileval')),
        environ=dict(MY_OPTION=b'envval'),
    )
    assert 'fileval' == v

    m = Mapping('my:option')
    assert 'MY_OPTION' in m.env
    assert 'MYOPTION' in m.env

    # Prefer env over file
    v = m.process(
        default='defval',
        file_config=dict(my=dict(option='fileval')),
        environ=dict(MY_OPTION=b'envval'),
    )
    assert 'envval' == v

    # Prefer argv over env
    v = m.process(
        default='defval',
        file_config=dict(my=dict(option='fileval')),
        environ=dict(MY_OPTION='envval'),
        args=MockArgs(my_option='argval')
    )
    assert 'argval' == v
Ejemplo n.º 3
0
def test_mapping_security():
    from ldap2pg.config import Mapping

    m = Mapping('ldap:password', secret=True)
    with pytest.raises(ValueError):
        # Something in file but it's not secure
        m.process(
            default='DEFAULT',
            file_config=dict(
                world_readable=True,
                ldap=dict(password='******'),
            ),
            environ=dict(),
        )

    # File is unsecure but env var overrides value and error.
    v = m.process(
        default='DEFAULT',
        file_config=dict(ldap=dict(password='******')),
        environ=dict(LDAP_PASSWORD=b'fromenv'),
    )
    assert 'fromenv' == v

    # File is secure, use it.
    v = m.process(
        default='DEFAULT',
        file_config=dict(world_readable=False, ldap=dict(password='******')),
        environ=dict(),
    )
    assert '53cUr3!' == v

    m = Mapping('postgres:dsn', secret="password=")
    with pytest.raises(ValueError):
        # Something in file but it's not secure
        m.process(
            default='DEFAULT',
            file_config=dict(
                world_readable=True,
                postgres=dict(dsn='password=unsecure'),
            ),
            environ=dict(),
        )