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.

109 lines
3.3 KiB

  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, patch
  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.status = "down"
  22. self.check.save()
  23. def test_it_sends_report(self):
  24. cmd = Command()
  25. cmd.stdout = Mock() # silence output to stdout
  26. cmd.pause = Mock() # don't pause for 1s
  27. found = cmd.handle_one_monthly_report()
  28. self.assertTrue(found)
  29. self.profile.refresh_from_db()
  30. self.assertTrue(self.profile.next_report_date > now())
  31. self.assertEqual(len(mail.outbox), 1)
  32. email = mail.outbox[0]
  33. self.assertTrue("List-Unsubscribe" in email.extra_headers)
  34. def test_it_obeys_next_report_date(self):
  35. self.profile.next_report_date = now() + td(days=1)
  36. self.profile.save()
  37. found = Command().handle_one_monthly_report()
  38. self.assertFalse(found)
  39. def test_it_obeys_reports_allowed_flag(self):
  40. self.profile.reports_allowed = False
  41. self.profile.save()
  42. found = Command().handle_one_monthly_report()
  43. self.assertFalse(found)
  44. def test_it_requires_pinged_checks(self):
  45. self.check.delete()
  46. found = Command().handle_one_monthly_report()
  47. self.assertTrue(found)
  48. # No email should have been sent:
  49. self.assertEqual(len(mail.outbox), 0)
  50. def test_it_sends_nag(self):
  51. cmd = Command()
  52. cmd.stdout = Mock() # silence output to stdout
  53. cmd.pause = Mock() # don't pause for 1s
  54. found = cmd.handle_one_nag()
  55. self.assertTrue(found)
  56. self.profile.refresh_from_db()
  57. self.assertTrue(self.profile.next_nag_date > now())
  58. self.assertEqual(len(mail.outbox), 1)
  59. def test_it_obeys_next_nag_date(self):
  60. self.profile.next_nag_date = now() + td(days=1)
  61. self.profile.save()
  62. # If next_nag_date is in future, a nag should not get sent.
  63. found = Command().handle_one_nag()
  64. self.assertFalse(found)
  65. def test_it_obeys_nag_period(self):
  66. self.profile.nag_period = td()
  67. self.profile.save()
  68. # If nag_period is 0 ("disabled"), a nag should not get sent.
  69. found = Command().handle_one_nag()
  70. self.assertFalse(found)
  71. def test_nags_require_down_checks(self):
  72. self.check.status = "up"
  73. self.check.save()
  74. found = Command().handle_one_nag()
  75. self.assertTrue(found)
  76. # No email should have been sent:
  77. self.assertEqual(len(mail.outbox), 0)
  78. # next_nag_date should now be unset
  79. self.profile.refresh_from_db()
  80. self.assertIsNone(self.profile.next_nag_date)