def test_nonmultipart_no_text(self): """ Tests the get_content function with a nonmultipart email that doesn't contain the requested content type. """ msg = self.part2 actual = accessors.get_content(email=msg, content_types=('text/plain', )) self.assertEqual(actual, None)
def test_sanitize(self): """ Makes sure the _get_values method strips malicious code. """ parser = MailParser(source_field='Content', method='COPY', regex='Bad Robots') assert 'SCRIPT' in str(get_content(self.msg)) result = parser.process(self.msg) self.assertFalse(re.search('SCRIPT', str(result), re.IGNORECASE))
def test_multipart_default(self): """ Tests the get_content function with a multipart email and default settings for content_types and remove_tags. """ self.msg.attach(self.part1) self.msg.attach(self.part2) actual = accessors.get_content(self.msg) expected = self.text.encode('UTF-8') self.assertEqual(actual, expected)
def test_nested_multipart(self): """ Tests the get_content function with a multipart email that contains another multipart message. """ msg = MIMEMultipart('alternative') self.msg.attach(self.part2) self.msg.attach(self.part1) msg.attach(self.msg) actual = accessors.get_content(msg) expected = self.text.encode('UTF-8') self.assertEqual(actual, expected)
def test_multipart_html_with_tags(self): """ Tests the get_content function for a multipart email with text/html content preferred and remove_tags=False. """ self.msg.attach(self.part1) self.msg.attach(self.part2) actual = accessors.get_content(email=self.msg, content_types=('text/html', 'text/plain'), remove_tags=False) expected = self.html.encode('UTF-8') self.assertEqual(actual, expected)
def test_nonmultipart_html_w_tags(self): """ Tests the get_content function with a nonmultipart html email, the default setting for content_types, and remove_tags=False. """ self.msg.attach(self.part2) content_types = ('text/html', 'text/plain') self.mock_mailsifter_settings[ 'EMAIL_CONTENT_PREFERENCES'] = content_types with patch('sifter.mailsifter.accessors.settings.MAILSIFTER', return_value=content_types): actual = accessors.get_content(self.msg, remove_tags=False) expected = self.html.encode('UTF-8') self.assertEqual(actual, expected)
def test_nonmultipart_html(self): """ Tests the get_content function with a nonmultipart html email and default settings for content_types and remove_tags. """ self.msg.attach(self.part2) content_types = ('text/html', 'text/plain') self.mock_mailsifter_settings[ 'EMAIL_CONTENT_PREFERENCES'] = content_types with patch('sifter.mailsifter.accessors.settings.MAILSIFTER', self.mock_mailsifter_settings): actual = accessors.get_content(self.msg) expected = self.cleaned_html self.assertEqual(actual, expected)
def test_multipart_html(self): """ Tests the get_content function for a multipart email with text/html content preferred and default setting for remove_tags. """ self.msg.attach(self.part1) self.msg.attach(self.part2) content_types = ('text/html', 'text/plain') self.mock_mailsifter_settings[ 'EMAIL_CONTENT_PREFERENCES'] = content_types with patch.dict('sifter.mailsifter.accessors.settings.MAILSIFTER', self.mock_mailsifter_settings): actual = accessors.get_content(self.msg, content_types=('text/html', 'text/plain')) expected = self.cleaned_html self.assertEqual(actual, expected)
def test_multipart_no_text(self): """ Tests the get_content function with a multipart email with no text. """ self.assertEqual(accessors.get_content(self.msg), None)