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.

45 lines
1.6 KiB

  1. from datetime import timedelta as td
  2. from django.core import signing
  3. from django.utils.timezone import now
  4. from hc.test import BaseTestCase
  5. class UnsubscribeReportsTestCase(BaseTestCase):
  6. def test_token_works(self):
  7. self.profile.next_report_date = now()
  8. self.profile.nag_period = td(hours=1)
  9. self.profile.next_nag_date = now()
  10. self.profile.save()
  11. token = signing.Signer().sign("foo")
  12. url = "/accounts/unsubscribe_reports/alice/?token=%s" % token
  13. r = self.client.get(url)
  14. self.assertContains(r, "You have been unsubscribed")
  15. self.profile.refresh_from_db()
  16. self.assertFalse(self.profile.reports_allowed)
  17. self.assertIsNone(self.profile.next_report_date)
  18. self.assertEqual(self.profile.nag_period.total_seconds(), 0)
  19. self.assertIsNone(self.profile.next_nag_date)
  20. def test_bad_token_gets_rejected(self):
  21. url = "/accounts/unsubscribe_reports/alice/?token=invalid"
  22. r = self.client.get(url)
  23. self.assertContains(r, "Incorrect Link")
  24. def test_signed_username_works(self):
  25. sig = signing.TimestampSigner(salt="reports").sign("alice")
  26. url = "/accounts/unsubscribe_reports/%s/" % sig
  27. r = self.client.get(url)
  28. self.assertContains(r, "You have been unsubscribed")
  29. self.profile.refresh_from_db()
  30. self.assertFalse(self.profile.reports_allowed)
  31. def test_bad_signature_gets_rejected(self):
  32. url = "/accounts/unsubscribe_reports/invalid/"
  33. r = self.client.get(url)
  34. self.assertContains(r, "Incorrect Link")