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.

81 lines
2.9 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(UnsubscribeEmailTestCase, self).setUp()
  9. self.channel = Channel(project=self.project, kind="email")
  10. self.channel.value = "[email protected]"
  11. self.channel.save()
  12. def test_it_serves_confirmation_form(self):
  13. token = self.channel.make_token()
  14. url = "/integrations/%s/unsub/%s/" % (self.channel.code, token)
  15. r = self.client.get(url)
  16. self.assertContains(r, "Please press the button below")
  17. self.assertNotContains(r, "submit()")
  18. def test_post_unsubscribes(self):
  19. token = self.channel.make_token()
  20. url = "/integrations/%s/unsub/%s/" % (self.channel.code, token)
  21. r = self.client.post(url)
  22. self.assertContains(r, "has been unsubscribed", status_code=200)
  23. q = Channel.objects.filter(code=self.channel.code)
  24. self.assertEqual(q.count(), 0)
  25. def test_fresh_signature_does_not_autosubmit(self):
  26. signer = TimestampSigner(salt="alerts")
  27. signed_token = signer.sign(self.channel.make_token())
  28. url = "/integrations/%s/unsub/%s/" % (self.channel.code, signed_token)
  29. r = self.client.get(url)
  30. self.assertContains(
  31. r, "Please press the button below to unsubscribe", status_code=200
  32. )
  33. self.assertNotContains(r, "submit()", status_code=200)
  34. def test_aged_signature_does_autosubmit(self):
  35. with patch("django.core.signing.time") as mock_time:
  36. mock_time.time.return_value = time.time() - 301
  37. signer = TimestampSigner(salt="alerts")
  38. signed_token = signer.sign(self.channel.make_token())
  39. url = "/integrations/%s/unsub/%s/" % (self.channel.code, signed_token)
  40. r = self.client.get(url)
  41. self.assertContains(
  42. r, "Please press the button below to unsubscribe", status_code=200
  43. )
  44. self.assertContains(r, "submit()", status_code=200)
  45. def test_it_checks_signature(self):
  46. signed_token = self.channel.make_token() + ":bad:signature"
  47. url = "/integrations/%s/unsub/%s/" % (self.channel.code, signed_token)
  48. r = self.client.get(url)
  49. self.assertContains(r, "link you just used is incorrect", status_code=200)
  50. def test_it_checks_token(self):
  51. url = "/integrations/%s/unsub/faketoken/" % self.channel.code
  52. r = self.client.get(url)
  53. self.assertContains(r, "link you just used is incorrect", status_code=200)
  54. def test_it_checks_channel_kind(self):
  55. self.channel.kind = "webhook"
  56. self.channel.save()
  57. token = self.channel.make_token()
  58. url = "/integrations/%s/unsub/%s/" % (self.channel.code, token)
  59. r = self.client.get(url)
  60. self.assertEqual(r.status_code, 404)