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.

114 lines
3.6 KiB

  1. from datetime import datetime, timedelta as td
  2. from unittest.mock import Mock, patch
  3. from django.core import mail
  4. from django.utils.timezone import now, utc
  5. from hc.test import BaseTestCase
  6. from hc.api.models import Check
  7. CURRENT_TIME = datetime(2020, 1, 15, tzinfo=utc)
  8. MOCK_NOW = Mock(return_value=CURRENT_TIME)
  9. class ProfileModelTestCase(BaseTestCase):
  10. @patch("hc.lib.date.timezone.now", MOCK_NOW)
  11. def test_it_sends_report(self):
  12. check = Check(project=self.project, name="Test Check")
  13. check.last_ping = now()
  14. check.save()
  15. sent = self.profile.send_report()
  16. self.assertTrue(sent)
  17. # And an email should have been sent
  18. self.assertEqual(len(mail.outbox), 1)
  19. message = mail.outbox[0]
  20. self.assertEqual(message.subject, "Monthly Report")
  21. self.assertIn("Test Check", message.body)
  22. html, _ = message.alternatives[0]
  23. self.assertNotIn("Jan. 2020", html)
  24. self.assertIn("Dec. 2019", html)
  25. self.assertIn("Nov. 2019", html)
  26. self.assertNotIn("Oct. 2019", html)
  27. def test_it_skips_report_if_no_pings(self):
  28. check = Check(project=self.project, name="Test Check")
  29. check.save()
  30. sent = self.profile.send_report()
  31. self.assertFalse(sent)
  32. self.assertEqual(len(mail.outbox), 0)
  33. def test_it_skips_report_if_no_recent_pings(self):
  34. check = Check(project=self.project, name="Test Check")
  35. check.last_ping = now() - td(days=365)
  36. check.save()
  37. sent = self.profile.send_report()
  38. self.assertFalse(sent)
  39. self.assertEqual(len(mail.outbox), 0)
  40. def test_it_sends_nag(self):
  41. check = Check(project=self.project, name="Test Check")
  42. check.status = "down"
  43. check.last_ping = now()
  44. check.save()
  45. self.profile.nag_period = td(hours=1)
  46. self.profile.save()
  47. sent = self.profile.send_report(nag=True)
  48. self.assertTrue(sent)
  49. # And an email should have been sent
  50. self.assertEqual(len(mail.outbox), 1)
  51. message = mail.outbox[0]
  52. self.assertEqual(message.subject, "Reminder: 1 check still down")
  53. self.assertIn("Test Check", message.body)
  54. def test_it_skips_nag_if_none_down(self):
  55. check = Check(project=self.project, name="Test Check")
  56. check.last_ping = now()
  57. check.save()
  58. self.profile.nag_period = td(hours=1)
  59. self.profile.save()
  60. sent = self.profile.send_report(nag=True)
  61. self.assertFalse(sent)
  62. self.assertEqual(len(mail.outbox), 0)
  63. def test_it_sets_next_nag_date(self):
  64. Check.objects.create(project=self.project, status="down")
  65. self.profile.nag_period = td(hours=1)
  66. self.profile.update_next_nag_date()
  67. self.assertTrue(self.profile.next_nag_date)
  68. def test_it_does_not_set_next_nag_date_if_no_nag_period(self):
  69. Check.objects.create(project=self.project, status="down")
  70. self.profile.update_next_nag_date()
  71. self.assertIsNone(self.profile.next_nag_date)
  72. def test_it_does_not_update_existing_next_nag_date(self):
  73. Check.objects.create(project=self.project, status="down")
  74. original_nag_date = now() - td(minutes=30)
  75. self.profile.next_nag_date = original_nag_date
  76. self.profile.nag_period = td(hours=1)
  77. self.profile.update_next_nag_date()
  78. self.assertEqual(self.profile.next_nag_date, original_nag_date)
  79. def test_it_clears_next_nag_date(self):
  80. self.profile.next_nag_date = now()
  81. self.profile.update_next_nag_date()
  82. self.assertIsNone(self.profile.next_nag_date)