Exemplo n.º 1
0
 def test_render(self):
     com = commands.ListEntries("search")
     assert com.render(None)
     assert com.render([
         database.DatabaseEntry("n", "l", "p"),
         database.DatabaseEntry("nn", "ll", "pp"),
     ])
Exemplo n.º 2
0
    def test_perform_checks(self):
        com = commands.UpdateEntry("")
        db = database.Database()
        with pytest.raises(commands.CommandException):
            com.perform_checks(db)

        com.name_or_alias = "name"
        com.perform_checks(db)

        entry1 = database.DatabaseEntry("entry1", None, None)
        db.add_entry(entry1)

        com.name_or_alias = "entry1"
        com.add_aliases = ["entry2"]
        com.perform_checks(db)

        entry2 = database.DatabaseEntry("entry2", None, None)
        entry2.aliases = {"alias2"}
        db.add_entry(entry2)

        com.name_or_alias = "not existing entry"
        com.add_aliases = ["entry2"]
        com.perform_checks(db)

        com.name_or_alias = "entry1"
        com.add_aliases = ["entry2"]
        with pytest.raises(commands.CommandException):
            com.perform_checks(db)

        com.rm_aliases = ["entry2"]
        com.perform_checks(db)

        com.add_aliases = ["alias2"]
        with pytest.raises(commands.CommandException):
            com.perform_checks(db)
    def test_filter_with_name_or_alias_part(self):
        assert not database.Database.filter_with_name_or_alias_part(
            "st", list())
        entry_1 = database.DatabaseEntry("test_name", None, None)
        entry_2 = database.DatabaseEntry("toto", None, None)
        entries = [entry_1, entry_2]

        matching_items = database.Database.filter_with_name_or_alias_part(
            None, entries)
        assert len(matching_items) == 2
        assert entry_1 in matching_items and entry_2 in matching_items

        matching_items = database.Database.filter_with_name_or_alias_part(
            "", entries)
        assert len(matching_items) == 2
        assert entry_1 in matching_items and entry_2 in matching_items

        matching_items = database.Database.filter_with_name_or_alias_part(
            "st", entries)
        assert len(matching_items) == 1
        assert entry_1 in matching_items

        matching_items = database.Database.filter_with_name_or_alias_part(
            "t", entries)
        assert len(matching_items) == 2
        assert entry_1 in matching_items and entry_2 in matching_items

        assert not database.Database.filter_with_name_or_alias_part(
            "lol", entries)
        entry_1.aliases.add("ololo")
        matching_items = database.Database.filter_with_name_or_alias_part(
            "lol", entries)
        assert len(matching_items) == 1
        assert entry_1 in matching_items
    def test_filter_with_tag_part(self):
        assert not database.Database.filter_with_tag_part("em", list())
        entry_1 = database.DatabaseEntry("test_name", None, None)
        entry_1.tags = ["email"]
        entry_2 = database.DatabaseEntry("toto", None, None)
        entry_2.tags = ["dev"]
        entries = [entry_1, entry_2]

        assert not database.Database.filter_with_tag_part("o", list())

        matching_items = database.Database.filter_with_tag_part(None, entries)
        assert len(matching_items) == 2
        assert entry_1 in matching_items and entry_2 in matching_items

        matching_items = database.Database.filter_with_tag_part("", entries)
        assert len(matching_items) == 2
        assert entry_1 in matching_items and entry_2 in matching_items

        matching_items = database.Database.filter_with_tag_part("em", entries)
        assert len(matching_items) == 1
        assert entry_1 in matching_items

        matching_items = database.Database.filter_with_tag_part("e", entries)
        assert len(matching_items) == 2
        assert entry_1 in matching_items and entry_2 in matching_items
Exemplo n.º 5
0
    def test_execute(self):
        com = commands.ShowEntry("search")

        db = database.Database(dict())
        assert not com.execute(db)
        test_entry = database.DatabaseEntry("test", None, None)
        db.add_entry(test_entry)
        assert not com.execute(db)
        search_entry = database.DatabaseEntry("search", None, None)
        db.add_entry(search_entry)
        assert com.execute(db) == search_entry
        com.search = "search_alias"
        assert not com.execute(db)
        test_entry.aliases.add("search_alias")
        assert com.execute(db) == test_entry
