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.

113 lines
4.0 KiB

  1. from datetime import timedelta as td
  2. from django.utils.timezone import now
  3. from hc.api.models import Check
  4. from hc.test import BaseTestCase
  5. class NotificationsTestCase(BaseTestCase):
  6. url = "/accounts/profile/notifications/"
  7. def _payload(self, **kwargs):
  8. result = {"reports": "monthly", "nag_period": "0", "tz": "Europe/Riga"}
  9. result.update(kwargs)
  10. return result
  11. def test_it_saves_reports_monthly(self):
  12. self.profile.reports = "off"
  13. self.profile.save()
  14. self.client.login(username="[email protected]", password="password")
  15. r = self.client.post(self.url, self._payload())
  16. self.assertEqual(r.status_code, 200)
  17. self.profile.refresh_from_db()
  18. self.assertEqual(self.profile.reports, "monthly")
  19. self.assertEqual(self.profile.next_report_date.day, 1)
  20. def test_it_saves_reports_weekly(self):
  21. self.profile.reports = "off"
  22. self.profile.save()
  23. self.client.login(username="[email protected]", password="password")
  24. r = self.client.post(self.url, self._payload(reports="weekly"))
  25. self.assertEqual(r.status_code, 200)
  26. self.profile.refresh_from_db()
  27. self.assertEqual(self.profile.reports, "weekly")
  28. self.assertEqual(self.profile.next_report_date.weekday(), 0)
  29. def test_it_saves_reports_off(self):
  30. self.profile.reports = "monthly"
  31. self.profile.next_report_date = now()
  32. self.profile.save()
  33. self.client.login(username="[email protected]", password="password")
  34. r = self.client.post(self.url, self._payload(reports="off"))
  35. self.assertEqual(r.status_code, 200)
  36. self.profile.refresh_from_db()
  37. self.assertEqual(self.profile.reports, "off")
  38. self.assertIsNone(self.profile.next_report_date)
  39. def test_it_sets_next_nag_date_when_setting_hourly_nag_period(self):
  40. Check.objects.create(project=self.project, status="down")
  41. self.client.login(username="[email protected]", password="password")
  42. r = self.client.post(self.url, self._payload(nag_period="3600"))
  43. self.assertEqual(r.status_code, 200)
  44. self.profile.refresh_from_db()
  45. self.assertEqual(self.profile.nag_period.total_seconds(), 3600)
  46. self.assertIsNotNone(self.profile.next_nag_date)
  47. def test_it_clears_next_nag_date_when_setting_hourly_nag_period(self):
  48. self.profile.next_nag_date = now() + td(minutes=30)
  49. self.profile.save()
  50. self.client.login(username="[email protected]", password="password")
  51. r = self.client.post(self.url, self._payload(nag_period="3600"))
  52. self.assertEqual(r.status_code, 200)
  53. self.profile.refresh_from_db()
  54. self.assertEqual(self.profile.nag_period.total_seconds(), 3600)
  55. self.assertIsNone(self.profile.next_nag_date)
  56. def test_it_does_not_save_nonstandard_nag_period(self):
  57. self.profile.nag_period = td(seconds=3600)
  58. self.profile.save()
  59. self.client.login(username="[email protected]", password="password")
  60. r = self.client.post(self.url, self._payload(nag_period="1234"))
  61. self.assertEqual(r.status_code, 200)
  62. self.profile.refresh_from_db()
  63. self.assertEqual(self.profile.nag_period.total_seconds(), 3600)
  64. def test_it_saves_tz(self):
  65. self.client.login(username="[email protected]", password="password")
  66. r = self.client.post(self.url, self._payload())
  67. self.assertEqual(r.status_code, 200)
  68. self.profile.refresh_from_db()
  69. self.assertEqual(self.profile.tz, "Europe/Riga")
  70. def test_it_ignores_bad_tz(self):
  71. self.profile.tz = "Europe/Riga"
  72. self.profile.save()
  73. self.client.login(username="[email protected]", password="password")
  74. r = self.client.post(self.url, self._payload(reports="weekly", tz="Foo/Bar"))
  75. self.assertEqual(r.status_code, 200)
  76. self.profile.refresh_from_db()
  77. self.assertEqual(self.profile.reports, "weekly")
  78. self.assertEqual(self.profile.tz, "Europe/Riga")