From febacf79585b7420b65ec3a7a793ca9ae15b62fc Mon Sep 17 00:00:00 2001 From: Sage Abdullah Date: Mon, 21 Aug 2023 10:32:41 +0100 Subject: [PATCH] Use WagtailTestUtils.get_soup() more widely --- wagtail/admin/tests/pages/test_create_page.py | 3 +-- wagtail/admin/tests/test_dbwhitelister.py | 16 +++++++-------- wagtail/admin/tests/test_edit_handlers.py | 3 +-- wagtail/admin/tests/test_rich_text.py | 5 ++--- wagtail/admin/tests/test_userbar.py | 3 +-- wagtail/documents/tests/test_rich_text.py | 6 +++--- wagtail/embeds/tests/test_rich_text.py | 6 +++--- wagtail/images/tests/test_rich_text.py | 4 +--- wagtail/test/utils/form_data.py | 4 +++- wagtail/tests/test_whitelist.py | 20 +++++++++---------- 10 files changed, 33 insertions(+), 37 deletions(-) diff --git a/wagtail/admin/tests/pages/test_create_page.py b/wagtail/admin/tests/pages/test_create_page.py index cff8685609..9b52125033 100644 --- a/wagtail/admin/tests/pages/test_create_page.py +++ b/wagtail/admin/tests/pages/test_create_page.py @@ -2,7 +2,6 @@ import datetime import unittest from unittest import mock -from bs4 import BeautifulSoup from django.contrib.auth.models import Group, Permission from django.http import HttpRequest, HttpResponse from django.test import TestCase @@ -1022,7 +1021,7 @@ class TestPageCreation(WagtailTestUtils, TestCase): ) ) - html = BeautifulSoup(response.content, "html5lib") + html = self.get_soup(response.content) actual_attrs = html.find("input", {"name": "title"}).attrs diff --git a/wagtail/admin/tests/test_dbwhitelister.py b/wagtail/admin/tests/test_dbwhitelister.py index 8e854bfb2b..4de8b8ec94 100644 --- a/wagtail/admin/tests/test_dbwhitelister.py +++ b/wagtail/admin/tests/test_dbwhitelister.py @@ -1,22 +1,22 @@ -from bs4 import BeautifulSoup from django.test import TestCase from wagtail.admin.rich_text.converters.editor_html import EditorHTMLConverter +from wagtail.test.utils import WagtailTestUtils -class TestDbWhitelisterMethods(TestCase): +class TestDbWhitelisterMethods(WagtailTestUtils, TestCase): def setUp(self): self.whitelister = EditorHTMLConverter().whitelister def test_clean_tag_node_div(self): - soup = BeautifulSoup("
foo
", "html5lib") + soup = self.get_soup("
foo
", "html5lib") tag = soup.div self.assertEqual(tag.name, "div") self.whitelister.clean_tag_node(soup, tag) self.assertEqual(tag.name, "p") def test_clean_tag_node_with_data_embedtype(self): - soup = BeautifulSoup( + soup = self.get_soup( '

foo

', "html5lib", ) @@ -27,7 +27,7 @@ class TestDbWhitelisterMethods(TestCase): ) def test_clean_tag_node_with_data_linktype(self): - soup = BeautifulSoup( + soup = self.get_soup( 'foo', "html5lib", ) @@ -36,13 +36,13 @@ class TestDbWhitelisterMethods(TestCase): self.assertEqual(str(tag), 'foo') def test_clean_tag_node(self): - soup = BeautifulSoup('foo', "html5lib") + soup = self.get_soup('foo', "html5lib") tag = soup.a self.whitelister.clean_tag_node(soup, tag) self.assertEqual(str(tag), "foo") -class TestDbWhitelister(TestCase): +class TestDbWhitelister(WagtailTestUtils, TestCase): def setUp(self): self.whitelister = EditorHTMLConverter().whitelister @@ -52,7 +52,7 @@ class TestDbWhitelister(TestCase): (necessary because we can't guarantee the order that attributes are output in) """ self.assertEqual( - BeautifulSoup(str1, "html5lib"), BeautifulSoup(str2, "html5lib") + self.get_soup(str1, "html5lib"), self.get_soup(str2, "html5lib") ) def test_page_link_is_rewritten(self): diff --git a/wagtail/admin/tests/test_edit_handlers.py b/wagtail/admin/tests/test_edit_handlers.py index 24c382d678..650058b498 100644 --- a/wagtail/admin/tests/test_edit_handlers.py +++ b/wagtail/admin/tests/test_edit_handlers.py @@ -3,7 +3,6 @@ from functools import wraps from typing import Any, List, Mapping, Optional from unittest import mock -from bs4 import BeautifulSoup from django import forms from django.conf import settings from django.contrib.auth import get_user_model @@ -2223,7 +2222,7 @@ class TestTitleFieldPanel(WagtailTestUtils, TestCase): instance=instance, ) html = bound_edit_handler.render_form_content() - return BeautifulSoup(html, "html5lib") + return self.get_soup(html) @clear_edit_handler(Page) def test_default_page_content_panels_uses_title_field(self): diff --git a/wagtail/admin/tests/test_rich_text.py b/wagtail/admin/tests/test_rich_text.py index 356de8e3cb..a590ca0814 100644 --- a/wagtail/admin/tests/test_rich_text.py +++ b/wagtail/admin/tests/test_rich_text.py @@ -1,6 +1,5 @@ import unittest -from bs4 import BeautifulSoup from django.conf import settings from django.test import SimpleTestCase, TestCase from django.test.utils import override_settings @@ -431,11 +430,11 @@ class TestDraftailWithAdditionalFeatures( self.assertNotContains(response, '"type": "ITALIC"') -class TestPageLinkHandler(TestCase): +class TestPageLinkHandler(WagtailTestUtils, TestCase): fixtures = ["test.json"] def test_get_db_attributes(self): - soup = BeautifulSoup('foo', "html5lib") + soup = self.get_soup('foo') tag = soup.a result = PageLinkHandler.get_db_attributes(tag) self.assertEqual(result, {"id": "test-id"}) diff --git a/wagtail/admin/tests/test_userbar.py b/wagtail/admin/tests/test_userbar.py index aa7eed9c29..5c2c731f3f 100644 --- a/wagtail/admin/tests/test_userbar.py +++ b/wagtail/admin/tests/test_userbar.py @@ -1,6 +1,5 @@ import json -from bs4 import BeautifulSoup from django.contrib.auth.models import AnonymousUser, Permission from django.template import Context, Template from django.test import TestCase @@ -193,7 +192,7 @@ class TestAccessibilityCheckerConfig(WagtailTestUtils, TestCase): def get_script(self): template = Template("{% load wagtailuserbar %}{% wagtailuserbar %}") content = template.render(Context({"request": self.request})) - soup = BeautifulSoup(content, "html.parser") + soup = self.get_soup(content) # Should include the configuration as a JSON script with the specific id return soup.find("script", id="accessibility-axe-configuration") diff --git a/wagtail/documents/tests/test_rich_text.py b/wagtail/documents/tests/test_rich_text.py index 86082e322b..1711f85c8e 100644 --- a/wagtail/documents/tests/test_rich_text.py +++ b/wagtail/documents/tests/test_rich_text.py @@ -1,4 +1,3 @@ -from bs4 import BeautifulSoup from django.test import TestCase from wagtail.documents import get_document_model @@ -9,13 +8,14 @@ from wagtail.documents.rich_text.editor_html import ( DocumentLinkHandler as EditorHtmlDocumentLinkHandler, ) from wagtail.fields import RichTextField +from wagtail.test.utils import WagtailTestUtils -class TestEditorHtmlDocumentLinkHandler(TestCase): +class TestEditorHtmlDocumentLinkHandler(WagtailTestUtils, TestCase): fixtures = ["test.json"] def test_get_db_attributes(self): - soup = BeautifulSoup('foo', "html5lib") + soup = self.get_soup('foo') tag = soup.a result = EditorHtmlDocumentLinkHandler.get_db_attributes(tag) self.assertEqual(result, {"id": "test-id"}) diff --git a/wagtail/embeds/tests/test_rich_text.py b/wagtail/embeds/tests/test_rich_text.py index dfe6c4cd2a..876394fedf 100644 --- a/wagtail/embeds/tests/test_rich_text.py +++ b/wagtail/embeds/tests/test_rich_text.py @@ -1,6 +1,5 @@ from unittest.mock import patch -from bs4 import BeautifulSoup from django.test import TestCase, override_settings from wagtail.embeds.exceptions import EmbedNotFoundException @@ -10,11 +9,12 @@ from wagtail.embeds.rich_text.editor_html import ( MediaEmbedHandler as EditorHtmlMediaEmbedHandler, ) from wagtail.rich_text import expand_db_html +from wagtail.test.utils import WagtailTestUtils -class TestEditorHtmlMediaEmbedHandler(TestCase): +class TestEditorHtmlMediaEmbedHandler(WagtailTestUtils, TestCase): def test_get_db_attributes(self): - soup = BeautifulSoup('foo', "html5lib") + soup = self.get_soup('foo') tag = soup.b result = EditorHtmlMediaEmbedHandler.get_db_attributes(tag) self.assertEqual(result, {"url": "test-url"}) diff --git a/wagtail/images/tests/test_rich_text.py b/wagtail/images/tests/test_rich_text.py index 78f9af345a..7a969887ed 100644 --- a/wagtail/images/tests/test_rich_text.py +++ b/wagtail/images/tests/test_rich_text.py @@ -1,4 +1,3 @@ -from bs4 import BeautifulSoup from django.test import TestCase from wagtail.fields import RichTextField @@ -13,9 +12,8 @@ from .utils import Image, get_test_image_file class TestEditorHtmlImageEmbedHandler(WagtailTestUtils, TestCase): def test_get_db_attributes(self): - soup = BeautifulSoup( + soup = self.get_soup( 'foo', - "html5lib", ) tag = soup.b result = EditorHtmlImageEmbedHandler.get_db_attributes(tag) diff --git a/wagtail/test/utils/form_data.py b/wagtail/test/utils/form_data.py index 5582c76086..31819fc5e9 100644 --- a/wagtail/test/utils/form_data.py +++ b/wagtail/test/utils/form_data.py @@ -10,6 +10,8 @@ from django.http import QueryDict from wagtail.admin.rich_text import get_rich_text_editor_widget +from .wagtail_tests import WagtailTestUtils + def _nested_form_data(data): if isinstance(data, dict): @@ -186,7 +188,7 @@ def _querydict_from_form(form: bs4.Tag, exclude_csrf: bool = True) -> QueryDict: def querydict_from_html( html: str, form_id: str = None, form_index: int = 0, exclude_csrf: bool = True ) -> QueryDict: - soup = bs4.BeautifulSoup(html, "html5lib") + soup = WagtailTestUtils.get_soup(html) if form_id is not None: form = soup.find("form", attrs={"id": form_id}) if form is None: diff --git a/wagtail/tests/test_whitelist.py b/wagtail/tests/test_whitelist.py index 3c29a076af..ddd2d4fd04 100644 --- a/wagtail/tests/test_whitelist.py +++ b/wagtail/tests/test_whitelist.py @@ -1,6 +1,6 @@ -from bs4 import BeautifulSoup from django.test import TestCase +from wagtail.test.utils import WagtailTestUtils from wagtail.whitelist import ( Whitelister, allow_without_attributes, @@ -26,9 +26,9 @@ class TestCheckUrl(TestCase): self.assertFalse(bool(check_url("jav\tascript:alert('XSS')"))) -class TestAttributeRule(TestCase): +class TestAttributeRule(WagtailTestUtils, TestCase): def setUp(self): - self.soup = BeautifulSoup('baz', "html5lib") + self.soup = self.get_soup('baz', "html5lib") def test_no_rule_for_attr(self): """ @@ -87,7 +87,7 @@ class TestAttributeRule(TestCase): Test that attribute_rule() with will drop all attributes. """ - soup = BeautifulSoup( + soup = self.get_soup( '', "html5lib" ) tag = soup.b @@ -95,7 +95,7 @@ class TestAttributeRule(TestCase): self.assertEqual(str(tag), "") -class TestWhitelister(TestCase): +class TestWhitelister(WagtailTestUtils, TestCase): def setUp(self): self.whitelister = Whitelister() @@ -103,7 +103,7 @@ class TestWhitelister(TestCase): """ Unknown node should remove a node from the parent document """ - soup = BeautifulSoup("bazquux", "html5lib") + soup = self.get_soup("bazquux", "html5lib") tag = soup.foo self.whitelister.clean_unknown_node("", soup.bar) self.assertEqual(str(tag), "quux") @@ -113,7 +113,7 @@ class TestWhitelister(TestCase): tags are allowed without attributes. This remains true when tags are nested. """ - soup = BeautifulSoup('foo', "html5lib") + soup = self.get_soup('foo', "html5lib") tag = soup.b self.whitelister.clean_tag_node(tag, tag) self.assertEqual(str(tag), "foo") @@ -122,19 +122,19 @@ class TestWhitelister(TestCase): """ tags should be removed, even when nested. """ - soup = BeautifulSoup("bar", "html5lib") + soup = self.get_soup("bar", "html5lib") tag = soup.b self.whitelister.clean_tag_node(tag, tag) self.assertEqual(str(tag), "bar") def test_clean_string_node_does_nothing(self): - soup = BeautifulSoup("bar", "html5lib") + soup = self.get_soup("bar", "html5lib") string = soup.b.string self.whitelister.clean_string_node(string, string) self.assertEqual(str(string), "bar") def test_clean_node_does_not_change_navigable_strings(self): - soup = BeautifulSoup("bar", "html5lib") + soup = self.get_soup("bar", "html5lib") string = soup.b.string self.whitelister.clean_node(string, string) self.assertEqual(str(string), "bar")