Browse Source

feat: treat failure before success

pull/540/head
swoga 3 years ago
committed by Pēteris Caune
parent
commit
b70e2c9a25
2 changed files with 16 additions and 3 deletions
  1. +3
    -3
      hc/api/management/commands/smtpd.py
  2. +13
    -0
      hc/api/tests/test_smtpd.py

+ 3
- 3
hc/api/management/commands/smtpd.py 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)


+ 13
- 0
hc/api/tests/test_smtpd.py View File

@ -99,6 +99,19 @@ class SmtpdTestCase(BaseTestCase):
self.assertEqual(ping.ua, "Email from [email protected]")
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", "[email protected]", self.email, body.encode("utf8"))
ping = Ping.objects.latest("id")
self.assertEqual(ping.scheme, "email")
self.assertEqual(ping.ua, "Email from [email protected]")
self.assertEqual(ping.kind, "fail")
def test_it_handles_encoded_subject(self):
self.check.subject = "SUCCESS"
self.check.save()


Loading…
Cancel
Save