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.

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