Esempio n. 1
0
class TestNonReplacement(SimpleTestCase):
    """Test that email addresses do not get replaced under certain circumstances"""
    def setUp(self):
        self.middleware = ObfuscateEmailAddressMiddleware()

    def test_skips_non_html(self):
        content = 'Contact me at: [email protected]'
        response = HttpResponse(content, content_type='text/plain')
        self.assertEqual(
            force_text(
                self.middleware.process_response(None, response).content),
            content)

    @skipUnless(StreamingHttpResponse,
                'StreamingHttpResponse is not available')
    def test_skips_streaming(self):
        content = 'Contact me at: [email protected]'
        response = StreamingHttpResponse(content)
        self.assertEqual(
            ''.join(
                force_text(s)
                for s in self.middleware.process_response(None, response)),
            content)

    def test_twitter_username(self):
        content = 'On twitter I am known as @example'
        response = HttpResponse(content)
        self.assertEqual(
            force_text(
                self.middleware.process_response(None, response).content),
            content)
class TestEmailAddressObfuscation(SimpleTestCase):
    """Test the obfuscation method"""
    def setUp(self):
        self.middleware = ObfuscateEmailAddressMiddleware()

    def test_is_obfuscated(self):
        """Check if the given content is really not present in the response"""
        content = '*****@*****.**'
        self.assertNotEqual(
            force_text(
                self.middleware.process_response(
                    None, HttpResponse(content)).content), content)

    def test_is_html_escaped(self):
        """Unescape the escaped response to see if it's the original content"""
        try:
            h = html
        except:
            h = HTMLParser()
        content = '*****@*****.**'
        self.assertEqual(
            h.unescape(
                force_text(
                    self.middleware.process_response(
                        None, HttpResponse(content)).content)), content)
class TestEmailAddressObfuscation(SimpleTestCase):
    """Test the obfuscation method"""
    def setUp(self):
        self.middleware = ObfuscateEmailAddressMiddleware()

    def test_is_obfuscated(self):
        """Check if the given content is really not present in the response"""
        content = '*****@*****.**'
        self.assertNotEqual(self.middleware.process_response(None, HttpResponse(content)).content, content)

    def test_is_html_escaped(self):
        """Unescape the escaped response to see if it's the original content"""
        h = HTMLParser()
        content = '*****@*****.**'
        self.assertEqual(h.unescape(self.middleware.process_response(None, HttpResponse(content)).content), content)
class TestEmailAddressReplacement(SimpleTestCase):
    """Test if email addresses get detected, and replaced, correctly"""
    def setUp(self):
        """Mock the encoding method, so we can get predictable output"""
        self.middleware = ObfuscateEmailAddressMiddleware()
        self.middleware.encode_email = lambda matches: '!!%s!!' % matches.group(0)

    def assertResponse(self, content, expected):
        """Little helper assertion to dry things up"""
        self.assertEqual(
            force_text(self.middleware.process_response(None, HttpResponse(content)).content),
            expected)

    def test_simple(self):
        email = '*****@*****.**'
        self.assertResponse(email, '!!%s!!' % email)

    def test_common(self):
        email = '*****@*****.**'
        self.assertResponse(email, '!!%s!!' % email)

    def test_long(self):
        email = '*****@*****.**'
        self.assertResponse(email, '!!%s!!' % email)

    def test_with_plus_symbol(self):
        email = '*****@*****.**'
        self.assertResponse(email, '!!%s!!' % email)

    def test_dashes(self):
        email = '*****@*****.**'
        self.assertResponse(email, '!!%s!!' % email)

    def test_replaces_single_email(self):
        self.assertResponse('Contact me at: [email protected]', 'Contact me at: [email protected]!!')

    def test_replaces_multiple_email_addresses(self):
        content = ('Contact me at: [email protected]\n'
                   '[email protected] is the email address of my friend\n'
                   'We share [email protected] for email')
        expected = ('Contact me at: [email protected]!!\n'
                    '[email protected]!! is the email address of my friend\n'
                    'We share [email protected]!! for email')
        self.assertResponse(content, expected)

    def test_replaces_single_email_in_anchor(self):
        content = 'Contact me at: <a href="mailto:[email protected]">[email protected]</a>'
        expected = 'Contact me at: <a href="!!mailto:[email protected]!!">[email protected]!!</a>'
        self.assertResponse(content, expected)

    def test_replaces_multiple_email_addresses_in_anchors(self):
        content = ('Contact me at: <a href="mailto:[email protected]">[email protected]</a>\n'
                   '<a href="mailto:[email protected]">[email protected]</a> is the email address of my friend\n'
                   'We share <a href="mailto:[email protected]">[email protected]</a> for email')
        expected = ('Contact me at: <a href="!!mailto:[email protected]!!">[email protected]!!</a>\n'
                    '<a href="!!mailto:[email protected]!!">[email protected]!!</a> is the email address of my friend\n'
                    'We share <a href="!!mailto:[email protected]!!">[email protected]!!</a> for email')
        self.assertResponse(content, expected)
