|
@ -2,7 +2,7 @@ from datetime import timedelta |
|
|
from mock import Mock, patch |
|
|
from mock import Mock, patch |
|
|
|
|
|
|
|
|
from django.core.management import call_command |
|
|
from django.core.management import call_command |
|
|
from django.utils import timezone |
|
|
|
|
|
|
|
|
from django.utils.timezone import now |
|
|
from hc.api.management.commands.sendalerts import Command, notify |
|
|
from hc.api.management.commands.sendalerts import Command, notify |
|
|
from hc.api.models import Check |
|
|
from hc.api.models import Check |
|
|
from hc.test import BaseTestCase |
|
|
from hc.test import BaseTestCase |
|
@ -13,7 +13,7 @@ class SendAlertsTestCase(BaseTestCase): |
|
|
def test_it_handles_grace_period(self): |
|
|
def test_it_handles_grace_period(self): |
|
|
check = Check(user=self.alice, status="up") |
|
|
check = Check(user=self.alice, status="up") |
|
|
# 1 day 30 minutes after ping the check is in grace period: |
|
|
# 1 day 30 minutes after ping the check is in grace period: |
|
|
check.last_ping = timezone.now() - timedelta(days=1, minutes=30) |
|
|
|
|
|
|
|
|
check.last_ping = now() - timedelta(days=1, minutes=30) |
|
|
check.alert_after = check.get_alert_after() |
|
|
check.alert_after = check.get_alert_after() |
|
|
check.save() |
|
|
check.save() |
|
|
|
|
|
|
|
@ -23,7 +23,7 @@ class SendAlertsTestCase(BaseTestCase): |
|
|
@patch("hc.api.management.commands.sendalerts.notify_on_thread") |
|
|
@patch("hc.api.management.commands.sendalerts.notify_on_thread") |
|
|
def test_it_notifies_when_check_goes_down(self, mock_notify): |
|
|
def test_it_notifies_when_check_goes_down(self, mock_notify): |
|
|
check = Check(user=self.alice, status="up") |
|
|
check = Check(user=self.alice, status="up") |
|
|
check.last_ping = timezone.now() - timedelta(days=2) |
|
|
|
|
|
|
|
|
check.last_ping = now() - timedelta(days=2) |
|
|
check.alert_after = check.get_alert_after() |
|
|
check.alert_after = check.get_alert_after() |
|
|
check.save() |
|
|
check.save() |
|
|
|
|
|
|
|
@ -42,7 +42,7 @@ class SendAlertsTestCase(BaseTestCase): |
|
|
@patch("hc.api.management.commands.sendalerts.notify_on_thread") |
|
|
@patch("hc.api.management.commands.sendalerts.notify_on_thread") |
|
|
def test_it_notifies_when_check_goes_up(self, mock_notify): |
|
|
def test_it_notifies_when_check_goes_up(self, mock_notify): |
|
|
check = Check(user=self.alice, status="down") |
|
|
check = Check(user=self.alice, status="down") |
|
|
check.last_ping = timezone.now() |
|
|
|
|
|
|
|
|
check.last_ping = now() |
|
|
check.alert_after = check.get_alert_after() |
|
|
check.alert_after = check.get_alert_after() |
|
|
check.save() |
|
|
check.save() |
|
|
|
|
|
|
|
@ -64,7 +64,7 @@ class SendAlertsTestCase(BaseTestCase): |
|
|
@patch("hc.api.management.commands.sendalerts.notify_on_thread") |
|
|
@patch("hc.api.management.commands.sendalerts.notify_on_thread") |
|
|
def test_it_updates_alert_after(self, mock_notify): |
|
|
def test_it_updates_alert_after(self, mock_notify): |
|
|
check = Check(user=self.alice, status="up") |
|
|
check = Check(user=self.alice, status="up") |
|
|
check.last_ping = timezone.now() - timedelta(hours=1) |
|
|
|
|
|
|
|
|
check.last_ping = now() - timedelta(hours=1) |
|
|
check.alert_after = check.last_ping |
|
|
check.alert_after = check.last_ping |
|
|
check.save() |
|
|
check.save() |
|
|
|
|
|
|
|
@ -85,7 +85,7 @@ class SendAlertsTestCase(BaseTestCase): |
|
|
@patch("hc.api.management.commands.sendalerts.notify") |
|
|
@patch("hc.api.management.commands.sendalerts.notify") |
|
|
def test_it_works_synchronously(self, mock_notify): |
|
|
def test_it_works_synchronously(self, mock_notify): |
|
|
check = Check(user=self.alice, status="up") |
|
|
check = Check(user=self.alice, status="up") |
|
|
check.last_ping = timezone.now() - timedelta(days=2) |
|
|
|
|
|
|
|
|
check.last_ping = now() - timedelta(days=2) |
|
|
check.alert_after = check.get_alert_after() |
|
|
check.alert_after = check.get_alert_after() |
|
|
check.save() |
|
|
check.save() |
|
|
|
|
|
|
|
@ -99,7 +99,7 @@ class SendAlertsTestCase(BaseTestCase): |
|
|
self.profile.save() |
|
|
self.profile.save() |
|
|
|
|
|
|
|
|
check = Check(user=self.alice, status="down") |
|
|
check = Check(user=self.alice, status="down") |
|
|
check.last_ping = timezone.now() - timedelta(days=2) |
|
|
|
|
|
|
|
|
check.last_ping = now() - timedelta(days=2) |
|
|
check.alert_after = check.get_alert_after() |
|
|
check.alert_after = check.get_alert_after() |
|
|
check.save() |
|
|
check.save() |
|
|
|
|
|
|
|
@ -113,7 +113,7 @@ class SendAlertsTestCase(BaseTestCase): |
|
|
self.bobs_profile.save() |
|
|
self.bobs_profile.save() |
|
|
|
|
|
|
|
|
check = Check(user=self.alice, status="down") |
|
|
check = Check(user=self.alice, status="down") |
|
|
check.last_ping = timezone.now() - timedelta(days=2) |
|
|
|
|
|
|
|
|
check.last_ping = now() - timedelta(days=2) |
|
|
check.alert_after = check.get_alert_after() |
|
|
check.alert_after = check.get_alert_after() |
|
|
check.save() |
|
|
check.save() |
|
|
|
|
|
|
|
@ -121,3 +121,19 @@ class SendAlertsTestCase(BaseTestCase): |
|
|
|
|
|
|
|
|
self.bobs_profile.refresh_from_db() |
|
|
self.bobs_profile.refresh_from_db() |
|
|
self.assertIsNotNone(self.bobs_profile.next_nag_date) |
|
|
self.assertIsNotNone(self.bobs_profile.next_nag_date) |
|
|
|
|
|
|
|
|
|
|
|
def test_it_does_not_touch_future_next_nag_dates(self): |
|
|
|
|
|
original_nag_date = now() + timedelta(minutes=30) |
|
|
|
|
|
self.profile.nag_period = timedelta(hours=1) |
|
|
|
|
|
self.profile.next_nag_date = original_nag_date |
|
|
|
|
|
self.profile.save() |
|
|
|
|
|
|
|
|
|
|
|
check = Check(user=self.alice, status="down") |
|
|
|
|
|
check.last_ping = now() - timedelta(days=2) |
|
|
|
|
|
check.alert_after = check.get_alert_after() |
|
|
|
|
|
check.save() |
|
|
|
|
|
|
|
|
|
|
|
notify(check.id, Mock()) |
|
|
|
|
|
|
|
|
|
|
|
self.profile.refresh_from_db() |
|
|
|
|
|
self.assertEqual(self.profile.next_nag_date, original_nag_date) |