You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

110 lines
3.4 KiB

6 years ago
6 years ago
  1. from datetime import timedelta as td
  2. from django.core import mail
  3. from django.utils.timezone import now
  4. from hc.api.management.commands.sendreports import Command
  5. from hc.api.models import Check
  6. from hc.test import BaseTestCase
  7. from mock import Mock
  8. class SendAlertsTestCase(BaseTestCase):
  9. def setUp(self):
  10. super(SendAlertsTestCase, self).setUp()
  11. # Make alice eligible for reports:
  12. # account needs to be more than one month old
  13. self.alice.date_joined = now() - td(days=365)
  14. self.alice.save()
  15. # Make alice eligible for nags:
  16. self.profile.nag_period = td(hours=1)
  17. self.profile.next_nag_date = now() - td(seconds=10)
  18. self.profile.save()
  19. # And it needs at least one check that has been pinged.
  20. self.check = Check(user=self.alice, last_ping=now())
  21. self.check.project = self.project
  22. self.check.status = "down"
  23. self.check.save()
  24. def test_it_sends_report(self):
  25. cmd = Command()
  26. cmd.stdout = Mock() # silence output to stdout
  27. cmd.pause = Mock() # don't pause for 1s
  28. found = cmd.handle_one_monthly_report()
  29. self.assertTrue(found)
  30. self.profile.refresh_from_db()
  31. self.assertTrue(self.profile.next_report_date > now())
  32. self.assertEqual(len(mail.outbox), 1)
  33. email = mail.outbox[0]
  34. self.assertTrue("List-Unsubscribe" in email.extra_headers)
  35. def test_it_obeys_next_report_date(self):
  36. self.profile.next_report_date = now() + td(days=1)
  37. self.profile.save()
  38. found = Command().handle_one_monthly_report()
  39. self.assertFalse(found)
  40. def test_it_obeys_reports_allowed_flag(self):
  41. self.profile.reports_allowed = False
  42. self.profile.save()
  43. found = Command().handle_one_monthly_report()
  44. self.assertFalse(found)
  45. def test_it_requires_pinged_checks(self):
  46. self.check.delete()
  47. found = Command().handle_one_monthly_report()
  48. self.assertTrue(found)
  49. # No email should have been sent:
  50. self.assertEqual(len(mail.outbox), 0)
  51. def test_it_sends_nag(self):
  52. cmd = Command()
  53. cmd.stdout = Mock() # silence output to stdout
  54. cmd.pause = Mock() # don't pause for 1s
  55. found = cmd.handle_one_nag()
  56. self.assertTrue(found)
  57. self.profile.refresh_from_db()
  58. self.assertTrue(self.profile.next_nag_date > now())
  59. self.assertEqual(len(mail.outbox), 1)
  60. def test_it_obeys_next_nag_date(self):
  61. self.profile.next_nag_date = now() + td(days=1)
  62. self.profile.save()
  63. # If next_nag_date is in future, a nag should not get sent.
  64. found = Command().handle_one_nag()
  65. self.assertFalse(found)
  66. def test_it_obeys_nag_period(self):
  67. self.profile.nag_period = td()
  68. self.profile.save()
  69. # If nag_period is 0 ("disabled"), a nag should not get sent.
  70. found = Command().handle_one_nag()
  71. self.assertFalse(found)
  72. def test_nags_require_down_checks(self):
  73. self.check.status = "up"
  74. self.check.save()
  75. found = Command().handle_one_nag()
  76. self.assertTrue(found)
  77. # No email should have been sent:
  78. self.assertEqual(len(mail.outbox), 0)
  79. # next_nag_date should now be unset
  80. self.profile.refresh_from_db()
  81. self.assertIsNone(self.profile.next_nag_date)