class TestNonReplacement(SimpleTestCase):
    """Test that email addresses do not get replaced under certain circumstances"""
    def setUp(self):
        self.middleware = ObfuscateEmailAddressMiddleware()

    def test_skips_non_html(self):
        content = 'Contact me at: [email protected]'
        response = HttpResponse(content, content_type='text/plain')
        self.assertEqual(self.middleware.process_response(None, response).content, content)

    @skipUnless(StreamingHttpResponse, 'StreamingHttpResponse is not available')
    def test_skips_streaming(self):
        content = 'Contact me at: [email protected]'
        response = StreamingHttpResponse(content)
        self.assertEqual(''.join(self.middleware.process_response(None, response)), content)

    def test_twitter_username(self):
        content = 'On twitter I am known as @example'
        response = HttpResponse(content)
        self.assertEqual(self.middleware.process_response(None, response).content, content)
Esempio n. 6
0
 def setUp(self):
     """Mock the encoding method, so we can get predictable output"""
     self.middleware = ObfuscateEmailAddressMiddleware()
     self.middleware.encode_email = lambda email: '!!%s!!' % email
Esempio n. 7
0
class TestEmailAddressReplacement(SimpleTestCase):
    """Test if email addresses get detected, and replaced, correctly"""
    def setUp(self):
        """Mock the encoding method, so we can get predictable output"""
        self.middleware = ObfuscateEmailAddressMiddleware()
        self.middleware.encode_email = lambda email: '!!%s!!' % email

    def assertResponse(self, content, expected):
        """Little helper assertion to dry things up"""
        self.assertEqual(
            force_text(self.middleware.process_response(None, HttpResponse(content)).content),
            expected)

    def test_simple(self):
        email = '*****@*****.**'
        self.assertResponse(email, '!!%s!!' % email)

    def test_common(self):
        email = '*****@*****.**'
        self.assertResponse(email, '!!%s!!' % email)

    def test_long(self):
        email = '*****@*****.**'
        self.assertResponse(email, '!!%s!!' % email)

    def test_with_plus_symbol(self):
        email = '*****@*****.**'
        self.assertResponse(email, '!!%s!!' % email)

    def test_dashes(self):
        email = '*****@*****.**'
        self.assertResponse(email, '!!%s!!' % email)

    def test_replaces_single_email(self):
        self.assertResponse('Contact me at: [email protected]', 'Contact me at: [email protected]!!')

    def test_replaces_multiple_email_addresses(self):
        content = ('Contact me at: [email protected]\n'
                   '[email protected] is the email address of my friend\n'
                   'We share [email protected] for email')
        expected = ('Contact me at: [email protected]!!\n'
                    '[email protected]!! is the email address of my friend\n'
                    'We share [email protected]!! for email')
        self.assertResponse(content, expected)

    def test_replaces_single_email_in_anchor(self):
        content = 'Contact me at: <a href="mailto:[email protected]">[email protected]</a>'
        expected = 'Contact me at: <a href="!!mailto:[email protected]!!">[email protected]!!</a>'
        self.assertResponse(content, expected)

    def test_replaces_multiple_email_addresses_in_anchors(self):
        content = ('Contact me at: <a href="mailto:[email protected]">[email protected]</a>\n'
                   '<a href="mailto:[email protected]">[email protected]</a> is the email address of my friend\n'
                   'We share <a href="mailto:[email protected]">[email protected]</a> for email')
        expected = ('Contact me at: <a href="!!mailto:[email protected]!!">[email protected]!!</a>\n'
                    '<a href="!!mailto:[email protected]!!">[email protected]!!</a> is the email address of my friend\n'
                    'We share <a href="!!mailto:[email protected]!!">[email protected]!!</a> for email')
        self.assertResponse(content, expected)

    def test_replacement_in_brackets(self):
        content = 'Email Me <*****@*****.**>'
        expected = 'Email Me <[email protected]!!>'
        self.assertResponse(content, expected)

    def test_replacement_with_dot(self):
        content = 'Email Me [email protected].'
        expected = 'Email Me [email protected]!!.'
        self.assertResponse(content, expected)

    def test_replacement_in_very_large_page(self):
        with open(os.path.join(os.path.dirname(__file__), 'very_large_page.html')) as f:
            content = f.read()
        output = force_text(self.middleware.process_response(None, HttpResponse(content)).content)
        self.assertIn('<a href="!!mailto:[email protected]!!">[email protected]!!</a>', output, msg='href not replaced')
        self.assertIn('<img alt="[email protected]!!"', output, msg='alt not replaced')
