-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Escape dictionary curly bracket before renderingin HTML (#42)
* Escape dictionary curly bracket before renderingin HTML * use mark_safe instead of format_html * add line * Use mark_safe one the html has been already escaped * no need to mark_safe a string * Add some basic tests for render_diff * add another case * add test docstring
- Loading branch information
Showing
2 changed files
with
71 additions
and
7 deletions.
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,64 @@ | ||
"""Test Render_diff templatetags.""" | ||
import unittest | ||
from nautobot_ssot.templatetags.render_diff import render_diff | ||
|
||
|
||
test_params = [ | ||
( | ||
{ | ||
"region": { | ||
"Catalonia": {"+": {"parent_name": None}, "-": {"parent_name": "Europe"}}, | ||
} | ||
}, | ||
'<ul><li>region<ul><li class="diff-changed">Catalonia<ul><li class="diff-added">parent_name: None</li><li class="diff-subtracted">parent_name: Europe</li></ul></li></ul></li></ul>', | ||
), | ||
( | ||
{ | ||
"region": { | ||
"Barcelona": { | ||
"+": { | ||
"cfs": {"asw_owner": ""}, | ||
"slug": "barcelona", | ||
"description": "", | ||
"parent_name": "Catalonia", | ||
} | ||
}, | ||
} | ||
}, | ||
'<ul><li>region<ul><li class="diff-added">Barcelona<ul><li class="diff-added">cfs: {'asw_owner': ''}</li><li class="diff-added">slug: barcelona</li><li class="diff-added">description: </li><li class="diff-added">parent_name: Catalonia</li></ul></li></ul></li></ul>', | ||
), | ||
( | ||
{ | ||
"model_name": { | ||
"element": { | ||
"-": { | ||
"cfs": {"this is a XSS": "<script>alert(document.cookie)</script>"}, | ||
} | ||
}, | ||
} | ||
}, | ||
'<ul><li>model_name<ul><li class="diff-subtracted">element<ul><li class="diff-subtracted">cfs: {'this is a XSS': '<script>alert(document.cookie)</script>'}</li></ul></li></ul></li></ul>', | ||
), | ||
( | ||
{ | ||
"model_name": { | ||
"element": { | ||
"-": { | ||
"description": "<script>alert(document.cookie)</script>", | ||
} | ||
}, | ||
} | ||
}, | ||
'<ul><li>model_name<ul><li class="diff-subtracted">element<ul><li class="diff-subtracted">description: <script>alert(document.cookie)</script></li></ul></li></ul></li></ul>', | ||
), | ||
] | ||
|
||
|
||
class TestRenderDiff(unittest.TestCase): | ||
"""Tests for render_diff function.""" | ||
|
||
def test_render_diff_as_expected(self): | ||
"""Testing expected escaped and rendered HTML.""" | ||
for input_dict, rendered_diff in test_params: | ||
with self.subTest(): | ||
self.assertEqual(render_diff(input_dict), rendered_diff) |