@@ -15,89 +15,85 @@ 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 setUp(self):
28
29
# self.factory = RequestFactory()
29
30
# self.async_factory = AsyncRequestFactory()
30
31
31
32
def _fail_if_missing (
32
- self , root : Element , path : str , namespaces : Dict [str , str ],
33
- nonce : str ):
33
+ self , root : Element , path : str , namespaces : Dict [str , str ], nonce : str
34
+ ):
34
35
"""
35
36
Search elements, fail if a `nonce` attribute is missing on them.
36
37
"""
37
38
elements = root .findall (path = path , namespaces = namespaces )
38
39
for item in elements :
39
- if item .attrib .get (' nonce' ) != nonce :
40
- raise self .failureException (f' { item } has no nonce attribute.' )
40
+ if item .attrib .get (" nonce" ) != nonce :
41
+ raise self .failureException (f" { item } has no nonce attribute." )
41
42
42
- def _fail_if_found (
43
- self , root : Element , path : str , namespaces : Dict [str , str ]):
43
+ def _fail_if_found (self , root : Element , path : str , namespaces : Dict [str , str ]):
44
44
"""
45
45
Search elements, fail if a `nonce` attribute is found on them.
46
46
"""
47
47
elements = root .findall (path = path , namespaces = namespaces )
48
48
for item in elements :
49
- if ' nonce' in item .attrib :
50
- raise self .failureException (f' { item } has no nonce attribute.' )
49
+ if " nonce" in item .attrib :
50
+ raise self .failureException (f" { item } has no nonce attribute." )
51
51
52
52
def _fail_on_invalid_html (self , content : bytes , parser : HTMLParser ):
53
- ' Fail if the passed HTML is invalid.'
53
+ " Fail if the passed HTML is invalid."
54
54
if parser .errors :
55
- default_msg = [' Content is invalid HTML:' ]
56
- lines = content .split (b' \n ' )
55
+ default_msg = [" Content is invalid HTML:" ]
56
+ lines = content .split (b" \n " )
57
57
for position , errorcode , datavars in parser .errors :
58
- default_msg .append (' %s' % E [errorcode ] % datavars )
59
- default_msg .append (' %r' % lines [position [0 ] - 1 ])
60
- msg = self ._formatMessage (None , ' \n ' .join (default_msg ))
58
+ default_msg .append (" %s" % E [errorcode ] % datavars )
59
+ default_msg .append (" %r" % lines [position [0 ] - 1 ])
60
+ msg = self ._formatMessage (None , " \n " .join (default_msg ))
61
61
raise self .failureException (msg )
62
62
63
63
@override_settings (
64
- DEBUG = True , MIDDLEWARE = settings .MIDDLEWARE + [
65
- 'csp.middleware.CSPMiddleware'
66
- ])
64
+ DEBUG = True , MIDDLEWARE = settings .MIDDLEWARE + ["csp.middleware.CSPMiddleware" ]
65
+ )
67
66
def test_exists (self ):
68
- ' A `nonce` should exists when using the `CSPMiddleware`.'
69
- response = self .client .get (path = ' /regular/basic/' )
67
+ " A `nonce` should exists when using the `CSPMiddleware`."
68
+ response = self .client .get (path = " /regular/basic/" )
70
69
if not isinstance (response , HttpResponse ):
71
- raise self .failureException (f' { response !r} is not a HttpResponse' )
70
+ raise self .failureException (f" { response !r} is not a HttpResponse" )
72
71
self .assertEqual (response .status_code , 200 )
73
72
parser = HTMLParser ()
74
73
el_htmlroot : Element = parser .parse (stream = response .content )
75
74
self ._fail_on_invalid_html (content = response .content , parser = parser )
76
- self .assertContains (response , ' djDebug' )
75
+ self .assertContains (response , " djDebug" )
77
76
namespaces = _get_ns (element = el_htmlroot )
78
- context : ContextList = \
79
- response .context # pyright: ignore[reportAttributeAccessIssue]
80
- nonce = str (context ['toolbar' ].request .csp_nonce )
77
+ context : ContextList = response .context # pyright: ignore[reportAttributeAccessIssue]
78
+ nonce = str (context ["toolbar" ].request .csp_nonce )
81
79
self ._fail_if_missing (
82
- root = el_htmlroot , path = ' .//link' , namespaces = namespaces ,
83
- nonce = nonce )
80
+ root = el_htmlroot , path = " .//link" , namespaces = namespaces , nonce = nonce
81
+ )
84
82
self ._fail_if_missing (
85
- root = el_htmlroot , path = ' .//script' , namespaces = namespaces ,
86
- nonce = nonce )
83
+ root = el_htmlroot , path = " .//script" , namespaces = namespaces , nonce = nonce
84
+ )
87
85
88
86
@override_settings (DEBUG = True )
89
87
def test_missing (self ):
90
- ' A `nonce` should not exist when not using the `CSPMiddleware`.'
91
- response = self .client .get (path = ' /regular/basic/' )
88
+ " A `nonce` should not exist when not using the `CSPMiddleware`."
89
+ response = self .client .get (path = " /regular/basic/" )
92
90
if not isinstance (response , HttpResponse ):
93
- raise self .failureException (f' { response !r} is not a HttpResponse' )
91
+ raise self .failureException (f" { response !r} is not a HttpResponse" )
94
92
self .assertEqual (response .status_code , 200 )
95
93
parser = HTMLParser ()
96
94
el_htmlroot : Element = parser .parse (stream = response .content )
97
95
self ._fail_on_invalid_html (content = response .content , parser = parser )
98
- self .assertContains (response , ' djDebug' )
96
+ self .assertContains (response , " djDebug" )
99
97
namespaces = _get_ns (element = el_htmlroot )
100
- self ._fail_if_found (
101
- root = el_htmlroot , path = './/link' , namespaces = namespaces )
102
- self ._fail_if_found (
103
- root = el_htmlroot , path = './/script' , namespaces = namespaces )
98
+ self ._fail_if_found (root = el_htmlroot , path = ".//link" , namespaces = namespaces )
99
+ self ._fail_if_found (root = el_htmlroot , path = ".//script" , namespaces = namespaces )
0 commit comments