11from __future__ import annotations
22
3- from typing import cast
4- from xml .etree .ElementTree import Element
5-
63from django .conf import settings
7- from django .http .response import HttpResponse
8- from django .test .utils import ContextList , override_settings
4+ from django .test .utils import override_settings
95from html5lib .constants import E
106from html5lib .html5parser import HTMLParser
117
2117MIDDLEWARE_CSP_LAST = settings .MIDDLEWARE + ["csp.middleware.CSPMiddleware" ]
2218
2319
24- def get_namespaces (element : Element ) -> dict [ str , str ] :
20+ def get_namespaces (element ) :
2521 """
2622 Return the default `xmlns`. See
2723 https://docs.python.org/3/library/xml.etree.elementtree.html#parsing-xml-with-namespaces
@@ -39,9 +35,7 @@ def setUp(self):
3935 super ().setUp ()
4036 self .parser = HTMLParser ()
4137
42- def _fail_if_missing (
43- self , root : Element , path : str , namespaces : dict [str , str ], nonce : str
44- ):
38+ def _fail_if_missing (self , root , path , namespaces , nonce ):
4539 """
4640 Search elements, fail if a `nonce` attribute is missing on them.
4741 """
@@ -50,7 +44,7 @@ def _fail_if_missing(
5044 if item .attrib .get ("nonce" ) != nonce :
5145 raise self .failureException (f"{ item } has no nonce attribute." )
5246
53- def _fail_if_found (self , root : Element , path : str , namespaces : dict [ str , str ] ):
47+ def _fail_if_found (self , root , path , namespaces ):
5448 """
5549 Search elements, fail if a `nonce` attribute is found on them.
5650 """
@@ -59,7 +53,7 @@ def _fail_if_found(self, root: Element, path: str, namespaces: dict[str, str]):
5953 if "nonce" in item .attrib :
6054 raise self .failureException (f"{ item } has a nonce attribute." )
6155
62- def _fail_on_invalid_html (self , content : bytes , parser : HTMLParser ):
56+ def _fail_on_invalid_html (self , content , parser ):
6357 """Fail if the passed HTML is invalid."""
6458 if parser .errors :
6559 default_msg = ["Content is invalid HTML:" ]
@@ -74,10 +68,10 @@ def test_exists(self):
7468 """A `nonce` should exist when using the `CSPMiddleware`."""
7569 for middleware in [MIDDLEWARE_CSP_BEFORE , MIDDLEWARE_CSP_LAST ]:
7670 with self .settings (MIDDLEWARE = middleware ):
77- response = cast ( HttpResponse , self .client .get (path = "/csp_view/" ) )
71+ response = self .client .get (path = "/csp_view/" )
7872 self .assertEqual (response .status_code , 200 )
7973
80- html_root : Element = self .parser .parse (stream = response .content )
74+ html_root = self .parser .parse (stream = response .content )
8175 self ._fail_on_invalid_html (content = response .content , parser = self .parser )
8276 self .assertContains (response , "djDebug" )
8377
@@ -98,10 +92,10 @@ def test_does_not_exist_nonce_wasnt_used(self):
9892 """
9993 for middleware in [MIDDLEWARE_CSP_BEFORE , MIDDLEWARE_CSP_LAST ]:
10094 with self .settings (MIDDLEWARE = middleware ):
101- response = cast ( HttpResponse , self .client .get (path = "/regular/basic/" ) )
95+ response = self .client .get (path = "/regular/basic/" )
10296 self .assertEqual (response .status_code , 200 )
10397
104- html_root : Element = self .parser .parse (stream = response .content )
98+ html_root = self .parser .parse (stream = response .content )
10599 self ._fail_on_invalid_html (content = response .content , parser = self .parser )
106100 self .assertContains (response , "djDebug" )
107101
@@ -119,15 +113,15 @@ def test_does_not_exist_nonce_wasnt_used(self):
119113 def test_redirects_exists (self ):
120114 for middleware in [MIDDLEWARE_CSP_BEFORE , MIDDLEWARE_CSP_LAST ]:
121115 with self .settings (MIDDLEWARE = middleware ):
122- response = cast ( HttpResponse , self .client .get (path = "/csp_view/" ) )
116+ response = self .client .get (path = "/csp_view/" )
123117 self .assertEqual (response .status_code , 200 )
124118
125- html_root : Element = self .parser .parse (stream = response .content )
119+ html_root = self .parser .parse (stream = response .content )
126120 self ._fail_on_invalid_html (content = response .content , parser = self .parser )
127121 self .assertContains (response , "djDebug" )
128122
129123 namespaces = get_namespaces (element = html_root )
130- context : ContextList = response .context # pyright: ignore[reportAttributeAccessIssue]
124+ context = response .context
131125 nonce = str (context ["toolbar" ].csp_nonce )
132126 self ._fail_if_missing (
133127 root = html_root , path = ".//link" , namespaces = namespaces , nonce = nonce
@@ -139,14 +133,14 @@ def test_redirects_exists(self):
139133 def test_panel_content_nonce_exists (self ):
140134 for middleware in [MIDDLEWARE_CSP_BEFORE , MIDDLEWARE_CSP_LAST ]:
141135 with self .settings (MIDDLEWARE = middleware ):
142- response = cast ( HttpResponse , self .client .get (path = "/csp_view/" ) )
136+ response = self .client .get (path = "/csp_view/" )
143137 self .assertEqual (response .status_code , 200 )
144138
145139 toolbar = list (DebugToolbar ._store .values ())[- 1 ]
146140 panels_to_check = ["HistoryPanel" , "TimerPanel" ]
147141 for panel in panels_to_check :
148142 content = toolbar .get_panel_by_id (panel ).content
149- html_root : Element = self .parser .parse (stream = content )
143+ html_root = self .parser .parse (stream = content )
150144 namespaces = get_namespaces (element = html_root )
151145 nonce = str (toolbar .csp_nonce )
152146 self ._fail_if_missing (
@@ -164,10 +158,10 @@ def test_panel_content_nonce_exists(self):
164158
165159 def test_missing (self ):
166160 """A `nonce` should not exist when not using the `CSPMiddleware`."""
167- response = cast ( HttpResponse , self .client .get (path = "/regular/basic/" ) )
161+ response = self .client .get (path = "/regular/basic/" )
168162 self .assertEqual (response .status_code , 200 )
169163
170- html_root : Element = self .parser .parse (stream = response .content )
164+ html_root = self .parser .parse (stream = response .content )
171165 self ._fail_on_invalid_html (content = response .content , parser = self .parser )
172166 self .assertContains (response , "djDebug" )
173167
0 commit comments