Files
sigal/tests/test_encrypt.py

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

103 lines
3.2 KiB
Python
Raw Normal View History

2020-04-13 16:34:40 +08:00
import os
import pickle
from io import BytesIO
2022-08-15 20:21:19 +02:00
import pytest
2020-04-13 16:34:40 +08:00
from sigal.gallery import Gallery
from sigal.plugins.encrypt import endec
from sigal.plugins.encrypt.encrypt import cache_key
2023-07-15 12:58:10 +02:00
from sigal.utils import init_plugins
2024-02-22 11:42:27 -06:00
from sigal.writer import AbstractWriter
2020-04-13 16:34:40 +08:00
CURRENT_DIR = os.path.dirname(__file__)
def get_key_tag(settings):
options = settings["encrypt_options"]
key = endec.kdf_gen_key(
options["password"], options["kdf_salt"], options["kdf_iters"]
2020-04-13 16:34:40 +08:00
)
tag = options["gcm_tag"].encode("utf-8")
return (key, tag)
2021-07-06 22:02:48 +02:00
2022-08-15 20:21:19 +02:00
def test_encrypt(settings, tmpdir, disconnect_signals, caplog):
2023-07-15 17:07:07 +02:00
settings["source"] = os.path.join(settings["source"], "encryptTest")
2023-02-12 17:31:08 +01:00
settings["destination"] = str(tmpdir)
2023-07-15 17:07:07 +02:00
settings["plugins"] = ["sigal.plugins.encrypt"]
2020-04-13 16:34:40 +08:00
2022-08-15 20:21:19 +02:00
init_plugins(settings)
2023-07-15 17:07:07 +02:00
gal = Gallery(settings, ncpu=1)
2022-08-15 20:21:19 +02:00
with pytest.raises(ValueError, match="no encrypt_options in settings"):
gal.build()
2023-02-12 17:31:08 +01:00
settings["encrypt_options"] = {}
2022-08-15 20:21:19 +02:00
2023-07-15 17:07:07 +02:00
gal = Gallery(settings, ncpu=1)
2022-08-15 20:21:19 +02:00
with pytest.raises(ValueError, match="no password provided"):
gal.build()
2023-02-12 17:31:08 +01:00
settings["encrypt_options"] = {
"password": "password",
"ask_password": True,
"encrypt_symlinked_originals": False,
2020-04-13 16:34:40 +08:00
}
2023-07-15 17:07:07 +02:00
gal = Gallery(settings, ncpu=1)
2020-04-13 16:34:40 +08:00
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)
2023-07-15 17:07:07 +02:00
testAlbum = gal.albums["."]
2020-04-13 16:34:40 +08:00
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)
2021-07-06 22:02:48 +02:00
)
2020-04-13 16:34:40 +08:00
# 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
2023-02-12 17:31:08 +01:00
static = os.path.join(settings["destination"], "static")
2020-04-13 16:34:40 +08:00
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(
2023-02-12 17:31:08 +01:00
os.path.join(settings["destination"], "static", "keycheck.txt"), "rb"
2021-07-06 22:02:48 +02:00
) as infile:
2020-04-13 16:34:40 +08:00
with BytesIO() as outfile:
endec.decrypt(key, infile, outfile, tag)
2022-08-15 20:21:19 +02:00
caplog.clear()
2023-02-12 17:31:08 +01:00
caplog.set_level("DEBUG")
2023-07-15 17:07:07 +02:00
gal = Gallery(settings, ncpu=1)
2022-08-15 20:21:19 +02:00
gal.build()
2022-08-15 22:16:23 +02:00
# Doesn't work on Actions ...
# assert 'Loaded cache with 34 entries' in caplog.messages
# assert 'Loaded encryption cache with 27 entries' in caplog.messages