From 11d5f8bbc5cb16459862d3461310b201842457de Mon Sep 17 00:00:00 2001 From: Jessica McKellar Date: Sun, 1 Dec 2013 20:38:53 -0500 Subject: [PATCH] Add a test for unsubscribing from digest emails. (imported from commit 145c25ae99cf66b9a447b2e3a44a3b7344dc9f59) --- zerver/tests.py | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/zerver/tests.py b/zerver/tests.py index 579cc8b8f1..091bfe92f8 100644 --- a/zerver/tests.py +++ b/zerver/tests.py @@ -29,6 +29,7 @@ from zerver.lib.cache import bounce_key_prefix_for_testing from zerver.lib.rate_limiter import clear_user_history from zerver.lib.alert_words import alert_words_in_realm, user_alert_words, \ add_user_alert_words, remove_user_alert_words +from zerver.lib.digest import send_digest_email from zerver.forms import not_mit_mailing_list from zerver.worker import queue_processors @@ -4755,6 +4756,35 @@ class EmailUnsubscribeTests(AuthedTestCase): self.assertEqual(0, len(ScheduledJob.objects.filter( type=ScheduledJob.EMAIL, filter_string__iexact=email))) + def test_digest_unsubscribe(self): + """ + We provide one-click unsubscribe links in digest e-mails that you can + click even when logged out to stop receiving them. + + Unsubscribing from these emails also dequeues any digest email jobs that + have been queued. + """ + email = "hamlet@zulip.com" + user_profile = get_user_profile_by_email("hamlet@zulip.com") + self.assertTrue(user_profile.enable_digest_emails) + + # Enqueue a fake digest email. + send_digest_email(user_profile, "", "") + self.assertEqual(1, len(ScheduledJob.objects.filter( + type=ScheduledJob.EMAIL, filter_string__iexact=email))) + + # Simulate unsubscribing from digest e-mails. + unsubscribe_link = one_click_unsubscribe_link(user_profile, "digest") + result = self.client.get(urlparse(unsubscribe_link).path) + + # The setting is toggled off, and scheduled jobs have been removed. + self.assertEqual(result.status_code, 200) + # Circumvent user_profile caching. + user_profile = UserProfile.objects.get(email="hamlet@zulip.com") + self.assertFalse(user_profile.enable_digest_emails) + self.assertEqual(0, len(ScheduledJob.objects.filter( + type=ScheduledJob.EMAIL, filter_string__iexact=email))) + class Runner(DjangoTestSuiteRunner): option_list = ()