Ejemplo n.º 1
0
    def test_html_non_generation(self):
        message = MessageFactory(
            author=self.user,
            track_open=False,
            html='<body><h1>Foo</h1>{}</body>'.format(
                settings.OPTOUTS['UNSUBSCRIBE_PLACEHOLDER']))
        with patch('munch.apps.spamcheck.SpamChecker.check',
                   side_effect=get_spam_result_mock):
            message.save()

        mail = MailFactory(message=message)
        content = message.to_mail(mail)
        self.assertNotIn('pixel.gif', content.alternatives[0][0])
Ejemplo n.º 2
0
    def test_html_generation_preserve_doctype(self):
        message = MessageFactory(
            author=self.user,
            track_open=True,
            html=(
                '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//E'
                'N" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">'
                '<html xmlns="http://www.w3.org/1999/xhtml">{}</html>').format(
                    settings.OPTOUTS['UNSUBSCRIBE_PLACEHOLDER']))

        with patch('munch.apps.spamcheck.SpamChecker.check',
                   side_effect=get_spam_result_mock):
            message.save()

        mail = MailFactory(message=message)
        content = message.to_mail(mail)
        self.assertIn(
            '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" '
            '"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">',
            content.alternatives[0][0])
Ejemplo n.º 3
0
class TestContentFilters(TestCase):
    def setUp(self):
        self.user = UserFactory()
        self.message = MessageFactory(
            author=self.user,
            track_clicks=True,
            track_open=False,
            msg_links={'AAAAABBBBB': 'http://example.com'},
            html='<body><a href="http://example.com">Hi</a>{}</body>'.format(
                settings.OPTOUTS['UNSUBSCRIBE_PLACEHOLDER']))
        with patch('munch.apps.spamcheck.SpamChecker.check',
                   side_effect=get_spam_result_mock):
            self.message.save()

    def test_rewrite_footlink(self):
        text = """Hi
        We are [1]

        [1]: http://example.com
        """
        mail = MailFactory(message=self.message)
        out = rewrite_plaintext_links(text,
                                      app_url=self.message.get_app_url(),
                                      unsubscribe_url=mail.unsubscribe_url,
                                      mail_identifier=mail.identifier,
                                      links_map=self.message.msg_links)
        self.assertIn('/clicks/m/', out)

    def test_dont_rewrite_bodylink(self):
        text = """Hi

        Go to http://example.com
        """
        mail = MailFactory(message=self.message)
        out = rewrite_plaintext_links(text,
                                      app_url=self.message.get_app_url(),
                                      unsubscribe_url=mail.unsubscribe_url,
                                      mail_identifier=mail.identifier,
                                      links_map=self.message.msg_links)
        self.assertNotIn('/clicks/m', out)
Ejemplo n.º 4
0
class TestTrackingLinks(TestCase):
    def setUp(self):
        self.user = UserFactory()
        self.message = MessageFactory(
            author=self.user,
            track_clicks=True,
            track_open=False,
            html=('<body><a href="http://example.com">'
                  'Hi</a>{}</body>').format(
                      settings.OPTOUTS['UNSUBSCRIBE_PLACEHOLDER']))
        with patch('munch.apps.spamcheck.SpamChecker.check',
                   side_effect=get_spam_result_mock):
            self.message.save()
        self.mail = MailFactory(message=self.message)

    def test_html_generation(self):
        with patch('munch.apps.spamcheck.SpamChecker.check',
                   side_effect=get_spam_result_mock):
            self.message.save()
        content = self.message.to_mail(self.mail)
        self.assertIn('/clicks/m/', content.alternatives[0][0])

    def test_plaintext_generation(self):
        with patch('munch.apps.spamcheck.SpamChecker.check',
                   side_effect=get_spam_result_mock):
            self.message.save()

        content = self.message.to_mail(self.mail)
        self.assertIn('/clicks/m/', content.body)

    def test_html_no_unsubscribe_or_viewonline_rewrite(self):
        self.message.track_clicks = True
        self.message.html = '<body><h1>Hi</h1>{}</body>'.format(
            settings.OPTOUTS['UNSUBSCRIBE_PLACEHOLDER'])
        with patch('munch.apps.spamcheck.SpamChecker.check',
                   side_effect=get_spam_result_mock):
            self.message.save()

        mail = MailFactory(message=self.message)
        content = self.message.to_mail(mail)
        self.assertNotIn('/clicks/m/', content.alternatives[0][0])

    def test_mk_redirection(self):
        mail = MailFactory(message=self.message)
        url = 'http://example.com'
        redir_url = WebVersionLinksRewriter.rewrite(mail.identifier,
                                                    self.message.get_app_url(),
                                                    mail.message.msg_links,
                                                    url)

        # build local url from fully qualified url
        local_redir_url = redir_url.split('://')[1].split('/', 1)[1]

        resp = self.client.get('/' + local_redir_url)
        self.assertEqual(resp.status_code, 302)
        self.assertEqual(resp['Location'], url)

    def test_mk_web_redirection(self):
        mail = MailFactory(message=self.message)
        url = 'http://example.com'
        redir_url = WebVersionLinksRewriter.rewrite(mail.identifier,
                                                    self.message.get_app_url(),
                                                    mail.message.msg_links,
                                                    url)

        # build local url from fully qualified url
        local_redir_url = redir_url.split('://')[1].split('/', 1)[1]

        self.assertEqual(LinkMap.objects.count(), 1)
        self.assertEqual(
            TrackRecord.objects.filter(kind='click',
                                       identifier=mail.identifier).count(), 0)
        resp = self.client.get('/' + local_redir_url)
        self.assertEqual(resp.status_code, 302)
        self.assertEqual(resp['Location'], url)
        self.assertEqual(
            TrackRecord.objects.filter(kind='click',
                                       identifier=mail.identifier).count(), 1)

    def test_invalid_tracker(self):
        resp = self.client.get('/t/clicks/m/0202020202020202/CCCCCDDDDD')
        self.assertEqual(resp.status_code, 200)
        self.assertEqual(resp.content, b'No URL found with this ID')

    def test_html_generation_preserve_doctype(self):
        self.message.track_clicks = True
        self.message.html = (
            '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" '
            '"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">'
            '<html xmlns="http://www.w3.org/1999/xhtml">{}</html>').format(
                settings.OPTOUTS['UNSUBSCRIBE_PLACEHOLDER'])
        with patch('munch.apps.spamcheck.SpamChecker.check',
                   side_effect=get_spam_result_mock):
            self.message.save()

        mail = MailFactory(message=self.message)
        content = self.message.to_mail(mail)
        self.assertIn(
            ('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//'
             'EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.'
             'dtd">'), content.alternatives[0][0])

    def test_web_version_links_can_track(self):
        self.message.track_clicks = True
        self.message.save()

        mail = MailFactory(message=self.message)

        web_token = WebKey.from_instance(mail.identifier)
        response = self.client.get('/archive/{}/?web_key={}'.format(
            self.message.identifier, web_token.token))

        self.assertIn(b'/t/clicks/w/', response.content)

    def test_web_version_links_can_no_track(self):
        self.message.track_clicks = False
        self.message.save()

        mail = MailFactory(message=self.message)
        web_key = WebKey.from_instance(mail.identifier)
        response = self.client.get('/archive/{}/?web_key={}'.format(
            self.message.identifier, web_key.token))

        self.assertNotIn(b'/t/clicks/w/', response.content)

    def test_web_versions_open_can_no_track(self):
        self.message.track_open = False
        self.message.save()
        mail = MailFactory(message=self.message)
        self.assertNotIn('web_key', mail.web_view_url)

    def test_web_versions_open_can_track(self):
        self.message.track_open = True
        self.message.save()
        mail = MailFactory(message=self.message)
        self.assertIn('web_key', mail.web_view_url)
