Exemple #1
0
 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()
Exemple #2
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])
Exemple #3
0
 def test_form_validation(self):
     user = UserFactory()
     message = MessageFactory(author=user)
     mail = MailFactory(message=message)
     f = AbuseNotificationForm({
         'mail': mail.identifier,
         'comments': ''})
     self.assertFalse(f.is_valid())
Exemple #4
0
 def test_abuse_url(self):
     user = UserFactory()
     message = MessageFactory(author=user)
     mail = MailFactory(message=message)
     self.assertEqual(
         mail.abuse_url,
         'http://test.munch.example.com/abuse/report/{}/'.format(
             mail.identifier))
Exemple #5
0
 def test_mail_resolution(self):
     user = UserFactory()
     message = MessageFactory(author=user)
     mail = MailFactory(message=message)
     f = AbuseNotificationForm({
         'mail': mail.identifier,
         'comments': 'test'})
     f.is_valid()
     f.save()
Exemple #6
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])
Exemple #7
0
 def test_abuse_makes_optout(self):
     """ An abuse should create an optout for the given mail """
     user = UserFactory()
     message = MessageFactory(author=user)
     mail = MailFactory(message=message)
     response = self.client.post(
         'http://test.munch.example.com/abuse/report/{}/'.format(
             mail.identifier),
         {'mail': mail.identifier, 'comments': 'test'})
     self.assertEqual(response.status_code, 302)
     optout = OptOut.objects.get(identifier=mail.identifier)
     self.assertEqual(optout.origin, OptOut.BY_ABUSE)
Exemple #8
0
    def test_cannot_delete_non_empty_category(self):
        category = CategoryFactory(author=self.user)

        MessageFactory(category=category, author=self.user)

        self.assertEqual(Category.objects.count(), 1)
        self.assertEqual(Category.objects.first().messages.count(), 1)
        resp = self.client.delete('/{}/categories/{}/'.format(
            self.api_prefix, category.pk))
        self.assertEqual(resp.status_code, 403)
        self.assertEqual(Category.objects.count(), 1)
        self.assertEqual(Category.objects.first().messages.count(), 1)
Exemple #9
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)
Exemple #10
0
    def test_reaction_time(self):
        with fake_time('2016-10-10 08:00:00'):
            message = MessageFactory(status='message_ok', author=self.user)
            mail = MailFactory(message=message)
            MailStatusFactory(mail=mail)
            MailStatusFactory(mail=mail, status=MailStatus.SENDING)
            MailStatusFactory(mail=mail, status=MailStatus.DELIVERED)
        with fake_time('2016-10-10 08:00:15'):
            self.client.get('/t/open/{}'.format(mail.identifier))
            records = TrackRecord.objects.filter(identifier=mail.identifier,
                                                 kind='read')

        self.assertEqual(records.first().properties['reaction_time'], '15')
Exemple #11
0
    def test_valid_unsubscribe(self):
        user = UserFactory()
        message = MessageFactory(author=user)
        mail = MailFactory(message=message)

        recipient = 'unsubscribe-{}@test.munch.example.com'.format(
            mail.identifier)
        unsubscribe = UNSUBSCRIBE_EMAIL.replace('||TO||', recipient)
        unsubscribe = unsubscribe.replace('||RETURNPATH||', recipient)
        message = email.message_from_string(unsubscribe)
        envelope = Envelope()
        envelope.parse_msg(message)
        envelope.recipients = [recipient]
        handler = UnsubscribeHandler(envelope, message)
        self.assertIsNotNone(handler.apply())
Exemple #12
0
    def test_valid_arf(self):
        user = UserFactory()
        message = MessageFactory(author=user)
        mail = MailFactory(message=message)

        recipient = 'return-{}@test.munch.example.com'.format(
            mail.identifier)
        arf = ARF_REPORT.replace('||TO||', '*****@*****.**')
        arf = arf.replace('||RETURNPATH||', recipient)
        message = email.message_from_string(arf)
        envelope = Envelope()
        envelope.parse_msg(message)
        envelope.recipients = [recipient]
        handler = ARFHandler(envelope, message)
        self.assertIsNotNone(handler.apply())
Exemple #13
0
    def test_valid_tracker(self):
        with fake_time('2016-10-10 08:00:00'):
            message = MessageFactory(status='message_ok', author=self.user)
            mail = MailFactory(message=message)
            MailStatusFactory(mail=mail)
            MailStatusFactory(mail=mail, status=MailStatus.SENDING)
            MailStatusFactory(mail=mail, status=MailStatus.DELIVERED)
        with fake_time('2016-10-10 08:00:15'):
            resp = self.client.get('/t/open/{}'.format(mail.identifier))

        self.assertEqual(resp.status_code, 200)
        self.assertEqual(resp['Content-Type'], 'image/gif')
        self.assertEqual(resp.content, TRACKER_PIXEL)

        records = TrackRecord.objects.filter(identifier=mail.identifier,
                                             kind='read',
                                             properties__source=READ_MUA_PIXEL)

        self.assertEqual(records.count(), 1)
Exemple #14
0
 def setUp(self):
     self.user = UserFactory()
     self.message = MessageFactory(author=self.user)
Exemple #15
0
 def test_invalid_method(self):
     message = MessageFactory(status='message_ok', author=self.user)
     resp = self.client.post('/t/open/{}'.format(message.identifier))
     self.assertEqual(resp.status_code, 405)
Exemple #16
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())
Exemple #17
0
 def _mk_message(self, sender):
     return MessageFactory(sender_email=sender, author=self.user_1)
Exemple #18
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)