Esempio n. 8
0
 def setUp(self):
     self.middleware = ObfuscateEmailAddressMiddleware()
Esempio n. 9
0
class TestEmailAddressReplacement(SimpleTestCase):
    """Test if email addresses get detected, and replaced, correctly"""
    def setUp(self):
        """Mock the encoding method, so we can get predictable output"""
        self.middleware = ObfuscateEmailAddressMiddleware()
        self.middleware.encode_email = lambda matches: '!!%s!!' % matches.group(
            0)

    def assertResponse(self, content, expected):
        """Little helper assertion to dry things up"""
        self.assertEqual(
            self.middleware.process_response(None,
                                             HttpResponse(content)).content,
            expected)

    def test_simple(self):
        email = '*****@*****.**'
        self.assertResponse(email, '!!%s!!' % email)

    def test_common(self):
        email = '*****@*****.**'
        self.assertResponse(email, '!!%s!!' % email)

    def test_long(self):
        email = '*****@*****.**'
        self.assertResponse(email, '!!%s!!' % email)

    def test_with_plus_symbol(self):
        email = '*****@*****.**'
        self.assertResponse(email, '!!%s!!' % email)

    def test_dashes(self):
        email = '*****@*****.**'
        self.assertResponse(email, '!!%s!!' % email)

    def test_replaces_single_email(self):
        self.assertResponse('Contact me at: [email protected]',
                            'Contact me at: [email protected]!!')

    def test_replaces_multiple_email_addresses(self):
        content = ('Contact me at: [email protected]\n'
                   '[email protected] is the email address of my friend\n'
                   'We share [email protected] for email')
        expected = (
            'Contact me at: [email protected]!!\n'
            '[email protected]!! is the email address of my friend\n'
            'We share [email protected]!! for email')
        self.assertResponse(content, expected)

    def test_replaces_single_email_in_anchor(self):
        content = 'Contact me at: <a href="mailto:[email protected]">[email protected]</a>'
        expected = 'Contact me at: <a href="!!mailto:[email protected]!!">[email protected]!!</a>'
        self.assertResponse(content, expected)

    def test_replaces_multiple_email_addresses_in_anchors(self):
        content = (
            'Contact me at: <a href="mailto:[email protected]">[email protected]</a>\n'
            '<a href="mailto:[email protected]">[email protected]</a> is the email address of my friend\n'
            'We share <a href="mailto:[email protected]">[email protected]</a> for email'
        )
        expected = (
            'Contact me at: <a href="!!mailto:[email protected]!!">[email protected]!!</a>\n'
            '<a href="!!mailto:[email protected]!!">[email protected]!!</a> is the email address of my friend\n'
            'We share <a href="!!mailto:[email protected]!!">[email protected]!!</a> for email'
        )
        self.assertResponse(content, expected)
 def setUp(self):
     self.middleware = ObfuscateEmailAddressMiddleware()
 def setUp(self):
     """Mock the encoding method, so we can get predictable output"""
     self.middleware = ObfuscateEmailAddressMiddleware()
     self.middleware.encode_email = lambda matches: '!!%s!!' % matches.group(0)
