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.

128 lines
4.2 KiB

9 years ago
  1. from datetime import timedelta as td
  2. from django.core import mail
  3. from django.conf import settings
  4. from django.utils.timezone import now
  5. from hc.test import BaseTestCase
  6. from hc.api.models import Check
  7. class ProfileTestCase(BaseTestCase):
  8. def test_it_sends_set_password_link(self):
  9. self.client.login(username="[email protected]", password="password")
  10. form = {"set_password": "1"}
  11. r = self.client.post("/accounts/profile/", form)
  12. assert r.status_code == 302
  13. # profile.token should be set now
  14. self.profile.refresh_from_db()
  15. token = self.profile.token
  16. self.assertTrue(len(token) > 10)
  17. # And an email should have been sent
  18. self.assertEqual(len(mail.outbox), 1)
  19. expected_subject = "Set password on %s" % settings.SITE_NAME
  20. self.assertEqual(mail.outbox[0].subject, expected_subject)
  21. def test_it_sends_report(self):
  22. check = Check(project=self.project, name="Test Check")
  23. check.last_ping = now()
  24. check.save()
  25. sent = self.profile.send_report()
  26. self.assertTrue(sent)
  27. # And an email should have been sent
  28. self.assertEqual(len(mail.outbox), 1)
  29. message = mail.outbox[0]
  30. self.assertEqual(message.subject, "Monthly Report")
  31. self.assertIn("Test Check", message.body)
  32. def test_it_skips_report_if_no_pings(self):
  33. check = Check(project=self.project, name="Test Check")
  34. check.save()
  35. sent = self.profile.send_report()
  36. self.assertFalse(sent)
  37. self.assertEqual(len(mail.outbox), 0)
  38. def test_it_skips_report_if_no_recent_pings(self):
  39. check = Check(project=self.project, name="Test Check")
  40. check.last_ping = now() - td(days=365)
  41. check.save()
  42. sent = self.profile.send_report()
  43. self.assertFalse(sent)
  44. self.assertEqual(len(mail.outbox), 0)
  45. def test_it_sends_nag(self):
  46. check = Check(project=self.project, name="Test Check")
  47. check.status = "down"
  48. check.last_ping = now()
  49. check.save()
  50. self.profile.nag_period = td(hours=1)
  51. self.profile.save()
  52. sent = self.profile.send_report(nag=True)
  53. self.assertTrue(sent)
  54. # And an email should have been sent
  55. self.assertEqual(len(mail.outbox), 1)
  56. message = mail.outbox[0]
  57. self.assertEqual(message.subject, "Reminder: 1 check still down")
  58. self.assertIn("Test Check", message.body)
  59. def test_it_skips_nag_if_none_down(self):
  60. check = Check(project=self.project, name="Test Check")
  61. check.last_ping = now()
  62. check.save()
  63. self.profile.nag_period = td(hours=1)
  64. self.profile.save()
  65. sent = self.profile.send_report(nag=True)
  66. self.assertFalse(sent)
  67. self.assertEqual(len(mail.outbox), 0)
  68. def test_it_sends_change_email_link(self):
  69. self.client.login(username="[email protected]", password="password")
  70. form = {"change_email": "1"}
  71. r = self.client.post("/accounts/profile/", form)
  72. assert r.status_code == 302
  73. # profile.token should be set now
  74. self.profile.refresh_from_db()
  75. token = self.profile.token
  76. self.assertTrue(len(token) > 10)
  77. # And an email should have been sent
  78. self.assertEqual(len(mail.outbox), 1)
  79. expected_subject = "Change email address on %s" % settings.SITE_NAME
  80. self.assertEqual(mail.outbox[0].subject, expected_subject)
  81. def test_leaving_works(self):
  82. self.client.login(username="[email protected]", password="password")
  83. form = {"code": str(self.project.code), "leave_project": "1"}
  84. r = self.client.post("/accounts/profile/", form)
  85. self.assertContains(r, "Left project")
  86. self.assertNotContains(r, "Alice's Project")
  87. self.bobs_profile.refresh_from_db()
  88. self.assertIsNone(self.bobs_profile.current_project)
  89. self.assertFalse(self.bob.memberships.exists())
  90. def test_leaving_checks_membership(self):
  91. self.client.login(username="[email protected]", password="password")
  92. form = {"code": str(self.project.code), "leave_project": "1"}
  93. r = self.client.post("/accounts/profile/", form)
  94. self.assertEqual(r.status_code, 400)