Example #1
0
    def send_campaign(self):
        # create campaign
        landing_page = LandingPage.objects.create(name='test',
                                                  html="""<html>
                <body>
                    <form method="POST">
                        <input name="login" />
                    </form>
                </html>
            """)
        # use json build (archive in attachment unit test)
        attachment_path = os.path.join(settings.MEDIA_ROOT, 'test_attachment')
        copyfile(os.path.join(FILES_PATH, 'archive.zip'), attachment_path)
        attachment = Attachment(name='test',
                                attachment_name='test.doc',
                                buildable=True)
        attachment.file = os.path.join('..', attachment_path)
        attachment.save()
        email_template = EmailTemplate.objects.create(
            name='test',
            email_subject='test',
            from_email='*****@*****.**',
            landing_page=landing_page,
            text_content='{{ landing_page_url }}',
            html_content='')
        email_template.attachments.add(attachment)
        target_group = TargetGroup.objects.create(name='test')
        Target.objects.create(email='*****@*****.**', group=target_group)
        campaign = Campaign.objects.create(name='test',
                                           email_template=email_template)
        campaign.target_groups_add(target_group)
        self.assertTrue(campaign.send())

        return campaign
Example #2
0
    def test_build(self):
        attachment_name = 'build.json'
        attachment_path = os.path.join(settings.MEDIA_ROOT, 'test_attachment')
        copyfile(os.path.join(FILES_PATH, 'archive.zip'), attachment_path)
        attachment = Attachment(name=attachment_name, buildable=True)
        attachment.file = os.path.join('..', attachment_path)
        attachment.save()

        kwargs = {
            'key': TRACKER_ATTACHMENT_EXECUTED,
            'campaign_id': 1,
            'target_id': 1,
            'value': 'tracker: not opened',
        }
        tracker = Tracker.objects.create(**kwargs)

        res = attachment.build(tracker)
        received = json.loads(res.read().decode())

        tracker_url = 'http://localhost/en/tracker/%s.png' % tracker.pk
        self.assertEqual(received['tracker_url'], tracker_url)
        self.assertEqual(received['target_email'], '*****@*****.**')
        self.assertEqual(received['target_first_name'], 'John')
        self.assertEqual(received['target_last_name'], 'Doe')

        # Clean media dir after test
        os.remove(attachment_path)
Example #3
0
    def test_build_invalid_zip(self):
        attachment_name = 'build.json'
        attachment_path = os.path.join(settings.MEDIA_ROOT, 'test_attachment')
        copyfile(os.path.join(FILES_PATH, 'invalid_archive.zip'),
                 attachment_path)
        attachment = Attachment(name=attachment_name, buildable=True)
        attachment.file = os.path.join('..', attachment_path)
        attachment.save()

        kwargs = {
            'key': TRACKER_ATTACHMENT_EXECUTED,
            'campaign_id': 1,
            'target_id': 1,
            'value': 'tracker: not opened',
        }
        tracker = Tracker.objects.create(**kwargs)
        with self.assertRaises(SuspiciousOperation):
            attachment.build(tracker)
Example #4
0
    def test_build_static(self):
        attachment_name = 'b64.png'
        attachment_path = os.path.join(settings.MEDIA_ROOT, 'test_attachment')
        copyfile(os.path.join(FILES_PATH, 'image.png'), attachment_path)
        attachment = Attachment(name=attachment_name, buildable=False)
        attachment.file = os.path.join('..', attachment_path)
        attachment.save()

        kwargs = {
            'key': TRACKER_ATTACHMENT_EXECUTED,
            'campaign_id': 1,
            'target_id': 1,
            'value': 'tracker: not opened',
        }
        tracker = Tracker.objects.create(**kwargs)

        res = attachment.build(tracker)
        self.assertEqual(res, attachment.file)

        # Clean media dir after test
        os.remove(attachment_path)
