-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bc05a0e
commit ebd3826
Showing
6 changed files
with
72 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
<div class="inner-component"> | ||
{% csrf_token %} | ||
{{ contents }} | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<div class="outer"> | ||
{% includecontents "test_csrf/inner.html" %} | ||
Testing CSRF token passing | ||
{% endincludecontents %} | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<div class="component"> | ||
<include:csrf>Testing CSRF in component</include:csrf> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
<div class="inner"> | ||
{% csrf_token %} | ||
{{ contents }} | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
from django.middleware.csrf import get_token | ||
from django.template.loader import render_to_string | ||
|
||
|
||
def test_csrf_token_passing(rf): | ||
"""Test that CSRF token is properly passed through includecontents.""" | ||
request = rf.get("/") | ||
csrf_token = get_token(request) | ||
|
||
context = { | ||
"request": request, | ||
"csrf_token": csrf_token, | ||
} | ||
|
||
rendered = render_to_string("test_csrf/base.html", context) | ||
assert csrf_token in rendered | ||
assert '<input type="hidden" name="csrfmiddlewaretoken"' in rendered | ||
|
||
|
||
def test_csrf_token_in_component(rf): | ||
"""Test that CSRF token works in component-style includecontents.""" | ||
request = rf.get("/") | ||
csrf_token = get_token(request) | ||
|
||
context = { | ||
"request": request, | ||
"csrf_token": csrf_token, | ||
} | ||
|
||
rendered = render_to_string("test_csrf/component.html", context) | ||
assert csrf_token in rendered | ||
assert '<input type="hidden" name="csrfmiddlewaretoken"' in rendered | ||
|
||
|
||
def test_csrf_token_isolated_context(rf): | ||
"""Test that CSRF token is passed even with isolated context.""" | ||
request = rf.get("/") | ||
csrf_token = get_token(request) | ||
|
||
context = { | ||
"request": request, | ||
"csrf_token": csrf_token, | ||
"isolated_context": True, | ||
} | ||
|
||
rendered = render_to_string("test_csrf/base.html", context) | ||
assert csrf_token in rendered | ||
assert '<input type="hidden" name="csrfmiddlewaretoken"' in rendered |