@@ -15,85 +15,81 @@ def _get_ns(element: Element) -> Dict[str, str]:
15
15
Return the default `xmlns`. See
16
16
https://docs.python.org/3/library/xml.etree.elementtree.html#parsing-xml-with-namespaces
17
17
"""
18
- if not element .tag .startswith ('{' ):
18
+ if not element .tag .startswith ("{" ):
19
19
return {}
20
- return {'' : element .tag [1 :].split ('}' , maxsplit = 1 )[0 ]}
20
+ return {"" : element .tag [1 :].split ("}" , maxsplit = 1 )[0 ]}
21
21
22
22
23
23
class CspRenderingTestCase (BaseTestCase ):
24
- 'Testing if `csp-nonce` renders.'
24
+ "Testing if `csp-nonce` renders."
25
+
25
26
panel_id = "StaticFilesPanel"
26
27
27
28
def _fail_if_missing (
28
- self , root : Element , path : str , namespaces : Dict [str , str ],
29
- nonce : str ):
29
+ self , root : Element , path : str , namespaces : Dict [str , str ], nonce : str
30
+ ):
30
31
"""
31
32
Search elements, fail if a `nonce` attribute is missing on them.
32
33
"""
33
34
elements = root .findall (path = path , namespaces = namespaces )
34
35
for item in elements :
35
- if item .attrib .get (' nonce' ) != nonce :
36
- raise self .failureException (f' { item } has no nonce attribute.' )
36
+ if item .attrib .get (" nonce" ) != nonce :
37
+ raise self .failureException (f" { item } has no nonce attribute." )
37
38
38
- def _fail_if_found (
39
- self , root : Element , path : str , namespaces : Dict [str , str ]):
39
+ def _fail_if_found (self , root : Element , path : str , namespaces : Dict [str , str ]):
40
40
"""
41
41
Search elements, fail if a `nonce` attribute is found on them.
42
42
"""
43
43
elements = root .findall (path = path , namespaces = namespaces )
44
44
for item in elements :
45
- if ' nonce' in item .attrib :
46
- raise self .failureException (f' { item } has no nonce attribute.' )
45
+ if " nonce" in item .attrib :
46
+ raise self .failureException (f" { item } has no nonce attribute." )
47
47
48
48
def _fail_on_invalid_html (self , content : bytes , parser : HTMLParser ):
49
- ' Fail if the passed HTML is invalid.'
49
+ " Fail if the passed HTML is invalid."
50
50
if parser .errors :
51
- default_msg = [' Content is invalid HTML:' ]
52
- lines = content .split (b' \n ' )
51
+ default_msg = [" Content is invalid HTML:" ]
52
+ lines = content .split (b" \n " )
53
53
for position , errorcode , datavars in parser .errors :
54
- default_msg .append (' %s' % E [errorcode ] % datavars )
55
- default_msg .append (' %r' % lines [position [0 ] - 1 ])
56
- msg = self ._formatMessage (None , ' \n ' .join (default_msg ))
54
+ default_msg .append (" %s" % E [errorcode ] % datavars )
55
+ default_msg .append (" %r" % lines [position [0 ] - 1 ])
56
+ msg = self ._formatMessage (None , " \n " .join (default_msg ))
57
57
raise self .failureException (msg )
58
58
59
59
@override_settings (
60
- DEBUG = True , MIDDLEWARE = settings .MIDDLEWARE + [
61
- 'csp.middleware.CSPMiddleware'
62
- ])
60
+ DEBUG = True , MIDDLEWARE = settings .MIDDLEWARE + ["csp.middleware.CSPMiddleware" ]
61
+ )
63
62
def test_exists (self ):
64
- ' A `nonce` should exists when using the `CSPMiddleware`.'
65
- response = self .client .get (path = ' /regular/basic/' )
63
+ " A `nonce` should exists when using the `CSPMiddleware`."
64
+ response = self .client .get (path = " /regular/basic/" )
66
65
if not isinstance (response , HttpResponse ):
67
- raise self .failureException (f' { response !r} is not a HttpResponse' )
66
+ raise self .failureException (f" { response !r} is not a HttpResponse" )
68
67
self .assertEqual (response .status_code , 200 )
69
68
parser = HTMLParser ()
70
69
el_htmlroot : Element = parser .parse (stream = response .content )
71
70
self ._fail_on_invalid_html (content = response .content , parser = parser )
72
- self .assertContains (response , ' djDebug' )
71
+ self .assertContains (response , " djDebug" )
73
72
namespaces = _get_ns (element = el_htmlroot )
74
- context : ContextList = \
75
- response .context # pyright: ignore[reportAttributeAccessIssue]
76
- nonce = str (context ['toolbar' ].request .csp_nonce )
73
+ context : ContextList = response .context # pyright: ignore[reportAttributeAccessIssue]
74
+ nonce = str (context ["toolbar" ].request .csp_nonce )
77
75
self ._fail_if_missing (
78
- root = el_htmlroot , path = ' .//link' , namespaces = namespaces ,
79
- nonce = nonce )
76
+ root = el_htmlroot , path = " .//link" , namespaces = namespaces , nonce = nonce
77
+ )
80
78
self ._fail_if_missing (
81
- root = el_htmlroot , path = ' .//script' , namespaces = namespaces ,
82
- nonce = nonce )
79
+ root = el_htmlroot , path = " .//script" , namespaces = namespaces , nonce = nonce
80
+ )
83
81
84
82
@override_settings (DEBUG = True )
85
83
def test_missing (self ):
86
- ' A `nonce` should not exist when not using the `CSPMiddleware`.'
87
- response = self .client .get (path = ' /regular/basic/' )
84
+ " A `nonce` should not exist when not using the `CSPMiddleware`."
85
+ response = self .client .get (path = " /regular/basic/" )
88
86
if not isinstance (response , HttpResponse ):
89
- raise self .failureException (f' { response !r} is not a HttpResponse' )
87
+ raise self .failureException (f" { response !r} is not a HttpResponse" )
90
88
self .assertEqual (response .status_code , 200 )
91
89
parser = HTMLParser ()
92
90
el_htmlroot : Element = parser .parse (stream = response .content )
93
91
self ._fail_on_invalid_html (content = response .content , parser = parser )
94
- self .assertContains (response , ' djDebug' )
92
+ self .assertContains (response , " djDebug" )
95
93
namespaces = _get_ns (element = el_htmlroot )
96
- self ._fail_if_found (
97
- root = el_htmlroot , path = './/link' , namespaces = namespaces )
98
- self ._fail_if_found (
99
- root = el_htmlroot , path = './/script' , namespaces = namespaces )
94
+ self ._fail_if_found (root = el_htmlroot , path = ".//link" , namespaces = namespaces )
95
+ self ._fail_if_found (root = el_htmlroot , path = ".//script" , namespaces = namespaces )
0 commit comments