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.

105 lines
3.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
  5. from hc.test import BaseTestCase
  6. class AddEmailTestCase(BaseTestCase):
  7. url = "/integrations/add_email/"
  8. def test_instructions_work(self):
  9. self.client.login(username="[email protected]", password="password")
  10. r = self.client.get(self.url)
  11. self.assertContains(r, "Get an email message")
  12. self.assertContains(r, "Requires confirmation")
  13. def test_it_creates_channel(self):
  14. form = {"value": "[email protected]"}
  15. self.client.login(username="[email protected]", password="password")
  16. r = self.client.post(self.url, form)
  17. self.assertRedirects(r, "/integrations/")
  18. c = Channel.objects.get()
  19. doc = json.loads(c.value)
  20. self.assertEqual(c.kind, "email")
  21. self.assertEqual(doc["value"], "[email protected]")
  22. self.assertFalse(c.email_verified)
  23. self.assertEqual(c.project, self.project)
  24. # Email should have been sent
  25. self.assertEqual(len(mail.outbox), 1)
  26. email = mail.outbox[0]
  27. self.assertTrue(email.subject.startswith("Verify email address on"))
  28. # Make sure we're sending to an email address, not a JSON string:
  29. self.assertEqual(email.to[0], "[email protected]")
  30. def test_team_access_works(self):
  31. form = {"value": "[email protected]"}
  32. self.client.login(username="[email protected]", password="password")
  33. self.client.post(self.url, form)
  34. ch = Channel.objects.get()
  35. # Added by bob, but should belong to alice (bob has team access)
  36. self.assertEqual(ch.project, self.project)
  37. def test_it_rejects_bad_email(self):
  38. form = {"value": "not an email address"}
  39. self.client.login(username="[email protected]", password="password")
  40. r = self.client.post(self.url, form)
  41. self.assertContains(r, "Enter a valid email address.")
  42. def test_it_trims_whitespace(self):
  43. form = {"value": " [email protected] "}
  44. self.client.login(username="[email protected]", password="password")
  45. self.client.post(self.url, form)
  46. c = Channel.objects.get()
  47. doc = json.loads(c.value)
  48. self.assertEqual(doc["value"], "[email protected]")
  49. @override_settings(EMAIL_USE_VERIFICATION=False)
  50. def test_it_hides_confirmation_needed_notice(self):
  51. self.client.login(username="[email protected]", password="password")
  52. r = self.client.get(self.url)
  53. self.assertNotContains(r, "Requires confirmation")
  54. @override_settings(EMAIL_USE_VERIFICATION=False)
  55. def test_it_auto_verifies_email(self):
  56. form = {"value": "[email protected]"}
  57. self.client.login(username="[email protected]", password="password")
  58. r = self.client.post(self.url, form)
  59. self.assertRedirects(r, "/integrations/")
  60. c = Channel.objects.get()
  61. doc = json.loads(c.value)
  62. self.assertEqual(c.kind, "email")
  63. self.assertEqual(doc["value"], "[email protected]")
  64. self.assertTrue(c.email_verified)
  65. # Email should *not* have been sent
  66. self.assertEqual(len(mail.outbox), 0)
  67. def test_it_auto_verifies_own_email(self):
  68. form = {"value": "[email protected]"}
  69. self.client.login(username="[email protected]", password="password")
  70. r = self.client.post(self.url, form)
  71. self.assertRedirects(r, "/integrations/")
  72. c = Channel.objects.get()
  73. doc = json.loads(c.value)
  74. self.assertEqual(c.kind, "email")
  75. self.assertEqual(doc["value"], "[email protected]")
  76. self.assertTrue(c.email_verified)
  77. # Email should *not* have been sent
  78. self.assertEqual(len(mail.outbox), 0)