Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add database name and description property #389

Merged
merged 2 commits into from
May 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
janbrummer marked this conversation as resolved.
Show resolved Hide resolved
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
janbrummer marked this conversation as resolved.
Show resolved Hide resolved

@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