def tearDownClass(cls):
     BLOCKED_EMAIL_DOMAIN_RECIPIENTS.set('thisisatestemail1111.com',
                                         False,
                                         namespace=NAMESPACE_EMAIL_DOMAIN)
     BLOCKED_DOMAIN_EMAIL_SENDERS.set(cls.bad_domain,
                                      False,
                                      namespace=NAMESPACE_DOMAIN)
     ComplaintBounceMeta.objects.all().delete()
     PermanentBounceMeta.objects.all().delete()
     BouncedEmail.objects.all().delete()
     super().tearDownClass()
Example #2
0
    def get_hard_bounced_emails(cls, list_of_emails):
        # these are any Bounced Email Records we have
        bad_emails = set()

        for email_address in list_of_emails:
            if (BLOCKED_EMAIL_DOMAIN_RECIPIENTS.enabled(email_address)
                    or cls.is_bad_email_format(email_address)):
                bad_emails.add(email_address)

        list_of_emails = set(list_of_emails).difference(bad_emails)

        if len(list_of_emails) == 0:
            # don't query the db if we don't have to
            return bad_emails

        bounced_emails = set(
            BouncedEmail.objects.filter(email__in=list_of_emails).values_list(
                'email', flat=True))

        transient_emails = set(TransientBounceEmail.get_active_query().filter(
            email__in=list_of_emails, ).values_list('email', flat=True))
        bounced_emails.update(transient_emails)

        # These are emails that were marked as Suppressed or Undetermined
        # by SNS metadata, meaning they definitely hard bounced
        permanent_bounces = set(
            PermanentBounceMeta.objects.filter(
                sub_type__in=[
                    BounceSubType.UNDETERMINED, BounceSubType.SUPPRESSED
                ],
                bounced_email__email__in=bounced_emails).values_list(
                    'bounced_email__email', flat=True))
        bad_emails.update(permanent_bounces)

        # These are definite complaints against us
        complaints = set(
            ComplaintBounceMeta.objects.filter(
                bounced_email__email__in=bounced_emails.difference(
                    bad_emails)).values_list('bounced_email__email',
                                             flat=True))
        bad_emails.update(complaints)

        # see note surrounding LEGACY_BOUNCE_MANAGER_DATE above
        legacy_bounced_emails = set(
            BouncedEmail.objects.filter(
                email__in=list_of_emails,
                created__lte=LEGACY_BOUNCE_MANAGER_DATE,
            ).values_list('email', flat=True))
        bad_emails.update(legacy_bounced_emails)

        for remaining_email in bounced_emails.difference(bad_emails):
            if cls.is_email_over_limits(remaining_email):
                bad_emails.add(remaining_email)

        from corehq.util.email_event_utils import get_emails_to_never_bounce
        return bad_emails.difference(get_emails_to_never_bounce())
    def setUpClass(cls):
        super().setUpClass()
        cls._create_permanent_meta(cls.SUPPRESSED_BOUNCE,
                                   BounceSubType.SUPPRESSED)
        cls._create_permanent_meta(cls.UNDETERMINED_BOUNCE,
                                   BounceSubType.UNDETERMINED)
        cls._create_permanent_meta(cls.GENERAL_BOUNCE, BounceSubType.GENERAL)
        cls._create_permanent_meta(
            cls.GENERAL_AT_TRESH,
            BounceSubType.GENERAL,
            BOUNCE_EVENT_THRESHOLD,
        )
        cls._create_permanent_meta(
            cls.GENERAL_ABOVE_THRESH,
            BounceSubType.GENERAL,
            BOUNCE_EVENT_THRESHOLD + 1,
        )
        cls._create_permanent_meta(
            cls.SYSTEM_EMAIL,
            BounceSubType.GENERAL,
            BOUNCE_EVENT_THRESHOLD + 1,
        )

        legacy_email = BouncedEmail.objects.create(
            email=cls.LEGACY_BOUNCE,
            created=LEGACY_BOUNCE_MANAGER_DATE - datetime.timedelta(days=1))
        legacy_email.created = LEGACY_BOUNCE_MANAGER_DATE - datetime.timedelta(
            days=1)
        legacy_email.save()

        complaint_email = BouncedEmail.objects.create(email=cls.COMPLAINT)
        ComplaintBounceMeta.objects.create(
            bounced_email=complaint_email,
            timestamp=datetime.datetime.now(),
        )

        BLOCKED_EMAIL_DOMAIN_RECIPIENTS.set('thisisatestemail1111.com',
                                            True,
                                            namespace=NAMESPACE_EMAIL_DOMAIN)
        cls.bad_domain = 'bad-domain'
        BLOCKED_DOMAIN_EMAIL_SENDERS.set(cls.bad_domain,
                                         True,
                                         namespace=NAMESPACE_DOMAIN)