def _process_edit_command(args, model): """Handle the edit command: modify an existing password entry.""" assert args.command == 'edit' # Find the entry specified on the command line. try: path_spec = storepass.model.path_string_to_spec(args.entry) old_entry = model.get_entry(path_spec) except storepass.exc.ModelException as e: print(f"{e}", file=sys.stderr) return 1 # If no new type is specified then validate that property arguments are # valid for the existing type. if args.type is None: res = _check_property_arguments(args, type(old_entry)) if res != 0: return res # Create a replacement entry. generator = _EntryGenerator(path_spec[-1]) generator.set_from_entry(old_entry) generator.set_from_args(args) new_entry = generator.get_entry() # Update the entry in the model. try: model.replace_entry(old_entry, new_entry) except storepass.exc.ModelException as e: print(f"{e}", file=sys.stderr) return 1 return 0
def test_replace_entry_empty_folder(self): """Check Model.replace_entry() can freely replace an empty folder.""" folder = storepass.model.Folder("E1 name", None, None, None, []) root = storepass.model.Root([folder]) model = storepass.model.Model(root) # Replace the (empty) folder with a generic account. generic = storepass.model.Generic("E2 name", None, None, None, None, None, None) model.replace_entry(folder, generic) self.assertEqual(len(root.children), 1) self.assertEqual(root.children[0], generic)
def test_replace_entry_transfer_children(self): """Check Model.replace_entry() transfers children between folders.""" generic = storepass.model.Generic("E1 name", None, None, None, None, None, None) folder_0 = storepass.model.Folder("E2 name", None, None, None, [generic]) root = storepass.model.Root([folder_0]) model = storepass.model.Model(root) # Replace the folder with another folder. folder_1 = storepass.model.Folder("E3 name", None, None, None, []) model.replace_entry(folder_0, folder_1) self.assertEqual(len(root.children), 1) self.assertEqual(root.children[0], folder_1) self.assertEqual(len(folder_1.children), 1) self.assertEqual(folder_1.children[0], generic)
def test_replace_entry(self): """Check Model.replace_entry() replaces entries correctly.""" generic_0 = storepass.model.Generic("E1 name", None, None, None, None, None, None) folder = storepass.model.Folder("E2 name", None, None, None, [generic_0]) root = storepass.model.Root([folder]) model = storepass.model.Model(root) # Replace the generic account. generic_1 = storepass.model.Generic("E3 name", None, None, None, None, None, None) model.replace_entry(generic_0, generic_1) self.assertEqual(len(root.children), 1) self.assertEqual(root.children[0], folder) self.assertEqual(len(folder.children), 1) self.assertEqual(folder.children[0], generic_1)
def test_replace_entry_non_empty_folder(self): """Check Model.replace_entry() rejects losing children of a folder.""" generic_0 = storepass.model.Generic("E1 name", None, None, None, None, None, None) folder = storepass.model.Folder("E2 name", None, None, None, [generic_0]) root = storepass.model.Root([folder]) model = storepass.model.Model(root) # Try replacing the (non-empty) folder with a generic account. generic_1 = storepass.model.Generic("E3 name", None, None, None, None, None, None) with self.assertRaises(storepass.exc.ModelException) as cm: model.replace_entry(folder, generic_1) self.assertEqual( str(cm.exception), "Entry 'E2 name' is not empty and cannot be replaced by " "a non-folder type") self.assertEqual(len(root.children), 1) self.assertEqual(root.children[0], folder) self.assertEqual(len(folder.children), 1) self.assertEqual(folder.children[0], generic_0)
def test_replace_entry_duplicated(self): """Check Model.replace_entry() rejects rename to a duplicated entry.""" generic_0 = storepass.model.Generic("E1 name", None, None, None, None, None, None) generic_1 = storepass.model.Generic("E2 name", None, None, None, None, None, None) folder = storepass.model.Folder("E3 name", None, None, None, [generic_0, generic_1]) root = storepass.model.Root([folder]) model = storepass.model.Model(root) # Try replacing the first generic account with an entry that has the # same name as the second one. generic_2 = storepass.model.Generic("E2 name", None, None, None, None, None, None) with self.assertRaises(storepass.exc.ModelException) as cm: model.replace_entry(generic_0, generic_2) self.assertEqual(str(cm.exception), "Entry 'E3 name/E2 name' already exists") self.assertEqual(len(root.children), 1) self.assertEqual(root.children[0], folder) self.assertEqual(len(folder.children), 2) self.assertEqual(folder.children[0], generic_0) self.assertEqual(folder.children[1], generic_1)