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.

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