More tests for sendreports.

This commit is contained in:
Pēteris Caune 2017-01-15 16:24:08 +02:00
parent 8d956b3365
commit ae4144b1cd

View File

@ -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.management.commands.sendreports import Command
from hc.api.models import Check from hc.api.models import Check
from hc.test import BaseTestCase from hc.test import BaseTestCase
@ -8,13 +8,42 @@ from hc.test import BaseTestCase
class SendAlertsTestCase(BaseTestCase): class SendAlertsTestCase(BaseTestCase):
def test_it_sends_report(self): def setUp(self):
# Make alice eligible for reports super(SendAlertsTestCase, self).setUp()
self.alice.date_joined = timezone.now() - timedelta(days=365)
# 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() self.alice.save()
check = Check(user=self.alice, last_ping=timezone.now()) # And it needs at least one check that has been pinged.
check.save() self.check = Check(user=self.alice, last_ping=now())
self.check.save()
def test_it_sends_report(self):
sent = Command().handle_one_run() sent = Command().handle_one_run()
self.assertEqual(sent, 1) 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)