11from __future__ import annotations
22
3+ import django
34from django .conf import settings
45from django .test .utils import override_settings
56from html5lib .constants import E
67from html5lib .html5parser import HTMLParser
78
89from debug_toolbar .store import get_store
910from debug_toolbar .toolbar import DebugToolbar
11+ from debug_toolbar .utils import get_csp_nonce
1012
1113from .base import IntegrationTestCase
1214
13- MIDDLEWARE_CSP_BEFORE = settings .MIDDLEWARE .copy ()
14- MIDDLEWARE_CSP_BEFORE .insert (
15- MIDDLEWARE_CSP_BEFORE .index ("debug_toolbar.middleware.DebugToolbarMiddleware" ),
15+ MIDDLEWARE_CSP_LIB_BEFORE = settings .MIDDLEWARE .copy ()
16+ MIDDLEWARE_CSP_LIB_BEFORE .insert (
17+ MIDDLEWARE_CSP_LIB_BEFORE .index ("debug_toolbar.middleware.DebugToolbarMiddleware" ),
1618 "csp.middleware.CSPMiddleware" ,
1719)
18- MIDDLEWARE_CSP_LAST = settings .MIDDLEWARE + ["csp.middleware.CSPMiddleware" ]
20+ MIDDLEWARE_CSP_LIB_LAST = settings .MIDDLEWARE + ["csp.middleware.CSPMiddleware" ]
21+
22+ VALID_MIDDLEWARE_VARIATIONS = [MIDDLEWARE_CSP_LIB_BEFORE , MIDDLEWARE_CSP_LIB_LAST ]
23+
24+ django_has_builtin_csp_support = django .VERSION >= (6 , 0 )
25+ if django_has_builtin_csp_support :
26+ MIDDLEWARE_CSP_BUILTIN_BEFORE = settings .MIDDLEWARE .copy ()
27+ MIDDLEWARE_CSP_BUILTIN_BEFORE .insert (
28+ MIDDLEWARE_CSP_BUILTIN_BEFORE .index (
29+ "debug_toolbar.middleware.DebugToolbarMiddleware"
30+ ),
31+ "django.middleware.csp.ContentSecurityPolicyMiddleware" ,
32+ )
33+ MIDDLEWARE_CSP_BUILTIN_LAST = settings .MIDDLEWARE + [
34+ "django.middleware.csp.ContentSecurityPolicyMiddleware"
35+ ]
36+ VALID_MIDDLEWARE_VARIATIONS += [
37+ MIDDLEWARE_CSP_BUILTIN_BEFORE ,
38+ MIDDLEWARE_CSP_BUILTIN_LAST ,
39+ ]
1940
2041
2142def get_namespaces (element ):
@@ -67,7 +88,7 @@ def _fail_on_invalid_html(self, content, parser):
6788
6889 def test_exists (self ):
6990 """A `nonce` should exist when using the `CSPMiddleware`."""
70- for middleware in [ MIDDLEWARE_CSP_BEFORE , MIDDLEWARE_CSP_LAST ] :
91+ for middleware in VALID_MIDDLEWARE_VARIATIONS :
7192 with self .settings (MIDDLEWARE = middleware ):
7293 response = self .client .get (path = "/csp_view/" )
7394 self .assertEqual (response .status_code , 200 )
@@ -77,7 +98,8 @@ def test_exists(self):
7798 self .assertContains (response , "djDebug" )
7899
79100 namespaces = get_namespaces (element = html_root )
80- nonce = response .context ["request" ].csp_nonce
101+ nonce = get_csp_nonce (response .context ["request" ])
102+ assert nonce is not None
81103 self ._fail_if_missing (
82104 root = html_root , path = ".//link" , namespaces = namespaces , nonce = nonce
83105 )
@@ -88,9 +110,9 @@ def test_exists(self):
88110 def test_does_not_exist_nonce_wasnt_used (self ):
89111 """
90112 A `nonce` should not exist even when using the `CSPMiddleware`
91- if the view didn't access the request.csp_nonce attribute .
113+ if the view didn't access the request's CSP nonce .
92114 """
93- for middleware in [ MIDDLEWARE_CSP_BEFORE , MIDDLEWARE_CSP_LAST ] :
115+ for middleware in VALID_MIDDLEWARE_VARIATIONS :
94116 with self .settings (MIDDLEWARE = middleware ):
95117 response = self .client .get (path = "/regular/basic/" )
96118 self .assertEqual (response .status_code , 200 )
@@ -111,7 +133,7 @@ def test_does_not_exist_nonce_wasnt_used(self):
111133 DEBUG_TOOLBAR_CONFIG = {"DISABLE_PANELS" : set ()},
112134 )
113135 def test_redirects_exists (self ):
114- for middleware in [ MIDDLEWARE_CSP_BEFORE , MIDDLEWARE_CSP_LAST ] :
136+ for middleware in VALID_MIDDLEWARE_VARIATIONS :
115137 with self .settings (MIDDLEWARE = middleware ):
116138 response = self .client .get (path = "/csp_view/" )
117139 self .assertEqual (response .status_code , 200 )
@@ -132,7 +154,7 @@ def test_redirects_exists(self):
132154
133155 def test_panel_content_nonce_exists (self ):
134156 store = get_store ()
135- for middleware in [ MIDDLEWARE_CSP_BEFORE , MIDDLEWARE_CSP_LAST ] :
157+ for middleware in VALID_MIDDLEWARE_VARIATIONS :
136158 with self .settings (MIDDLEWARE = middleware ):
137159 response = self .client .get (path = "/csp_view/" )
138160 self .assertEqual (response .status_code , 200 )
0 commit comments