-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[CHANGE] Calculate SRI only when not in DEBUG
- Loading branch information
Showing
2 changed files
with
67 additions
and
18 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 |
---|---|---|
@@ -1,36 +1,84 @@ | ||
""" | ||
Tests for our template tags | ||
Test the apps' template tags | ||
""" | ||
|
||
# Django | ||
from django.template import Context, Template | ||
from django.test import TestCase | ||
from django.test import TestCase, override_settings | ||
|
||
# AA Time Zones | ||
from timezones import __version__ | ||
from timezones.helper.static_files import calculate_integrity_hash | ||
|
||
|
||
class TestVersionedStatic(TestCase): | ||
""" | ||
Test versioned static template tag | ||
Test timezones_static template tag | ||
""" | ||
|
||
def test_versioned_static(self): | ||
@override_settings(DEBUG=False) | ||
def test_versioned_static_without_debug_enabled(self) -> None: | ||
""" | ||
Test versioned static template tag | ||
Test versioned static template tag without DEBUG enabled | ||
:return: | ||
:rtype: | ||
""" | ||
|
||
context = Context({"version": __version__}) | ||
template_to_render = Template( | ||
"{% load timezones %}" "{% timezones_static 'css/timezones.min.css' %}" | ||
template_string=( | ||
"{% load timezones %}" | ||
"{% timezones_static 'css/timezones.min.css' %}" | ||
"{% timezones_static 'js/timezones.min.js' %}" | ||
) | ||
) | ||
|
||
rendered_template = template_to_render.render(context) | ||
rendered_template = template_to_render.render(context=context) | ||
|
||
expected_static_css_src = ( | ||
f'/static/timezones/css/timezones.min.css?v={context["version"]}' | ||
) | ||
expected_static_css_src_integrity = calculate_integrity_hash( | ||
"css/timezones.min.css" | ||
) | ||
expected_static_js_src = ( | ||
f'/static/timezones/js/timezones.min.js?v={context["version"]}' | ||
) | ||
expected_static_js_src_integrity = calculate_integrity_hash( | ||
"js/timezones.min.js" | ||
) | ||
|
||
self.assertIn(member=expected_static_css_src, container=rendered_template) | ||
self.assertIn( | ||
member=f'/static/timezones/css/timezones.min.css?v={context["version"]}', | ||
container=rendered_template, | ||
member=expected_static_css_src_integrity, container=rendered_template | ||
) | ||
self.assertIn(member=expected_static_js_src, container=rendered_template) | ||
self.assertIn( | ||
member=expected_static_js_src_integrity, container=rendered_template | ||
) | ||
|
||
@override_settings(DEBUG=True) | ||
def test_versioned_static_with_debug_enabled(self) -> None: | ||
""" | ||
Test versioned static template tag with DEBUG enabled | ||
:return: | ||
:rtype: | ||
""" | ||
|
||
context = Context({"version": __version__}) | ||
template_to_render = Template( | ||
template_string=( | ||
"{% load timezones %}" "{% timezones_static 'css/timezones.min.css' %}" | ||
) | ||
) | ||
|
||
rendered_template = template_to_render.render(context=context) | ||
|
||
expected_static_css_src = ( | ||
f'/static/timezones/css/timezones.min.css?v={context["version"]}' | ||
) | ||
|
||
self.assertIn(member=expected_static_css_src, container=rendered_template) | ||
self.assertNotIn(member="integrity=", container=rendered_template) |