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
84from django .test .utils import override_settings
95from html5lib .constants import E
106from html5lib .html5parser import HTMLParser
2218MIDDLEWARE_CSP_LAST = settings .MIDDLEWARE + ["csp.middleware.CSPMiddleware" ]
2319
2420
25- def get_namespaces (element : Element ) -> dict [ str , str ] :
21+ def get_namespaces (element ) :
2622 """
2723 Return the default `xmlns`. See
2824 https://docs.python.org/3/library/xml.etree.elementtree.html#parsing-xml-with-namespaces
@@ -40,9 +36,7 @@ def setUp(self):
4036 super ().setUp ()
4137 self .parser = HTMLParser ()
4238
43- def _fail_if_missing (
44- self , root : Element , path : str , namespaces : dict [str , str ], nonce : str
45- ):
39+ def _fail_if_missing (self , root , path , namespaces , nonce ):
4640 """
4741 Search elements, fail if a `nonce` attribute is missing on them.
4842 """
@@ -51,7 +45,7 @@ def _fail_if_missing(
5145 if item .attrib .get ("nonce" ) != nonce :
5246 raise self .failureException (f"{ item } has no nonce attribute." )
5347
54- def _fail_if_found (self , root : Element , path : str , namespaces : dict [ str , str ] ):
48+ def _fail_if_found (self , root , path , namespaces ):
5549 """
5650 Search elements, fail if a `nonce` attribute is found on them.
5751 """
@@ -60,7 +54,7 @@ def _fail_if_found(self, root: Element, path: str, namespaces: dict[str, str]):
6054 if "nonce" in item .attrib :
6155 raise self .failureException (f"{ item } has a nonce attribute." )
6256
63- def _fail_on_invalid_html (self , content : bytes , parser : HTMLParser ):
57+ def _fail_on_invalid_html (self , content , parser ):
6458 """Fail if the passed HTML is invalid."""
6559 if parser .errors :
6660 default_msg = ["Content is invalid HTML:" ]
@@ -75,10 +69,10 @@ def test_exists(self):
7569 """A `nonce` should exist when using the `CSPMiddleware`."""
7670 for middleware in [MIDDLEWARE_CSP_BEFORE , MIDDLEWARE_CSP_LAST ]:
7771 with self .settings (MIDDLEWARE = middleware ):
78- response = cast ( HttpResponse , self .client .get (path = "/csp_view/" ) )
72+ response = self .client .get (path = "/csp_view/" )
7973 self .assertEqual (response .status_code , 200 )
8074
81- html_root : Element = self .parser .parse (stream = response .content )
75+ html_root = self .parser .parse (stream = response .content )
8276 self ._fail_on_invalid_html (content = response .content , parser = self .parser )
8377 self .assertContains (response , "djDebug" )
8478
@@ -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,16 @@ 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- nonce = response .context ["request" ].csp_nonce
124+ context = response .context
125+ nonce = str (context ["toolbar" ].csp_nonce )
131126 self ._fail_if_missing (
132127 root = html_root , path = ".//link" , namespaces = namespaces , nonce = nonce
133128 )
@@ -139,15 +134,15 @@ def test_panel_content_nonce_exists(self):
139134 store = get_store ()
140135 for middleware in [MIDDLEWARE_CSP_BEFORE , MIDDLEWARE_CSP_LAST ]:
141136 with self .settings (MIDDLEWARE = middleware ):
142- response = cast ( HttpResponse , self .client .get (path = "/csp_view/" ) )
137+ response = self .client .get (path = "/csp_view/" )
143138 self .assertEqual (response .status_code , 200 )
144139
145140 request_ids = list (store .request_ids ())
146141 toolbar = DebugToolbar .fetch (request_ids [- 1 ])
147142 panels_to_check = ["HistoryPanel" , "TimerPanel" ]
148143 for panel in panels_to_check :
149144 content = toolbar .get_panel_by_id (panel ).content
150- html_root : Element = self .parser .parse (stream = content )
145+ html_root = self .parser .parse (stream = content )
151146 namespaces = get_namespaces (element = html_root )
152147 nonce = str (toolbar .csp_nonce )
153148 self ._fail_if_missing (
@@ -165,10 +160,10 @@ def test_panel_content_nonce_exists(self):
165160
166161 def test_missing (self ):
167162 """A `nonce` should not exist when not using the `CSPMiddleware`."""
168- response = cast ( HttpResponse , self .client .get (path = "/regular/basic/" ) )
163+ response = self .client .get (path = "/regular/basic/" )
169164 self .assertEqual (response .status_code , 200 )
170165
171- html_root : Element = self .parser .parse (stream = response .content )
166+ html_root = self .parser .parse (stream = response .content )
172167 self ._fail_on_invalid_html (content = response .content , parser = self .parser )
173168 self .assertContains (response , "djDebug" )
174169
0 commit comments