Example #5
0
    def test_scenario(self):
        # add landing page
        landing_page_domain = 'https://my-fake-domain.com'
        landing_page_html = '<!DOCTYPE html>' \
                            '<html lang="en">' \
                            '<body>' \
                            'test' \
                            '</body>' \
                            '</html>'
        landing_page_name = 'landing page title'
        landing_page = LandingPage.objects.create(
            domain=landing_page_domain,
            html=landing_page_html,
            name=landing_page_name,
        )

        # add email template
        email_template_content = 'click: {{ landing_page_url }}'
        email_template_from = '*****@*****.**'
        email_template_name = 'email template title'
        email_template_subject = 'Hello!'
        email_template = EmailTemplate.objects.create(
            email_subject=email_template_subject,
            from_email=email_template_from,
            landing_page=landing_page,
            name=email_template_name,
            text_content=email_template_content,
        )

        self.assertEqual(landing_page, email_template.landing_page)

        # add attachment
        files_path = os.path.join(os.path.dirname(os.path.abspath(__file__)),
                                  'files')

        attachment_1_name = 'b64.png'
        attachment_1_path = 'media/test_attachment_1'
        copyfile(os.path.join(files_path, 'image.png'), attachment_1_path)
        attachment_1 = Attachment(
            name=attachment_1_name,
            buildable=False
        )
        attachment_1.file = os.path.join('..', attachment_1_path)
        attachment_1.save()
        email_template.attachments.add(attachment_1)

        attachment_2_name = 'build.json'
        attachment_2_path = 'media/test_attachment_2'
        copyfile(os.path.join(files_path, 'archive.zip'), attachment_2_path)
        attachment_2 = Attachment(
            name=attachment_2_name,
            buildable=True
        )
        attachment_2.file = os.path.join('..', attachment_2_path)
        attachment_2.save()
        email_template.attachments.add(attachment_2)

        # add targets emails
        target_group1_emails = ('*****@*****.**', '*****@*****.**', '*****@*****.**')
        target_group1_title = 'target group 1 title'
        target_group1 = TargetGroup.objects.create(name=target_group1_title)

        for email in target_group1_emails:
            Target.objects.create(email=email, group=target_group1)

        self.assertEqual(len(target_group1.targets.all()), 3)

        # add other target group
        target_group2_emails = ('*****@*****.**', '*****@*****.**')
        target_group2_title = 'target group 2 title'
        target_group2 = TargetGroup.objects.create(name=target_group2_title)

        for email in target_group2_emails:
            Target.objects.create(email=email, group=target_group2)

        self.assertEqual(len(target_group2.targets.all()), 2)

        # create campaign
        campaign_name = 'campaign title'
        campaign = Campaign.objects.create(
            email_template=email_template,
            name=campaign_name,
        )

        self.assertEqual(len(mail.outbox), 0)  # not email sended

        # send emails to group 1
        campaign.target_groups.add(target_group1)
        self.run_jobs()

        # There is three target and four trackers by target,
        # so we must have 12 trackers at all
        trackers_count = campaign.trackers.count()

        # If you need some debug on why your email is in fail state ;)
        # if campaign.trackers.filter(key='email_send', value='fail').exists():
        #     # debug for email send error
        #     print('error details:')
        #     for name, value in campaign.trackers.first().__dict__.items():
        #         if not name.startswith('_'):
        #             print(' %s: %s' % (name, value))

        self.assertEqual(trackers_count, 12, campaign.trackers.all())
        self.assertEqual(len(mail.outbox), 3, mail.outbox)
        self.assertIn('click: %s' % landing_page_domain, mail.outbox[0].body)

        for email in mail.outbox:
            message = email.message()
            self.assertEqual(message['Subject'], email_template_subject)
            self.assertEqual(message['From'], email_template_from)
            self.assertIn(message['To'], target_group1_emails)

        # We send campaign to target_group2.
        # So we must have the previous 10 trackers and 4 more, so 16 at all
        campaign.target_groups.add(target_group2)
        self.run_jobs()

        self.assertEqual(campaign.trackers.count(), 16)
        self.assertEqual(len(mail.outbox), 4)
        message = mail.outbox[-1].message()
        self.assertEqual(message['Subject'], email_template_subject)
        self.assertEqual(message['From'], email_template_from)
        self.assertEqual(message['To'], '*****@*****.**')

        # clean
        os.remove(attachment_1_path)
        os.remove(attachment_2_path)