Skip to content

Commit

Permalink
[CHANGE] Calculate SRI only when not in DEBUG
Browse files Browse the repository at this point in the history
  • Loading branch information
ppfeufer committed Jan 21, 2025
1 parent b9393f3 commit 3c5b789
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 18 deletions.
19 changes: 10 additions & 9 deletions timezones/templatetags/timezones.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import os

# Django
from django.conf import settings
from django.template.defaulttags import register
from django.templatetags.static import static
from django.utils.safestring import mark_safe
Expand Down Expand Up @@ -44,10 +45,16 @@ def timezones_static(relative_file_path: str) -> str | None:
if file_type not in ["css", "js"]:
raise ValueError(f"Unsupported file type: {file_type}")

Check warning on line 46 in timezones/templatetags/timezones.py

View check run for this annotation

Codecov / codecov/patch

timezones/templatetags/timezones.py#L46

Added line #L46 was not covered by tests

integrity_hash = calculate_integrity_hash(relative_file_path)
static_file_path = os.path.join("timezones", relative_file_path)
static_url = static(static_file_path)

# Integrity hash calculation only for non-debug mode
sri_string = (
f' integrity="{calculate_integrity_hash(relative_file_path)}" crossorigin="anonymous"'
if not settings.DEBUG
else ""
)

# Versioned URL for CSS and JS files
# Add version query parameter to break browser caches when changing the app version
# Do not add version query parameter for libs as they are already versioned through their file path
Expand All @@ -59,16 +66,10 @@ def timezones_static(relative_file_path: str) -> str | None:

# Return the versioned URL with integrity hash for CSS
if file_type == "css":
logger.debug(f"Integrity hash for {relative_file_path}: {integrity_hash}")

return mark_safe(
f'<link rel="stylesheet" href="{versioned_url}" integrity="{integrity_hash}" crossorigin="anonymous">'
)
return mark_safe(f'<link rel="stylesheet" href="{versioned_url}"{sri_string}>')

# Return the versioned URL with integrity hash for JS files
if file_type == "js":
return mark_safe(
f'<script src="{versioned_url}" integrity="{integrity_hash}" crossorigin="anonymous"></script>'
)
return mark_safe(f'<script src="{versioned_url}"{sri_string}></script>')

return None

Check warning on line 75 in timezones/templatetags/timezones.py

View check run for this annotation

Codecov / codecov/patch

timezones/templatetags/timezones.py#L75

Added line #L75 was not covered by tests
66 changes: 57 additions & 9 deletions timezones/tests/test_templatetags.py
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)

0 comments on commit 3c5b789

Please sign in to comment.