From bc2bae8e07759b8b0d2bcce2a73f749c80be7cc5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A1szl=C3=B3=20K=C3=A1rolyi?= Date: Fri, 2 Aug 2024 15:27:48 +0200 Subject: [PATCH] Fix typing errors --- tests/test_csp_rendering.py | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/tests/test_csp_rendering.py b/tests/test_csp_rendering.py index b54103730..b012944c6 100644 --- a/tests/test_csp_rendering.py +++ b/tests/test_csp_rendering.py @@ -2,6 +2,7 @@ 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 html5lib.constants import E from html5lib.html5parser import HTMLParser @@ -47,7 +48,7 @@ def _fail_if_found(self, root: Element, path: str, namespaces: Dict[str, str]): elements = root.findall(path=path, namespaces=namespaces) for item in elements: if "nonce" in item.attrib: - raise self.failureException(f"{item} has no nonce attribute.") + raise self.failureException(f"{item} has a nonce attribute.") def _fail_on_invalid_html(self, content: bytes, parser: HTMLParser): """Fail if the passed HTML is invalid.""" @@ -66,6 +67,8 @@ def _fail_on_invalid_html(self, content: bytes, parser: HTMLParser): def test_exists(self): """A `nonce` should exist when using the `CSPMiddleware`.""" response = self.client.get(path="/regular/basic/") + if not isinstance(response, HttpResponse): + raise self.failureException(f'{response!r} is not a HttpResponse') self.assertEqual(response.status_code, 200) html_root: Element = self.parser.parse(stream=response.content) @@ -88,6 +91,8 @@ def test_exists(self): ) def test_redirects_exists(self): response = self.client.get("/redirect/") + if not isinstance(response, HttpResponse): + raise self.failureException(f'{response!r} is not a HttpResponse') self.assertEqual(response.status_code, 200) html_root: Element = self.parser.parse(stream=response.content) @@ -95,7 +100,8 @@ def test_redirects_exists(self): self.assertContains(response, "djDebug") namespaces = get_namespaces(element=html_root) - context: ContextList = response.context + context: ContextList = \ + response.context # pyright: ignore[reportAttributeAccessIssue] nonce = str(context["toolbar"].request.csp_nonce) self._fail_if_missing( root=html_root, path=".//link", namespaces=namespaces, nonce=nonce @@ -109,6 +115,8 @@ def test_redirects_exists(self): ) def test_panel_content_nonce_exists(self): response = self.client.get("/regular/basic/") + if not isinstance(response, HttpResponse): + raise self.failureException(f'{response!r} is not a HttpResponse') self.assertEqual(response.status_code, 200) toolbar = list(DebugToolbar._store.values())[0] @@ -128,6 +136,8 @@ def test_panel_content_nonce_exists(self): def test_missing(self): """A `nonce` should not exist when not using the `CSPMiddleware`.""" response = self.client.get(path="/regular/basic/") + if not isinstance(response, HttpResponse): + raise self.failureException(f'{response!r} is not a HttpResponse') self.assertEqual(response.status_code, 200) html_root: Element = self.parser.parse(stream=response.content)