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])
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])
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)