@ -0,0 +1,23 @@ | |||||
# Generated by Django 3.0.8 on 2020-07-21 10:00 | |||||
from django.db import migrations, models | |||||
class Migration(migrations.Migration): | |||||
dependencies = [ | |||||
('api', '0072_auto_20200701_1007'), | |||||
] | |||||
operations = [ | |||||
migrations.AddField( | |||||
model_name='check', | |||||
name='subject_fail', | |||||
field=models.CharField(blank=True, max_length=100), | |||||
), | |||||
migrations.AlterField( | |||||
model_name='channel', | |||||
name='kind', | |||||
field=models.CharField(choices=[('email', 'Email'), ('webhook', 'Webhook'), ('hipchat', 'HipChat'), ('slack', 'Slack'), ('pd', 'PagerDuty'), ('pagertree', 'PagerTree'), ('pagerteam', 'Pager Team'), ('po', 'Pushover'), ('pushbullet', 'Pushbullet'), ('opsgenie', 'OpsGenie'), ('victorops', 'VictorOps'), ('discord', 'Discord'), ('telegram', 'Telegram'), ('sms', 'SMS'), ('zendesk', 'Zendesk'), ('trello', 'Trello'), ('matrix', 'Matrix'), ('whatsapp', 'WhatsApp'), ('apprise', 'Apprise'), ('mattermost', 'Mattermost'), ('msteams', 'Microsoft Teams'), ('shell', 'Shell Command'), ('zulip', 'Zulip'), ('spike', 'Spike')], max_length=20), | |||||
), | |||||
] |
@ -0,0 +1,76 @@ | |||||
from hc.api.models import Check, Ping | |||||
from hc.test import BaseTestCase | |||||
from hc.api.management.commands.smtpd import _process_message | |||||
PAYLOAD_TMPL = """ | |||||
From: "User Name" <username@gmail.com> | |||||
To: "John Smith" <john@example.com> | |||||
Subject: %s | |||||
... | |||||
""".strip() | |||||
class SmtpdTestCase(BaseTestCase): | |||||
def setUp(self): | |||||
super().setUp() | |||||
self.check = Check.objects.create(project=self.project) | |||||
self.email = "%s@does.not.matter" % self.check.code | |||||
def test_it_works(self): | |||||
_process_message("1.2.3.4", "[email protected]", self.email, b"hello world") | |||||
ping = Ping.objects.latest("id") | |||||
self.assertEqual(ping.scheme, "email") | |||||
self.assertEqual(ping.ua, "Email from [email protected]") | |||||
self.assertEqual(ping.body, "hello world") | |||||
self.assertEqual(ping.kind, None) | |||||
def test_it_handles_subject_filter_match(self): | |||||
self.check.subject = "SUCCESS" | |||||
self.check.save() | |||||
body = PAYLOAD_TMPL % "[SUCCESS] Backup completed" | |||||
_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, None) | |||||
def test_it_handles_subject_filter_miss(self): | |||||
self.check.subject = "SUCCESS" | |||||
self.check.save() | |||||
body = PAYLOAD_TMPL % "[FAIL] 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, "ign") | |||||
def test_it_handles_subject_fail_filter_match(self): | |||||
self.check.subject_fail = "FAIL" | |||||
self.check.save() | |||||
body = PAYLOAD_TMPL % "[FAIL] 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_subject_fail_filter_miss(self): | |||||
self.check.subject_fail = "FAIL" | |||||
self.check.save() | |||||
body = PAYLOAD_TMPL % "[SUCCESS] Backup completed" | |||||
_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, "ign") |
@ -11,15 +11,21 @@ class FilteringRulesTestCase(BaseTestCase): | |||||
self.redirect_url = "/checks/%s/details/" % self.check.code | self.redirect_url = "/checks/%s/details/" % self.check.code | ||||
def test_it_works(self): | def test_it_works(self): | ||||
payload = { | |||||
"subject": "SUCCESS", | |||||
"subject_fail": "ERROR", | |||||
"methods": "POST", | |||||
"manual_resume": "1", | |||||
"filter_by_subject": "yes", | |||||
} | |||||
self.client.login(username="[email protected]", password="password") | self.client.login(username="[email protected]", password="password") | ||||
r = self.client.post( | |||||
self.url, | |||||
data={"subject": "SUCCESS", "methods": "POST", "manual_resume": "1"}, | |||||
) | |||||
r = self.client.post(self.url, data=payload,) | |||||
self.assertRedirects(r, self.redirect_url) | self.assertRedirects(r, self.redirect_url) | ||||
self.check.refresh_from_db() | self.check.refresh_from_db() | ||||
self.assertEqual(self.check.subject, "SUCCESS") | self.assertEqual(self.check.subject, "SUCCESS") | ||||
self.assertEqual(self.check.subject_fail, "ERROR") | |||||
self.assertEqual(self.check.methods, "POST") | self.assertEqual(self.check.methods, "POST") | ||||
self.assertTrue(self.check.manual_resume) | self.assertTrue(self.check.manual_resume) | ||||
@ -27,8 +33,10 @@ class FilteringRulesTestCase(BaseTestCase): | |||||
self.check.method = "POST" | self.check.method = "POST" | ||||
self.check.save() | self.check.save() | ||||
payload = {"subject": "SUCCESS", "methods": "", "filter_by_subject": "yes"} | |||||
self.client.login(username="[email protected]", password="password") | self.client.login(username="[email protected]", password="password") | ||||
r = self.client.post(self.url, data={"subject": "SUCCESS", "methods": ""}) | |||||
r = self.client.post(self.url, data=payload) | |||||
self.assertRedirects(r, self.redirect_url) | self.assertRedirects(r, self.redirect_url) | ||||
self.check.refresh_from_db() | self.check.refresh_from_db() | ||||
@ -36,21 +44,30 @@ class FilteringRulesTestCase(BaseTestCase): | |||||
def test_it_clears_subject(self): | def test_it_clears_subject(self): | ||||
self.check.subject = "SUCCESS" | self.check.subject = "SUCCESS" | ||||
self.check.subject_fail = "ERROR" | |||||
self.check.save() | self.check.save() | ||||
payload = { | |||||
"methods": "", | |||||
"filter_by_subject": "no", | |||||
"subject": "foo", | |||||
"subject_fail": "bar", | |||||
} | |||||
self.client.login(username="[email protected]", password="password") | self.client.login(username="[email protected]", password="password") | ||||
r = self.client.post(self.url, data={"methods": ""}) | |||||
r = self.client.post(self.url, data=payload) | |||||
self.assertRedirects(r, self.redirect_url) | self.assertRedirects(r, self.redirect_url) | ||||
self.check.refresh_from_db() | self.check.refresh_from_db() | ||||
self.assertEqual(self.check.subject, "") | self.assertEqual(self.check.subject, "") | ||||
self.assertEqual(self.check.subject_fail, "") | |||||
def test_it_clears_manual_resume_flag(self): | def test_it_clears_manual_resume_flag(self): | ||||
self.check.manual_resume = True | self.check.manual_resume = True | ||||
self.check.save() | self.check.save() | ||||
self.client.login(username="[email protected]", password="password") | self.client.login(username="[email protected]", password="password") | ||||
r = self.client.post(self.url, data={}) | |||||
r = self.client.post(self.url, data={"filter_by_subject": "no"}) | |||||
self.assertRedirects(r, self.redirect_url) | self.assertRedirects(r, self.redirect_url) | ||||
self.check.refresh_from_db() | self.check.refresh_from_db() | ||||