Exemplo n.º 1
0
def test_hba_error(mocker):
    from pgtoolkit.hba import parse, ParseError

    with pytest.raises(ParseError) as ei:
        parse(["lcal all all\n"])
    e = ei.value
    assert 'line #1' in str(e)
    assert repr(e)

    with pytest.raises(ParseError) as ei:
        parse(["local incomplete\n"])
Exemplo n.º 2
0
def test_merge():
    import os

    from pgtoolkit.hba import parse

    sample = """\
    # comment
    host replication all all trust
    # other comment
    host replication  all        127.0.0.1  255.255.255.255         trust
    # Comment should be kept
    host all all all trust"""
    lines = sample.splitlines(True)
    hba = parse(lines)

    other_sample = """\
    # line with no address
    local   all             all                                     trust
    # comment before 1.2.3.4 line
    host replication all 1.2.3.4 trust
    # method changed to 'peer'
    # second comment
    host all all all peer
    """
    other_lines = other_sample.splitlines(True)
    other_hba = parse(other_lines)
    hba.merge(other_hba)

    expected_sample = """\
    # comment
    host replication all all trust
    # other comment
    host replication  all        127.0.0.1  255.255.255.255         trust
    # Comment should be kept
    # method changed to 'peer'
    # second comment
    host all all all peer
    # line with no address
    local   all             all                                     trust
    # comment before 1.2.3.4 line
    host replication all 1.2.3.4 trust
    """
    expected_lines = expected_sample.splitlines(True)
    expected_hba = parse(expected_lines)

    def r(hba):
        return os.linesep.join([str(line) for line in hba.lines])

    assert r(hba) == r(expected_hba)
Exemplo n.º 3
0
def test_hba(mocker):
    from pgtoolkit.hba import parse

    lines = HBA_SAMPLE.splitlines(True)
    hba = parse(lines)
    entries = list(iter(hba))

    assert 7 == len(entries)

    hba.save(mocker.Mock(name='file'))
Exemplo n.º 4
0
def test_func_init(initdb):
    datadir, waldir, __ = initdb
    assert (datadir / "PG_VERSION").exists()
    assert (waldir / "archive_status").is_dir()
    with (datadir / "pg_hba.conf").open() as f:
        pghba = hba.parse(f)
    assert next(iter(pghba)).method == "scram-sha-256"
    st_mode = datadir.stat().st_mode
    assert st_mode & stat.S_IRGRP
    assert st_mode & stat.S_IXGRP
    assert not st_mode & stat.S_IWGRP
Exemplo n.º 5
0
def test_parse_file(mocker):
    from pgtoolkit.hba import parse, HBAComment

    m = mocker.mock_open()
    try:
        mocker.patch('builtins.open', m)
    except Exception:
        mocker.patch('__builtin__.open', m)
    pgpass = parse('filename')
    pgpass.lines.append(HBAComment('# Something'))

    assert m.called
    pgpass.save()
    handle = m()
    handle.write.assert_called_with('# Something\n')

    # Also works for other string types
    m.reset_mock()
    pgpass = parse(u'filename')
    pgpass.lines.append(HBAComment('# Something'))
    assert m.called
Exemplo n.º 6
0
def test_parse_file(mocker):
    from pgtoolkit.hba import HBAComment, parse

    m = mocker.mock_open()
    try:
        mocker.patch("builtins.open", m)
    except Exception:
        mocker.patch("__builtin__.open", m)
    pgpass = parse("filename")
    pgpass.lines.append(HBAComment("# Something"))

    assert m.called
    pgpass.save()
    handle = m()
    handle.write.assert_called_with("# Something\n")

    # Also works for other string types
    m.reset_mock()
    pgpass = parse("filename")
    pgpass.lines.append(HBAComment("# Something"))
    assert m.called
Exemplo n.º 7
0
def test_remove():

    from pgtoolkit.hba import parse

    lines = HBA_SAMPLE.splitlines(True)
    hba = parse(lines)

    with pytest.raises(ValueError):
        hba.remove()

    hba.remove(database='replication')
    entries = list(iter(hba))
    assert 4 == len(entries)

    hba = parse(lines)
    hba.remove(filter=lambda r: r.database == 'replication')
    entries = list(iter(hba))
    assert 4 == len(entries)

    hba = parse(lines)
    hba.remove(conntype='host', database='replication')
    entries = list(iter(hba))
    assert 5 == len(entries)

    # Works even for fields that may not be valid for all records
    # `address` is not valid for `local` connection type
    hba = parse(lines)
    hba.remove(address='127.0.0.1/32')
    entries = list(iter(hba))
    assert 6 == len(entries)

    def filter(r):
        return r.conntype == 'host' and r.database == 'replication'

    hba = parse(lines)
    hba.remove(filter=filter)
    entries = list(iter(hba))
    assert 5 == len(entries)

    # Only filter is taken into account
    hba = parse(lines)
    with pytest.warns(UserWarning):
        hba.remove(filter=filter, database='replication')
    entries = list(iter(hba))
    assert 5 == len(entries)

    # Error if attribute name is not valid
    hba = parse(lines)
    with pytest.raises(AttributeError):
        hba.remove(foo='postgres')