Skip to content

Commit

Permalink
Update tests to use new multi-policy format
Browse files Browse the repository at this point in the history
  • Loading branch information
DylanYoung committed Mar 9, 2020
1 parent ece24ad commit d9f5590
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 14 deletions.
20 changes: 14 additions & 6 deletions csp/tests/test_decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def view_without_decorator(request):
def view_with_decorator(request):
return HttpResponse()
response = view_with_decorator(REQUEST)
assert response._csp_update == {'img-src': 'bar.com'}
assert dict(response._csp_update) == {'default': {'img-src': ['bar.com']}}
mw.process_response(REQUEST, response)
policy_list = sorted(response['Content-Security-Policy'].split("; "))
assert policy_list == ["default-src 'self'", "img-src foo.com bar.com"]
Expand All @@ -56,7 +56,7 @@ def view_without_decorator(request):
def view_with_decorator(request):
return HttpResponse()
response = view_with_decorator(REQUEST)
assert response._csp_replace == {'img-src': 'bar.com'}
assert dict(response._csp_replace) == {'default': {'img-src': ['bar.com']}}
mw.process_response(REQUEST, response)
policy_list = sorted(response['Content-Security-Policy'].split("; "))
assert policy_list == ["default-src 'self'", "img-src bar.com"]
Expand Down Expand Up @@ -87,8 +87,12 @@ def view_without_decorator(request):
def view_with_decorator(request):
return HttpResponse()
response = view_with_decorator(REQUEST)
assert response._csp_config == \
{'img-src': ['foo.com'], 'font-src': ['bar.com']}
assert response._csp_config == {
policy_names.last_policy_name: {
'img-src': ['foo.com'],
'font-src': ['bar.com'],
}
}
mw.process_response(REQUEST, response)
policy_list = sorted(response['Content-Security-Policy'].split("; "))
assert policy_list == ["font-src bar.com", "img-src foo.com"]
Expand All @@ -105,8 +109,12 @@ def test_csp_string_values():
def view_with_decorator(request):
return HttpResponse()
response = view_with_decorator(REQUEST)
assert response._csp_config == \
{'img-src': ['foo.com'], 'font-src': ['bar.com']}
assert dict(response._csp_config) == {
policy_names.last_policy_name: {
'img-src': ['foo.com'],
'font-src': ['bar.com'],
}
}
mw.process_response(REQUEST, response)
policy_list = sorted(response['Content-Security-Policy'].split("; "))
assert policy_list == ["font-src bar.com", "img-src foo.com"]
8 changes: 6 additions & 2 deletions csp/tests/test_middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,19 @@ def test_dont_replace():
def test_use_config():
request = rf.get('/')
response = HttpResponse()
response._csp_config = {'default-src': ['example.com']}
response._csp_config = {'default': {
'default-src': ['example.com'],
}}
mw.process_response(request, response)
assert response[HEADER] == 'default-src example.com'


def test_use_update():
request = rf.get('/')
response = HttpResponse()
response._csp_update = {'default-src': ['example.com']}
response._csp_update = {'default': {
'default-src': ['example.com']
}}
mw.process_response(request, response)
assert response[HEADER] == "default-src 'self' example.com"

Expand Down
19 changes: 13 additions & 6 deletions csp/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,22 @@
from csp.utils import build_policy


def policy_eq(a, b, msg='%r != %r'):
parts_a = sorted(a.split('; '))
parts_b = sorted(b.split('; '))
assert parts_a == parts_b, msg % (a, b)
def policy_eq(a, b, msg='%r != %r', report_only=False):
if not isinstance(a, list):
a = [(a, report_only)]
if not isinstance(a, list):
b = [(b, report_only)]

for csp_a, csp_b in zip(a, b):
parts_a = sorted(csp_a[0].split('; '))
parts_b = sorted(csp_b[0].split('; '))
assert csp_a[1] == csp_b[1]
assert parts_a == parts_b, msg % (a, b)


def test_empty_policy():
policy = build_policy()
assert "default-src 'self'" == policy
assert [("default-src 'self'", False)] == policy


def literal(s):
Expand All @@ -30,7 +37,7 @@ def literal(s):
@override_settings(CSP_DEFAULT_SRC=['example.com', 'example2.com'])
def test_default_src():
policy = build_policy()
assert 'default-src example.com example2.com' == policy
assert [('default-src example.com example2.com', False)] == policy


@override_settings(CSP_SCRIPT_SRC=['example.com'])
Expand Down

0 comments on commit d9f5590

Please sign in to comment.