Ejemplo n.º 5
0
class TestOptOut(TestCase):
    def setUp(self):
        self.user = UserFactory()
        self.message = MessageFactory(author=self.user)

    def test_new_optout(self):
        """ OptOut when no other opptout exist for this email"""
        mail = MailFactory(message=self.message)

        self.assertFalse(
            OptOut.objects.filter(identifier=mail.identifier).exists())
        response = self.client.post('/h/subscriptions/{}/optout/'.format(
            mail.identifier))
        self.assertEqual(response.status_code, 302)

        o = OptOut.objects.get(identifier=mail.identifier)
        self.assertEqual(o.origin, OptOut.BY_WEB)

    def test_optout_again(self):
        """ OptOut when a previous optout exists:

        Keep it but change date and origin"""
        mail = MailFactory(message=self.message)
        first_optout = OptOutFactory(identifier=mail.identifier,
                                     address=mail.recipient,
                                     origin=OptOut.BY_MAIL)
        first_optout_date = first_optout.creation_date

        response = self.client.post('/h/subscriptions/{}/optout/'.format(
            mail.identifier))
        self.assertEqual(response.status_code, 302)

        o = OptOut.objects.get(identifier=mail.identifier)
        self.assertEqual(o, first_optout)
        self.assertEqual(o.origin, OptOut.BY_WEB)
        self.assertNotEqual(o.creation_date, first_optout_date)

    def test_new_optout_external_post(self):
        self.message.external_optout = True
        self.message.save()
        mail = MailFactory(message=self.message)

        self.assertFalse(
            OptOut.objects.filter(identifier=mail.identifier).exists())
        response = self.client.post('/h/subscriptions/{}/optout/'.format(
            mail.identifier))
        self.assertEqual(response.status_code, 405)

        self.assertFalse(
            OptOut.objects.filter(identifier=mail.identifier).exists())

    def test_new_optout_external_get(self):
        self.message.external_optout = True
        self.message.save()
        mail = MailFactory(message=self.message)

        self.assertFalse(
            OptOut.objects.filter(identifier=mail.identifier).exists())
        response = self.client.get('/h/subscriptions/{}/optout/'.format(
            mail.identifier))
        self.assertEqual(response.status_code, 200)
        self.assertIn('should get in touch with him',
                      response.content.decode())

    def test_optout_page_not_preview(self):
        """ Unsubscribe page displays no warning"""
        mail = MailFactory(message=self.message)
        response = self.client.get('/h/subscriptions/{}/optout/'.format(
            mail.identifier))
        self.assertEqual(response.status_code, 200)

        self.assertNotIn("from a preview message", response.content.decode())

    def test_optout_page_preview(self):
        """ Unsubscribe page displays a warning"""

        pm = PreviewMailFactory(message=self.message)
        response = self.client.get('/h/subscriptions/{}/optout/'.format(
            pm.identifier))
        self.assertEqual(response.status_code, 200)
        self.assertIn("from a preview message", response.content.decode())