-
-
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.
Merge pull request #162 from ppfeufer/integrity-hash-calculation
- Loading branch information
Showing
10 changed files
with
170 additions
and
50 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
Empty file.
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,40 @@ | ||
""" | ||
Helper functions for static integrity calculations | ||
""" | ||
|
||
# Standard Library | ||
import os | ||
from pathlib import Path | ||
|
||
# Third Party | ||
from sri import Algorithm, calculate_integrity | ||
|
||
# Alliance Auth | ||
from allianceauth.services.hooks import get_extension_logger | ||
|
||
# Alliance Auth (External Libs) | ||
from app_utils.logging import LoggerAddTag | ||
|
||
# AA Time Zones | ||
from timezones import __title__ | ||
from timezones.constants import AA_TIMEZONES_STATIC_DIR | ||
|
||
logger = LoggerAddTag(my_logger=get_extension_logger(__name__), prefix=__title__) | ||
|
||
|
||
def calculate_integrity_hash(relative_file_path: str) -> str: | ||
""" | ||
Calculates the integrity hash for a given static file | ||
:param self: | ||
:type self: | ||
:param relative_file_path: The file path relative to the `aa-timezones/timezones/static/timezones` folder | ||
:type relative_file_path: str | ||
:return: The integrity hash | ||
:rtype: str | ||
""" | ||
|
||
file_path = os.path.join(AA_TIMEZONES_STATIC_DIR, relative_file_path) | ||
integrity_hash = calculate_integrity(Path(file_path), Algorithm.SHA512) | ||
|
||
return integrity_hash |
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,8 +1,3 @@ | ||
{% load timezones %} | ||
|
||
<link | ||
rel="stylesheet" | ||
href="{% timezones_static 'timezones/css/timezones.min.css' %}" | ||
integrity="sha512-8lYk4QlNTYSaxTlTWH4Z6Qslhf+NrgoxzD78IzMN+//b9WwMdQWzFWWrqX5dl7z/jQHU1hg38fYrm2F0+1KVEg==" | ||
crossorigin="anonymous" | ||
> | ||
{% timezones_static 'css/timezones.min.css' %} |
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,7 +1,3 @@ | ||
{% load timezones %} | ||
|
||
<script | ||
src="{% timezones_static 'timezones/js/timezones.min.js' %}" | ||
integrity="sha512-p/T1Wmq0lWiYMNBbODA7yXFsFtCzugX8mEsh9Z9S8rlMP3sx6GRvP3q4mhjnnk3Y8h76HaGcKh6kJzV1QW/ZmA==" | ||
crossorigin="anonymous" | ||
></script> | ||
{% timezones_static 'js/timezones.min.js' %} |
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,7 +1,3 @@ | ||
{% load static %} | ||
{% load timezones %} | ||
|
||
<script | ||
src="{% static 'timezones/libs/jquery-timeago/1.6.7/jquery.timeago.min.js' %}" | ||
integrity="sha512-Fa5vf0oXzZKvD2EWgLQaebZ7X+7IMDAbdCvOjEivYkFLc830J6x7jYy7N9g+Rz0rVx4UZA7xSUboc14jSrsKVA==" | ||
crossorigin="anonymous" | ||
></script> | ||
{% timezones_static 'libs/jquery-timeago/1.6.7/jquery.timeago.min.js' %} |
8 changes: 2 additions & 6 deletions
8
timezones/templates/timezones/bundles/moment-timezone-js.html
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,7 +1,3 @@ | ||
{% load static %} | ||
{% load timezones %} | ||
|
||
<script | ||
src="{% static 'timezones/libs/moment-timezone/0.5.36/moment-timezone-with-data-1970-2030.min.js' %}" | ||
integrity="sha512-HdWVl8NTC8n0bl0Ypuw1baRuZvO4bUcnGWhLTiI9yiisXzHbgKxlrmf2AMaa3yaXsi8GVzwonsASxsLs7ffAFw==" | ||
crossorigin="anonymous" | ||
></script> | ||
{% timezones_static 'libs/moment-timezone/0.5.36/moment-timezone-with-data-1970-2030.min.js' %} |
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,8 +1,3 @@ | ||
{% load static %} | ||
{% load timezones %} | ||
|
||
<link | ||
rel="stylesheet" | ||
href="{% static 'timezones/libs/weather-icons/2.0.10/css/weather-icons.min.css' %}" | ||
integrity="sha512-DIFdHjUUNaRvJdlgqogTC0kqKpSBBLaa/4cFiwj/WJKaAQCJLBmEuRHddEeJOiTJ1hynZ6/6ZH4ouPhcep+E+g==" | ||
crossorigin="anonymous" | ||
> | ||
{% timezones_static 'libs/weather-icons/2.0.10/css/weather-icons.min.css' %} |
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,37 +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 'timezones/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) | ||
|
||
self.assertInHTML( | ||
needle=f'/static/timezones/css/timezones.min.css?v={context["version"]}', | ||
haystack=rendered_template, | ||
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=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) |