def test_fetch_entries_empty_database(tmpdir):
    test_database = _move_test_file_to_tmpdir(tmpdir, "empty.kdb")

    backend = Backend(test_database)
    backend.open_database("abcd123")

    backend.sync_entries()
    assert len(backend.entries.items) == 0
def test_get_config(tmpdir):
    test_database = _move_test_file_to_tmpdir(tmpdir, "test_get_config.kdb")
    backend = Backend(test_database, "abcd123")

    # existing associated client
    assert backend.get_config("test_name") == "test_key"

    # unknown client
    assert backend.get_config("test_unknown") is None
def test_create_config(tmpdir):
    test_database = _move_test_file_to_tmpdir(tmpdir, "test_create_config.kdb")

    backend = Backend(test_database, "abcd123")
    backend.sync_entries()
    assert len(backend.entries.items) == 0

    backend.create_config_key("test_name", "test_key")
    assert len(backend.entries.items) == 1
    assert backend.entries.items.pop() == EntrySpec(login="",
                                                    url='',
                                                    password='******',
                                                    title='test_name',
                                                    uuid="")
def test_get_search_entries(tmpdir):
    test_database = _move_test_file_to_tmpdir(tmpdir, "test_search_for_entries.kdb")

    backend = Backend(test_database)
    backend.open_database("abcd123")

    backend.sync_entries()
    assert len(backend.entries.items) == 5

    # FIXME: remove config for client!
    # first one is the client config (key)
    del backend.entries.items[0]

    results = backend.search_entries("url", "https://www.google.com/login")
    assert len(results) == 1

    results = backend.search_entries("url", "https://gmx.net/login")
    assert len(results) == 2

    results = backend.search_entries("url", "http://web.de/login/form.php")
    assert len(results) == 1
def test_create_login(tmpdir):
    test_database = _move_test_file_to_tmpdir(tmpdir, "test_create_login.kdb")

    test_databse_passphrase = "abcd123"

    x = Backend(test_database, test_databse_passphrase)

    x.sync_entries()
    assert len(x.entries.items) == 1
    # valid logins
    x.create_login(u"test_name", "*****@*****.**", "geheim", "https://www.google.com/login")
    # 2 accounts, same domain
    x.create_login(u"test_name", "*****@*****.**", "geheim2", "https://gmx.net/login")
    x.create_login(u"test_name", "*****@*****.**", "geheim3", "https://gmx.net/login")
    # another login
    x.create_login(u"test_name", "*****@*****.**", "geheim4", "http://web.de/login/form.php")
    # x.get_entries(purge_cache=True)

    assert len(x.entries.items) == 5

    # FIXME: remove config for client!
    del x.entries.items[0]
    # first one is the client config (key)

    assert x.entries.items[0] == EntrySpec(login="******",
                                           url='https://www.google.com/login',
                                           password='******',
                                           title="www.google.com",
                                           uuid="")
    assert x.entries.items[1] == EntrySpec(login="******",
                                           url='https://gmx.net/login',
                                           password='******',
                                           title="gmx.net",
                                           uuid="")
    assert x.entries.items[2] == EntrySpec(login="******",
                                           url='https://gmx.net/login',
                                           password='******',
                                           title="gmx.net",
                                           uuid="")
    assert x.entries.items[3] == EntrySpec(login="******",
                                           url='http://web.de/login/form.php',
                                           password='******',
                                           title="web.de",
                                           uuid="")