コード例 #1
0
def test_boolean_directive():
    policy = ContentSecurityPolicy()

    policy.block_all_mixed_content = True
    assert policy.text == "block-all-mixed-content"

    policy.block_all_mixed_content = False
    assert policy.text == ""

    with pytest.raises(TypeError):
        policy.block_all_mixed_content = None
コード例 #2
0
def test_singevalue_directive():
    policy = ContentSecurityPolicy()

    policy.sandbox = "allow-forms"
    assert policy.text == "sandbox allow-forms"

    policy.sandbox = ""
    assert policy.text == ""

    with pytest.raises(TypeError):
        policy.sandbox = None
コード例 #3
0
def test_multiple_directives():
    policy = ContentSecurityPolicy()

    policy.default_src.add("https://example.org")
    policy.default_src.add("https://foobar.org")
    policy.sandbox = "allow-forms"
    policy.block_all_mixed_content = True

    assert policy.text == (
        "block-all-mixed-content;"
        "default-src https://example.org https://foobar.org;"
        "sandbox allow-forms")
コード例 #4
0
def test_copy_directive():
    policy = ContentSecurityPolicy()
    assert policy.text == ""

    policy.default_src.add("https://example.org")
    assert policy.text == "default-src https://example.org"

    copied = policy.copy()
    assert copied.text == "default-src https://example.org"

    copied.default_src.clear()
    assert copied.text == ""
    assert policy.text == "default-src https://example.org"
コード例 #5
0
def test_multivalue_directive():
    policy = ContentSecurityPolicy()
    assert policy.text == ""

    policy.default_src.add("https://example.org")
    assert policy.text == "default-src https://example.org"

    policy.default_src.add("https://foobar.org")
    assert policy.text == "default-src https://example.org https://foobar.org"

    policy.default_src.clear()
    assert policy.text == ""

    policy.default_src = set()
    assert policy.text == ""

    with pytest.raises(TypeError):
        policy.default_src = []
コード例 #6
0
def test_policy_initialisation():
    policy = ContentSecurityPolicy(default_src={"https://example.org"})
    assert policy.text == "default-src https://example.org"

    policy = ContentSecurityPolicy(**{"default-src": {"https://example.org"}})
    assert policy.text == "default-src https://example.org"
コード例 #7
0
 def default_policy():
     return ContentSecurityPolicy(default_src={SELF})
コード例 #8
0
 def default_policy():
     return ContentSecurityPolicy(report_only=True, default_src={SELF})