Example #1
0
def test_parse_record_with_double_quoting():
    from pgtoolkit.hba import HBARecord

    record = HBARecord.parse(
        r'host all all all radius radiusservers="server1,server2" radiussecrets="""secret one"",""secret two"""'
    )
    assert record.radiussecrets == '""secret one"",""secret two""'
Example #2
0
def test_parse_record_blank_in_quotes():
    from pgtoolkit.hba import HBARecord

    record = HBARecord.parse(
        r"host all all all ldap ldapserver=ldap.example.net"
        r' ldapbasedn="dc=example, dc=net"'
        r' ldapsearchfilter="(|(uid=$username)(mail=$username))"')
    assert record.ldapserver == "ldap.example.net"
    assert record.ldapbasedn == "dc=example, dc=net"
    assert record.ldapsearchfilter == "(|(uid=$username)(mail=$username))"
Example #3
0
def test_parse_record_with_comment():
    from pgtoolkit.hba import HBARecord

    record = HBARecord.parse("local    all     all     trust  # My  comment")
    assert 'local' == record.conntype
    assert 'all' == record.database
    assert ['all'] == record.users
    assert 'trust' == record.method
    assert 'My comment' == record.comment

    fields = str(record).split()
    assert ['local', 'all', 'all', 'trust', '#', 'My', 'comment'] == fields
Example #4
0
def test_parse_record_with_comment():
    from pgtoolkit.hba import HBARecord

    record = HBARecord.parse("local    all     all     trust  # My  comment")
    assert "local" == record.conntype
    assert "all" == record.database
    assert ["all"] == record.users
    assert "trust" == record.method
    assert "My comment" == record.comment

    fields = str(record).split()
    assert ["local", "all", "all", "trust", "#", "My", "comment"] == fields
Example #5
0
def test_parse_local_line():
    from pgtoolkit.hba import HBARecord

    record = HBARecord.parse("local    all     all     trust")
    assert 'local' == record.conntype
    assert 'all' == record.database
    assert ['all'] == record.users
    assert 'trust' == record.method

    with pytest.raises(AttributeError):
        record.address

    wanted = 'local   all             all                                     trust'  # noqa
    assert wanted == str(record)
Example #6
0
def test_parse_host_line():
    from pgtoolkit.hba import HBARecord

    record = HBARecord.parse("host    replication   all   ::1/128       trust")
    assert 'host' in repr(record)
    assert 'host' == record.conntype
    assert 'replication' == record.database
    assert ['replication'] == record.databases
    assert 'all' == record.user
    assert ['all'] == record.users
    assert '::1/128' == record.address
    assert 'trust' == record.method

    # This is not actually a public API. But let's keep it stable.
    values = record.common_values
    assert 'trust' in values
Example #7
0
def test_parse_auth_option():
    from pgtoolkit.hba import HBARecord

    record = HBARecord.parse(
        "local veryverylongdatabasenamethatdonotfit all ident map=omicron",
    )
    assert 'local' == record.conntype
    assert 'veryverylongdatabasenamethatdonotfit' == record.database
    assert ['all'] == record.users
    assert 'ident' == record.method
    assert 'omicron' == record.map

    wanted = [
        'local', 'veryverylongdatabasenamethatdonotfit', 'all', 'ident',
        'map="omicron"',
    ]
    assert wanted == str(record).split()
Example #8
0
def test_hba_create():
    from pgtoolkit.hba import HBA, HBAComment, HBARecord

    hba = HBA([
        HBAComment('# a comment'),
        HBARecord(
            conntype='local', database='all', user='******', method='trust',
        ),
    ])
    assert 2 == len(hba.lines)

    r = hba.lines[1]
    assert ['all'] == r.databases

    # Should be a list
    with pytest.raises(ValueError):
        HBA('blah')
Example #9
0
def test_parse_auth_option():
    from pgtoolkit.hba import HBARecord

    record = HBARecord.parse(
        "local veryverylongdatabasenamethatdonotfit all ident map=omicron", )
    assert "local" == record.conntype
    assert "veryverylongdatabasenamethatdonotfit" == record.database
    assert ["all"] == record.users
    assert "ident" == record.method
    assert "omicron" == record.map

    wanted = [
        "local",
        "veryverylongdatabasenamethatdonotfit",
        "all",
        "ident",
        'map="omicron"',
    ]
    assert wanted == str(record).split()
Example #10
0
def test_hba_create():
    from pgtoolkit.hba import HBA, HBAComment, HBARecord

    hba = HBA([
        HBAComment("# a comment"),
        HBARecord(
            conntype="local",
            database="all",
            user="******",
            method="trust",
        ),
    ])
    assert 2 == len(hba.lines)

    r = hba.lines[1]
    assert ["all"] == r.databases

    # Should be a list
    with pytest.raises(ValueError):
        HBA("blah")
Example #11
0
def test_parse_invalid_connection_type():
    from pgtoolkit.hba import HBARecord

    with pytest.raises(ValueError, match="Unknown connection type 'pif'"):
        HBARecord.parse("pif    all     all")
Example #12
0
def test_parse_record_with_backslash():
    from pgtoolkit.hba import HBARecord

    record = HBARecord.parse(
        r'host all all all ldap ldapserver=host.local ldapprefix="DOMAINE\"')
    assert record.ldapprefix == "DOMAINE\\"