From 785ea7b614ff77d9de4b030ab35a3a1a9b4a1add Mon Sep 17 00:00:00 2001 From: swoga Date: Tue, 29 Jun 2021 11:17:23 +0200 Subject: [PATCH] feat: treat failure before success --- hc/api/management/commands/smtpd.py | 6 +++--- hc/api/tests/test_smtpd.py | 13 +++++++++++++ 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/hc/api/management/commands/smtpd.py b/hc/api/management/commands/smtpd.py index 001906ec..da288d22 100644 --- a/hc/api/management/commands/smtpd.py +++ b/hc/api/management/commands/smtpd.py @@ -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) diff --git a/hc/api/tests/test_smtpd.py b/hc/api/tests/test_smtpd.py index db8a0a8f..0a909cc5 100644 --- a/hc/api/tests/test_smtpd.py +++ b/hc/api/tests/test_smtpd.py @@ -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()