예제 #1
0
def test_compare():
    from pgtoolkit.pgpass import PassComment, PassEntry

    a = PassEntry.parse(":*:*:*:confidentiel")
    b = PassEntry.parse("hostname:*:*:*:otherpassword")
    c = PassEntry.parse("hostname:5442:*:username:otherpassword")

    assert a < b
    assert c < b
    assert a != b

    assert [c, a, b] == sorted([a, b, c])

    d = PassComment("# Comment")
    e = PassComment("# hostname:5432:*:*:password")

    assert "Comment" in repr(d)

    # Preserve comment order.
    assert not d < e
    assert not e < d
    assert not d < a
    assert not a < d
    assert a != d

    assert e < a
    assert c < e

    with pytest.raises(TypeError):
        a < 42
    with pytest.raises(TypeError):
        "meh" > a
    assert (a == [1, 2]) is False
예제 #2
0
def test_compare():
    from pgtoolkit.pgpass import PassComment, PassEntry

    a = PassEntry.parse(':*:*:*:confidentiel')
    b = PassEntry.parse('hostname:*:*:*:otherpassword')
    c = PassEntry.parse('hostname:5442:*:username:otherpassword')

    assert a < b
    assert c < b

    assert [c, a, b] == sorted([a, b, c])

    d = PassComment('# Comment')
    e = PassComment('# hostname:5432:*:*:password')

    assert 'Comment' in repr(d)

    # Preserve comment order.
    assert not d < e
    assert not e < d
    assert not d < a
    assert not a < d
    assert a != d

    assert e < a
    assert c < e
예제 #3
0
def test_save_nofile():
    from pgtoolkit.pgpass import PassComment, PassFile

    pgpass = PassFile()
    pgpass.lines.append(PassComment("# Something"))
    with pytest.raises(ValueError):
        pgpass.save()
예제 #4
0
def test_parse_file(pathtype, tmp_path):
    from pgtoolkit.pgpass import PassComment, parse

    fpath = tmp_path / "pgpass"
    fpath.touch()
    pgpass = parse(pathtype(fpath))
    pgpass.lines.append(PassComment("# Something"))
    pgpass.save()
    assert fpath.read_text() == "# Something\n"
예제 #5
0
def test_passfile_create():
    from pgtoolkit.pgpass import PassComment, PassEntry, PassFile

    pgpass = PassFile(
        [PassComment("# Comment"),
         PassEntry.parse("foo:*:bar:baz:dude")])
    assert 2 == len(pgpass.lines)

    with pytest.raises(ValueError):
        PassFile("blah")
예제 #6
0
def test_parse_file(mocker):
    from pgtoolkit.pgpass import parse, PassComment

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

    assert m.called
    pgpass.save()
    handle = m()
    handle.write.assert_called_with("# Something\n")
예제 #7
0
def test_edit(tmp_path):
    from pgtoolkit.pgpass import PassComment, PassEntry, edit

    fpath = tmp_path / "pgpass"
    assert not fpath.exists()
    with edit(fpath) as passfile:
        passfile.lines.append(PassComment("# commented"))
    assert fpath.read_text() == "# commented\n"

    with edit(fpath) as passfile:
        passfile.lines.extend([
            PassEntry("*", "5443", "*", "username", "otherpassword"),
            PassEntry("hostname", "5443", "*", "username", "password"),
        ])
        passfile.sort()
    assert fpath.read_text().splitlines() == [
        "hostname:5443:*:username:password",
        "# commented",
        "*:5443:*:username:otherpassword",
    ]
예제 #8
0
def test_matches():
    from pgtoolkit.pgpass import PassComment, PassEntry

    a = PassEntry(
        hostname="/var/run/postgresql",
        port=5432,
        database="db",
        username="******",
        password="******",
    )
    assert a.matches(port=5432, database="db")
    with pytest.raises(AttributeError):
        assert a.matches(dbname="newpassword")
    assert not a.matches(port=5433)

    b = PassComment("# some non-entry comment")
    assert not b.matches(port=5432)

    c = PassComment("# hostname:5432:*:*:password")
    assert c.matches(port=5432)
예제 #9
0
def test_matches():
    from pgtoolkit.pgpass import PassComment, PassEntry

    a = PassEntry(
        hostname='/var/run/postgresql',
        port=5432,
        database='db',
        username='******',
        password='******',
    )
    assert a.matches(port=5432, database='db')
    with pytest.raises(AttributeError):
        assert a.matches(dbname='newpassword')
    assert not a.matches(port=5433)

    b = PassComment('# some non-entry comment')
    assert not b.matches(port=5432)

    c = PassComment('# hostname:5432:*:*:password')
    assert c.matches(port=5432)