1
1
from __future__ import annotations
2
2
3
- from typing import cast
4
- from xml .etree .ElementTree import Element
5
-
6
3
from 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
9
5
from html5lib .constants import E
10
6
from html5lib .html5parser import HTMLParser
11
7
21
17
MIDDLEWARE_CSP_LAST = settings .MIDDLEWARE + ["csp.middleware.CSPMiddleware" ]
22
18
23
19
24
- def get_namespaces (element : Element ) -> dict [ str , str ] :
20
+ def get_namespaces (element ) :
25
21
"""
26
22
Return the default `xmlns`. See
27
23
https://docs.python.org/3/library/xml.etree.elementtree.html#parsing-xml-with-namespaces
@@ -39,9 +35,7 @@ def setUp(self):
39
35
super ().setUp ()
40
36
self .parser = HTMLParser ()
41
37
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 ):
45
39
"""
46
40
Search elements, fail if a `nonce` attribute is missing on them.
47
41
"""
@@ -50,7 +44,7 @@ def _fail_if_missing(
50
44
if item .attrib .get ("nonce" ) != nonce :
51
45
raise self .failureException (f"{ item } has no nonce attribute." )
52
46
53
- def _fail_if_found (self , root : Element , path : str , namespaces : dict [ str , str ] ):
47
+ def _fail_if_found (self , root , path , namespaces ):
54
48
"""
55
49
Search elements, fail if a `nonce` attribute is found on them.
56
50
"""
@@ -59,7 +53,7 @@ def _fail_if_found(self, root: Element, path: str, namespaces: dict[str, str]):
59
53
if "nonce" in item .attrib :
60
54
raise self .failureException (f"{ item } has a nonce attribute." )
61
55
62
- def _fail_on_invalid_html (self , content : bytes , parser : HTMLParser ):
56
+ def _fail_on_invalid_html (self , content , parser ):
63
57
"""Fail if the passed HTML is invalid."""
64
58
if parser .errors :
65
59
default_msg = ["Content is invalid HTML:" ]
@@ -74,10 +68,10 @@ def test_exists(self):
74
68
"""A `nonce` should exist when using the `CSPMiddleware`."""
75
69
for middleware in [MIDDLEWARE_CSP_BEFORE , MIDDLEWARE_CSP_LAST ]:
76
70
with self .settings (MIDDLEWARE = middleware ):
77
- response = cast ( HttpResponse , self .client .get (path = "/csp_view/" ) )
71
+ response = self .client .get (path = "/csp_view/" )
78
72
self .assertEqual (response .status_code , 200 )
79
73
80
- html_root : Element = self .parser .parse (stream = response .content )
74
+ html_root = self .parser .parse (stream = response .content )
81
75
self ._fail_on_invalid_html (content = response .content , parser = self .parser )
82
76
self .assertContains (response , "djDebug" )
83
77
@@ -98,10 +92,10 @@ def test_does_not_exist_nonce_wasnt_used(self):
98
92
"""
99
93
for middleware in [MIDDLEWARE_CSP_BEFORE , MIDDLEWARE_CSP_LAST ]:
100
94
with self .settings (MIDDLEWARE = middleware ):
101
- response = cast ( HttpResponse , self .client .get (path = "/regular/basic/" ) )
95
+ response = self .client .get (path = "/regular/basic/" )
102
96
self .assertEqual (response .status_code , 200 )
103
97
104
- html_root : Element = self .parser .parse (stream = response .content )
98
+ html_root = self .parser .parse (stream = response .content )
105
99
self ._fail_on_invalid_html (content = response .content , parser = self .parser )
106
100
self .assertContains (response , "djDebug" )
107
101
@@ -119,15 +113,15 @@ def test_does_not_exist_nonce_wasnt_used(self):
119
113
def test_redirects_exists (self ):
120
114
for middleware in [MIDDLEWARE_CSP_BEFORE , MIDDLEWARE_CSP_LAST ]:
121
115
with self .settings (MIDDLEWARE = middleware ):
122
- response = cast ( HttpResponse , self .client .get (path = "/csp_view/" ) )
116
+ response = self .client .get (path = "/csp_view/" )
123
117
self .assertEqual (response .status_code , 200 )
124
118
125
- html_root : Element = self .parser .parse (stream = response .content )
119
+ html_root = self .parser .parse (stream = response .content )
126
120
self ._fail_on_invalid_html (content = response .content , parser = self .parser )
127
121
self .assertContains (response , "djDebug" )
128
122
129
123
namespaces = get_namespaces (element = html_root )
130
- context : ContextList = response .context # pyright: ignore[reportAttributeAccessIssue]
124
+ context = response .context
131
125
nonce = str (context ["toolbar" ].csp_nonce )
132
126
self ._fail_if_missing (
133
127
root = html_root , path = ".//link" , namespaces = namespaces , nonce = nonce
@@ -139,14 +133,14 @@ def test_redirects_exists(self):
139
133
def test_panel_content_nonce_exists (self ):
140
134
for middleware in [MIDDLEWARE_CSP_BEFORE , MIDDLEWARE_CSP_LAST ]:
141
135
with self .settings (MIDDLEWARE = middleware ):
142
- response = cast ( HttpResponse , self .client .get (path = "/csp_view/" ) )
136
+ response = self .client .get (path = "/csp_view/" )
143
137
self .assertEqual (response .status_code , 200 )
144
138
145
139
toolbar = list (DebugToolbar ._store .values ())[- 1 ]
146
140
panels_to_check = ["HistoryPanel" , "TimerPanel" ]
147
141
for panel in panels_to_check :
148
142
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 )
150
144
namespaces = get_namespaces (element = html_root )
151
145
nonce = str (toolbar .csp_nonce )
152
146
self ._fail_if_missing (
@@ -164,10 +158,10 @@ def test_panel_content_nonce_exists(self):
164
158
165
159
def test_missing (self ):
166
160
"""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/" )
168
162
self .assertEqual (response .status_code , 200 )
169
163
170
- html_root : Element = self .parser .parse (stream = response .content )
164
+ html_root = self .parser .parse (stream = response .content )
171
165
self ._fail_on_invalid_html (content = response .content , parser = self .parser )
172
166
self .assertContains (response , "djDebug" )
173
167
0 commit comments