From 6ed66173754f33b41f389771495bc870fd2dc5da Mon Sep 17 00:00:00 2001 From: Erik Cederstrand Date: Thu, 2 May 2024 23:57:47 +0200 Subject: [PATCH] fix: allow deleting even though the rule is not valid. Refs #1305 --- exchangelib/properties.py | 7 ++++++- tests/test_account.py | 13 +++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/exchangelib/properties.py b/exchangelib/properties.py index ab3e16b2..593b157e 100644 --- a/exchangelib/properties.py +++ b/exchangelib/properties.py @@ -2302,8 +2302,13 @@ def save(self): def delete(self): if self.is_enabled is False: - # Cannot delete a disabled rule - server throws 'ErrorItemNotFound' + # Cannot delete a disabled rule - the server throws 'ErrorItemNotFound'. We need to enable it first. self.is_enabled = True + # Make sure we can save the rule by wiping all possibly-misconfigured fields + self.priority = 10**6 + self.conditions = None + self.exceptions = None + self.actions = Actions(stop_processing_rules=True) self.save() self.account.delete_rule(self) diff --git a/tests/test_account.py b/tests/test_account.py index 21e18e7c..a65f3a24 100644 --- a/tests/test_account.py +++ b/tests/test_account.py @@ -372,6 +372,19 @@ def test_basic_inbox_rule(self): rule.delete() self.assertNotIn(rule.display_name, {r.display_name for r in self.account.rules}) + def test_disabled_inbox_rule(self): + # Make sure we can delete a disabled rule + rule = Rule( + account=self.account, + display_name=get_random_string(16), + priority=10**6, + is_enabled=False, + actions=Actions(stop_processing_rules=True), + ) + rule.save() + rule.actions = Actions(forward_to_recipients=[[Address()]]) # Test with an invalid action + rule.delete() + def test_all_inbox_rule_actions(self): for action_name, action in { "assign_categories": ["foo", "bar"],