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.

71 lines
2.5 KiB

  1. import time
  2. from unittest.mock import patch
  3. from django.core.signing import TimestampSigner
  4. from hc.api.models import Channel
  5. from hc.test import BaseTestCase
  6. class UnsubscribeEmailTestCase(BaseTestCase):
  7. def setUp(self):
  8. super().setUp()
  9. self.channel = Channel(project=self.project, kind="email")
  10. self.channel.value = "[email protected]"
  11. self.channel.save()
  12. signer = TimestampSigner(salt="alerts")
  13. signed_token = signer.sign(self.channel.make_token())
  14. self.url = f"/integrations/{self.channel.code}/unsub/{signed_token}/"
  15. def test_it_serves_confirmation_form(self):
  16. r = self.client.get(self.url)
  17. self.assertContains(r, "Please press the button below")
  18. self.assertNotContains(r, "submit()")
  19. def test_post_unsubscribes(self):
  20. r = self.client.post(self.url)
  21. self.assertContains(r, "has been unsubscribed", status_code=200)
  22. q = Channel.objects.filter(code=self.channel.code)
  23. self.assertEqual(q.count(), 0)
  24. def test_fresh_signature_does_not_autosubmit(self):
  25. r = self.client.get(self.url)
  26. self.assertContains(
  27. r, "Please press the button below to unsubscribe", status_code=200
  28. )
  29. self.assertNotContains(r, "submit()", status_code=200)
  30. def test_aged_signature_does_autosubmit(self):
  31. with patch("django.core.signing.time") as mock_time:
  32. mock_time.time.return_value = time.time() - 301
  33. signer = TimestampSigner(salt="alerts")
  34. signed_token = signer.sign(self.channel.make_token())
  35. url = f"/integrations/{self.channel.code}/unsub/{signed_token}/"
  36. r = self.client.get(url)
  37. self.assertContains(
  38. r, "Please press the button below to unsubscribe", status_code=200
  39. )
  40. self.assertContains(r, "submit()", status_code=200)
  41. def test_it_checks_signature(self):
  42. signed_token = self.channel.make_token() + ":bad:signature"
  43. url = f"/integrations/{self.channel.code}/unsub/{signed_token}/"
  44. r = self.client.get(url)
  45. self.assertContains(r, "link you just used is incorrect", status_code=200)
  46. def test_it_checks_token(self):
  47. url = f"/integrations/{self.channel.code}/unsub/faketoken/"
  48. r = self.client.get(url)
  49. self.assertContains(r, "link you just used is incorrect", status_code=200)
  50. def test_it_checks_channel_kind(self):
  51. self.channel.kind = "webhook"
  52. self.channel.save()
  53. r = self.client.get(self.url)
  54. self.assertEqual(r.status_code, 404)