diff --git a/pykeepass/pykeepass.py b/pykeepass/pykeepass.py index ac43ae7b..e94ecd90 100644 --- a/pykeepass/pykeepass.py +++ b/pykeepass/pykeepass.py @@ -268,6 +268,43 @@ def entries(self): excluding history""" return self.find_entries() + @property + def database_name(self): + """Name of database""" + elem = self._xpath('/KeePassFile/Meta/DatabaseName', first=True) + return elem.text + + @database_name.setter + def database_name(self, name): + item = self._xpath('/KeePassFile/Meta/DatabaseName', first=True) + item.text = str(name) + + @property + def database_description(self): + """Description of the database""" + elem = self._xpath('/KeePassFile/Meta/DatabaseDescription', first=True) + return elem.text + + @database_description.setter + def database_description(self, name): + item = self._xpath('/KeePassFile/Meta/DatabaseDescription', first=True) + item.text = str(name) + + @property + def default_username(self): + """Default Username + + Returns: + user name or None if not set. + """ + elem = self._xpath('/KeePassFile/Meta/DefaultUserName', first=True) + return elem.text + + @default_username.setter + def default_username(self, name): + item = self._xpath('/KeePassFile/Meta/DefaultUserName', first=True) + item.text = str(name) + def xml(self): """Get XML part of database as string diff --git a/tests/tests.py b/tests/tests.py index 791d1977..e7fc5fac 100644 --- a/tests/tests.py +++ b/tests/tests.py @@ -1110,8 +1110,39 @@ def test_ctx_manager(self): results = kp.find_entries_by_username('foobar_user', first=True) self.assertEqual('foobar_user', results.username) +class PyKeePassTests3(KDBX3Tests): + """Tests on PyKeePass class that don't involve attachments or finding entries/groups""" + + def test_database_info(self): + """Test database properties""" + + # Test name + self.assertEqual(self.kp_tmp.database_name, None) + self.kp_tmp.database_name = "Test Name" + self.assertEqual(self.kp_tmp.database_name, "Test Name") + + # Test Description + self.assertEqual(self.kp_tmp.database_description, None) + self.kp_tmp.database_description = "Test Description" + self.assertEqual(self.kp_tmp.database_description, "Test Description") + + # Test Default User Name + self.assertEqual(self.kp_tmp.default_username, None) + self.kp_tmp.default_username = "Test User" + self.assertEqual(self.kp_tmp.default_username, "Test User") + + self.kp_tmp.save() + self.kp_tmp.reload() + + self.assertEqual(self.kp_tmp.database_name, "Test Name") + self.assertEqual(self.kp_tmp.database_description, "Test Description") + self.assertEqual(self.kp_tmp.default_username, "Test User") + +class PyKeePassTests4(KDBX4Tests, PyKeePassTests3): + pass class KDBXTests(unittest.TestCase): + """Tests on KDBX parsing""" def test_open_save(self): """try to open all databases, save them, then open the result"""