class TestEmailAddressReplacement(SimpleTestCase):
    """Test if email addresses get detected, and replaced, correctly"""

    def setUp(self):
        """Mock the encoding method, so we can get predictable output"""
        self.middleware = ObfuscateEmailAddressMiddleware()
        self.middleware.encode_email = lambda email: '!!%s!!' % email

    def assertResponse(self, content, expected):
        """Little helper assertion to dry things up"""
        self.assertEqual(
            force_text(self.middleware.process_response(None, HttpResponse(content)).content),
            expected)

    def test_simple(self):
        email = '*****@*****.**'
        self.assertResponse(email, '!!%s!!' % email)

    def test_common(self):
        email = '*****@*****.**'
        self.assertResponse(email, '!!%s!!' % email)

    def test_long(self):
        email = '*****@*****.**'
        self.assertResponse(email, '!!%s!!' % email)

    def test_with_plus_symbol(self):
        email = '*****@*****.**'
        self.assertResponse(email, '!!%s!!' % email)

    def test_dashes(self):
        email = '*****@*****.**'
        self.assertResponse(email, '!!%s!!' % email)

    def test_replaces_single_email(self):
        self.assertResponse('Contact me at: [email protected]', 'Contact me at: [email protected]!!')

    def test_replaces_multiple_email_addresses(self):
        content = ('Contact me at: [email protected]\n'
                   '[email protected] is the email address of my friend\n'
                   'We share [email protected] for email')
        expected = ('Contact me at: [email protected]!!\n'
                    '[email protected]!! is the email address of my friend\n'
                    'We share [email protected]!! for email')
        self.assertResponse(content, expected)

    def test_replaces_single_email_in_anchor(self):
        content = 'Contact me at: <a href="mailto:[email protected]">[email protected]</a>'
        expected = 'Contact me at: <a href="!!mailto:[email protected]!!">[email protected]!!</a>'
        self.assertResponse(content, expected)

    def test_replaces_multiple_email_addresses_in_anchors(self):
        content = ('Contact me at: <a href="mailto:[email protected]">[email protected]</a>\n'
                   '<a href="mailto:[email protected]">[email protected]</a> is the email address of my friend\n'
                   'We share <a href="mailto:[email protected]">[email protected]</a> for email')
        expected = ('Contact me at: <a href="!!mailto:[email protected]!!">[email protected]!!</a>\n'
                    '<a href="!!mailto:[email protected]!!">[email protected]!!</a> is the email address of my friend\n'
                    'We share <a href="!!mailto:[email protected]!!">[email protected]!!</a> for email')
        self.assertResponse(content, expected)

    def test_replacement_in_brackets(self):
        content = 'Email Me <*****@*****.**>'
        expected = 'Email Me <[email protected]!!>'
        self.assertResponse(content, expected)

    def test_replacement_with_dot(self):
        content = 'Email Me [email protected].'
        expected = 'Email Me [email protected]!!.'
        self.assertResponse(content, expected)

    def test_replacement_in_very_large_page(self):
        with open(os.path.join(os.path.dirname(__file__), 'very_large_page.html')) as f:
            content = f.read()
        output = force_text(self.middleware.process_response(None, HttpResponse(content)).content)
        self.assertIn('<a href="!!mailto:[email protected]!!">[email protected]!!</a>', output, msg='href not replaced')
        self.assertIn('<img alt="[email protected]!!"', output, msg='alt not replaced')