diff --git a/setup.cfg b/setup.cfg index c18838f..98dc2b3 100644 --- a/setup.cfg +++ b/setup.cfg @@ -37,7 +37,7 @@ install_requires = pilkit [options.extras_require] -all = boto; brotli; feedgenerator; zopfli; cryptography; beautifulsoup4 +all = boto; brotli; feedgenerator; zopfli; cryptography tests = pytest; pytest-cov docs = Sphinx; alabaster diff --git a/sigal/plugins/encrypt/encrypt.py b/sigal/plugins/encrypt/encrypt.py index f3e1f5c..ee2ffc5 100644 --- a/sigal/plugins/encrypt/encrypt.py +++ b/sigal/plugins/encrypt/encrypt.py @@ -58,7 +58,6 @@ from sigal import signals from sigal.utils import url_from_path, copy from sigal.settings import get_thumb from click import progressbar -from bs4 import BeautifulSoup from .endec import encrypt, kdf_gen_key diff --git a/tests/sample/pictures/encryptTest/21.jpg b/tests/sample/pictures/encryptTest/21.jpg new file mode 100644 index 0000000..0b60190 Binary files /dev/null and b/tests/sample/pictures/encryptTest/21.jpg differ diff --git a/tests/sample/pictures/encryptTest/22.jpg b/tests/sample/pictures/encryptTest/22.jpg new file mode 100644 index 0000000..e2bd3d3 Binary files /dev/null and b/tests/sample/pictures/encryptTest/22.jpg differ diff --git a/tests/sample/sigal.conf.py b/tests/sample/sigal.conf.py index b07d771..4db648a 100644 --- a/tests/sample/sigal.conf.py +++ b/tests/sample/sigal.conf.py @@ -17,16 +17,7 @@ plugins = [ 'sigal.plugins.nomedia', 'sigal.plugins.watermark', 'sigal.plugins.zip_gallery', - 'sigal.plugins.encrypt' ] -encrypt_options = { - 'password': 'password', - 'ask_password': True, - 'gcm_tag': 'AuTheNTiCatIoNtAG', - 'kdf_salt': 'saltysaltsweetysweet', - 'kdf_iters': 10000, - 'encrypt_symlinked_originals': False -} copyright = '© An example copyright message' adjust_options = {'color': 0.9, 'brightness': 1.0, 'contrast': 1.0, 'sharpness': 0.0} diff --git a/tests/test_encrypt.py b/tests/test_encrypt.py new file mode 100644 index 0000000..8fa10da --- /dev/null +++ b/tests/test_encrypt.py @@ -0,0 +1,91 @@ +import os +import pickle +from io import BytesIO + +import pytest +from sigal import init_plugins +from sigal.gallery import Gallery +from sigal.plugins.encrypt import endec +from sigal.plugins.encrypt.encrypt import cache_key + +CURRENT_DIR = os.path.dirname(__file__) + + +@pytest.fixture() +def remove_cache(settings): + yield + cachepath = os.path.join(settings['destination'], ".encryptCache") + os.remove(cachepath) + +def get_key_tag(settings): + options = settings["encrypt_options"] + key = endec.kdf_gen_key( + options["password"].encode("utf-8"), + options["kdf_salt"].encode("utf-8"), + options["kdf_iters"] + ) + tag = options["gcm_tag"].encode("utf-8") + return (key, tag) + +def test_encrypt(settings, tmpdir, disconnect_signals, remove_cache): + settings['destination'] = str(tmpdir) + if "sigal.plugins.encrypt" not in settings["plugins"]: + settings['plugins'] += ["sigal.plugins.encrypt"] + + settings['encrypt_options'] = { + 'password': 'password', + 'ask_password': True, + 'gcm_tag': 'AuTheNTiCatIoNtAG', + 'kdf_salt': 'saltysaltsweetysweet', + 'kdf_iters': 10000, + 'encrypt_symlinked_originals': False + } + + init_plugins(settings) + gal = Gallery(settings) + gal.build() + + # check the encrypt cache exists + cachePath = os.path.join(settings["destination"], ".encryptCache") + assert os.path.isfile(cachePath) + + encryptCache = None + with open(cachePath, "rb") as cacheFile: + encryptCache = pickle.load(cacheFile) + assert isinstance(encryptCache, dict) + + testAlbum = gal.albums["encryptTest"] + key, tag = get_key_tag(settings) + + for media in testAlbum: + # check if sizes are stored in cache + assert cache_key(media) in encryptCache + assert "size" in encryptCache[cache_key(media)] + assert "thumb_size" in encryptCache[cache_key(media)] + assert "encrypted" in encryptCache[cache_key(media)] + + encryptedImages = [ + media.dst_path, + media.thumb_path + ] + if settings["keep_orig"]: + encryptedImages.append(os.path.join(settings["destination"], + media.path, media.big)) + + # check if images are encrypted by trying to decrypt + for image in encryptedImages: + with open(image, "rb") as infile: + with BytesIO() as outfile: + endec.decrypt(key, infile, outfile, tag) + + # check static files have been copied + static = os.path.join(settings["destination"], 'static') + assert os.path.isfile(os.path.join(static, "decrypt.js")) + assert os.path.isfile(os.path.join(static, "keycheck.txt")) + assert os.path.isfile(os.path.join(settings["destination"], "sw.js")) + + # check keycheck file + with open(os.path.join(settings["destination"], + 'static', "keycheck.txt"), "rb") as infile: + with BytesIO() as outfile: + endec.decrypt(key, infile, outfile, tag) diff --git a/tox.ini b/tox.ini index 96ef8eb..57777b3 100644 --- a/tox.ini +++ b/tox.ini @@ -39,7 +39,6 @@ usedevelop = true deps = feedgenerator cryptography - beautifulsoup4 commands = sigal build -c tests/sample/sigal.conf.py sigal serve tests/sample/_build