-
Notifications
You must be signed in to change notification settings - Fork 18
Add csrf_token template tag #118
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
joshuadavidthomas
wants to merge
5
commits into
LilyFirefly:main
Choose a base branch
from
joshuadavidthomas:csrf_token
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
0531aba
Add csrf_token template tag
joshuadavidthomas fd0068f
oops forgot to commit this part
joshuadavidthomas 093e59e
add test
joshuadavidthomas aeade44
Apply suggestions from code review
joshuadavidthomas ce1b984
fix CI errors
joshuadavidthomas File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,133 @@ | ||
import warnings | ||
from django.template import engines | ||
from django.test import override_settings | ||
|
||
|
||
def test_csrf_token_basic(): | ||
template = "{% csrf_token %}" | ||
|
||
django_template = engines["django"].from_string(template) | ||
rust_template = engines["rusty"].from_string(template) | ||
|
||
context = {"csrf_token": "test123"} | ||
expected = '<input type="hidden" name="csrfmiddlewaretoken" value="test123">' | ||
|
||
assert django_template.render(context) == expected | ||
assert rust_template.render(context) == expected | ||
|
||
|
||
def test_csrf_token_not_provided(): | ||
template = "{% csrf_token %}" | ||
|
||
django_template = engines["django"].from_string(template) | ||
rust_template = engines["rusty"].from_string(template) | ||
|
||
context = {"csrf_token": "NOTPROVIDED"} | ||
|
||
assert django_template.render(context) == "" | ||
assert rust_template.render(context) == "" | ||
|
||
|
||
def test_csrf_token_missing(): | ||
template = "{% csrf_token %}" | ||
|
||
django_template = engines["django"].from_string(template) | ||
rust_template = engines["rusty"].from_string(template) | ||
|
||
assert django_template.render({}) == "" | ||
assert rust_template.render({}) == "" | ||
|
||
|
||
def test_csrf_token_escaping(): | ||
template = "{% csrf_token %}" | ||
|
||
django_template = engines["django"].from_string(template) | ||
rust_template = engines["rusty"].from_string(template) | ||
|
||
context = {"csrf_token": 'test"with<quotes>&'} | ||
|
||
django_result = django_template.render(context) | ||
rust_result = rust_template.render(context) | ||
|
||
assert django_result == rust_result | ||
assert 'value="test"with<quotes>&amp;"' in rust_result | ||
|
||
|
||
def test_csrf_token_empty_string(): | ||
template = "{% csrf_token %}" | ||
|
||
django_template = engines["django"].from_string(template) | ||
rust_template = engines["rusty"].from_string(template) | ||
|
||
context = {"csrf_token": ""} | ||
|
||
assert django_template.render(context) == "" | ||
assert rust_template.render(context) == "" | ||
|
||
|
||
def test_csrf_token_none_value(): | ||
template = "{% csrf_token %}" | ||
|
||
django_template = engines["django"].from_string(template) | ||
rust_template = engines["rusty"].from_string(template) | ||
|
||
context = {"csrf_token": None} | ||
|
||
assert django_template.render(context) == "" | ||
assert rust_template.render(context) == "" | ||
|
||
|
||
def test_csrf_token_numeric_value(): | ||
template = "{% csrf_token %}" | ||
django_template = engines["django"].from_string(template) | ||
rust_template = engines["rusty"].from_string(template) | ||
|
||
context = {"csrf_token": 12345} | ||
expected = '<input type="hidden" name="csrfmiddlewaretoken" value="12345">' | ||
|
||
assert django_template.render(context) == expected | ||
assert rust_template.render(context) == expected | ||
|
||
|
||
def test_csrf_token_zero_value(): | ||
template = "{% csrf_token %}" | ||
|
||
django_template = engines["django"].from_string(template) | ||
rust_template = engines["rusty"].from_string(template) | ||
|
||
context = {"csrf_token": 0} | ||
|
||
assert django_template.render(context) == "" | ||
assert rust_template.render(context) == "" | ||
|
||
|
||
def test_csrf_token_false_value(): | ||
template = "{% csrf_token %}" | ||
|
||
django_template = engines["django"].from_string(template) | ||
rust_template = engines["rusty"].from_string(template) | ||
|
||
context = {"csrf_token": False} | ||
|
||
assert django_template.render(context) == "" | ||
assert rust_template.render(context) == "" | ||
|
||
|
||
@override_settings(DEBUG=True) | ||
def test_csrf_token_missing_debug_warning(): | ||
template = "{% csrf_token %}" | ||
|
||
django_template = engines["django"].from_string(template) | ||
rust_template = engines["rusty"].from_string(template) | ||
|
||
expected = "A {% csrf_token %} was used in a template, but the context did not provide the value. This is usually caused by not using RequestContext." | ||
|
||
with warnings.catch_warnings(record=True) as w: | ||
warnings.simplefilter("always") | ||
assert django_template.render({}) == "" | ||
assert str(w[0].message) == expected | ||
|
||
with warnings.catch_warnings(record=True) as w: | ||
warnings.simplefilter("always") | ||
assert rust_template.render({}) == "" | ||
assert str(w[0].message) == expected |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the main tests should add
csrf_token
via the context processor.This means these tests will need some refactoring to ensure the context processors are run for both Python and Rust engines.
We should also have extra tests that also set it explicitly like this.