Exemplo n.º 1
0
class SendCampaignEmailTestMailingListTests(TestCase):
    def setUp(self):
        super().setUp()
        self.email = EmailFactory(
            from_email='*****@*****.**',
            from_name='John Doe',
            subject='My email subject',
        )
        self.email.campaign.mailing_list = MailingListFactory()
        self.email.campaign.save()
        self.email.set_template_content()
        email_content = {
            'content':
            '<p>Hi there!</p><p>Test email body.</p><a href="https://google.com">google</a>'
        }
        self.email.set_blocks(email_content)
        self.email.save()
        send_campaign_email_test(self.email, ['*****@*****.**'])
        self.email_message = mail.outbox[0]

    def test_email_contents_unsub_link(self):
        unsubscribe_url = get_absolute_url(
            'subscribers:unsubscribe_manual', {
                'mailing_list_uuid': self.email.campaign.mailing_list.uuid,
            })
        text_body = self.email_message.body
        html_body, mimetype = self.email_message.alternatives[0]
        self.assertIn(unsubscribe_url, text_body,
                      'Email plain text body must contain unsubscribe link.')
        self.assertIn(unsubscribe_url, html_body,
                      'Email HTML body must contain unsubscribe link.')
Exemplo n.º 2
0
class SendCampaignEmailTestMailingListTests(TestCase):
    def setUp(self):
        super().setUp()
        self.email = EmailFactory(
            from_email='*****@*****.**',
            from_name='John Doe',
            subject='My email subject',
        )
        self.email.campaign.mailing_list = MailingListFactory()
        self.email.campaign.save()
        self.email.set_template_content()
        email_content = {
            'content': '<p>Hi there!</p><p>Test email body.</p><a href="https://google.com">google</a>'
        }
        self.email.set_blocks(email_content)
        self.email.save()
        send_campaign_email_test(self.email, ['*****@*****.**'])
        self.email_message = mail.outbox[0]

    def test_email_contents_unsub_link(self):
        unsubscribe_url = get_absolute_url('subscribers:unsubscribe_manual', {
            'mailing_list_uuid': self.email.campaign.mailing_list.uuid,
        })
        text_body = self.email_message.body
        html_body, mimetype = self.email_message.alternatives[0]
        self.assertIn(unsubscribe_url, text_body, 'Email plain text body must contain unsubscribe link.')
        self.assertIn(unsubscribe_url, html_body, 'Email HTML body must contain unsubscribe link.')
Exemplo n.º 3
0
class SendCampaignTests(TestCase):
    def setUp(self):
        super().setUp()
        self.mailing_list = MailingListFactory()
        self.subscribers = SubscriberFactory.create_batch(
            10, mailing_list=self.mailing_list)
        self.campaign = CampaignFactory(mailing_list=self.mailing_list,
                                        track_clicks=True,
                                        track_opens=True)
        self.email = EmailFactory(
            campaign=self.campaign,
            from_email='*****@*****.**',
            from_name='John Doe',
            subject='Test email subject',
        )
        self.email.set_template_content()
        email_content = {
            'content':
            '<p>Hi there!</p><p>Test email body.</p><a href="https://google.com">google</a>'
        }
        self.email.set_blocks(email_content)
        self.email.save()
        send_campaign(self.campaign)

    def test_setup(self):
        self.assertEqual(MailingList.objects.count(), 1)
        self.assertEqual(self.mailing_list.subscribers.count(), 10)
        self.assertEqual(self.mailing_list.campaigns.count(), 1)
        self.assertEqual(self.campaign.emails.count(), 1)
        self.assertTrue(self.campaign.can_send)

    def test_campaign_status(self):
        self.campaign.refresh_from_db()
        self.assertEqual(self.campaign.status, CampaignStatus.SENT)

    def test_subscribers_last_sent(self):
        for subscriber in self.subscribers:
            subscriber.refresh_from_db()
            with self.subTest(subscriber=subscriber):
                self.assertIsNotNone(subscriber.last_sent)

    def test_activity_sent_created(self):
        activities_count = Activity.objects.filter(
            activity_type=ActivityTypes.SENT).count()
        self.assertEqual(activities_count, 10)

    def test_emails_sent(self):
        self.assertEqual(len(mail.outbox), 10,
                         'Campaign must send 1 email for each subscriber.')

    def test_emails_contents(self):
        for email in mail.outbox:
            with self.subTest(email=email):
                self.assertEqual(email.subject, 'Test email subject')
                subscriber = Subscriber.objects.get(email=email.to[0])
                unsubscribe_url = get_absolute_url(
                    'subscribers:unsubscribe', {
                        'mailing_list_uuid': self.mailing_list.uuid,
                        'subscriber_uuid': subscriber.uuid,
                        'campaign_uuid': self.campaign.uuid
                    })
                text_body = email.body
                html_body, mimetype = email.alternatives[0]
                self.assertIn(
                    unsubscribe_url, text_body,
                    'Email plain text body must contain unsubscribe link.')
                self.assertIn(
                    '/track/click/', text_body,
                    'Email plain text body must contain track click links.')
                self.assertNotIn(
                    '/track/open/', text_body,
                    'Email plain text body must NOT contain track open pixel.')
                self.assertIn(
                    unsubscribe_url, html_body,
                    'Email HTML body must contain unsubscribe link.')
                self.assertIn(
                    '/track/click/', html_body,
                    'Email HTML body must contain track click links.')
                self.assertIn(
                    '/track/open/', html_body,
                    'Email HTML body must contain track open pixel.')
