2016-02-10 15:47:00 +00:00
|
|
|
|
# coding=utf-8
|
|
|
|
|
|
|
|
|
|
|
|
from app.asset_fingerprinter import AssetFingerprinter
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class TestAssetFingerprint(object):
|
2017-07-07 14:16:18 +01:00
|
|
|
|
def test_url_format(self, mocker):
|
|
|
|
|
|
get_file_content_mock = mocker.patch.object(AssetFingerprinter, 'get_asset_file_contents')
|
2016-02-10 15:47:00 +00:00
|
|
|
|
get_file_content_mock.return_value = """
|
|
|
|
|
|
body {
|
|
|
|
|
|
font-family: nta;
|
|
|
|
|
|
}
|
2018-11-29 14:28:29 +00:00
|
|
|
|
""".encode('utf-8')
|
2016-02-10 15:47:00 +00:00
|
|
|
|
asset_fingerprinter = AssetFingerprinter(
|
|
|
|
|
|
asset_root='/suppliers/static/'
|
|
|
|
|
|
)
|
|
|
|
|
|
assert (
|
|
|
|
|
|
asset_fingerprinter.get_url('application.css') ==
|
|
|
|
|
|
'/suppliers/static/application.css?418e6f4a6cdf1142e45c072ed3e1c90a' # noqa
|
|
|
|
|
|
)
|
|
|
|
|
|
assert (
|
|
|
|
|
|
asset_fingerprinter.get_url('application-ie6.css') ==
|
|
|
|
|
|
'/suppliers/static/application-ie6.css?418e6f4a6cdf1142e45c072ed3e1c90a' # noqa
|
|
|
|
|
|
)
|
|
|
|
|
|
|
2017-07-07 14:16:18 +01:00
|
|
|
|
def test_building_file_path(self, mocker):
|
|
|
|
|
|
get_file_content_mock = mocker.patch.object(AssetFingerprinter, 'get_asset_file_contents')
|
2016-02-10 15:47:00 +00:00
|
|
|
|
get_file_content_mock.return_value = """
|
|
|
|
|
|
document.write('Hello world!');
|
2018-11-29 14:28:29 +00:00
|
|
|
|
""".encode('utf-8')
|
2016-02-10 15:47:00 +00:00
|
|
|
|
fingerprinter = AssetFingerprinter()
|
|
|
|
|
|
fingerprinter.get_url('javascripts/application.js')
|
|
|
|
|
|
fingerprinter.get_asset_file_contents.assert_called_with(
|
|
|
|
|
|
'app/static/javascripts/application.js'
|
|
|
|
|
|
)
|
|
|
|
|
|
|
2017-07-07 14:16:18 +01:00
|
|
|
|
def test_hashes_are_consistent(self, mocker):
|
|
|
|
|
|
get_file_content_mock = mocker.patch.object(AssetFingerprinter, 'get_asset_file_contents')
|
2016-02-10 15:47:00 +00:00
|
|
|
|
get_file_content_mock.return_value = """
|
|
|
|
|
|
body {
|
|
|
|
|
|
font-family: nta;
|
|
|
|
|
|
}
|
2018-11-29 14:28:29 +00:00
|
|
|
|
""".encode('utf-8')
|
2016-02-10 15:47:00 +00:00
|
|
|
|
asset_fingerprinter = AssetFingerprinter()
|
|
|
|
|
|
assert (
|
|
|
|
|
|
asset_fingerprinter.get_asset_fingerprint('application.css') ==
|
|
|
|
|
|
asset_fingerprinter.get_asset_fingerprint('same_contents.css')
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def test_hashes_are_different_for_different_files(
|
2017-07-07 14:16:18 +01:00
|
|
|
|
self, mocker
|
2016-02-10 15:47:00 +00:00
|
|
|
|
):
|
2017-07-07 14:16:18 +01:00
|
|
|
|
get_file_content_mock = mocker.patch.object(AssetFingerprinter, 'get_asset_file_contents')
|
2016-02-10 15:47:00 +00:00
|
|
|
|
asset_fingerprinter = AssetFingerprinter()
|
|
|
|
|
|
get_file_content_mock.return_value = """
|
|
|
|
|
|
body {
|
|
|
|
|
|
font-family: nta;
|
|
|
|
|
|
}
|
2018-11-29 14:28:29 +00:00
|
|
|
|
""".encode('utf-8')
|
2016-02-10 15:47:00 +00:00
|
|
|
|
css_hash = asset_fingerprinter.get_asset_fingerprint('application.css')
|
|
|
|
|
|
get_file_content_mock.return_value = """
|
|
|
|
|
|
document.write('Hello world!');
|
2018-11-29 14:28:29 +00:00
|
|
|
|
""".encode('utf-8')
|
2016-02-10 15:47:00 +00:00
|
|
|
|
js_hash = asset_fingerprinter.get_asset_fingerprint('application.js')
|
|
|
|
|
|
assert (
|
|
|
|
|
|
js_hash != css_hash
|
|
|
|
|
|
)
|
|
|
|
|
|
|
2017-07-07 14:16:18 +01:00
|
|
|
|
def test_hash_gets_cached(self, mocker):
|
|
|
|
|
|
get_file_content_mock = mocker.patch.object(AssetFingerprinter, 'get_asset_file_contents')
|
2016-02-10 15:47:00 +00:00
|
|
|
|
get_file_content_mock.return_value = """
|
|
|
|
|
|
body {
|
|
|
|
|
|
font-family: nta;
|
|
|
|
|
|
}
|
2018-11-29 14:28:29 +00:00
|
|
|
|
""".encode('utf-8')
|
2016-02-10 15:47:00 +00:00
|
|
|
|
fingerprinter = AssetFingerprinter()
|
|
|
|
|
|
assert (
|
|
|
|
|
|
fingerprinter.get_url('application.css') ==
|
|
|
|
|
|
'/static/application.css?418e6f4a6cdf1142e45c072ed3e1c90a'
|
|
|
|
|
|
)
|
|
|
|
|
|
fingerprinter._cache[
|
|
|
|
|
|
'application.css'
|
|
|
|
|
|
] = 'a1a1a1'
|
|
|
|
|
|
assert (
|
|
|
|
|
|
fingerprinter.get_url('application.css') ==
|
|
|
|
|
|
'a1a1a1'
|
|
|
|
|
|
)
|
|
|
|
|
|
fingerprinter.get_asset_file_contents.assert_called_once_with(
|
|
|
|
|
|
'app/static/application.css'
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class TestAssetFingerprintWithUnicode(object):
|
|
|
|
|
|
def test_can_read_self(self):
|
2017-10-18 14:51:26 +01:00
|
|
|
|
'Ralph’s apostrophe is a string containing a unicode character'
|
2016-02-10 15:47:00 +00:00
|
|
|
|
AssetFingerprinter(filesystem_path='tests/app/main/').get_url('test_asset_fingerprinter.py')
|