diff --git a/hc/api/tests/test_sendreports.py b/hc/api/tests/test_sendreports.py index c17e1cd1..09ed4c95 100644 --- a/hc/api/tests/test_sendreports.py +++ b/hc/api/tests/test_sendreports.py @@ -1,6 +1,6 @@ -from datetime import timedelta +from datetime import timedelta as td -from django.utils import timezone +from django.utils.timezone import now from hc.api.management.commands.sendreports import Command from hc.api.models import Check from hc.test import BaseTestCase @@ -8,13 +8,42 @@ from hc.test import BaseTestCase class SendAlertsTestCase(BaseTestCase): - def test_it_sends_report(self): - # Make alice eligible for reports - self.alice.date_joined = timezone.now() - timedelta(days=365) + def setUp(self): + super(SendAlertsTestCase, self).setUp() + + # Make alice eligible for reports: + # account needs to be more than one month old + self.alice.date_joined = now() - td(days=365) self.alice.save() - check = Check(user=self.alice, last_ping=timezone.now()) - check.save() + # And it needs at least one check that has been pinged. + self.check = Check(user=self.alice, last_ping=now()) + self.check.save() + def test_it_sends_report(self): sent = Command().handle_one_run() self.assertEqual(sent, 1) + + # Alice's profile should have been updated + self.profile.refresh_from_db() + self.assertTrue(self.profile.next_report_date > now()) + + def test_it_obeys_next_report_date(self): + self.profile.next_report_date = now() + td(days=1) + self.profile.save() + + sent = Command().handle_one_run() + self.assertEqual(sent, 0) + + def test_it_obeys_reports_allowed_flag(self): + self.profile.reports_allowed = False + self.profile.save() + + sent = Command().handle_one_run() + self.assertEqual(sent, 0) + + def test_it_requires_pinged_checks(self): + self.check.delete() + + sent = Command().handle_one_run() + self.assertEqual(sent, 0)