2
2
from xml .etree .ElementTree import Element
3
3
4
4
from django .conf import settings
5
+ from django .http .response import HttpResponse
5
6
from django .test .utils import ContextList , override_settings
6
7
from html5lib .constants import E
7
8
from html5lib .html5parser import HTMLParser
@@ -66,6 +67,8 @@ def _fail_on_invalid_html(self, content: bytes, parser: HTMLParser):
66
67
def test_exists (self ):
67
68
"""A `nonce` should exist when using the `CSPMiddleware`."""
68
69
response = self .client .get (path = "/regular/basic/" )
70
+ if not isinstance (response , HttpResponse ):
71
+ raise self .failureException (f'{ response !r} is not a HttpResponse' )
69
72
self .assertEqual (response .status_code , 200 )
70
73
71
74
html_root : Element = self .parser .parse (stream = response .content )
@@ -88,14 +91,17 @@ def test_exists(self):
88
91
)
89
92
def test_redirects_exists (self ):
90
93
response = self .client .get ("/redirect/" )
94
+ if not isinstance (response , HttpResponse ):
95
+ raise self .failureException (f'{ response !r} is not a HttpResponse' )
91
96
self .assertEqual (response .status_code , 200 )
92
97
93
98
html_root : Element = self .parser .parse (stream = response .content )
94
99
self ._fail_on_invalid_html (content = response .content , parser = self .parser )
95
100
self .assertContains (response , "djDebug" )
96
101
97
102
namespaces = get_namespaces (element = html_root )
98
- context : ContextList = response .context
103
+ context : ContextList = \
104
+ response .context # pyright: ignore[reportAttributeAccessIssue]
99
105
nonce = str (context ["toolbar" ].request .csp_nonce )
100
106
self ._fail_if_missing (
101
107
root = html_root , path = ".//link" , namespaces = namespaces , nonce = nonce
@@ -109,6 +115,8 @@ def test_redirects_exists(self):
109
115
)
110
116
def test_panel_content_nonce_exists (self ):
111
117
response = self .client .get ("/regular/basic/" )
118
+ if not isinstance (response , HttpResponse ):
119
+ raise self .failureException (f'{ response !r} is not a HttpResponse' )
112
120
self .assertEqual (response .status_code , 200 )
113
121
114
122
toolbar = list (DebugToolbar ._store .values ())[0 ]
@@ -128,6 +136,8 @@ def test_panel_content_nonce_exists(self):
128
136
def test_missing (self ):
129
137
"""A `nonce` should not exist when not using the `CSPMiddleware`."""
130
138
response = self .client .get (path = "/regular/basic/" )
139
+ if not isinstance (response , HttpResponse ):
140
+ raise self .failureException (f'{ response !r} is not a HttpResponse' )
131
141
self .assertEqual (response .status_code , 200 )
132
142
133
143
html_root : Element = self .parser .parse (stream = response .content )
0 commit comments