|
@ -1,5 +1,8 @@ |
|
|
|
|
|
import time |
|
|
|
|
|
from django.core.signing import TimestampSigner |
|
|
from hc.api.models import Channel |
|
|
from hc.api.models import Channel |
|
|
from hc.test import BaseTestCase |
|
|
from hc.test import BaseTestCase |
|
|
|
|
|
from mock import patch |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class UnsubscribeEmailTestCase(BaseTestCase): |
|
|
class UnsubscribeEmailTestCase(BaseTestCase): |
|
@ -9,7 +12,15 @@ class UnsubscribeEmailTestCase(BaseTestCase): |
|
|
self.channel.value = "[email protected]" |
|
|
self.channel.value = "[email protected]" |
|
|
self.channel.save() |
|
|
self.channel.save() |
|
|
|
|
|
|
|
|
def test_it_works(self): |
|
|
|
|
|
|
|
|
def test_it_serves_confirmation_form(self): |
|
|
|
|
|
token = self.channel.make_token() |
|
|
|
|
|
url = "/integrations/%s/unsub/%s/" % (self.channel.code, token) |
|
|
|
|
|
|
|
|
|
|
|
r = self.client.get(url) |
|
|
|
|
|
self.assertContains(r, "Please press the button below") |
|
|
|
|
|
self.assertNotContains(r, "submit()") |
|
|
|
|
|
|
|
|
|
|
|
def test_post_unsubscribes(self): |
|
|
token = self.channel.make_token() |
|
|
token = self.channel.make_token() |
|
|
url = "/integrations/%s/unsub/%s/" % (self.channel.code, token) |
|
|
url = "/integrations/%s/unsub/%s/" % (self.channel.code, token) |
|
|
|
|
|
|
|
@ -19,6 +30,39 @@ class UnsubscribeEmailTestCase(BaseTestCase): |
|
|
q = Channel.objects.filter(code=self.channel.code) |
|
|
q = Channel.objects.filter(code=self.channel.code) |
|
|
self.assertEqual(q.count(), 0) |
|
|
self.assertEqual(q.count(), 0) |
|
|
|
|
|
|
|
|
|
|
|
def test_fresh_signature_does_not_autosubmit(self): |
|
|
|
|
|
signer = TimestampSigner(salt="alerts") |
|
|
|
|
|
signed_token = signer.sign(self.channel.make_token()) |
|
|
|
|
|
|
|
|
|
|
|
url = "/integrations/%s/unsub/%s/" % (self.channel.code, signed_token) |
|
|
|
|
|
|
|
|
|
|
|
r = self.client.get(url) |
|
|
|
|
|
self.assertContains( |
|
|
|
|
|
r, "Please press the button below to unsubscribe", status_code=200 |
|
|
|
|
|
) |
|
|
|
|
|
self.assertNotContains(r, "submit()", status_code=200) |
|
|
|
|
|
|
|
|
|
|
|
def test_aged_signature_does_autosubmit(self): |
|
|
|
|
|
with patch("django.core.signing.time") as mock_time: |
|
|
|
|
|
mock_time.time.return_value = time.time() - 301 |
|
|
|
|
|
signer = TimestampSigner(salt="alerts") |
|
|
|
|
|
signed_token = signer.sign(self.channel.make_token()) |
|
|
|
|
|
|
|
|
|
|
|
url = "/integrations/%s/unsub/%s/" % (self.channel.code, signed_token) |
|
|
|
|
|
|
|
|
|
|
|
r = self.client.get(url) |
|
|
|
|
|
self.assertContains( |
|
|
|
|
|
r, "Please press the button below to unsubscribe", status_code=200 |
|
|
|
|
|
) |
|
|
|
|
|
self.assertContains(r, "submit()", status_code=200) |
|
|
|
|
|
|
|
|
|
|
|
def test_it_checks_signature(self): |
|
|
|
|
|
signed_token = self.channel.make_token() + ":bad:signature" |
|
|
|
|
|
url = "/integrations/%s/unsub/%s/" % (self.channel.code, signed_token) |
|
|
|
|
|
|
|
|
|
|
|
r = self.client.get(url) |
|
|
|
|
|
self.assertContains(r, "link you just used is incorrect", status_code=200) |
|
|
|
|
|
|
|
|
def test_it_checks_token(self): |
|
|
def test_it_checks_token(self): |
|
|
url = "/integrations/%s/unsub/faketoken/" % self.channel.code |
|
|
url = "/integrations/%s/unsub/faketoken/" % self.channel.code |
|
|
|
|
|
|
|
@ -33,11 +77,4 @@ class UnsubscribeEmailTestCase(BaseTestCase): |
|
|
url = "/integrations/%s/unsub/%s/" % (self.channel.code, token) |
|
|
url = "/integrations/%s/unsub/%s/" % (self.channel.code, token) |
|
|
|
|
|
|
|
|
r = self.client.get(url) |
|
|
r = self.client.get(url) |
|
|
self.assertEqual(r.status_code, 400) |
|
|
|
|
|
|
|
|
|
|
|
def test_it_serves_confirmation_form(self): |
|
|
|
|
|
token = self.channel.make_token() |
|
|
|
|
|
url = "/integrations/%s/unsub/%s/" % (self.channel.code, token) |
|
|
|
|
|
|
|
|
|
|
|
r = self.client.get(url) |
|
|
|
|
|
self.assertContains(r, "Please press the button below") |
|
|
|
|
|
|
|
|
self.assertEqual(r.status_code, 404) |