from nose.tools import eq_ import bleach def test_delinkify(): eq_('test', bleach.delinkify('test')) eq_('footestbar', bleach.delinkify('footestbar')) def test_whitelist(): html = 'test' eq_(html, bleach.delinkify(html, allow_domains=['ex.mp'])) eq_('test', bleach.delinkify(html, allow_domains=['ex2.mp'])) # Allow a single domain as a special case. eq_(html, bleach.delinkify(html, allow_domains='ex.mp')) def test_nested_a(): html = 'testtest' eq_('testtest', bleach.delinkify(html)) eq_('testtest', bleach.delinkify(html, allow_domains=['ex.mp'])) def test_nested_tag(): html = 'testtest' eq_('testtest', bleach.delinkify(html)) def test_a_name(): """Don't screw with non-link tags.""" html = 'bar' eq_(html, bleach.delinkify(html)) def test_relative(): """Relative links are optionally OK.""" html = 'some link' eq_('some link', bleach.delinkify(html)) eq_(html, bleach.delinkify(html, allow_relative=True)) def test_protocol_relative(): """Protocol-relative links aren't relative.""" html = 'bad link' expect = 'bad link' eq_(expect, bleach.delinkify(html)) eq_(expect, bleach.delinkify(html, allow_relative=True)) eq_(html, bleach.delinkify(html, allow_domains='ex.mp')) def test_domain_match(): tests = ( ('ex.mp', 'ex.mp', True), ('ex.mp', '*.ex.mp', True), ('test.ex.mp', '*.ex.mp', True), ('test.ex.mp', 'ex.mp', False), ('test.test.ex.mp', '*.ex.mp', False), ('test.test.ex.mp', '**.ex.mp', True), ('wrong.mp', 'ex.mp', False), ('wrong.mp', '*.ex.mp', False), ('really.wrong.mp', 'ex.mp', False), ('really.wrong.mp', '*.ex.mp', False), ('really.very.wrong.mp', '*.ex.mp', False), ('EX.mp', 'ex.mp', True), # Domains are case-insensitive. ('ex.mp', 'an.ex.mp', False), ('ex.mp', '*.an.ex.mp', False), ('an.ex.am.pl', 'an.*.am.pl', True), ('a.ex.am.pl', 'an.*.am.pl', False), ('ex.am.pl', 'an.*.am.pl', False), ) def _check(t, c, v): eq_(v, bleach._domain_match(t, c)) for t, c, v in tests: yield _check, t, c, v def test_double_star(): assert bleach._domain_match('ex.mp', '**.ex.mp') try: bleach._domain_match('ex.mp', 'an.**.ex.mp') except bleach.ValidationError: pass else: assert False, '_domain_match should not accept an.**.ex.mp' def test_allow_subdomains(): domains = ('ex.mp', '*.exa.mp', 'an.exam.pl', '*.my.examp.le') html = ( ('bad', 'bad'), ('good', None), ('good', None), ('good', None), ('bad', 'bad'), ('bad', 'bad'), ('bad', 'bad'), ) def _check(html, text): output = bleach.delinkify(html, allow_domains=domains) eq_(html if text is None else text, output) for t, o in html: yield _check, t, o