Skip to content

Commit

Permalink
fix: rm/restore without parameters
Browse files Browse the repository at this point in the history
rm/restore without parameters must not recycle/restore all entries
  • Loading branch information
cizmazia committed Jan 6, 2024
1 parent 8179a67 commit d7f1989
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 12 deletions.
33 changes: 21 additions & 12 deletions kdbx_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,11 @@ def do_sshgen(self, _arg=None):

def do_rm(self, arg):
'rm ENTRY... # move entries to recycle bin'
for e in self.ui.entries(arg.split(), required=False):
entries = self.ui.find_entries(arg.split())
if not entries:
print(None)
return
for e in entries:
self.ui.trash_entry(e)
self.ui.save()

Expand All @@ -429,9 +433,9 @@ def complete_rm(self, text, line, begin, end):

def do_restore(self, arg):
'restore [ENTRY]... # restore entries from recycle bin'
e = self.ui.entries(arg.split(), required=False)
if e:
self.ui.restore_entry(e)
entries = self.ui.find_entries(arg.split())
if entries:
self.ui.restore_entry(entries)
self.ui.save()
else:
print('\n'.join(sorted(self.ui.titles(recycled=True))))
Expand All @@ -442,8 +446,8 @@ def complete_restore(self, text, line, begin, end):
def do_cp(self, arg):
'cp FILE ENTRY... # copy entries to db file'
a = arg.split() or ['']
e = self.ui.entries(a[1:])
if export(a[0], e):
entries = self.ui.find_entries(a[1:])
if export(a[0], entries):
pass

def complete_cp(self, text, line, begin, end):
Expand All @@ -459,9 +463,9 @@ def complete_cp(self, text, line, begin, end):
def do_mv(self, arg):
'mv FILE ENTRY... # copy entries to db file, then move to recycle bin'
args = arg.split() or ['']
e = self.ui.entries(args[1:])
if export(args[0], e):
for entry in e:
entries = self.ui.find_entries(args[1:])
if export(args[0], entries):
for entry in entries:
self.ui.trash_entry(entry)
self.ui.save()

Expand Down Expand Up @@ -859,13 +863,18 @@ def find_entry(self, title):
return None
return self._kp.find_entries(title=title, first=True)

def entries(self, titles=None, required=True):
def find_entries(self, titles):
if self.locked():
return []
if not titles:
return self._kp.entries
return []
res = [e for e in self._kp.entries if e.title in titles]
return res if not required or len(titles) == len(res) else None
return res if len(titles) == len(res) else []

def entries(self):
if self.locked():
return []
return self._kp.entries

def titles(self, prefix='', recycled=False):
if self.locked():
Expand Down
39 changes: 39 additions & 0 deletions test_kdbx_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,34 @@ def test_do_rename(self):
e = self.open_kp().find_entries(title=title, first=True)
self.assertEqual(e.title, title)

def test_do_rm(self):
cmd = KpCmd(self.kp)
title = 'test_do_rm'
e = cmd.ui.add_entry(title)
self.assertFalse(cmd.ui.in_recycle_bin(e))
cmd.do_rm(title)
self.assertTrue(cmd.ui.in_recycle_bin(e))

@patch('builtins.print')
def test_dont_rm(self, mock_print):
cmd = KpCmd(self.kp)
title = 'test_dont_rm'
e = cmd.ui.add_entry(title)
self.assertFalse(cmd.ui.in_recycle_bin(e))
cmd.do_rm('')
self.assertFalse(cmd.ui.in_recycle_bin(e))
self.assertEqual(mock_print.mock_calls, [call(None)])

@patch('builtins.print')
def test_dont_rm_wrong(self, mock_print):
cmd = KpCmd(self.kp)
title = 'test_dont_rm_wrong'
e = cmd.ui.add_entry(title)
self.assertFalse(cmd.ui.in_recycle_bin(e))
cmd.do_rm(f'{title} wrong')
self.assertFalse(cmd.ui.in_recycle_bin(e))
self.assertEqual(mock_print.mock_calls, [call(None)])

def test_do_restore(self):
cmd = KpCmd(self.kp)
title = 'test_do_restore'
Expand All @@ -59,6 +87,17 @@ def test_do_restore(self):
cmd.do_restore(title)
self.assertFalse(cmd.ui.in_recycle_bin(e))

@patch('builtins.print')
def test_dont_restore_but_list(self, mock_print):
cmd = KpCmd(self.kp)
title = 'test_dont_restore_but_list'
e = cmd.ui.add_entry(title)
cmd.ui.trash_entry(e)
self.assertTrue(cmd.ui.in_recycle_bin(e))
cmd.do_restore('')
self.assertTrue(cmd.ui.in_recycle_bin(e))
self.assertIn(title, mock_print.call_args.args[0])

def test_do_dedup(self):
cmd = KpCmd(self.kp)
title = 'test_dup'
Expand Down

0 comments on commit d7f1989

Please sign in to comment.