Exemplo n.º 6
0
    def test_perform_checks(self):
        db = database.Database()
        arg_list = [
            (None, "login", "pwd"),
            ("name", None, "pwd"),
            ("name", "login", None),
        ]
        for args in arg_list:
            com = commands.AddEntry(*args)
            with pytest.raises(commands.CommandException, match=".*empty.*"):
                com.perform_checks(db)

        com = commands.AddEntry("name", "login", "pwd")
        com.perform_checks(db)

        db.add_entry(database.DatabaseEntry("name", None, None))
        with pytest.raises(commands.CommandException, match=".*exists.*"):
            com.perform_checks(db)

        com = commands.AddEntry("alias", "login", "pwd")
        com.perform_checks(db)

        db["name"].aliases = ["alias"]
        with pytest.raises(commands.CommandException, match=".*exists.*"):
            com.perform_checks(db)
    def test_default(self):
        json_encoder = database.DatabaseJSONEncoder()
        empty_dict = dict()
        db = database.Database(empty_dict)
        assert json_encoder.default(db) == empty_dict

        entry = database.DatabaseEntry("name", "login", "mdp", "login_alias")
        entry_as_dict = json_encoder.default(entry)

        assert isinstance(entry_as_dict, dict)
        assert entry.name == entry_as_dict["name"]
        assert entry.login == entry_as_dict["login"]
        assert entry.pwd == entry_as_dict["pwd"]
        assert entry.login_alias == entry_as_dict["login_alias"]
        assert entry.creation_date == entry_as_dict["creation_date"]
        assert entry.last_update_date == entry_as_dict["last_update_date"]
        with pytest.raises(KeyError):
            entry_as_dict["aliases"]
        with pytest.raises(KeyError):
            entry_as_dict["tags"]

        entry.aliases = ["alias"]
        entry.tags = ["tags"]

        entry_as_dict = json_encoder.default(entry)
        assert entry.aliases == entry_as_dict["aliases"]
        assert entry.tags == entry_as_dict["tags"]
 def test_len(self, db):
     assert len(db) == 0
     entry = database.DatabaseEntry("test_name", None, None)
     db.db["entry"] = entry
     assert len(db) == 1
     del db.db["entry"]
     assert len(db) == 0
 def test_add_entry(self, db):
     assert len(db) == 0
     assert not db.modified
     entry = database.DatabaseEntry("test_name", None, None)
     db.add_entry(entry)
     assert len(db) == 1
     assert db.modified
     assert db["test_name"] == entry
Exemplo n.º 10
0
    def test_del_item(self, db):
        entry = database.DatabaseEntry("name", None, None)
        db.add_entry(entry)
        assert len(db) == 1
        assert db.__delitem__("name")
        assert len(db) == 0

        db.add_entry(entry)
        assert not db.__delitem__("alias")
        assert len(db) == 1

        entry.aliases.add("alias")
        assert db.__delitem__("alias")
        assert len(db) == 0
Exemplo n.º 11
0
    def test_save_db(self, tmpdir):
        db_file = tmpdir.join("database")
        db_loader = database.DBLoader(db_file.strpath)
        db = database.Database(dict())
        entry = database.DatabaseEntry("name", "login", "pwd")
        db.add_entry(entry)

        db_loader.save_db(db)
        db_as_dict = json.loads(db_file.read_binary().decode())
        assert "name" in db_as_dict
        entry_as_dict = db_as_dict["name"]
        assert entry_as_dict["name"] == entry.name
        assert entry_as_dict["login"] == entry.login
        assert entry_as_dict["pwd"] == entry.pwd
Exemplo n.º 12
0
    def test_get_item_contains(self, db):
        assert db["test_name"] is None
        assert "test_name" not in db
        entry = database.DatabaseEntry("test_name", None, None)
        db.db["test_name"] = entry
        assert db["test_name"] == entry
        assert "test_name" in db

        assert db["alias"] is None
        assert "alias" not in db
        entry.aliases = ["alias"]
        assert db["alias"] == entry
        assert "alias" in db

        assert db["alias2"] is None
        assert "alias2" not in db
        entry.aliases.append("alias2")
        assert db["alias2"] == entry
        assert "alias2" in db
Exemplo n.º 13
0
    def test_execute(self):
        db = database.Database()
        com = commands.UpdateEntry("name")
        com.pwd = "new_pwd"

        assert not com.execute(db)[0]
        assert not db.modified
        entry = database.DatabaseEntry("name", "login", "old_pwd")
        init_update_date = "update_date"
        entry.last_update_date = init_update_date
        db.add_entry(entry)
        res = com.execute(db)
        assert res[0]
        assert res[1] == entry.name
        assert entry.pwd == com.pwd
        assert db.modified
        assert entry.last_update_date != init_update_date

        com.add_aliases.extend(["alias_to_keep", "alias_to_rm"])
        com.rm_aliases.append("alias_to_rm")
        com.add_tags.extend(["tag_to_keep", "tag_to_rm"])
        com.rm_tags.append("tag_to_rm")
        res = com.execute(db)
        assert res[0]
        assert res[1] == entry.name
        assert entry.aliases == {"alias_to_keep"}
        assert entry.tags == {"tag_to_keep"}

        com = commands.UpdateEntry("alias_to_keep")
        com.login = "******"
        com.login_alias = "new_login_alias"
        res = com.execute(db)
        assert res[0]
        assert res[1] == entry.name
        assert entry.login == com.login
        assert entry.login_alias == com.login_alias
Exemplo n.º 14
0
    def test_render(self):
        com = commands.ShowEntry("search")

        assert not com.render(None)
        assert com.render(database.DatabaseEntry("test", None, None))