def test_external_link_with_background_img(self): s = ('<a href="https://somewhere/foo"><span>' '<div style="background-image: url(\'some.png\')"></div>' '</span></a>') output = parse_links(s) self.assertIn('external-site', output) self.assertNotIn('cf-icon-svg', output)
def test_rich_text_links_get_expanded(self): page = CFGOVPage(title='foo bar', slug='foo-bar') publish_page(page) link = f'<body><a id="{page.id}" linktype="page">foo bar</a></body>' output = parse_links(link) self.assertEqual('<body><a href="/foo-bar/">foo bar</a></body>', output)
def test_with_svg_not_at_the_end_still_gets_svg(self): s = ('<a href="https://external.gov">' '<span><svg>something</svg> Text after icon</span>' '</a>') output = parse_links(s) soup = BeautifulSoup(output, 'html.parser') self.assertEqual(len(soup.find_all('svg')), 2)
def test_files_get_download_icon(self): file_types = ['pdf', 'doc', 'docx', 'xls', 'xlsx', 'csv', 'zip'] for file_type in file_types: link = '<a href="/something.{}">link</a>'.format( file_type ).encode('utf-8') output = parse_links(link) self.assertIn(b'cf-icon-svg', output)
def test_external_link_outside_body_with_attributes(self): s = ''' <a href="https://somewhere/foo">Link</a> <body class="test"> </body> ''' output = parse_links(s) self.assertNotIn('external-site', output) self.assertNotIn('cf-icon-svg', output)
def test_external_link_with_attribute(self): s = ''' <body> <a href="https://somewhere" data-thing="something">Link</a> </body> ''' output = parse_links(s) self.assertIn('external-site', output) self.assertIn('cf-icon-svg', output)
def test_multiline_external_gov_link(self): s = '''<a class="m-list_link a-link" href="https://usa.gov/"> <span> USA .gov</span> </a> ''' output = parse_links(s) self.assertIn('cf-icon-svg', output)
def test_internal_link(self): """Internal links get neither icon nor redirect.""" link = ''' <body> <a href="https://www.consumerfinance.gov/foo">cfpb link</a> </body> ''' output = parse_links(link) self.assertNotIn('external-site', output) self.assertNotIn('cf-icon-svg', output)
def test_multiple_links(self): s = ''' <body> <a href="https://first.com">one</a> <a href="https://second.com">two</a> </body> ''' output = parse_links(s) soup = BeautifulSoup(output, 'html.parser') self.assertEqual(len(soup.find_all('a')), 2)
def test_external_link_with_img(self): s = '<body><a href="https://somewhere"><img src="some.png"></a></body>' output = parse_links(s) self.assertIn('external-site', output) self.assertNotIn('cf-icon-svg', output)
def test_external_link_with_header(self): s = '<body><a href="https://somewhere"><h3>Header</h3></a></body>' output = parse_links(s) self.assertIn('external-site', output) self.assertNotIn('cf-icon-svg', output)
def test_different_case_pdf_link_gets_download_icon(self): link = '<a href="/something.PDF">link</a>' output = parse_links(link) self.assertIn('cf-icon-svg', output)
def check_after_parse_links_has_a_single_svg(self, s): output = parse_links(s) soup = BeautifulSoup(output, 'html.parser') self.assertEqual(len(soup.find_all('svg')), 1)
def test_relative_link_remains_unmodified(self): self.assertEqual( parse_links('<body><a href="/something">text</a></body>'), '<body><a href="/something">text</a></body>')
def test_relative_link_remains_unmodified(self): self.assertEqual( parse_links('<a href="/something">text</a>'), '<a href="/something">text</a>' )
def test_rich_text_links_get_expanded(self): page = CFGOVPage(title='foo bar', slug='foo-bar') publish_page(page) link = b'<a id="{}" linktype="page">foo bar</a>'.format(page.id) output = parse_links(link) self.assertEqual(b'<a href="/foo-bar/">foo bar</a>', output)
def test_different_case_pdf_link_gets_download_icon(self): link = '<body><a href="/something.PDF">link</a></body>' output = parse_links(link) self.assertIn('cf-icon-svg', output)
def test_internal_link(self): """Internal links get neither icon nor redirect.""" link = '<a href="https://www.consumerfinance.gov/foo">cfpb link</a>' output = parse_links(link) self.assertNotIn('external-site', output) self.assertNotIn('cf-icon-svg', output)
def test_non_gov_link(self): """Non gov links get external link icon and redirect.""" link = '<body><a href="https://google.com">external link</a></body>' output = parse_links(link) self.assertIn('external-site', output) self.assertIn('cf-icon-svg', output)
def test_gov_link(self): """Gov links get external link icon but not redirect.""" link = '<a href="https://www.fdic.gov/bar">gov link</a>' output = parse_links(link) self.assertIn('cf-icon-svg', output)
def test_non_gov_link(self): """Non gov links get external link icon and redirect.""" link = '<a href="https://wwww.google.com">external link</a>' output = parse_links(link) self.assertIn('external-site', output) self.assertIn('cf-icon-svg', output)
def test_works_properly_on_bytestrings(self): self.assertEqual( parse_links(b'<a href="/something">text</a>'), '<a href="/something">text</a>' )
def check_after_parse_links_has_this_many_svgs(self, count, s): output = parse_links(s) soup = BeautifulSoup(output, 'html.parser') self.assertEqual(len(soup.find_all('svg')), count)
def test_rich_text_links_get_expanded(self): page = CFGOVPage(title='foo bar', slug='foo-bar') publish_page(page) link = '<a id="{}" linktype="page">foo bar</a>'.format(page.id) output = parse_links(link) self.assertEqual('<a href="/foo-bar/">foo bar</a>', output)
def test_in_page_anchor_links_have_current_path_stripped(self): s = '<body><a href="/foo/bar/#anchor">Anchor</a></body>' output = parse_links(s, request_path='/foo/bar/') self.assertNotIn('/foo/bar/', output) self.assertIn('href="#anchor"', output)
def test_non_default_encoding(self): s = '<body><a href="/something">哈哈</a></body>' encoding = 'gb2312' parsed = parse_links(s.encode(encoding), encoding=encoding) self.assertEqual(parsed, s)
def test_works_properly_on_bytestrings(self): self.assertEqual( parse_links(b'<body><a href="/something">text</a></body>'), '<body><a href="/something">text</a></body>')
def test_non_default_encoding(self): s = '<a href="/something">哈哈</a>' encoding = 'gb2312' parsed = parse_links(s.encode(encoding), encoding=encoding) self.assertEqual(parsed, s)
def test_gov_link(self): """Gov links get external link icon but not redirect.""" link = '<body><a href="https://www.fdic.gov/bar">gov link</a></body>' output = parse_links(link) self.assertIn('cf-icon-svg', output)
def test_files_get_download_icon(self): file_types = ['pdf', 'doc', 'docx', 'xls', 'xlsx', 'csv', 'zip'] for file_type in file_types: link = f'<body><a href="/something.{file_type}">link</a></body>' output = parse_links(link) self.assertIn('cf-icon-svg', output)