Skip to content

Commit

Permalink
simplify tests and handling of js versions
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelgrinberg committed May 19, 2019
1 parent d87eaf8 commit 1b273c4
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 70 deletions.
26 changes: 18 additions & 8 deletions flask_moment.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,23 @@
from jinja2 import Markup
from flask import current_app

# //code.jquery.com/jquery-3.4.1.min.js
default_jquery_version = '3.4.1'
default_jquery_sri = 'sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo='

# //cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment-with-locales.min.js
default_moment_version = '2.24.0'
default_moment_sri = 'sha256-AdQN98MVZs44Eq2yTwtoKufhnU+uZ7v2kXnD5vqzZVo='


class _moment(object):
@staticmethod
def include_moment(version='2.18.1', local_js=None, no_js=None, sri=None):
def include_moment(version=default_moment_version, local_js=None,
no_js=None, sri=None):
js = ''
if version == '2.18.1' and local_js is None and sri is None:
sri = ('sha384-iMhq1oHAQWG7+cVzHBvYynTbGZy'
'O4DniLR7bhY1Q39AMn8ePTV9uByV/06g2xqOS')
if version == default_moment_version and local_js is None and \
sri is None:
sri = default_moment_sri
if not no_js:
if local_js is not None:
if not sri:
Expand Down Expand Up @@ -53,11 +62,12 @@ def include_moment(version='2.18.1', local_js=None, no_js=None, sri=None):
</script>''' % js) # noqa: E501

@staticmethod
def include_jquery(version='2.1.0', local_js=None, sri=None):
def include_jquery(version=default_jquery_version, local_js=None,
sri=None):
js = ''
if sri is None and version == '2.1.0' and local_js is None:
sri = ('sha384-85/BFduEdDxQ86xztyNu4BBkVZmlv'
'u+iB7zhBu0VoYdq+ODs3PKpU6iVE3ZqPMut')
if sri is None and version == default_jquery_version and \
local_js is None:
sri = default_jquery_sri
if local_js is not None:
if not sri:
js = '<script src="%s"></script>\n' % local_js
Expand Down
83 changes: 21 additions & 62 deletions tests/test_flask_moment.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,8 @@
from datetime import datetime
from flask import render_template_string
from flask_moment import _moment, Moment
from jinja2 import Markup
import hashlib
import base64
import re

# Python 2 and 3 compatibility
try:
import urllib.request as request
except ImportError:
import urllib as request
from flask_moment import _moment, Moment, default_jquery_version, \
default_moment_version, default_moment_sri


# Mock Objects
Expand Down Expand Up @@ -71,7 +63,8 @@ def test_include_moment_directly(self):

assert isinstance(include_moment, Markup)
assert "<script" in str(include_moment)
assert "2.18.1/moment-with-locales.min.js" in str(include_moment)
assert default_moment_version + "/moment-with-locales.min.js" in str(
include_moment)

def test_include_moment_with_different_version_directly(self):
include_moment = _moment.include_moment(version="2.17.1")
Expand All @@ -92,14 +85,15 @@ def test_include_moment_renders_properly(self, app, moment):
ts = str(render_template_string("{{ moment.include_moment() }}"))

assert "<script" in ts
assert "2.18.1/moment-with-locales.min.js" in str(ts)
assert default_moment_version + "/moment-with-locales.min.js" in str(
ts)

def test_include_jquery_default(self):
include_jquery = _moment.include_jquery()

assert isinstance(include_jquery, Markup)
assert all([each in str(include_jquery) for each in [
'code.jquery.com', '2.1.0']])
'code.jquery.com', default_jquery_version]])

def test_include_jquery_local(self):
include_jquery = _moment.include_jquery(local_js=True)
Expand Down Expand Up @@ -262,7 +256,7 @@ def test_jquery_with_default_version(self):
include_jquery = _moment.include_jquery()

assert 'src=\"' in include_jquery
assert 'integrity=\"sha384' in include_jquery
assert 'integrity=\"sha' in include_jquery
assert 'crossorigin=\"anonymous\"' in include_jquery

def test_jquery_from_cdn_without_custom_sri_hash(self):
Expand Down Expand Up @@ -328,31 +322,27 @@ def _check_assertions():
def test_moment_with_default_version(self):
include_moment = _moment.include_moment()

assert include_moment.startswith('<script src="//cdnjs.cloudflare.com'
'/ajax/libs/moment.js/2.18.1/moment-'
'with-locales.min.js" integrity='
'"sha384-iMhq1oHAQWG7+cVzHBvYynTbGZ'
'yO4DniLR7bhY1Q39AMn8ePTV9uByV/06g2xq'
'OS" crossorigin="anonymous">'
'</script>')
assert include_moment.startswith(
'<script src="//cdnjs.cloudflare.com/ajax/libs/moment.js/{}'
'/moment-with-locales.min.js" integrity="{}" '
'crossorigin="anonymous"></script>'.format(
default_moment_version, default_moment_sri))

def test_moment_from_cdn_with_custom_sri_hash(self):
include_moment = _moment.include_moment(sri='sha384-12345678')

assert include_moment.startswith('<script src="//cdnjs.cloudflare.com'
'/ajax/libs/moment.js/2.18.1/moment-'
'with-locales.min.js" integrity='
'"sha384-12345678" crossorigin='
'"anonymous"></script>')
assert include_moment.startswith(
'<script src="//cdnjs.cloudflare.com/ajax/libs/moment.js/{}'
'/moment-with-locales.min.js" integrity="sha384-12345678" '
'crossorigin="anonymous"></script>'.format(default_moment_version))

include_moment = _moment.include_moment(version='2.0.0',
sri='sha384-12345678')

assert include_moment.startswith('<script src="//cdnjs.cloudflare.com'
'/ajax/libs/moment.js/2.0.0/moment-'
'with-langs.min.js" integrity="sha384'
'-12345678" crossorigin="anonymous">'
'</script>')
assert include_moment.startswith(
'<script src="//cdnjs.cloudflare.com/ajax/libs/moment.js/2.0.0'
'/moment-with-langs.min.js" integrity="sha384-12345678" '
'crossorigin="anonymous"></script>')

def test_moment_local(self):
include_moment = _moment.include_moment(local_js=True)
Expand Down Expand Up @@ -389,34 +379,3 @@ def test_disabling_moment_custom_version(self):
assert 'src=\"' in include_moment
assert 'integrity=\"' not in include_moment
assert 'crossorigin' not in include_moment

def test_default_hash_values(self):
def _sri_hash(data):
h = hashlib.sha384(data).digest()
h_64 = base64.b64encode(h).decode()
return 'sha384-{}'.format(h_64)

def _get_data(url):
response = request.urlopen(url)
data = response.read()
return data

pattern = 'integrity=\"(.+?)\"'
include_jquery = _moment.include_jquery()
include_moment = _moment.include_moment()

# JQUERY
h_64 = _sri_hash(
_get_data('https://code.jquery.com/jquery-2.1.0.min.js'))

m = re.search(pattern, include_jquery)

assert m.group(1) == h_64

# MOMENT
h_64 = _sri_hash(
_get_data('https://cdnjs.cloudflare.com/ajax/libs/moment.js'
'/2.18.1/moment-with-locales.min.js'))

m = re.search(pattern, include_moment)
assert m.group(1) == h_64

0 comments on commit 1b273c4

Please sign in to comment.