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.

115 lines
4.3 KiB

  1. import json
  2. from django.core import mail
  3. from django.test.utils import override_settings
  4. from hc.api.models import Channel, Check
  5. from hc.test import BaseTestCase
  6. class EditEmailTestCase(BaseTestCase):
  7. def setUp(self):
  8. super().setUp()
  9. self.check = Check.objects.create(project=self.project)
  10. self.channel = Channel(project=self.project, kind="email")
  11. self.channel.value = json.dumps(
  12. {"value": "[email protected]", "up": True, "down": True}
  13. )
  14. self.channel.email_verified = True
  15. self.channel.save()
  16. self.url = f"/integrations/{self.channel.code}/edit/"
  17. def test_it_shows_form(self):
  18. self.client.login(username="[email protected]", password="password")
  19. r = self.client.get(self.url)
  20. self.assertContains(r, "Get an email message when check goes up or down.")
  21. self.assertContains(r, "[email protected]")
  22. self.assertContains(r, "Email Settings")
  23. def test_it_saves_changes(self):
  24. form = {"value": "[email protected]", "down": "true", "up": "false"}
  25. self.client.login(username="[email protected]", password="password")
  26. r = self.client.post(self.url, form)
  27. self.assertRedirects(r, self.channels_url)
  28. self.channel.refresh_from_db()
  29. self.assertEqual(self.channel.email_value, "[email protected]")
  30. self.assertTrue(self.channel.email_notify_down)
  31. self.assertFalse(self.channel.email_notify_up)
  32. # It should send a verification link
  33. email = mail.outbox[0]
  34. self.assertTrue(email.subject.startswith("Verify email address on"))
  35. self.assertEqual(email.to[0], "[email protected]")
  36. # Make sure it does not call assign_all_checks
  37. self.assertFalse(self.channel.checks.exists())
  38. def test_it_skips_verification_if_email_unchanged(self):
  39. form = {"value": "[email protected]", "down": "false", "up": "true"}
  40. self.client.login(username="[email protected]", password="password")
  41. self.client.post(self.url, form)
  42. self.channel.refresh_from_db()
  43. self.assertEqual(self.channel.email_value, "[email protected]")
  44. self.assertFalse(self.channel.email_notify_down)
  45. self.assertTrue(self.channel.email_notify_up)
  46. self.assertTrue(self.channel.email_verified)
  47. # The email address did not change, so we should skip verification
  48. self.assertEqual(len(mail.outbox), 0)
  49. def test_team_access_works(self):
  50. form = {"value": "[email protected]", "down": "true", "up": "true"}
  51. self.client.login(username="[email protected]", password="password")
  52. self.client.post(self.url, form)
  53. self.channel.refresh_from_db()
  54. self.assertEqual(self.channel.email_value, "[email protected]")
  55. @override_settings(EMAIL_USE_VERIFICATION=False)
  56. def test_it_hides_confirmation_needed_notice(self):
  57. self.client.login(username="[email protected]", password="password")
  58. r = self.client.get(self.url)
  59. self.assertNotContains(r, "Requires confirmation")
  60. @override_settings(EMAIL_USE_VERIFICATION=False)
  61. def test_it_auto_verifies_email(self):
  62. form = {"value": "[email protected]", "down": "true", "up": "true"}
  63. self.client.login(username="[email protected]", password="password")
  64. r = self.client.post(self.url, form)
  65. self.assertRedirects(r, self.channels_url)
  66. self.channel.refresh_from_db()
  67. self.assertEqual(self.channel.email_value, "[email protected]")
  68. # Email should *not* have been sent
  69. self.assertEqual(len(mail.outbox), 0)
  70. def test_it_auto_verifies_own_email(self):
  71. form = {"value": "[email protected]", "down": "true", "up": "true"}
  72. self.client.login(username="[email protected]", password="password")
  73. r = self.client.post(self.url, form)
  74. self.assertRedirects(r, self.channels_url)
  75. self.channel.refresh_from_db()
  76. self.assertEqual(self.channel.email_value, "[email protected]")
  77. # Email should *not* have been sent
  78. self.assertEqual(len(mail.outbox), 0)
  79. def test_it_requires_rw_access(self):
  80. self.bobs_membership.role = "r"
  81. self.bobs_membership.save()
  82. self.client.login(username="[email protected]", password="password")
  83. r = self.client.get(self.url)
  84. self.assertEqual(r.status_code, 403)