Exemplo n.º 1
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"]
Exemplo n.º 3
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)
Exemplo n.º 4
0
    def test_execute(self):
        db = database.Database(dict())
        com = commands.ListEntries("search")
        assert not com.execute(db)

        db.find_matching_entries = MagicMock()
        db.find_matching_entries.return_value = "returned"

        assert com.execute(db) == "returned"
 def test_save_db_if_needed(self, db_manager):
     db = database.Database({"key": "value"})
     assert not db.modified
     db_manager.db = db
     assert not db_manager.save_db_if_needed()
     assert not db_manager.db_loader.save_db.called
     db.modified = True
     assert db_manager.save_db_if_needed()
     assert db_manager.db_loader.save_db.called
    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.º 7
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.º 8
0
    def test_execute(self):
        db = database.Database()
        command = commands.AddEntry("name", "login", "pwd", "login_alias")
        alias_list = ["alias1", "alias2"]
        command.aliases = alias_list
        tag_list = ["tag1", "tag2"]
        command.tags = tag_list

        assert len(db) == 0
        command.execute(db)
        assert len(db) == 1
        entry = db["name"]
        assert entry.name == "name"
        assert entry.login == "login"
        assert entry.pwd == "pwd"
        assert entry.login_alias == "login_alias"
        assert entry.aliases == alias_list
        assert entry.tags == tag_list
        assert entry.creation_date
        assert entry.last_update_date
Exemplo n.º 9
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.º 10
0
 def test_save_db(self, db_manager):
     db = database.Database({"key": "value"})
     db_manager.db = db
     db_manager.save_db()
     db_manager.db_loader.save_db.assert_called_with(db)
Exemplo n.º 11
0
 def empty_db_fixture(self):
     return database.Database()