Exemplo n.º 4
0
class SendCampaignTests(TestCase):
    def setUp(self):
        super().setUp()
        self.mailing_list = MailingListFactory()
        self.subscribers = SubscriberFactory.create_batch(10, mailing_list=self.mailing_list)
        self.campaign = CampaignFactory(mailing_list=self.mailing_list, track_clicks=True, track_opens=True)
        self.email = EmailFactory(
            campaign=self.campaign,
            from_email='*****@*****.**',
            from_name='John Doe',
            subject='Test email subject',
        )
        self.email.set_template_content()
        email_content = {
            'content': '<p>Hi there!</p><p>Test email body.</p><a href="https://google.com">google</a>'
        }
        self.email.set_blocks(email_content)
        self.email.save()
        send_campaign(self.campaign)

    def test_setup(self):
        self.assertEqual(MailingList.objects.count(), 1)
        self.assertEqual(self.mailing_list.subscribers.count(), 10)
        self.assertEqual(self.mailing_list.campaigns.count(), 1)
        self.assertEqual(self.campaign.emails.count(), 1)
        self.assertTrue(self.campaign.can_send)

    def test_campaign_status(self):
        self.campaign.refresh_from_db()
        self.assertEqual(self.campaign.status, CampaignStatus.SENT)

    def test_subscribers_last_sent(self):
        for subscriber in self.subscribers:
            subscriber.refresh_from_db()
            with self.subTest(subscriber=subscriber):
                self.assertIsNotNone(subscriber.last_sent)

    def test_activity_sent_created(self):
        activities_count = Activity.objects.filter(activity_type=ActivityTypes.SENT).count()
        self.assertEqual(activities_count, 10)

    def test_emails_sent(self):
        self.assertEqual(len(mail.outbox), 10, 'Campaign must send 1 email for each subscriber.')

    def test_emails_contents(self):
        for email in mail.outbox:
            with self.subTest(email=email):
                self.assertEqual(email.subject, 'Test email subject')
                subscriber = Subscriber.objects.get(email=email.to[0])
                unsubscribe_url = get_absolute_url('subscribers:unsubscribe', {
                    'mailing_list_uuid': self.mailing_list.uuid,
                    'subscriber_uuid': subscriber.uuid,
                    'campaign_uuid': self.campaign.uuid
                })
                text_body = email.body
                html_body, mimetype = email.alternatives[0]
                self.assertIn(unsubscribe_url, text_body, 'Email plain text body must contain unsubscribe link.')
                self.assertIn('/track/click/', text_body, 'Email plain text body must contain track click links.')
                self.assertNotIn('/track/open/', text_body, 'Email plain text body must NOT contain track open pixel.')
                self.assertIn(unsubscribe_url, html_body, 'Email HTML body must contain unsubscribe link.')
                self.assertIn('/track/click/', html_body, 'Email HTML body must contain track click links.')
                self.assertIn('/track/open/', html_body, 'Email HTML body must contain track open pixel.')