Skip to content

Commit

Permalink
Add WagtailTestUtils.get_soup() method to get a BeautifulSoup object
Browse files Browse the repository at this point in the history
  • Loading branch information
Stormheg authored and laymonage committed Aug 10, 2023
1 parent f0cfa62 commit 1c12d96
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Changelog
* Maintenance: Remove unused WorkflowStatus view, urlpattern, and workflow-status.js (Storm Heg)
* Maintenance: Add support for options/attrs in Telepath widgets so that attrs render on the created DOM (Storm Heg)
* Maintenance: Update pre-commit hooks to be in sync with latest changes to Eslint & Prettier for client-side changes (Storm Heg)
* Maintenance: Add `WagtailTestUtils.get_soup()` method to get a `BeautifulSoup` object from an `HttpResponse` object (Storm Heg)


5.1.1 (xx.xx.xxxx) - IN DEVELOPMENT
Expand Down
1 change: 1 addition & 0 deletions docs/releases/5.2.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ depth: 1
* Remove unused WorkflowStatus view, urlpattern, and workflow-status.js (Storm Heg)
* Add support for options/attrs in Telepath widgets so that attrs render on the created DOM (Storm Heg)
* Update pre-commit hooks to be in sync with latest changes to Eslint & Prettier for client-side changes (Storm Heg)
* Add `WagtailTestUtils.get_soup()` method to get a `BeautifulSoup` object from an `HttpResponse` object (Storm Heg)


## Upgrade considerations - changes affecting all projects
Expand Down
15 changes: 10 additions & 5 deletions wagtail/admin/tests/test_privacy.py
Original file line number Diff line number Diff line change
Expand Up @@ -364,11 +364,16 @@ def test_explorer_private(self):
# Check the response
self.assertEqual(response.status_code, 200)

# Check the privacy indicator is private
self.assertContains(response, '<div class="" data-privacy-sidebar-private>')
self.assertContains(
response, '<div class="w-hidden" data-privacy-sidebar-public>'
)
soup = self.get_soup(response)

# Check the private privacy indicator is visible
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 = soup.select_one("[data-privacy-sidebar-public].w-hidden")
self.assertIsNotNone(public_indicator)

def test_explorer_private_child(self):
"""
Expand Down
6 changes: 6 additions & 0 deletions wagtail/test/utils/wagtail_tests.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
import warnings
from contextlib import contextmanager

from bs4 import BeautifulSoup
from django import VERSION as DJANGO_VERSION
from django.contrib.auth import get_user_model
from django.http import HttpRequest
from django.test.testcases import assert_and_parse_html


class WagtailTestUtils:
@staticmethod
def get_soup(request: HttpRequest, parser="html.parser") -> BeautifulSoup:
return BeautifulSoup(request.content, parser)

@staticmethod
def create_test_user():
"""
Expand Down

0 comments on commit 1c12d96

Please sign in to comment.