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.

127 lines
4.7 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 AddEmailTestCase(BaseTestCase):
  7. def setUp(self):
  8. super().setUp()
  9. self.check = Check.objects.create(project=self.project)
  10. self.url = f"/projects/{self.project.code}/add_email/"
  11. def test_instructions_work(self):
  12. self.client.login(username="[email protected]", password="password")
  13. r = self.client.get(self.url)
  14. self.assertContains(r, "Get an email message")
  15. self.assertContains(r, "Requires confirmation")
  16. self.assertContains(r, "Set Up Email Notifications")
  17. def test_it_creates_channel(self):
  18. form = {"value": "[email protected]", "down": "true", "up": "true"}
  19. self.client.login(username="[email protected]", password="password")
  20. r = self.client.post(self.url, form)
  21. self.assertRedirects(r, self.channels_url)
  22. c = Channel.objects.get()
  23. doc = json.loads(c.value)
  24. self.assertEqual(c.kind, "email")
  25. self.assertEqual(doc["value"], "[email protected]")
  26. self.assertFalse(c.email_verified)
  27. self.assertEqual(c.project, self.project)
  28. # Email should have been sent
  29. self.assertEqual(len(mail.outbox), 1)
  30. email = mail.outbox[0]
  31. self.assertTrue(email.subject.startswith("Verify email address on"))
  32. # Make sure we're sending to an email address, not a JSON string:
  33. self.assertEqual(email.to[0], "[email protected]")
  34. # Make sure it calls assign_all_checks
  35. self.assertEqual(c.checks.count(), 1)
  36. def test_team_access_works(self):
  37. form = {"value": "[email protected]", "down": "true", "up": "true"}
  38. self.client.login(username="[email protected]", password="password")
  39. self.client.post(self.url, form)
  40. ch = Channel.objects.get()
  41. # Added by bob, but should belong to alice (bob has team access)
  42. self.assertEqual(ch.project, self.project)
  43. def test_it_rejects_bad_email(self):
  44. form = {"value": "not an email address", "down": "true", "up": "true"}
  45. self.client.login(username="[email protected]", password="password")
  46. r = self.client.post(self.url, form)
  47. self.assertContains(r, "Enter a valid email address.")
  48. def test_it_trims_whitespace(self):
  49. form = {"value": " [email protected] ", "down": "true", "up": "true"}
  50. self.client.login(username="[email protected]", password="password")
  51. self.client.post(self.url, form)
  52. c = Channel.objects.get()
  53. doc = json.loads(c.value)
  54. self.assertEqual(doc["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. c = Channel.objects.get()
  67. doc = json.loads(c.value)
  68. self.assertEqual(c.kind, "email")
  69. self.assertEqual(doc["value"], "[email protected]")
  70. self.assertTrue(c.email_verified)
  71. # Email should *not* have been sent
  72. self.assertEqual(len(mail.outbox), 0)
  73. def test_it_auto_verifies_own_email(self):
  74. form = {"value": "[email protected]", "down": "true", "up": "true"}
  75. self.client.login(username="[email protected]", password="password")
  76. r = self.client.post(self.url, form)
  77. self.assertRedirects(r, self.channels_url)
  78. c = Channel.objects.get()
  79. doc = json.loads(c.value)
  80. self.assertEqual(c.kind, "email")
  81. self.assertEqual(doc["value"], "[email protected]")
  82. self.assertTrue(c.email_verified)
  83. # Email should *not* have been sent
  84. self.assertEqual(len(mail.outbox), 0)
  85. def test_it_rejects_unchecked_up_and_down(self):
  86. form = {"value": "[email protected]"}
  87. self.client.login(username="[email protected]", password="password")
  88. r = self.client.post(self.url, form)
  89. self.assertContains(r, "Please select at least one.")
  90. def test_it_requires_rw_access(self):
  91. self.bobs_membership.role = "r"
  92. self.bobs_membership.save()
  93. self.client.login(username="[email protected]", password="password")
  94. r = self.client.get(self.url)
  95. self.assertEqual(r.status_code, 403)