Skip to content

Commit

Permalink
Add database name and description property (#389)
Browse files Browse the repository at this point in the history
* Add further database properties

Add database name, database description and default user name property.

* make separate test group for PyKeePass methods/properties

---------

Co-authored-by: Jan-Michael Brummer <[email protected]>
Co-authored-by: Evidlo <[email protected]>
  • Loading branch information
3 people authored May 17, 2024
1 parent 66bc409 commit ad8b110
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
37 changes: 37 additions & 0 deletions pykeepass/pykeepass.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
31 changes: 31 additions & 0 deletions tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"""
Expand Down

0 comments on commit ad8b110

Please sign in to comment.