4
4
from shutil import rmtree
5
5
from subprocess import call
6
6
from threading import Thread
7
- from unittest .mock import Mock , patch
7
+ from unittest .mock import patch
8
8
from unittest .mock import call as MockCall
9
9
10
10
from django .conf import settings
24
24
WebpackLoaderTimeoutError ,
25
25
)
26
26
from 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
28
28
29
29
BUNDLE_PATH = os .path .join (
30
30
settings .BASE_DIR , 'assets/django_webpack_loader_bundles/' )
@@ -219,20 +219,20 @@ def test_integrity(self):
219
219
self .compile_bundles ('webpack.config.integrity.js' )
220
220
221
221
loader = get_loader (DEFAULT_CONFIG )
222
- with patch .dict (loader .config , {'INTEGRITY' : True }):
222
+ with patch .dict (loader .config , {'INTEGRITY' : True , 'CACHE' : False }):
223
223
view = TemplateView .as_view (template_name = 'single.html' )
224
224
request = self .factory .get ('/' )
225
225
result = view (request )
226
226
227
227
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 ==" >'
233
233
'</script>' ), result .rendered_content )
234
234
self .assertIn ((
235
- '<link href="/static/django_webpack_loader_bundles /main.css" '
235
+ '<link href="http://custom-static-host.com /main.css" '
236
236
'rel="stylesheet" integrity="sha256-cYWwRvS04/VsttQYx4BalKYrB'
237
237
'Duw5t8vKFhWB/LKX30= sha384-V/UxbrsEy8BK5nd+sBlN31Emmq/WdDDdI'
238
238
'01UR8wKIFkIr6vEaT5YRaeLMfLcAQvS sha512-aigPxglXDA33t9s5i0vRa'
@@ -241,14 +241,14 @@ def test_integrity(self):
241
241
result .rendered_content
242
242
)
243
243
244
- def test_integrity_with_crosorigin_empty (self ):
244
+ def test_integrity_with_crossorigin_empty (self ):
245
245
self .compile_bundles ('webpack.config.integrity.js' )
246
246
247
247
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 }):
249
249
view = TemplateView .as_view (template_name = 'single.html' )
250
250
request = self .factory .get ('/' )
251
- request .META ['HTTP_HOST' ] = 'crossorigen -custom-static-host.com'
251
+ request .META ['HTTP_HOST' ] = 'crossorigin -custom-static-host.com'
252
252
result = view (request )
253
253
254
254
self .assertIn ((
@@ -270,14 +270,14 @@ def test_integrity_with_crosorigin_empty(self):
270
270
result .rendered_content
271
271
)
272
272
273
- def test_integrity_with_crosorigin_anonymous (self ):
273
+ def test_integrity_with_crossorigin_anonymous (self ):
274
274
self .compile_bundles ('webpack.config.integrity.js' )
275
275
276
276
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 }):
278
278
view = TemplateView .as_view (template_name = 'single.html' )
279
279
request = self .factory .get ('/' )
280
- request .META ['HTTP_HOST' ] = 'crossorigen -custom-static-host.com'
280
+ request .META ['HTTP_HOST' ] = 'crossorigin -custom-static-host.com'
281
281
result = view (request )
282
282
283
283
self .assertIn ((
@@ -299,14 +299,14 @@ def test_integrity_with_crosorigin_anonymous(self):
299
299
result .rendered_content
300
300
)
301
301
302
- def test_integrity_with_crosorigin_use_credentials (self ):
302
+ def test_integrity_with_crossorigin_use_credentials (self ):
303
303
self .compile_bundles ('webpack.config.integrity.js' )
304
304
305
305
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 }):
307
307
view = TemplateView .as_view (template_name = 'single.html' )
308
308
request = self .factory .get ('/' )
309
- request .META ['HTTP_HOST' ] = 'crossorigen -custom-static-host.com'
309
+ request .META ['HTTP_HOST' ] = 'crossorigin -custom-static-host.com'
310
310
result = view (request )
311
311
312
312
self .assertIn ((
@@ -340,11 +340,11 @@ def test_integrity_missing_config(self):
340
340
result = view (request )
341
341
342
342
self .assertIn ((
343
- '<script src="/static/django_webpack_loader_bundles /main.js" >'
343
+ '<script src="http://custom-static-host.com /main.js" >'
344
344
'</script>' ), result .rendered_content
345
345
)
346
346
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" />' ),
348
348
result .rendered_content
349
349
)
350
350
@@ -946,90 +946,76 @@ def test_get_as_tags_direct_usage(self):
946
946
self .assertEqual (tags [0 ], asset_vendor )
947
947
self .assertEqual (tags [1 ], asset_app1 )
948
948
self .assertEqual (tags [2 ], asset_app2 )
949
-
949
+
950
950
def test_get_url_to_tag_dict_with_nonce (self ):
951
951
"""Test the get_as_url_to_tag_dict function with nonce attribute handling."""
952
- # Setup FakeWebpackLoader with CSP_NONCE enabled
953
952
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' )
962
954
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' ])
964
999
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
-
1005
1000
def test_get_url_to_tag_dict_with_different_extensions (self ):
1006
1001
"""Test the get_as_url_to_tag_dict function with different file extensions."""
1007
1002
1003
+ self .compile_bundles ('webpack.config.simple.js' )
1008
1004
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 }):
1019
1007
# Create a request with csp_nonce
1020
1008
request = self .factory .get ('/' )
1021
1009
request .csp_nonce = "test-nonce-123"
1022
-
1023
- # Test with different extensions
1024
-
1010
+
1025
1011
# JavaScript file
1026
1012
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
+
1031
1017
# CSS file
1032
1018
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