@@ -55,6 +55,7 @@ def test_default_src() -> None:
5555 policy = build_policy ()
5656 policy_eq ("default-src example.com example2.com" , policy )
5757
58+
5859@override_settings (CONTENT_SECURITY_POLICY = {"DIRECTIVES" : {"default-src" : {"example.com" , "example2.com" }}})
5960def test_default_src_is_set () -> None :
6061 policy = build_policy ()
@@ -300,7 +301,7 @@ def test_require_trusted_types_for() -> None:
300301def test_trusted_types () -> None :
301302 policy = build_policy ()
302303 policy_eq (
303- "default-src 'self'; trusted-types strictPolicy laxPolicy default 'allow-duplicates'" ,
304+ "default-src 'self'; trusted-types 'allow-duplicates' default laxPolicy strictPolicy " ,
304305 policy ,
305306 )
306307
@@ -319,14 +320,14 @@ def test_block_all_mixed_content() -> None:
319320
320321def test_nonce () -> None :
321322 policy = build_policy (nonce = "abc123" )
322- policy_eq ("default-src 'self' ' nonce-abc123'" , policy )
323+ policy_eq ("default-src 'nonce-abc123' 'self '" , policy )
323324
324325
325326@override_settings (CONTENT_SECURITY_POLICY = {"DIRECTIVES" : {"default-src" : [SELF ], "script-src" : [SELF , NONCE ], "style-src" : [SELF , NONCE ]}})
326327def test_nonce_in_value () -> None :
327328 policy = build_policy (nonce = "abc123" )
328329 policy_eq (
329- "default -src 'self'; script -src 'self' 'nonce-abc123' ; style-src 'self' ' nonce-abc123'" ,
330+ "script -src 'nonce-abc123' ' self'; default -src 'self'; style-src 'nonce-abc123' 'self '" ,
330331 policy ,
331332 )
332333
@@ -337,6 +338,35 @@ def test_only_nonce_in_value() -> None:
337338 policy_eq ("default-src 'nonce-abc123'" , policy )
338339
339340
341+ @override_settings (CONTENT_SECURITY_POLICY = {"DIRECTIVES" : {"img-src" : ["example.com" , "example.com" ]}})
342+ def test_deduplicate_values () -> None :
343+ """
344+ GitHub issue #40 - given project settings as a tuple, and
345+ an update/replace with a string, concatenate correctly.
346+ """
347+ policy = build_policy ()
348+ policy_eq ("default-src 'self'; img-src example.com" , policy )
349+
350+
351+ @override_settings (CONTENT_SECURITY_POLICY = {"DIRECTIVES" : {"img-src" : ["example.com" , "example.com" ]}})
352+ def test_deduplicate_values_update () -> None :
353+ """
354+ GitHub issue #40 - given project settings as a tuple, and
355+ an update/replace with a string, concatenate correctly.
356+ """
357+ policy = build_policy (update = {"img-src" : "example.com" })
358+ policy_eq ("default-src 'self'; img-src example.com" , policy )
359+
360+
361+ @override_settings (CONTENT_SECURITY_POLICY = {"DIRECTIVES" : {"img-src" : ("example.com" ,)}})
362+ def test_deduplicate_values_replace () -> None :
363+ """
364+ Demonstrate that GitHub issue #40 doesn't affect replacements
365+ """
366+ policy = build_policy (replace = {"img-src" : ["example2.com" , "example2.com" ]})
367+ policy_eq ("default-src 'self'; img-src example2.com" , policy )
368+
369+
340370def test_boolean_directives () -> None :
341371 for directive in ["upgrade-insecure-requests" , "block-all-mixed-content" ]:
342372 with override_settings (CONTENT_SECURITY_POLICY = {"DIRECTIVES" : {directive : True }}):
0 commit comments