def check_generate_pages_html(self, current_page, total_pages, window=7, check_middle=False): extra_links = 4 # first, prev, next, last search = "'>\"/><img src=x onerror=alert(1)>" html_str = utils.generate_pages(current_page, total_pages, search=search) self.assertNotIn( search, html_str, "The raw search string shouldn't appear in the output") self.assertIn( 'search=%27%3E%22%2F%3E%3Cimg+src%3Dx+onerror%3Dalert%281%29%3E', html_str) self.assertTrue(callable(html_str.__html__), "Should return something that is HTML-escaping aware") dom = BeautifulSoup(html_str, 'html.parser') self.assertIsNotNone(dom) ulist = dom.ul ulist_items = ulist.find_all('li') self.assertEqual( min(window, total_pages) + extra_links, len(ulist_items)) page_items = ulist_items[2:-2] mid = int(len(page_items) / 2) for i, item in enumerate(page_items): a_node = item.a href_link = a_node['href'] node_text = a_node.string if node_text == str(current_page + 1): if check_middle: self.assertEqual(mid, i) self.assertEqual('javascript:void(0)', href_link) self.assertIn('active', item['class']) else: self.assertRegex(href_link, r'^\?', 'Link is page-relative') query = parse_qs(href_link[1:]) self.assertListEqual(query['page'], [str(int(node_text) - 1)]) self.assertListEqual(query['search'], [search])
def check_generate_pages_html(self, current_page, total_pages, window=7, check_middle=False): extra_links = 4 # first, prev, next, last html_str = utils.generate_pages(current_page, total_pages) # dom parser has issues with special « and » html_str = html_str.replace('«', '') html_str = html_str.replace('»', '') dom = minidom.parseString(html_str) self.assertIsNotNone(dom) ulist = dom.getElementsByTagName('ul')[0] ulist_items = ulist.getElementsByTagName('li') self.assertEqual( min(window, total_pages) + extra_links, len(ulist_items)) def get_text(nodelist): rc = [] for node in nodelist: if node.nodeType == node.TEXT_NODE: rc.append(node.data) return ''.join(rc) page_items = ulist_items[2:-2] mid = int(len(page_items) / 2) for i, item in enumerate(page_items): a_node = item.getElementsByTagName('a')[0] href_link = a_node.getAttribute('href') node_text = get_text(a_node.childNodes) if node_text == str(current_page + 1): if check_middle: self.assertEqual(mid, i) self.assertEqual('javascript:void(0)', a_node.getAttribute('href')) self.assertIn('active', item.getAttribute('class')) else: link_str = '?page=' + str(int(node_text) - 1) self.assertEqual(link_str, href_link)
def check_generate_pages_html(self, current_page, total_pages, window=7, check_middle=False): extra_links = 4 # first, prev, next, last html_str = utils.generate_pages(current_page, total_pages) # dom parser has issues with special « and » html_str = html_str.replace('«', '') html_str = html_str.replace('»', '') dom = minidom.parseString(html_str) self.assertIsNotNone(dom) ulist = dom.getElementsByTagName('ul')[0] ulist_items = ulist.getElementsByTagName('li') self.assertEqual(min(window, total_pages) + extra_links, len(ulist_items)) def get_text(nodelist): rc = [] for node in nodelist: if node.nodeType == node.TEXT_NODE: rc.append(node.data) return ''.join(rc) page_items = ulist_items[2:-2] mid = int(len(page_items) / 2) for i, item in enumerate(page_items): a_node = item.getElementsByTagName('a')[0] href_link = a_node.getAttribute('href') node_text = get_text(a_node.childNodes) if node_text == str(current_page + 1): if check_middle: self.assertEqual(mid, i) self.assertEqual('javascript:void(0)', a_node.getAttribute('href')) self.assertIn('active', item.getAttribute('class')) else: link_str = '?page=' + str(int(node_text) - 1) self.assertEqual(link_str, href_link)