0
0
mirror of https://github.com/wagtail/wagtail.git synced 2024-11-30 01:46:24 +01:00

Use WagtailTestUtils.get_soup() more widely

This commit is contained in:
Sage Abdullah 2023-08-21 10:32:41 +01:00
parent ad070af8fd
commit febacf7958
No known key found for this signature in database
GPG Key ID: EB1A33CC51CC0217
10 changed files with 33 additions and 37 deletions

View File

@ -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

View File

@ -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("<div>foo</div>", "html5lib")
soup = self.get_soup("<div>foo</div>", "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(
'<p><a data-embedtype="image" data-id=1 data-format="left" data-alt="bar" irrelevant="baz">foo</a></p>',
"html5lib",
)
@ -27,7 +27,7 @@ class TestDbWhitelisterMethods(TestCase):
)
def test_clean_tag_node_with_data_linktype(self):
soup = BeautifulSoup(
soup = self.get_soup(
'<a data-linktype="document" data-id="1" irrelevant="baz">foo</a>',
"html5lib",
)
@ -36,13 +36,13 @@ class TestDbWhitelisterMethods(TestCase):
self.assertEqual(str(tag), '<a id="1" linktype="document">foo</a>')
def test_clean_tag_node(self):
soup = BeautifulSoup('<a irrelevant="baz">foo</a>', "html5lib")
soup = self.get_soup('<a irrelevant="baz">foo</a>', "html5lib")
tag = soup.a
self.whitelister.clean_tag_node(soup, tag)
self.assertEqual(str(tag), "<a>foo</a>")
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):

View File

@ -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):

View File

@ -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('<a data-id="test-id">foo</a>', "html5lib")
soup = self.get_soup('<a data-id="test-id">foo</a>')
tag = soup.a
result = PageLinkHandler.get_db_attributes(tag)
self.assertEqual(result, {"id": "test-id"})

View File

@ -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")

View File

@ -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('<a data-id="test-id">foo</a>', "html5lib")
soup = self.get_soup('<a data-id="test-id">foo</a>')
tag = soup.a
result = EditorHtmlDocumentLinkHandler.get_db_attributes(tag)
self.assertEqual(result, {"id": "test-id"})

View File

@ -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('<b data-url="test-url">foo</b>', "html5lib")
soup = self.get_soup('<b data-url="test-url">foo</b>')
tag = soup.b
result = EditorHtmlMediaEmbedHandler.get_db_attributes(tag)
self.assertEqual(result, {"url": "test-url"})

View File

@ -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(
'<b data-id="test-id" data-format="test-format" data-alt="test-alt">foo</b>',
"html5lib",
)
tag = soup.b
result = EditorHtmlImageEmbedHandler.get_db_attributes(tag)

View File

@ -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:

View File

@ -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('<b foo="bar">baz</b>', "html5lib")
self.soup = self.get_soup('<b foo="bar">baz</b>', "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(
'<b foo="bar" baz="quux" snowman="barbecue"></b>', "html5lib"
)
tag = soup.b
@ -95,7 +95,7 @@ class TestAttributeRule(TestCase):
self.assertEqual(str(tag), "<b></b>")
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("<foo><bar>baz</bar>quux</foo>", "html5lib")
soup = self.get_soup("<foo><bar>baz</bar>quux</foo>", "html5lib")
tag = soup.foo
self.whitelister.clean_unknown_node("", soup.bar)
self.assertEqual(str(tag), "<foo>quux</foo>")
@ -113,7 +113,7 @@ class TestWhitelister(TestCase):
<b> tags are allowed without attributes. This remains true
when tags are nested.
"""
soup = BeautifulSoup('<b><b class="delete me">foo</b></b>', "html5lib")
soup = self.get_soup('<b><b class="delete me">foo</b></b>', "html5lib")
tag = soup.b
self.whitelister.clean_tag_node(tag, tag)
self.assertEqual(str(tag), "<b><b>foo</b></b>")
@ -122,19 +122,19 @@ class TestWhitelister(TestCase):
"""
<foo> tags should be removed, even when nested.
"""
soup = BeautifulSoup("<b><foo>bar</foo></b>", "html5lib")
soup = self.get_soup("<b><foo>bar</foo></b>", "html5lib")
tag = soup.b
self.whitelister.clean_tag_node(tag, tag)
self.assertEqual(str(tag), "<b>bar</b>")
def test_clean_string_node_does_nothing(self):
soup = BeautifulSoup("<b>bar</b>", "html5lib")
soup = self.get_soup("<b>bar</b>", "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("<b>bar</b>", "html5lib")
soup = self.get_soup("<b>bar</b>", "html5lib")
string = soup.b.string
self.whitelister.clean_node(string, string)
self.assertEqual(str(string), "bar")