diff --git a/tests/test_csp_rendering.py b/tests/test_csp_rendering.py index 144e65ba0..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 @@ -21,7 +17,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 +35,7 @@ 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 +44,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 +53,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 +68,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 +92,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 +113,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 +133,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 +158,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")