def test_bounce_over_limit(self): """ Tests that all the user's subscriptions are dropped when too many bounces are received. """ # Set up some prior bounces - one each day. date = timezone.now().date() for days in range(1, settings.DISTRO_TRACKER_MAX_DAYS_TOLERATE_BOUNCE): self.add_sent(self.user, date - timedelta(days=days)) self.add_bounce(self.user, date - timedelta(days=days)) # Set up a sent mail today. self.add_sent(self.user, date) # Make sure there were at least some subscriptions packages_subscribed_to = [ subscription.package.name for subscription in self.user.emailsettings.subscription_set.all() ] self.assertTrue(len(packages_subscribed_to) > 0) # Receive a bounce message. dispatch.handle_bounces(self.create_bounce_address(self.user.email)) # Assert that the user's subscriptions have been dropped. self.assertEqual(self.user.emailsettings.subscription_set.count(), 0) # A notification was sent to the user. self.assertEqual(len(mail.outbox), 1) self.assertIn(self.user.email, mail.outbox[0].to) # Check that the content of the email is correct. self.assertEqual(mail.outbox[0].body, distro_tracker_render_to_string( 'dispatch/unsubscribed-due-to-bounces-email.txt', { 'email': self.user.email, 'packages': packages_subscribed_to } ))
def test_bounce_recorded_with_differing_case(self): self.subscribe_user_to_package('*****@*****.**', 'dummy-package') self.user = UserEmailBounceStats.objects.get(email='*****@*****.**') self.assertEqual(self.user.bouncestats_set.count(), 0) dispatch.handle_bounces( self.create_bounce_address('*****@*****.**')) bounce_stats = self.user.bouncestats_set.all() self.assertEqual(bounce_stats.count(), 1) self.assertEqual(bounce_stats[0].date, timezone.now().date()) self.assertEqual(bounce_stats[0].mails_bounced, 1)
def test_bounce_recorded(self): """ Tests that a received bounce is recorded. """ # Make sure the user has no prior bounce stats self.assertEqual(self.user.bouncestats_set.count(), 0) dispatch.handle_bounces(self.create_bounce_address(self.user.email)) bounce_stats = self.user.bouncestats_set.all() self.assertEqual(bounce_stats.count(), 1) self.assertEqual(bounce_stats[0].date, timezone.now().date()) self.assertEqual(bounce_stats[0].mails_bounced, 1) self.assertEqual(self.user.emailsettings.subscription_set.count(), 1)
def test_spam_bounce_ignored(self): self.assertEqual(self.user.bouncestats_set.count(), 0) self.message.set_payload( """ [email protected] SMTP error from remote mail server after end of data: host aspmx.l.google.com [2607:f8b0:400e:c02::1a]: 552-5.7.0 This message was blocked because its content presents a potential 552-5.7.0 security issue. Please visit 552-5.7.0 https://support.google.com/mail/?p=BlockedMessage to review our 552 5.7.0 message content and attachment content guidelines. """) dispatch.handle_bounces(self.create_bounce_address(self.user.email), self.message) self.assertEqual(self.user.bouncestats_set.count(), 0)
def test_bounces_not_every_day(self): """ Tests that the user's subscriptions are not dropped when there is a day which had more sent messages. """ date = timezone.now().date() for days in range(1, settings.DISTRO_TRACKER_MAX_DAYS_TOLERATE_BOUNCE): self.add_sent(self.user, date - timedelta(days=days)) if days % 2 == 0: self.add_bounce(self.user, date - timedelta(days=days)) # Set up a sent mail today. self.add_sent(self.user, date) # Make sure there were at least some subscriptions subscription_count = self.user.emailsettings.subscription_set.count() self.assertTrue(subscription_count > 0) # Receive a bounce message. dispatch.handle_bounces(self.create_bounce_address(self.user.email)) # Assert that the user's subscriptions have not been dropped. self.assertEqual(self.user.emailsettings.subscription_set.count(), subscription_count)
def test_bounce_under_limit(self): """ Tests that the user's subscriptions are not dropped when there are too many bounces for less days than tolerated. """ # Set up some prior bounces - one each day. date = timezone.now().date() for days in range(1, settings.DISTRO_TRACKER_MAX_DAYS_TOLERATE_BOUNCE - 1): self.add_sent(self.user, date - timedelta(days=days)) self.add_bounce(self.user, date - timedelta(days=days)) # Set up a sent mail today. self.add_sent(self.user, date) # Make sure there were at least some subscriptions subscription_count = self.user.emailsettings.subscription_set.count() self.assertTrue(subscription_count > 0) # Receive a bounce message. dispatch.handle_bounces(self.create_bounce_address(self.user.email)) # Assert that the user's subscriptions have not been dropped. self.assertEqual(self.user.emailsettings.subscription_set.count(), subscription_count)
def test_bounce_handler_with_unknown_user_email(self): # This should just not generate any exception... dispatch.handle_bounces( self.create_bounce_address('*****@*****.**'))
def test_bounce_handler_with_base_email_address(self): """Mail for bounces@domain should not generate errors""" dispatch.handle_bounces( 'bounces@' + DISTRO_TRACKER_FQDN, self.message)
def _test_spam_bounce_ignored(self, payload): self.assertEqual(self.user.bouncestats_set.count(), 0) self.message.set_payload(payload) dispatch.handle_bounces(self.create_bounce_address(self.user.email), self.message) self.assertEqual(self.user.bouncestats_set.count(), 0)