feat: treat failure before success

This commit is contained in:
swoga 2021-06-29 11:17:23 +02:00 committed by Pēteris Caune
parent 8a154cbaf5
commit b70e2c9a25
2 changed files with 16 additions and 3 deletions

View File

@ -45,10 +45,10 @@ def _process_message(remote_addr, mailfrom, mailto, data):
# Specify policy, the default policy does not decode encoded headers:
parsed = email.message_from_string(data, policy=email.policy.SMTP)
subject = parsed.get("subject", "")
if check.subject and _match(subject, check.subject):
action = "success"
elif check.subject_fail and _match(subject, check.subject_fail):
if check.subject_fail and _match(subject, check.subject_fail):
action = "fail"
elif check.subject and _match(subject, check.subject):
action = "success"
ua = "Email from %s" % mailfrom
check.ping(remote_addr, "email", "", ua, data, action)

View File

@ -99,6 +99,19 @@ class SmtpdTestCase(BaseTestCase):
self.assertEqual(ping.ua, "Email from foo@example.org")
self.assertEqual(ping.kind, "fail")
def test_it_handles_subject_fail_before_success(self):
self.check.subject = "SUCCESS"
self.check.subject_fail = "FAIL"
self.check.save()
body = PAYLOAD_TMPL % "[SUCCESS] 1 Backup completed, [FAIL] 1 Backup did not complete"
_process_message("1.2.3.4", "foo@example.org", self.email, body.encode("utf8"))
ping = Ping.objects.latest("id")
self.assertEqual(ping.scheme, "email")
self.assertEqual(ping.ua, "Email from foo@example.org")
self.assertEqual(ping.kind, "fail")
def test_it_handles_encoded_subject(self):
self.check.subject = "SUCCESS"
self.check.save()