44from shutil import rmtree
55from subprocess import call
66from threading import Thread
7- from unittest .mock import Mock , patch
7+ from unittest .mock import patch
88from unittest .mock import call as MockCall
99
1010from django .conf import settings
2424 WebpackLoaderTimeoutError ,
2525)
2626from webpack_loader .templatetags .webpack_loader import _WARNING_MESSAGE
27- from webpack_loader .utils import get_as_tags , get_loader
27+ from webpack_loader .utils import get_as_tags , get_loader , get_as_url_to_tag_dict
2828
2929BUNDLE_PATH = os .path .join (
3030 settings .BASE_DIR , 'assets/django_webpack_loader_bundles/' )
@@ -219,20 +219,20 @@ def test_integrity(self):
219219 self .compile_bundles ('webpack.config.integrity.js' )
220220
221221 loader = get_loader (DEFAULT_CONFIG )
222- with patch .dict (loader .config , {'INTEGRITY' : True }):
222+ with patch .dict (loader .config , {'INTEGRITY' : True , 'CACHE' : False }):
223223 view = TemplateView .as_view (template_name = 'single.html' )
224224 request = self .factory .get ('/' )
225225 result = view (request )
226226
227227 self .assertIn ((
228- '<script src="/static/django_webpack_loader_bundles /main.js" '
229- 'integrity="sha256-1wgFMxcDlOWYV727qRvWNoPHdnOGFNVMLuKd25cjR+ '
230- 'o = sha384-3RnsU3Z2OODW6qaMAPVpNC5lBb4M5I1+joXv37ACuLvCO6gQ7o '
231- 'OD7IC1zN1uAakD sha512-9nLlV4v2pWvgeavHop1wXxdP34CfYv/xUZHwVB '
232- 'N+1p+pAvHDmBw4XkvvciSGW4zQlWhaUiIi7P6nXmsLE+8Fsw ==" >'
228+ '<script src="http://custom-static-host.com /main.js" '
229+ 'integrity="sha256-Yk6uAc7SoE41LSNc9zTBxij8YhVqBIIuRpLCaTyqrl '
230+ 'Q = sha384-cwtz5c2CaEK8Q8ZeraWgf3qo7eO5jUDE8XMo00QTUCcbmF/fLu '
231+ 'DtQFm8g4Jh9R5D sha512-s9uhbJTCZv4WfH/F81fgS6B6XNhOuH21Xouv5X '
232+ 'Pp35WlFR7ykkIafUG8cma4vbEfheH1NVbjsON5BHm8U13I4g ==" >'
233233 '</script>' ), result .rendered_content )
234234 self .assertIn ((
235- '<link href="/static/django_webpack_loader_bundles /main.css" '
235+ '<link href="http://custom-static-host.com /main.css" '
236236 'rel="stylesheet" integrity="sha256-cYWwRvS04/VsttQYx4BalKYrB'
237237 'Duw5t8vKFhWB/LKX30= sha384-V/UxbrsEy8BK5nd+sBlN31Emmq/WdDDdI'
238238 '01UR8wKIFkIr6vEaT5YRaeLMfLcAQvS sha512-aigPxglXDA33t9s5i0vRa'
@@ -241,14 +241,14 @@ def test_integrity(self):
241241 result .rendered_content
242242 )
243243
244- def test_integrity_with_crosorigin_empty (self ):
244+ def test_integrity_with_crossorigin_empty (self ):
245245 self .compile_bundles ('webpack.config.integrity.js' )
246246
247247 loader = get_loader (DEFAULT_CONFIG )
248- with patch .dict (loader .config , {'INTEGRITY' : True , 'CROSSORIGIN' : '' }):
248+ with patch .dict (loader .config , {'INTEGRITY' : True , 'CROSSORIGIN' : '' , 'CACHE' : False }):
249249 view = TemplateView .as_view (template_name = 'single.html' )
250250 request = self .factory .get ('/' )
251- request .META ['HTTP_HOST' ] = 'crossorigen -custom-static-host.com'
251+ request .META ['HTTP_HOST' ] = 'crossorigin -custom-static-host.com'
252252 result = view (request )
253253
254254 self .assertIn ((
@@ -270,14 +270,14 @@ def test_integrity_with_crosorigin_empty(self):
270270 result .rendered_content
271271 )
272272
273- def test_integrity_with_crosorigin_anonymous (self ):
273+ def test_integrity_with_crossorigin_anonymous (self ):
274274 self .compile_bundles ('webpack.config.integrity.js' )
275275
276276 loader = get_loader (DEFAULT_CONFIG )
277- with patch .dict (loader .config , {'INTEGRITY' : True , 'CROSSORIGIN' : 'anonymous' }):
277+ with patch .dict (loader .config , {'INTEGRITY' : True , 'CROSSORIGIN' : 'anonymous' , 'CACHE' : False }):
278278 view = TemplateView .as_view (template_name = 'single.html' )
279279 request = self .factory .get ('/' )
280- request .META ['HTTP_HOST' ] = 'crossorigen -custom-static-host.com'
280+ request .META ['HTTP_HOST' ] = 'crossorigin -custom-static-host.com'
281281 result = view (request )
282282
283283 self .assertIn ((
@@ -299,14 +299,14 @@ def test_integrity_with_crosorigin_anonymous(self):
299299 result .rendered_content
300300 )
301301
302- def test_integrity_with_crosorigin_use_credentials (self ):
302+ def test_integrity_with_crossorigin_use_credentials (self ):
303303 self .compile_bundles ('webpack.config.integrity.js' )
304304
305305 loader = get_loader (DEFAULT_CONFIG )
306- with patch .dict (loader .config , {'INTEGRITY' : True , 'CROSSORIGIN' : 'use-credentials' }):
306+ with patch .dict (loader .config , {'INTEGRITY' : True , 'CROSSORIGIN' : 'use-credentials' , 'CACHE' : False }):
307307 view = TemplateView .as_view (template_name = 'single.html' )
308308 request = self .factory .get ('/' )
309- request .META ['HTTP_HOST' ] = 'crossorigen -custom-static-host.com'
309+ request .META ['HTTP_HOST' ] = 'crossorigin -custom-static-host.com'
310310 result = view (request )
311311
312312 self .assertIn ((
@@ -340,11 +340,11 @@ def test_integrity_missing_config(self):
340340 result = view (request )
341341
342342 self .assertIn ((
343- '<script src="/static/django_webpack_loader_bundles /main.js" >'
343+ '<script src="http://custom-static-host.com /main.js" >'
344344 '</script>' ), result .rendered_content
345345 )
346346 self .assertIn ((
347- '<link href="/static/django_webpack_loader_bundles /main.css" rel="stylesheet" />' ),
347+ '<link href="http://custom-static-host.com /main.css" rel="stylesheet" />' ),
348348 result .rendered_content
349349 )
350350
@@ -946,90 +946,76 @@ def test_get_as_tags_direct_usage(self):
946946 self .assertEqual (tags [0 ], asset_vendor )
947947 self .assertEqual (tags [1 ], asset_app1 )
948948 self .assertEqual (tags [2 ], asset_app2 )
949-
949+
950950 def test_get_url_to_tag_dict_with_nonce (self ):
951951 """Test the get_as_url_to_tag_dict function with nonce attribute handling."""
952- # Setup FakeWebpackLoader with CSP_NONCE enabled
953952
954- with self .settings (
955- WEBPACK_LOADER = {
956- "DEFAULT" : {
957- "CSP_NONCE" : True ,
958- },
959- }
960- ):
961- from webpack_loader .utils import get_as_url_to_tag_dict , get_loader
953+ self .compile_bundles ('webpack.config.simple.js' )
962954
963- self .compile_bundles ('webpack.config.simple.js' )
955+ loader = get_loader (DEFAULT_CONFIG )
956+ with patch .dict (loader .config , {"CSP_NONCE" : True , 'CACHE' : False }):
957+ # Create a request with csp_nonce
958+ request = self .factory .get ('/' )
959+ request .csp_nonce = "test-nonce-123"
960+
961+ # Get tag dict with nonce enabled
962+ tag_dict = get_as_url_to_tag_dict ('main' , extension = 'js' , attrs = '' , request = request )
963+ # Verify nonce is in the tag
964+ self .assertIn ('nonce="test-nonce-123"' , tag_dict ['/static/django_webpack_loader_bundles/main.js' ])
965+
966+ # Test with existing nonce in attrs - should not duplicate
967+ tag_dict = get_as_url_to_tag_dict ('main' , extension = 'js' , attrs = 'nonce="existing-nonce"' , request = request )
968+ self .assertIn ('nonce="existing-nonce"' , tag_dict ['/static/django_webpack_loader_bundles/main.js' ])
969+ self .assertNotIn ('nonce="test-nonce-123"' , tag_dict ['/static/django_webpack_loader_bundles/main.js' ])
970+
971+ # Test without request - should not have nonce
972+ tag_dict = get_as_url_to_tag_dict ('main' , extension = 'js' , attrs = '' , request = None )
973+ self .assertNotIn ('nonce=' , tag_dict ['/static/django_webpack_loader_bundles/main.js' ])
974+
975+ # Test with request but no csp_nonce attribute - should not have nonce
976+ request_without_nonce = self .factory .get ('/' )
977+ tag_dict = get_as_url_to_tag_dict ('main' , extension = 'js' , attrs = '' , request = request_without_nonce )
978+ self .assertNotIn ('nonce=' , tag_dict ['/static/django_webpack_loader_bundles/main.js' ])
979+
980+ def test_get_url_to_tag_dict_with_nonce_disabled (self ):
981+ self .compile_bundles ('webpack.config.simple.js' )
982+
983+ loader = get_loader (DEFAULT_CONFIG )
984+ with patch .dict (loader .config , {"CSP_NONCE" : False , 'CACHE' : False }):
985+ # Create a request without csp_nonce
986+ request = self .factory .get ('/' )
987+
988+ # should not have nonce
989+ tag_dict = get_as_url_to_tag_dict ('main' , extension = 'js' , attrs = '' , request = request )
990+ self .assertNotIn ('nonce=' , tag_dict ['/static/django_webpack_loader_bundles/main.js' ])
991+
992+ # Create a request with csp_nonce
993+ request_with_nonce = self .factory .get ('/' )
994+ request_with_nonce .csp_nonce = "test-nonce-123"
995+
996+ # Test with CSP_NONCE disabled - should not have nonce
997+ tag_dict = get_as_url_to_tag_dict ('main' , extension = 'js' , attrs = '' , request = request_with_nonce )
998+ self .assertNotIn ('nonce=' , tag_dict ['/static/django_webpack_loader_bundles/main.js' ])
964999
965- # Use default config but enable CSP_NONCE
966- loader = get_loader (DEFAULT_CONFIG )
967- original_config = loader .config .copy ()
968- try :
969- # Test with CSP_NONCE enabled
970- loader .config ['CSP_NONCE' ] = True
971-
972- # Create a request with csp_nonce
973- request = self .factory .get ('/' )
974- request .csp_nonce = "test-nonce-123"
975-
976- # Get tag dict with nonce enabled
977- tag_dict = get_as_url_to_tag_dict ('main' , extension = 'js' , attrs = '' , request = request )
978-
979- # Verify nonce is in the tag
980- self .assertIn ('nonce="test-nonce-123"' , tag_dict ['/static/webpack_bundles/main.js' ])
981-
982- # Test with existing nonce in attrs - should not duplicate
983- tag_dict = get_as_url_to_tag_dict ('main' , extension = 'js' , attrs = 'nonce="existing-nonce"' , request = request )
984- self .assertIn ('nonce="existing-nonce"' , tag_dict ['/static/webpack_bundles/main.js' ])
985- self .assertNotIn ('nonce="test-nonce-123"' , tag_dict ['/static/webpack_bundles/main.js' ])
986-
987- # Test without request - should not have nonce and should emit warning
988- tag_dict = get_as_url_to_tag_dict ('main' , extension = 'js' , attrs = '' , request = None )
989- self .assertNotIn ('nonce=' , tag_dict ['/static/webpack_bundles/main.js' ])
990-
991- # Test with request but no csp_nonce attribute - should not have nonce and should emit warning
992- request_without_nonce = self .factory .get ('/' )
993- tag_dict = get_as_url_to_tag_dict ('main' , extension = 'js' , attrs = '' , request = request_without_nonce )
994- self .assertNotIn ('nonce=' , tag_dict ['/static/webpack_bundles/main.js' ])
995-
996- # Test with CSP_NONCE disabled - should not have nonce
997- loader .config ['CSP_NONCE' ] = False
998- tag_dict = get_as_url_to_tag_dict ('main' , extension = 'js' , attrs = '' , request = request )
999- self .assertNotIn ('nonce=' , tag_dict ['/static/webpack_bundles/main.js' ])
1000-
1001- finally :
1002- # Restore original config
1003- loader .config = original_config
1004-
10051000 def test_get_url_to_tag_dict_with_different_extensions (self ):
10061001 """Test the get_as_url_to_tag_dict function with different file extensions."""
10071002
1003+ self .compile_bundles ('webpack.config.simple.js' )
10081004
1009- with self .settings (
1010- WEBPACK_LOADER = {
1011- "DEFAULT" : {
1012- "CSP_NONCE" : True ,
1013- },
1014- }
1015- ):
1016- from webpack_loader .utils import get_as_url_to_tag_dict
1017- self .compile_bundles ('webpack.config.simple.js' )
1018-
1005+ loader = get_loader (DEFAULT_CONFIG )
1006+ with patch .dict (loader .config , {"CSP_NONCE" : True , 'CACHE' : False }):
10191007 # Create a request with csp_nonce
10201008 request = self .factory .get ('/' )
10211009 request .csp_nonce = "test-nonce-123"
1022-
1023- # Test with different extensions
1024-
1010+
10251011 # JavaScript file
10261012 tag_dict = get_as_url_to_tag_dict ('main' , extension = 'js' , attrs = '' , request = request )
1027- self .assertIn ('<script src="/static/webpack_bundles /main.js"' ,
1028- tag_dict ['/static/webpack_bundles /main.js' ])
1029- self .assertIn ('nonce="test-nonce-123"' , tag_dict ['/static/webpack_bundles /main.js' ])
1030-
1013+ self .assertIn ('<script src="/static/django_webpack_loader_bundles /main.js"' ,
1014+ tag_dict ['/static/django_webpack_loader_bundles /main.js' ])
1015+ self .assertIn ('nonce="test-nonce-123"' , tag_dict ['/static/django_webpack_loader_bundles /main.js' ])
1016+
10311017 # CSS file
10321018 tag_dict = get_as_url_to_tag_dict ('main' , extension = 'css' , attrs = '' , request = request )
1033- self .assertIn ('<link href="/static/webpack_bundles /main.css" rel="stylesheet"' ,
1034- tag_dict ['/static/webpack_bundles /main.css' ])
1035- self .assertIn ('nonce="test-nonce-123"' , tag_dict ['/static/webpack_bundles /main.css' ])
1019+ self .assertIn ('<link href="/static/django_webpack_loader_bundles /main.css" rel="stylesheet"' ,
1020+ tag_dict ['/static/django_webpack_loader_bundles /main.css' ])
1021+ self .assertIn ('nonce="test-nonce-123"' , tag_dict ['/static/django_webpack_loader_bundles /main.css' ])
0 commit comments