From 07b81eb48317bd1fd1868feac191c2d9a2d7b721 Mon Sep 17 00:00:00 2001 From: andoriyaprashant Date: Sat, 14 Jun 2025 11:29:26 +0530 Subject: [PATCH 1/2] Remove Type Hints from CspRenderingTestCase --- tests/test_csp_rendering.py | 33 ++++++++++++++++----------------- 1 file changed, 16 insertions(+), 17 deletions(-) diff --git a/tests/test_csp_rendering.py b/tests/test_csp_rendering.py index 144e65ba0..cea5b66dc 100644 --- a/tests/test_csp_rendering.py +++ b/tests/test_csp_rendering.py @@ -21,7 +21,7 @@ MIDDLEWARE_CSP_LAST = settings.MIDDLEWARE + ["csp.middleware.CSPMiddleware"] -def get_namespaces(element: Element) -> dict[str, str]: +def get_namespaces(element): """ Return the default `xmlns`. See https://docs.python.org/3/library/xml.etree.elementtree.html#parsing-xml-with-namespaces @@ -39,9 +39,8 @@ def setUp(self): super().setUp() self.parser = HTMLParser() - def _fail_if_missing( - self, root: Element, path: str, namespaces: dict[str, str], nonce: str - ): + def _fail_if_missing(self, root, path, namespaces, nonce): + """ Search elements, fail if a `nonce` attribute is missing on them. """ @@ -50,7 +49,7 @@ def _fail_if_missing( if item.attrib.get("nonce") != nonce: raise self.failureException(f"{item} has no nonce attribute.") - def _fail_if_found(self, root: Element, path: str, namespaces: dict[str, str]): + def _fail_if_found(self, root, path, namespaces): """ Search elements, fail if a `nonce` attribute is found on them. """ @@ -59,7 +58,7 @@ def _fail_if_found(self, root: Element, path: str, namespaces: dict[str, str]): if "nonce" in item.attrib: raise self.failureException(f"{item} has a nonce attribute.") - def _fail_on_invalid_html(self, content: bytes, parser: HTMLParser): + def _fail_on_invalid_html(self, content, parser): """Fail if the passed HTML is invalid.""" if parser.errors: default_msg = ["Content is invalid HTML:"] @@ -74,10 +73,10 @@ def test_exists(self): """A `nonce` should exist when using the `CSPMiddleware`.""" for middleware in [MIDDLEWARE_CSP_BEFORE, MIDDLEWARE_CSP_LAST]: with self.settings(MIDDLEWARE=middleware): - response = cast(HttpResponse, self.client.get(path="/csp_view/")) + response = self.client.get(path="/csp_view/") self.assertEqual(response.status_code, 200) - html_root: Element = self.parser.parse(stream=response.content) + html_root = self.parser.parse(stream=response.content) self._fail_on_invalid_html(content=response.content, parser=self.parser) self.assertContains(response, "djDebug") @@ -98,10 +97,10 @@ def test_does_not_exist_nonce_wasnt_used(self): """ for middleware in [MIDDLEWARE_CSP_BEFORE, MIDDLEWARE_CSP_LAST]: with self.settings(MIDDLEWARE=middleware): - response = cast(HttpResponse, self.client.get(path="/regular/basic/")) + response = self.client.get(path="/regular/basic/") self.assertEqual(response.status_code, 200) - html_root: Element = self.parser.parse(stream=response.content) + html_root = self.parser.parse(stream=response.content) self._fail_on_invalid_html(content=response.content, parser=self.parser) self.assertContains(response, "djDebug") @@ -119,15 +118,15 @@ def test_does_not_exist_nonce_wasnt_used(self): def test_redirects_exists(self): for middleware in [MIDDLEWARE_CSP_BEFORE, MIDDLEWARE_CSP_LAST]: with self.settings(MIDDLEWARE=middleware): - response = cast(HttpResponse, self.client.get(path="/csp_view/")) + response = self.client.get(path="/csp_view/") self.assertEqual(response.status_code, 200) - html_root: Element = self.parser.parse(stream=response.content) + html_root = self.parser.parse(stream=response.content) self._fail_on_invalid_html(content=response.content, parser=self.parser) self.assertContains(response, "djDebug") namespaces = get_namespaces(element=html_root) - context: ContextList = response.context # pyright: ignore[reportAttributeAccessIssue] + context = response.context nonce = str(context["toolbar"].csp_nonce) self._fail_if_missing( root=html_root, path=".//link", namespaces=namespaces, nonce=nonce @@ -139,14 +138,14 @@ def test_redirects_exists(self): def test_panel_content_nonce_exists(self): for middleware in [MIDDLEWARE_CSP_BEFORE, MIDDLEWARE_CSP_LAST]: with self.settings(MIDDLEWARE=middleware): - response = cast(HttpResponse, self.client.get(path="/csp_view/")) + response = self.client.get(path="/csp_view/") self.assertEqual(response.status_code, 200) toolbar = list(DebugToolbar._store.values())[-1] panels_to_check = ["HistoryPanel", "TimerPanel"] for panel in panels_to_check: content = toolbar.get_panel_by_id(panel).content - html_root: Element = self.parser.parse(stream=content) + html_root = self.parser.parse(stream=content) namespaces = get_namespaces(element=html_root) nonce = str(toolbar.csp_nonce) self._fail_if_missing( @@ -164,10 +163,10 @@ def test_panel_content_nonce_exists(self): def test_missing(self): """A `nonce` should not exist when not using the `CSPMiddleware`.""" - response = cast(HttpResponse, self.client.get(path="/regular/basic/")) + response = self.client.get(path="/regular/basic/") self.assertEqual(response.status_code, 200) - html_root: Element = self.parser.parse(stream=response.content) + html_root = self.parser.parse(stream=response.content) self._fail_on_invalid_html(content=response.content, parser=self.parser) self.assertContains(response, "djDebug") From 9e500015001d68110457ba518f1998462621806a Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 14 Jun 2025 06:01:47 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- tests/test_csp_rendering.py | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/tests/test_csp_rendering.py b/tests/test_csp_rendering.py index cea5b66dc..b17f05914 100644 --- a/tests/test_csp_rendering.py +++ b/tests/test_csp_rendering.py @@ -1,11 +1,7 @@ from __future__ import annotations -from typing import cast -from xml.etree.ElementTree import Element - from django.conf import settings -from django.http.response import HttpResponse -from django.test.utils import ContextList, override_settings +from django.test.utils import override_settings from html5lib.constants import E from html5lib.html5parser import HTMLParser @@ -40,7 +36,6 @@ def setUp(self): self.parser = HTMLParser() def _fail_if_missing(self, root, path, namespaces, nonce): - """ Search elements, fail if a `nonce` attribute is missing on them. """