diff --git a/wagtail/admin/tests/test_privacy.py b/wagtail/admin/tests/test_privacy.py index ac697e19a712..ccce52141790 100644 --- a/wagtail/admin/tests/test_privacy.py +++ b/wagtail/admin/tests/test_privacy.py @@ -364,13 +364,15 @@ def test_explorer_private(self): # Check the response self.assertEqual(response.status_code, 200) + soup = WagtailTestUtils.get_soup(response) + # Check the private privacy indicator is visible - private_indicator = response.select_one("[data-privacy-sidebar-private]") + private_indicator = soup.select_one("[data-privacy-sidebar-private]") # There should not be any classes applied self.assertEqual(private_indicator["class"], []) # Privacy indicator should be hidden - public_indicator = response.select_one("[data-privacy-sidebar-public].w-hidden") + public_indicator = soup.select_one("[data-privacy-sidebar-public].w-hidden") self.assertIsNotNone(public_indicator) def test_explorer_private_child(self): diff --git a/wagtail/test/utils/wagtail_tests.py b/wagtail/test/utils/wagtail_tests.py index fe5eed8e4f06..d1bbe8fca59f 100644 --- a/wagtail/test/utils/wagtail_tests.py +++ b/wagtail/test/utils/wagtail_tests.py @@ -1,76 +1,17 @@ import warnings from contextlib import contextmanager -from bs4 import BeautifulSoup, ResultSet, Tag +from bs4 import BeautifulSoup from django import VERSION as DJANGO_VERSION from django.contrib.auth import get_user_model -from django.http import HttpResponse -from django.test.client import Client as DjangoTestClient +from django.http import HttpRequest from django.test.testcases import assert_and_parse_html -from django.utils.functional import SimpleLazyObject - - -class SoupHttpResponse(HttpResponse): - """Subclass for type-hinting purposes. - - Use it like this for type-hinting: - response: SoupHttpResponse = self.client.get("/your-url/") - - # Wonderful type hinting! - response.soup.select(...) - """ - - soup: BeautifulSoup - - def select(self, selector: str) -> ResultSet[Tag]: - pass - - def select_one(self, selector: str) -> (Tag | None): - pass - - -class BS4SoupClient(DjangoTestClient): - """Convenience wrapper around Django's Client and BeautifulSoup's SoupSieve - - The intend of this wrapper is to make it easier to check the html of a response. - - Usage example: - - client = BS4SoupClient() - response = client.get("/good-vibes/") - - # Select a HTML SoupHttpResponse: - response = super().request(**request) - # Make the soup available lazily. We will only incur the cost of instantiating - # the soup upon the initial access of this attribute. - response.soup = SimpleLazyObject(lambda: self._get_soup(response)) - - # Alias these BeautifulSoup functions the the response for convenience - response.select = lambda selector: response.soup.select(selector) - response.select_one = lambda selector: response.soup.select_one(selector) - return response class WagtailTestUtils: - client_class = BS4SoupClient - - # To make IDE type hinting recognize the appropriate type - client: BS4SoupClient + @staticmethod + def get_soup(request: HttpRequest) -> BeautifulSoup: + return BeautifulSoup(request.content, "html.parser") @staticmethod def create_test_user():