test(wallpaper_changer): add tests for new passphrase
This commit is contained in:
1 parent
632bca8642
commit
88cb666848
5 files changed
+255
-229
No files matched your search
@@ -89,8 +89,8 @@ class ConfigTests(unittest.TestCase):
|
||||
mode,
|
||||
"--temp-dir",
|
||||
"/tmp/wallpaper-cache",
|
||||
"--private-key",
|
||||
"/tmp/private-key.pem",
|
||||
"--passphrase-file",
|
||||
"/tmp/encryption.passphrase",
|
||||
]
|
||||
if folder is not None:
|
||||
arguments.extend(("--folder", folder))
|
||||
@@ -101,30 +101,35 @@ class ConfigTests(unittest.TestCase):
|
||||
self.assertEqual(config.img_folder, expected_folder)
|
||||
self.assertEqual(config.img_dir, expected_img_dir)
|
||||
self.assertEqual(config.wallpaper_dir, Path("/tmp/wallpaper-cache/wallpaper"))
|
||||
self.assertEqual(config.private_key, Path("/tmp/private-key.pem"))
|
||||
self.assertEqual(
|
||||
config.passphrase_file, Path("/tmp/encryption.passphrase")
|
||||
)
|
||||
|
||||
def test_load_uses_config_default_private_key_when_not_set(self) -> None:
|
||||
def test_load_uses_default_passphrase_when_not_set(self) -> None:
|
||||
home = Path("/home/test-user")
|
||||
args = self.parse_args()
|
||||
args.private_key = None
|
||||
args.passphrase_file = None
|
||||
args.conf_dir = home
|
||||
|
||||
config = Config.load(args)
|
||||
|
||||
self.assertEqual(config.private_key, Path(args.conf_dir).expanduser().resolve() / "private_key.pem")
|
||||
self.assertEqual(
|
||||
config.passphrase_file,
|
||||
Path(args.conf_dir).expanduser().resolve() / "encryption.passphrase",
|
||||
)
|
||||
self.assertEqual(config.wallpaper_dir, Path(args.conf_dir).expanduser().resolve() / "wallpaper")
|
||||
|
||||
|
||||
def test_load_uses_arg_default_private_key_when_set(self) -> None:
|
||||
def test_load_uses_argument_passphrase_when_set(self) -> None:
|
||||
home = Path("/home/test-user")
|
||||
args = self.parse_args()
|
||||
assets = Path(__file__).resolve().parents[3] / "test" / "assets"
|
||||
args.private_key = assets / "test_privateKey.pem"
|
||||
args.passphrase_file = assets / "test_passphrase"
|
||||
|
||||
with patch("pathlib.Path.home", return_value=home):
|
||||
config = Config.load(args)
|
||||
|
||||
self.assertEqual(config.private_key, args.private_key)
|
||||
self.assertEqual(config.passphrase_file, args.passphrase_file)
|
||||
|
||||
def test_mode_shortcuts_and_folder_override(self) -> None:
|
||||
for arguments, expected_mode, expected_folder in (
|
||||
|
||||
@@ -4,9 +4,6 @@ import unittest
|
||||
from pathlib import Path
|
||||
from tempfile import TemporaryDirectory
|
||||
|
||||
from cryptography.hazmat.primitives import serialization
|
||||
from cryptography.hazmat.primitives.asymmetric import rsa
|
||||
|
||||
from wallpaper_changer.argument_parser import parse_args
|
||||
from wallpaper_changer.config import Config
|
||||
from wallpaper_changer.encryption_manager import EncryptionManager, FileInfo
|
||||
@@ -24,17 +21,17 @@ class EncryptionManagerTests(unittest.TestCase):
|
||||
assets = Path(__file__).resolve().parents[3] / "test" / "assets"
|
||||
args = self.parse_args("--conf-dir", str(image_root),
|
||||
"--folder", str(image_root / "corrupt"),
|
||||
"--private-key", str(assets / "test_private_key.pem"))
|
||||
"--passphrase-file", str(assets / "test_passphrase.txt"))
|
||||
config = Config.load(args)
|
||||
shutil.copytree(assets / "corrupt", config.img_dir)
|
||||
image_repository = ImageRepository(config.img_dir)
|
||||
images_enc = image_repository.scan(config.private_key)
|
||||
images_enc = image_repository.scan(config.passphrase_file)
|
||||
hash_enc = {image["hash"] for image in images_enc}
|
||||
image_repository.decode_all(config.private_key)
|
||||
images_dec = image_repository.scan(config.private_key, calc_hash=True)
|
||||
image_repository.decode_all(config.passphrase_file)
|
||||
images_dec = image_repository.scan(config.passphrase_file, calc_hash=True)
|
||||
hash_dec = {image["hash"] for image in images_dec}
|
||||
image_repository.check_encryption(config.private_key)
|
||||
images_reenc = image_repository.scan(config.private_key)
|
||||
image_repository.check_encryption(config.passphrase_file)
|
||||
images_reenc = image_repository.scan(config.passphrase_file)
|
||||
hash_reenc = {image["hash"] for image in images_reenc}
|
||||
|
||||
self.assertEqual(hash_enc, hash_dec)
|
||||
@@ -42,28 +39,15 @@ class EncryptionManagerTests(unittest.TestCase):
|
||||
|
||||
def test_decrypt_rejects_wrong_passphrase(self) -> None:
|
||||
"""Verify decryption fails when given a different passphrase."""
|
||||
with TemporaryDirectory() as temporary_directory, \
|
||||
self.assertRaisesRegex(ValueError, "Invalid padding bytes"):
|
||||
with TemporaryDirectory() as temporary_directory:
|
||||
assets = Path(__file__).resolve().parents[3] / "test" / "assets"
|
||||
shutil.copytree(assets / "corrupt", Path(temporary_directory) / "corrupt")
|
||||
image_root = Path(temporary_directory) / "corrupt"
|
||||
passphrase_file = Path(temporary_directory) / "wrong.passphrase"
|
||||
passphrase_file.write_text("wrong passphrase\n")
|
||||
|
||||
# Generate 4096-bit RSA private key
|
||||
private_key = rsa.generate_private_key(
|
||||
public_exponent=65537,
|
||||
key_size=4096
|
||||
)
|
||||
private_key_file = image_root / "private_key.pem"
|
||||
# Save to PEM file
|
||||
with open(private_key_file, "wb") as f:
|
||||
f.write(
|
||||
private_key.private_bytes(
|
||||
encoding=serialization.Encoding.PEM,
|
||||
format=serialization.PrivateFormat.TraditionalOpenSSL,
|
||||
encryption_algorithm=serialization.NoEncryption()
|
||||
)
|
||||
)
|
||||
ImageRepository(image_root).decode_all(private_key_file)
|
||||
with self.assertRaisesRegex(ValueError, "Invalid padding bytes"):
|
||||
ImageRepository(image_root).decode_all(passphrase_file)
|
||||
|
||||
def test_decrypt_rejects_invalid_or_truncated_ciphertext(self) -> None:
|
||||
"""Verify malformed headers and incomplete ciphertext are rejected."""
|
||||
@@ -109,22 +93,6 @@ class EncryptionManagerTests(unittest.TestCase):
|
||||
decrypted, _ = manager.decrypt(encrypted_file, None, passphrase)
|
||||
self.assertEqual(decrypted, plaintext)
|
||||
|
||||
def test_pem_private_key_uses_complete_file_contents(self) -> None:
|
||||
"""Verify PEM keys are not reduced to their generic header line."""
|
||||
with TemporaryDirectory() as temporary_directory:
|
||||
private_key_file = Path(temporary_directory) / "private_key.pem"
|
||||
pem_contents = (
|
||||
b"-----BEGIN RSA PRIVATE KEY-----\n"
|
||||
b"private-key-bytes\n"
|
||||
b"-----END RSA PRIVATE KEY-----\n"
|
||||
)
|
||||
private_key_file.write_bytes(pem_contents)
|
||||
|
||||
self.assertEqual(
|
||||
EncryptionManager._read_passphrase(private_key_file),
|
||||
pem_contents,
|
||||
)
|
||||
|
||||
def test_naming_table_round_trip_and_malformed_lines(self) -> None:
|
||||
"""Verify naming-table entries round-trip and malformed lines are handled."""
|
||||
with TemporaryDirectory() as temporary_directory:
|
||||
@@ -160,12 +128,12 @@ class EncryptionManagerTests(unittest.TestCase):
|
||||
assets = Path(__file__).resolve().parents[3] / "test" / "assets"
|
||||
args = self.parse_args("--conf-dir", str(image_root),
|
||||
"--folder", str(image_root / "corrupt"),
|
||||
"--private-key", str(assets / "test_private_key.pem"))
|
||||
"--passphrase-file", str(assets / "test_passphrase.txt"))
|
||||
config = Config.load(args)
|
||||
shutil.copytree(assets / "corrupt", config.img_dir)
|
||||
os.remove(config.img_dir / "naming_table.enc")
|
||||
image_repository = ImageRepository(config.img_dir)
|
||||
image_repository.decode_all(config.private_key)
|
||||
image_repository.decode_all(config.passphrase_file)
|
||||
|
||||
def test_naming_table_file_operations_remove_source_after_success(self) -> None:
|
||||
"""Verify file conversion removes its source only after a successful operation."""
|
||||
|
||||
@@ -11,28 +11,28 @@ from PIL import Image
|
||||
|
||||
from wallpaper_changer.argument_parser import parse_args
|
||||
from wallpaper_changer.config import Config
|
||||
from wallpaper_changer.encryption_manager import EncryptionManager
|
||||
from wallpaper_changer.encryption_manager import EncryptionManager, FileInfo
|
||||
from wallpaper_changer.image_repository import ImageRepository
|
||||
|
||||
|
||||
class ImageRepositoryTests(unittest.TestCase):
|
||||
def parse_args(self, *arguments: str):
|
||||
return parse_args(list(arguments))
|
||||
|
||||
|
||||
def test_scan_plaintext_folder_desktop(self) -> None:
|
||||
with TemporaryDirectory() as temporary_directory:
|
||||
image_root = Path(temporary_directory)
|
||||
assets = Path(__file__).resolve().parents[3] / "test" / "assets"
|
||||
args = self.parse_args("--conf-dir", str(image_root),
|
||||
"--folder", str(image_root / "Desktop1920x1080"),
|
||||
"--private-key", str(assets / "test_private_key.pem"))
|
||||
"--passphrase-file", str(assets / "test_passphrase.txt"))
|
||||
config = Config.load(args)
|
||||
|
||||
shutil.copytree(
|
||||
assets / "Desktop1920x1080",
|
||||
config.img_dir,
|
||||
)
|
||||
images = ImageRepository(config.img_dir).scan(config.private_key,
|
||||
images = ImageRepository(config.img_dir).scan(config.passphrase_file,
|
||||
disable_encryption=True)
|
||||
|
||||
self.assertEqual(len(images), 8)
|
||||
@@ -41,23 +41,23 @@ class ImageRepositoryTests(unittest.TestCase):
|
||||
)
|
||||
self.assertTrue(
|
||||
all(image["file"].suffix == ".jpg" for image in images)
|
||||
)
|
||||
|
||||
)
|
||||
|
||||
def test_scan_plaintext_folder_corrupt(self) -> None:
|
||||
with TemporaryDirectory() as temporary_directory:
|
||||
image_root = Path(temporary_directory)
|
||||
assets = Path(__file__).resolve().parents[3] / "test" / "assets"
|
||||
args = self.parse_args("--conf-dir", str(image_root),
|
||||
"--folder", str(image_root / "corrupt"),
|
||||
"--private-key", str(assets / "test_private_key.pem"))
|
||||
"--passphrase-file", str(assets / "test_passphrase.txt"))
|
||||
config = Config.load(args)
|
||||
shutil.copytree(assets / "Desktop1920x1080", image_root / "corrupt")
|
||||
shutil.copytree(assets / "Desktop1920x1080", image_root / "corrupt")
|
||||
|
||||
images = ImageRepository(config.img_dir).scan(config.private_key, disable_encryption=True)
|
||||
images = ImageRepository(config.img_dir).scan(config.passphrase_file, disable_encryption=True)
|
||||
self.assertEqual(len(images), 8)
|
||||
self.assertTrue(
|
||||
all(image["file"].suffix == ".jpg" for image in images)
|
||||
)
|
||||
)
|
||||
self.assertTrue(
|
||||
all(image["file"].parent.name == "corrupt" for image in images)
|
||||
)
|
||||
@@ -68,30 +68,30 @@ class ImageRepositoryTests(unittest.TestCase):
|
||||
assets = Path(__file__).resolve().parents[3] / "test" / "assets"
|
||||
args = self.parse_args("--conf-dir", str(image_root),
|
||||
"--folder", str(image_root / "corrupt"),
|
||||
"--private-key", str(assets / "test_private_key.pem"))
|
||||
"--passphrase-file", str(assets / "test_passphrase.txt"))
|
||||
config = Config.load(args)
|
||||
|
||||
|
||||
shutil.copytree(assets / "corrupt", image_root / "corrupt")
|
||||
images = ImageRepository(config.img_dir).scan(config.private_key, disable_encryption=False)
|
||||
images = ImageRepository(config.img_dir).scan(config.passphrase_file, disable_encryption=False)
|
||||
self.assertEqual(len(images), 8)
|
||||
self.assertTrue(
|
||||
all(image["file"].suffix == ".jpg_enc" for image in images)
|
||||
)
|
||||
)
|
||||
self.assertTrue(
|
||||
all(image["hash"] is not None for image in images)
|
||||
)
|
||||
)
|
||||
self.assertTrue(
|
||||
all(image["file"].parent.name == "corrupt" for image in images)
|
||||
)
|
||||
|
||||
def test_convert_to_jpg_handles_collisions_and_transparency(self) -> None:
|
||||
"""Verify conversion avoids overwrites and composites transparent pixels."""
|
||||
"""Verify conversion avoids overwrites and composites transparent pixels."""
|
||||
with TemporaryDirectory() as temporary_directory:
|
||||
image_root = Path(temporary_directory)
|
||||
assets = Path(__file__).resolve().parents[3] / "test" / "assets"
|
||||
args = self.parse_args("--conf-dir", str(image_root),
|
||||
"--folder", str(image_root / "corrupt"),
|
||||
"--private-key", str(assets / "test_private_key.pem"))
|
||||
"--passphrase-file", str(assets / "test_passphrase.txt"))
|
||||
config = Config.load(args)
|
||||
config.img_dir.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
@@ -100,12 +100,12 @@ class ImageRepositoryTests(unittest.TestCase):
|
||||
|
||||
image_repository = ImageRepository(config.img_dir)
|
||||
image_repository.convert_to_jpg()
|
||||
images = image_repository.scan(config.private_key)
|
||||
images = image_repository.scan(config.passphrase_file)
|
||||
|
||||
self.assertEqual(len(images), 5)
|
||||
self.assertTrue(
|
||||
all(image["file"].suffix == ".jpg" for image in images)
|
||||
)
|
||||
)
|
||||
for num in range(1, 6):
|
||||
image_path = config.img_dir / f"Foto_Berg{num:02d}.jpg"
|
||||
with self.subTest(image=image_path.name):
|
||||
@@ -117,7 +117,7 @@ class ImageRepositoryTests(unittest.TestCase):
|
||||
assets = Path(__file__).resolve().parents[3] / "test" / "assets"
|
||||
args = self.parse_args("--conf-dir", str(image_root),
|
||||
"--folder", str(image_root / "corrupt"),
|
||||
"--private-key", str(assets / "test_private_key.pem"))
|
||||
"--passphrase-file", str(assets / "test_passphrase.txt"))
|
||||
config = Config.load(args)
|
||||
|
||||
shutil.copytree(assets / "corrupt", config.img_dir)
|
||||
@@ -125,7 +125,7 @@ class ImageRepositoryTests(unittest.TestCase):
|
||||
shutil.copy(assets / "images"/ "Foto_Berg01.jpg", config.img_dir / "Foto01.jpg")
|
||||
|
||||
image_repository = ImageRepository(config.img_dir)
|
||||
image_repository.check_encryption(config.private_key)
|
||||
image_repository.check_encryption(config.passphrase_file)
|
||||
|
||||
self.assertEqual(len(list(config.img_dir.rglob("*.jpg"))), 1)
|
||||
self.assertEqual(len(list(config.img_dir.rglob("*.jpg_enc"))), 9)
|
||||
@@ -136,7 +136,7 @@ class ImageRepositoryTests(unittest.TestCase):
|
||||
assets = Path(__file__).resolve().parents[3] / "test" / "assets"
|
||||
args = self.parse_args("--conf-dir", str(image_root),
|
||||
"--folder", str(image_root / "corrupt"),
|
||||
"--private-key", str(assets / "test_private_key.pem"))
|
||||
"--passphrase-file", str(assets / "test_passphrase.txt"))
|
||||
config = Config.load(args)
|
||||
|
||||
shutil.copytree(assets / "corrupt", config.img_dir)
|
||||
@@ -144,7 +144,7 @@ class ImageRepositoryTests(unittest.TestCase):
|
||||
shutil.copy(assets / "images"/ "Foto_Berg01.jpg", config.img_dir)
|
||||
|
||||
image_repository = ImageRepository(config.img_dir)
|
||||
image_repository.check_encryption(config.private_key)
|
||||
image_repository.check_encryption(config.passphrase_file)
|
||||
|
||||
self.assertEqual(len(list(config.img_dir.rglob("*.jpg"))), 1)
|
||||
self.assertEqual(len(list(config.img_dir.rglob("*.jpg_enc"))), 9)
|
||||
@@ -155,7 +155,7 @@ class ImageRepositoryTests(unittest.TestCase):
|
||||
assets = Path(__file__).resolve().parents[3] / "test" / "assets"
|
||||
args = self.parse_args("--conf-dir", str(image_root),
|
||||
"--folder", str(image_root / "corrupt"),
|
||||
"--private-key", str(assets / "test_private_key.pem"))
|
||||
"--passphrase-file", str(assets / "test_passphrase.txt"))
|
||||
config = Config.load(args)
|
||||
|
||||
image_path = assets / "images"/ "Foto_Berg04.jpg"
|
||||
@@ -165,14 +165,14 @@ class ImageRepositoryTests(unittest.TestCase):
|
||||
shutil.copy(image_path, config.img_dir / new_image_name)
|
||||
|
||||
image_repository = ImageRepository(config.img_dir)
|
||||
images = image_repository.scan(config.private_key)
|
||||
images = image_repository.scan(config.passphrase_file)
|
||||
|
||||
old_image_hash = next(
|
||||
item["hash"] for item in images if item["name"] == new_image_name
|
||||
)
|
||||
|
||||
image_repository.check_encryption( config.private_key, replace=True)
|
||||
images = image_repository.scan(config.private_key)
|
||||
image_repository.check_encryption( config.passphrase_file, replace=True)
|
||||
images = image_repository.scan(config.passphrase_file)
|
||||
|
||||
self.assertEqual(len(list(config.img_dir.rglob("*.jpg"))), 0)
|
||||
self.assertEqual(len(list(config.img_dir.rglob("*.jpg_enc"))), 8)
|
||||
@@ -188,7 +188,7 @@ class ImageRepositoryTests(unittest.TestCase):
|
||||
assets = Path(__file__).resolve().parents[3] / "test" / "assets"
|
||||
args = self.parse_args("--conf-dir", str(image_root),
|
||||
"--folder", str(image_root / "corrupt"),
|
||||
"--private-key", str(assets / "test_private_key.pem"))
|
||||
"--passphrase-file", str(assets / "test_passphrase.txt"))
|
||||
config = Config.load(args)
|
||||
|
||||
image_path = assets / "images"/ "Foto_Berg01.jpg"
|
||||
@@ -199,10 +199,10 @@ class ImageRepositoryTests(unittest.TestCase):
|
||||
new_image_hash = hashlib.sha256(((config.img_dir / new_image_name)).read_bytes()).hexdigest()
|
||||
|
||||
image_repository = ImageRepository(config.img_dir)
|
||||
images = image_repository.scan(config.private_key)
|
||||
images = image_repository.scan(config.passphrase_file)
|
||||
|
||||
image_repository.check_encryption(config.private_key)
|
||||
images = image_repository.scan(config.private_key)
|
||||
image_repository.check_encryption(config.passphrase_file)
|
||||
images = image_repository.scan(config.passphrase_file)
|
||||
|
||||
self.assertEqual(len(list(config.img_dir.rglob("*.jpg"))), 1)
|
||||
self.assertEqual(len(list(config.img_dir.rglob("*.jpg_enc"))), 8)
|
||||
@@ -214,7 +214,7 @@ class ImageRepositoryTests(unittest.TestCase):
|
||||
assets = Path(__file__).resolve().parents[3] / "test" / "assets"
|
||||
args = self.parse_args("--conf-dir", str(image_root),
|
||||
"--folder", str(image_root / "corrupt"),
|
||||
"--private-key", str(assets / "test_private_key.pem"))
|
||||
"--passphrase-file", str(assets / "test_passphrase.txt"))
|
||||
config = Config.load(args)
|
||||
|
||||
shutil.copytree(assets / "corrupt", config.img_dir)
|
||||
@@ -222,12 +222,12 @@ class ImageRepositoryTests(unittest.TestCase):
|
||||
|
||||
image_repository = ImageRepository(config.img_dir)
|
||||
|
||||
image_repository.check_encryption(config.private_key)
|
||||
images = image_repository.scan(config.private_key)
|
||||
image_repository.check_encryption(config.passphrase_file)
|
||||
images = image_repository.scan(config.passphrase_file)
|
||||
|
||||
self.assertEqual(len(list(config.img_dir.rglob("*.jpg"))), 0)
|
||||
self.assertEqual(len(list(config.img_dir.rglob("*.jpg_enc"))), 9)
|
||||
self.assertTrue( image["name"] == "Foto_Berg05.jpg"
|
||||
self.assertTrue( image["name"] == "Foto_Berg05.jpg"
|
||||
for image in images if image["file"].name == "00009.jpg_enc")
|
||||
|
||||
def test_decode_all(self) -> None:
|
||||
@@ -236,31 +236,31 @@ class ImageRepositoryTests(unittest.TestCase):
|
||||
assets = Path(__file__).resolve().parents[3] / "test" / "assets"
|
||||
args = self.parse_args("--conf-dir", str(image_root),
|
||||
"--folder", str(image_root / "corrupt"),
|
||||
"--private-key", str(assets / "test_private_key.pem"))
|
||||
"--passphrase-file", str(assets / "test_passphrase.txt"))
|
||||
config = Config.load(args)
|
||||
|
||||
shutil.copytree(assets / "corrupt", config.img_dir)
|
||||
|
||||
image_repository = ImageRepository(config.img_dir)
|
||||
image_repository.decode_all(config.private_key)
|
||||
image_repository.decode_all(config.passphrase_file)
|
||||
self.assertEqual(len(list(config.img_dir.rglob("*.jpg"))), 8)
|
||||
self.assertEqual(len(list(config.img_dir.rglob("*.jpg_enc"))), 0)
|
||||
|
||||
|
||||
def test_decode_argument(self) -> None:
|
||||
with TemporaryDirectory() as temporary_directory:
|
||||
image_root = Path(temporary_directory)
|
||||
assets = Path(__file__).resolve().parents[3] / "test" / "assets"
|
||||
args = self.parse_args("--conf-dir", str(image_root),
|
||||
"--folder", str(image_root / "corrupt"),
|
||||
"--private-key", str(assets / "test_private_key.pem"))
|
||||
"--passphrase-file", str(assets / "test_passphrase.txt"))
|
||||
config = Config.load(args)
|
||||
|
||||
shutil.copytree(assets / "corrupt", config.img_dir)
|
||||
|
||||
image_repository = ImageRepository(config.img_dir)
|
||||
image_repository.decode_images(pattern="Kirchberg",
|
||||
conf_dir=config.conf_dir,
|
||||
private_key=config.private_key)
|
||||
conf_dir=config.conf_dir,
|
||||
passphrase_file=config.passphrase_file)
|
||||
self.assertEqual(len(list(config.img_dir.rglob("*.jpg_enc"))), 8)
|
||||
self.assertEqual(len(list((image_root/"New").rglob("*.jpg"))), 2)
|
||||
|
||||
@@ -270,15 +270,15 @@ class ImageRepositoryTests(unittest.TestCase):
|
||||
assets = Path(__file__).resolve().parents[3] / "test" / "assets"
|
||||
args = self.parse_args("--conf-dir", str(image_root),
|
||||
"--folder", str(image_root / "corrupt"),
|
||||
"--private-key", str(assets / "test_private_key.pem"))
|
||||
"--passphrase-file", str(assets / "test_passphrase.txt"))
|
||||
config = Config.load(args)
|
||||
|
||||
shutil.copytree(assets / "corrupt", config.img_dir)
|
||||
|
||||
image_repository = ImageRepository(config.img_dir)
|
||||
image_repository.decode_images(pattern="Kirchberg",
|
||||
conf_dir=config.conf_dir,
|
||||
private_key=config.private_key,
|
||||
conf_dir=config.conf_dir,
|
||||
passphrase_file=config.passphrase_file,
|
||||
remove=True)
|
||||
self.assertEqual(len(list(config.img_dir.rglob("*.jpg_enc"))), 6)
|
||||
self.assertEqual(len(list(image_root.rglob("*.jpg"))), 2)
|
||||
@@ -289,13 +289,13 @@ class ImageRepositoryTests(unittest.TestCase):
|
||||
assets = Path(__file__).resolve().parents[3] / "test" / "assets"
|
||||
args = self.parse_args("--conf-dir", str(image_root),
|
||||
"--folder", str(image_root / "Desktop1920x1080"),
|
||||
"--private-key", str(assets / "test_private_key.pem"))
|
||||
"--passphrase-file", str(assets / "test_passphrase.txt"))
|
||||
config = Config.load(args)
|
||||
|
||||
shutil.copytree(assets / "Desktop1920x1080", config.img_dir)
|
||||
image_repository = ImageRepository(config.img_dir)
|
||||
image_repository.check_consecutive_filenames(correct_wrong_numbers=True)
|
||||
images = image_repository.scan(config.private_key)
|
||||
images = image_repository.scan(config.passphrase_file)
|
||||
groups = defaultdict(list)
|
||||
|
||||
for item in images:
|
||||
@@ -305,13 +305,13 @@ class ImageRepositoryTests(unittest.TestCase):
|
||||
groups[stem].append(int(number))
|
||||
|
||||
self.assertEqual(len(list(config.img_dir.rglob("*.jpg"))), 8)
|
||||
self.assertEqual( [max(groups[key]) for key in groups],
|
||||
self.assertEqual( [max(groups[key]) for key in groups],
|
||||
[len(groups[key]) for key in groups] )
|
||||
|
||||
class ImageRepositoryEdgeCaseTests(unittest.TestCase):
|
||||
def parse_args(self, *arguments: str):
|
||||
return parse_args(list(arguments))
|
||||
|
||||
|
||||
def test_scan_skips_unsupported_and_corrupt_files(self) -> None:
|
||||
"""Verify scanning ignores unsupported files and handles corrupt image files."""
|
||||
with TemporaryDirectory() as temporary_directory:
|
||||
@@ -319,7 +319,7 @@ class ImageRepositoryEdgeCaseTests(unittest.TestCase):
|
||||
assets = Path(__file__).resolve().parents[3] / "test" / "assets"
|
||||
args = self.parse_args("--conf-dir", str(image_root),
|
||||
"--folder", str(image_root / "Desktop1920x1080"),
|
||||
"--private-key", str(assets / "test_private_key.pem"))
|
||||
"--passphrase-file", str(assets / "test_passphrase.txt"))
|
||||
config = Config.load(args)
|
||||
config.img_dir.mkdir(exist_ok=True)
|
||||
shutil.copy(assets / "Desktop1920x1080" / "Foto_Berg01.jpg",
|
||||
@@ -334,7 +334,7 @@ class ImageRepositoryEdgeCaseTests(unittest.TestCase):
|
||||
textfile = config.img_dir / "textfile.txt"
|
||||
textfile.write_text("text\n")
|
||||
|
||||
images = ImageRepository(config.img_dir).scan(config.private_key)
|
||||
images = ImageRepository(config.img_dir).scan(config.passphrase_file)
|
||||
self.assertEqual(len(images), 2)
|
||||
|
||||
def test_check_encryption_ignores_nested_directories(self) -> None:
|
||||
@@ -344,17 +344,17 @@ class ImageRepositoryEdgeCaseTests(unittest.TestCase):
|
||||
assets = Path(__file__).resolve().parents[3] / "test" / "assets"
|
||||
args = self.parse_args("--conf-dir", str(image_root),
|
||||
"--folder", str(image_root / "corrupt"),
|
||||
"--private-key", str(assets / "test_private_key.pem"))
|
||||
"--passphrase-file", str(assets / "test_passphrase.txt"))
|
||||
config = Config.load(args)
|
||||
|
||||
shutil.copytree(assets / "Desktop1920x1080", config.img_dir / "folder1")
|
||||
|
||||
image_repository = ImageRepository(config.img_dir)
|
||||
image_repository.check_encryption(config.private_key)
|
||||
image_repository.check_encryption(config.passphrase_file)
|
||||
|
||||
self.assertEqual(len(list(config.img_dir.rglob("*.jpg"))), 8)
|
||||
self.assertEqual(len(list(config.img_dir.rglob("*.jpg_enc"))), 0)
|
||||
|
||||
|
||||
def test_scan_with_encryption_disabled_includes_plain_jpeg_payloads_only(self) -> None:
|
||||
"""Verify plaintext JPEG payloads with an encrypted suffix are scanned safely."""
|
||||
with TemporaryDirectory() as temporary_directory:
|
||||
@@ -362,7 +362,7 @@ class ImageRepositoryEdgeCaseTests(unittest.TestCase):
|
||||
assets = Path(__file__).resolve().parents[3] / "test" / "assets"
|
||||
args = self.parse_args("--conf-dir", str(image_root),
|
||||
"--folder", str(image_root / "Desktop1920x1080"),
|
||||
"--private-key", str(assets / "test_private_key.pem"))
|
||||
"--passphrase-file", str(assets / "test_passphrase.txt"))
|
||||
config = Config.load(args)
|
||||
config.img_dir.mkdir(exist_ok=True)
|
||||
shutil.copy(assets / "Desktop1920x1080" / "Foto_Berg01.jpg",
|
||||
@@ -372,7 +372,7 @@ class ImageRepositoryEdgeCaseTests(unittest.TestCase):
|
||||
config.img_dir,
|
||||
)
|
||||
|
||||
images = ImageRepository(config.img_dir).scan(config.private_key, disable_encryption=True)
|
||||
images = ImageRepository(config.img_dir).scan(config.passphrase_file, disable_encryption=True)
|
||||
self.assertEqual(len(images), 1)
|
||||
|
||||
def test_convert_to_jpg_applies_exif_orientation(self) -> None:
|
||||
@@ -408,18 +408,18 @@ class ImageRepositoryEdgeCaseTests(unittest.TestCase):
|
||||
assets = Path(__file__).resolve().parents[3] / "test" / "assets"
|
||||
args = self.parse_args("--conf-dir", str(image_root),
|
||||
"--folder", str(image_root / "corrupt"),
|
||||
"--private-key", str(assets / "test_private_key.pem"))
|
||||
"--passphrase-file", str(assets / "test_passphrase.txt"))
|
||||
config = Config.load(args)
|
||||
|
||||
with self.subTest("test_missing_folder"):
|
||||
image_repository = ImageRepository(config.img_dir)
|
||||
image_repository.check_encryption(config.private_key,
|
||||
image_repository.check_encryption(config.passphrase_file,
|
||||
new_images_folder=(image_root/"New"))
|
||||
self.assertTrue((image_root/"New").exists())
|
||||
with self.subTest("test_empty_folder"):
|
||||
config.img_dir.mkdir(exist_ok=True)
|
||||
image_repository = ImageRepository(config.img_dir)
|
||||
image_repository.check_encryption(config.private_key)
|
||||
image_repository.check_encryption(config.passphrase_file)
|
||||
|
||||
def test_check_encryption_preserves_plaintext_on_encryption_failure(self) -> None:
|
||||
"""Verify failed encryption does not delete the original image."""
|
||||
@@ -428,21 +428,74 @@ class ImageRepositoryEdgeCaseTests(unittest.TestCase):
|
||||
assets = Path(__file__).resolve().parents[3] / "test" / "assets"
|
||||
args = self.parse_args("--conf-dir", str(image_root),
|
||||
"--folder", str(image_root / "corrupt"),
|
||||
"--private-key", str(assets / "test_private_key.pem"))
|
||||
"--passphrase-file", str(assets / "test_passphrase.txt"))
|
||||
config = Config.load(args)
|
||||
config.img_dir.mkdir(exist_ok=True)
|
||||
shutil.copy(assets / "Desktop1920x1080" / "Foto_Berg01.jpg", config.img_dir)
|
||||
image_repository = ImageRepository(config.img_dir)
|
||||
images_before_failed_enc = image_repository.scan(config.private_key)
|
||||
images_before_failed_enc = image_repository.scan(config.passphrase_file)
|
||||
|
||||
with patch("wallpaper_changer.encryption_manager.EncryptionManager.encrypt",
|
||||
with patch("wallpaper_changer.encryption_manager.EncryptionManager.encrypt",
|
||||
side_effect=RuntimeError("Encryption failed")), \
|
||||
self.assertRaises(RuntimeError):
|
||||
image_repository.check_encryption(config.private_key)
|
||||
images_after_failed_enc = image_repository.scan(config.private_key)
|
||||
image_repository.check_encryption(config.passphrase_file)
|
||||
images_after_failed_enc = image_repository.scan(config.passphrase_file)
|
||||
self.assertEqual(images_before_failed_enc, images_after_failed_enc)
|
||||
|
||||
|
||||
def test_check_encryption_saves_each_entry_before_next_image(self) -> None:
|
||||
for replace in (False, True):
|
||||
with self.subTest(replace=replace), TemporaryDirectory() as temporary_directory:
|
||||
root = Path(temporary_directory)
|
||||
passphrase = "pass:test passphrase"
|
||||
manager = EncryptionManager()
|
||||
table_path = root / "naming_table.enc"
|
||||
first = root / "first.jpg"
|
||||
second = root / "second.jpg"
|
||||
first.write_bytes(b"first image")
|
||||
second.write_bytes(b"second image")
|
||||
if replace:
|
||||
manager.encrypt_data(b"old image", root / "00001.jpg_enc", passphrase)
|
||||
manager.store_naming_table(
|
||||
[FileInfo(first.name, "00001.jpg_enc", hashlib.sha256(b"old image").hexdigest())],
|
||||
table_path,
|
||||
passphrase,
|
||||
)
|
||||
|
||||
real_encrypt = EncryptionManager.encrypt
|
||||
|
||||
def fail_on_second(manager, src, dst, key):
|
||||
if Path(src) == second:
|
||||
raise RuntimeError("Encryption interrupted")
|
||||
real_encrypt(manager, src, dst, key)
|
||||
|
||||
with patch.object(EncryptionManager, "encrypt", new=fail_on_second):
|
||||
with self.assertRaisesRegex(RuntimeError, "Encryption interrupted"):
|
||||
ImageRepository(root).check_encryption(passphrase, replace=replace)
|
||||
|
||||
entries = manager.load_naming_table(table_path, passphrase)
|
||||
self.assertEqual(
|
||||
entries,
|
||||
[FileInfo(first.name, "00001.jpg_enc", hashlib.sha256(b"first image").hexdigest())],
|
||||
)
|
||||
self.assertEqual(manager.decrypt(root / "00001.jpg_enc", None, passphrase)[0], b"first image")
|
||||
self.assertTrue(first.exists())
|
||||
self.assertTrue(second.exists())
|
||||
|
||||
def test_check_encryption_saves_final_image_entry(self) -> None:
|
||||
with TemporaryDirectory() as temporary_directory:
|
||||
root = Path(temporary_directory)
|
||||
passphrase = "pass:test passphrase"
|
||||
for name in ("first.jpg", "second.jpg"):
|
||||
(root / name).write_bytes(name.encode())
|
||||
|
||||
ImageRepository(root).check_encryption(passphrase)
|
||||
|
||||
entries = EncryptionManager().load_naming_table(root / "naming_table.enc", passphrase)
|
||||
self.assertEqual([entry.image_name for entry in entries], ["first.jpg", "second.jpg"])
|
||||
self.assertFalse((root / "first.jpg").exists())
|
||||
self.assertFalse((root / "second.jpg").exists())
|
||||
|
||||
|
||||
def test_decode_all_image_rejects_hash_mismatch(self) -> None:
|
||||
"""Verify decrypted content with an unexpected hash is rejected."""
|
||||
with TemporaryDirectory() as temporary_directory, \
|
||||
@@ -451,7 +504,7 @@ class ImageRepositoryEdgeCaseTests(unittest.TestCase):
|
||||
assets = Path(__file__).resolve().parents[3] / "test" / "assets"
|
||||
args = self.parse_args("--conf-dir", str(image_root),
|
||||
"--folder", str(image_root / "corrupt"),
|
||||
"--private-key", str(assets / "test_private_key.pem"))
|
||||
"--passphrase-file", str(assets / "test_passphrase.txt"))
|
||||
config = Config.load(args)
|
||||
shutil.copytree(assets / "corrupt" , config.img_dir)
|
||||
|
||||
@@ -465,7 +518,7 @@ class ImageRepositoryEdgeCaseTests(unittest.TestCase):
|
||||
f.seek(middle)
|
||||
f.write(corrupted)
|
||||
|
||||
ImageRepository(config.img_dir).decode_all(config.private_key)
|
||||
ImageRepository(config.img_dir).decode_all(config.passphrase_file)
|
||||
|
||||
def test_decode_all_handles_unmapped_encrypted_files(self) -> None:
|
||||
"""Verify decode-all behavior when an encrypted image has no table entry."""
|
||||
@@ -474,13 +527,13 @@ class ImageRepositoryEdgeCaseTests(unittest.TestCase):
|
||||
assets = Path(__file__).resolve().parents[3] / "test" / "assets"
|
||||
args = self.parse_args("--conf-dir", str(image_root),
|
||||
"--folder", str(image_root / "corrupt"),
|
||||
"--private-key", str(assets / "test_private_key.pem"))
|
||||
"--passphrase-file", str(assets / "test_passphrase.txt"))
|
||||
config = Config.load(args)
|
||||
shutil.copytree(assets / "corrupt" , config.img_dir)
|
||||
shutil.copy(assets / "images" / "00011.jpg_enc", config.img_dir)
|
||||
ImageRepository(config.img_dir).decode_all(config.private_key)
|
||||
ImageRepository(config.img_dir).decode_all(config.passphrase_file)
|
||||
self.assertEqual(len(list(config.img_dir.glob("*.jpg"))), 9)
|
||||
self.assertEqual(len(list(config.img_dir.glob("MissingImage01.jpg"))), 1)
|
||||
self.assertEqual(len(list(config.img_dir.glob("MissingImage01.jpg"))), 1)
|
||||
|
||||
def test_decode_operations_avoid_existing_destination_names(self) -> None:
|
||||
"""Verify decoded images do not overwrite existing files with the same name."""
|
||||
@@ -489,19 +542,19 @@ class ImageRepositoryEdgeCaseTests(unittest.TestCase):
|
||||
assets = Path(__file__).resolve().parents[3] / "test" / "assets"
|
||||
args = self.parse_args("--conf-dir", str(image_root),
|
||||
"--folder", str(image_root / "corrupt"),
|
||||
"--private-key", str(assets / "test_private_key.pem"))
|
||||
"--passphrase-file", str(assets / "test_passphrase.txt"))
|
||||
config = Config.load(args)
|
||||
shutil.copytree(assets / "corrupt" , config.img_dir)
|
||||
shutil.copy(assets / "Desktop1920x1080" / "Foto_Berg01.jpg" , config.img_dir)
|
||||
|
||||
|
||||
naming_table = EncryptionManager().load_naming_table(
|
||||
config.img_dir/"naming_table.enc", config.private_key
|
||||
config.img_dir/"naming_table.enc", config.passphrase_file
|
||||
)
|
||||
naming_table_element_hash = [item.image_hash for item in naming_table
|
||||
naming_table_element_hash = [item.image_hash for item in naming_table
|
||||
if item.image_name == "Foto_Berg01.jpg"][0]
|
||||
ImageRepository(config.img_dir).decode_all(config.private_key)
|
||||
|
||||
images = ImageRepository(config.img_dir).scan(config.private_key,calc_hash=True)
|
||||
ImageRepository(config.img_dir).decode_all(config.passphrase_file)
|
||||
|
||||
images = ImageRepository(config.img_dir).scan(config.passphrase_file,calc_hash=True)
|
||||
image = [image for image in images if image["hash"] == naming_table_element_hash]
|
||||
self.assertEqual(len(list(config.img_dir.rglob("*.jpg"))), 9)
|
||||
self.assertEqual(len(image), 2)
|
||||
@@ -514,7 +567,7 @@ class ImageRepositoryEdgeCaseTests(unittest.TestCase):
|
||||
assets = Path(__file__).resolve().parents[3] / "test" / "assets"
|
||||
args = self.parse_args("--conf-dir", str(image_root),
|
||||
"--folder", str(image_root / "corrupt"),
|
||||
"--private-key", str(assets / "test_private_key.pem"))
|
||||
"--passphrase-file", str(assets / "test_passphrase.txt"))
|
||||
config = Config.load(args)
|
||||
shutil.copytree(assets / "corrupt" , config.img_dir)
|
||||
|
||||
@@ -528,10 +581,10 @@ class ImageRepositoryEdgeCaseTests(unittest.TestCase):
|
||||
f.seek(middle)
|
||||
f.write(corrupted)
|
||||
|
||||
ImageRepository(config.img_dir).decode_all(config.private_key)
|
||||
ImageRepository(config.img_dir).decode_all(config.passphrase_file)
|
||||
|
||||
shutil.copy(assets / "corrupt" / "00001.jpg_enc" , config.img_dir)
|
||||
ImageRepository(config.img_dir).decode_all(config.private_key)
|
||||
ImageRepository(config.img_dir).decode_all(config.passphrase_file)
|
||||
self.assertEqual(len(list(config.img_dir.rglob("*.jpg_enc"))), 0)
|
||||
self.assertEqual(len(list(config.img_dir.rglob("*.jpg"))), 8)
|
||||
|
||||
|
||||
@@ -41,13 +41,13 @@ class WallpaperChangerTests(unittest.TestCase):
|
||||
|
||||
repository.assert_called_once_with(self.cfg.img_dir)
|
||||
repository.return_value.scan.assert_called_once_with(
|
||||
self.cfg.private_key,
|
||||
self.cfg.passphrase_file,
|
||||
disable_encryption=True,
|
||||
)
|
||||
builder.return_value.build.assert_called_once_with(
|
||||
images,
|
||||
self.layout,
|
||||
private_key=self.cfg.private_key,
|
||||
passphrase_file=self.cfg.passphrase_file,
|
||||
wallpaper_dir=self.cfg.wallpaper_dir,
|
||||
search_terms_monitor=search_terms_monitor,
|
||||
search_terms_gen=search_terms_gen,
|
||||
|
||||
@@ -17,38 +17,38 @@ from wallpaper_changer.wallpaper_builder import WallpaperBuilder
|
||||
class ImageRepositoryTests(unittest.TestCase):
|
||||
def parse_args(self, *arguments: str):
|
||||
return parse_args(list(arguments))
|
||||
|
||||
|
||||
def test_build_wallpaper(self) -> None:
|
||||
with TemporaryDirectory() as temporary_directory:
|
||||
image_root = Path(temporary_directory)
|
||||
assets = Path(__file__).resolve().parents[3] / "test" / "assets"
|
||||
args = self.parse_args("--conf-dir", str(image_root),
|
||||
"--folder", str(image_root / "Desktop1920x1080"),
|
||||
"--private-key", str(assets / "test_private_key.pem"))
|
||||
"--passphrase-file", str(assets / "test_passphrase.txt"))
|
||||
config = Config.load(args)
|
||||
shutil.copytree(assets / "Desktop1920x1080", config.img_dir)
|
||||
images = ImageRepository(config.img_dir).scan(config.private_key)
|
||||
shutil.copytree(assets / "Desktop1920x1080", config.img_dir)
|
||||
images = ImageRepository(config.img_dir).scan(config.passphrase_file)
|
||||
monitors = ScreenLayout((Monitor(1920, 1080,0,set("TestMon1")),))
|
||||
WallpaperBuilder().build(images, monitors, config.private_key, config.wallpaper_dir)
|
||||
|
||||
WallpaperBuilder().build(images, monitors, config.passphrase_file, config.wallpaper_dir)
|
||||
|
||||
self.assertTrue((config.wallpaper_dir/"wallpaper.jpg").exists())
|
||||
|
||||
|
||||
def test_build_wallpaper_encoded(self) -> None:
|
||||
with TemporaryDirectory() as temporary_directory:
|
||||
image_root = Path(temporary_directory)
|
||||
assets = Path(__file__).resolve().parents[3] / "test" / "assets"
|
||||
args = self.parse_args("--conf-dir", str(image_root),
|
||||
"--folder", str(image_root / "corrupt"),
|
||||
"--private-key", str(assets / "test_private_key.pem"))
|
||||
"--passphrase-file", str(assets / "test_passphrase.txt"))
|
||||
config = Config.load(args)
|
||||
shutil.copytree(assets / "corrupt", config.img_dir)
|
||||
images = ImageRepository(config.img_dir).scan(config.private_key)
|
||||
shutil.copytree(assets / "corrupt", config.img_dir)
|
||||
images = ImageRepository(config.img_dir).scan(config.passphrase_file)
|
||||
monitors = ScreenLayout((Monitor(1920, 1080,0,set("TestMon1")),))
|
||||
WallpaperBuilder().build(images, monitors, config.private_key, config.wallpaper_dir)
|
||||
WallpaperBuilder().build(images, monitors, config.passphrase_file, config.wallpaper_dir)
|
||||
|
||||
self.assertEqual(len(list(config.img_dir.rglob("*.jpg_enc"))), 8)
|
||||
self.assertTrue((config.wallpaper_dir/"wallpaper.jpg").exists())
|
||||
|
||||
|
||||
def test_build_wallpaper_search(self) -> None:
|
||||
with TemporaryDirectory() as temporary_directory:
|
||||
image_root = Path(temporary_directory)
|
||||
@@ -56,18 +56,18 @@ class ImageRepositoryTests(unittest.TestCase):
|
||||
args = self.parse_args("--conf-dir", str(image_root),
|
||||
"--folder", str(image_root / "Desktop1920x1080"),
|
||||
"-s", "Hochschwab",
|
||||
"--private-key", str(assets / "test_private_key.pem"))
|
||||
|
||||
"--passphrase-file", str(assets / "test_passphrase.txt"))
|
||||
|
||||
config = Config.load(args)
|
||||
shutil.copytree(assets / "Desktop1920x1080", config.img_dir)
|
||||
images = ImageRepository(config.img_dir).scan(config.private_key)
|
||||
shutil.copytree(assets / "Desktop1920x1080", config.img_dir)
|
||||
images = ImageRepository(config.img_dir).scan(config.passphrase_file)
|
||||
monitors = ScreenLayout((Monitor(1920, 1080,0, set("TestMon")),))
|
||||
_,images_used = WallpaperBuilder().build(images,
|
||||
monitors,
|
||||
config.private_key,
|
||||
_,images_used = WallpaperBuilder().build(images,
|
||||
monitors,
|
||||
config.passphrase_file,
|
||||
config.wallpaper_dir,
|
||||
search_terms_monitor=args.search)
|
||||
|
||||
|
||||
self.assertTrue(images_used[0] == "Foto_Berg_Hochschwab01.jpg")
|
||||
|
||||
def test_build_wallpaper_decode_and_leave(self) -> None:
|
||||
@@ -76,16 +76,16 @@ class ImageRepositoryTests(unittest.TestCase):
|
||||
assets = Path(__file__).resolve().parents[3] / "test" / "assets"
|
||||
args = self.parse_args("--conf-dir", str(image_root),
|
||||
"--folder", str(image_root / "corrupt"),
|
||||
"--private-key", str(assets / "test_private_key.pem"))
|
||||
"--passphrase-file", str(assets / "test_passphrase.txt"))
|
||||
config = Config.load(args)
|
||||
shutil.copytree(assets / "corrupt", config.img_dir)
|
||||
images = ImageRepository(config.img_dir).scan(config.private_key)
|
||||
shutil.copytree(assets / "corrupt", config.img_dir)
|
||||
images = ImageRepository(config.img_dir).scan(config.passphrase_file)
|
||||
monitors = ScreenLayout((Monitor(1920, 1080,0,set("TestMon1")),))
|
||||
WallpaperBuilder().build(images, monitors,
|
||||
config.private_key, config.wallpaper_dir,
|
||||
WallpaperBuilder().build(images, monitors,
|
||||
config.passphrase_file, config.wallpaper_dir,
|
||||
decode_and_leave=True)
|
||||
|
||||
images = ImageRepository(config.img_dir).scan(config.private_key)
|
||||
|
||||
images = ImageRepository(config.img_dir).scan(config.passphrase_file)
|
||||
|
||||
self.assertEqual(len(list(config.img_dir.rglob("*.jpg"))), 1)
|
||||
self.assertEqual(len(list(config.img_dir.rglob("*.jpg_enc"))), 8)
|
||||
@@ -97,17 +97,17 @@ class ImageRepositoryTests(unittest.TestCase):
|
||||
img_folder = image_root / "Desktop1920x1080"
|
||||
args = self.parse_args("--conf-dir", str(image_root),
|
||||
"--folder", str(image_root / "Desktop1920x1080"),
|
||||
"--private-key", str(assets / "test_private_key.pem"))
|
||||
"--passphrase-file", str(assets / "test_passphrase.txt"))
|
||||
config = Config.load(args)
|
||||
img_folder.mkdir(parents=True, exist_ok=True)
|
||||
shutil.copy(
|
||||
assets / "images" /"half-white-half-black.jpg",
|
||||
img_folder / "Foto_Left_WhiteBlack.jpg",
|
||||
)
|
||||
images = ImageRepository(config.img_dir).scan(config.private_key)
|
||||
images = ImageRepository(config.img_dir).scan(config.passphrase_file)
|
||||
monitors = ScreenLayout((Monitor(1920, 1080,0,set("TestMon1")),))
|
||||
WallpaperBuilder().build(images, monitors,
|
||||
config.private_key,
|
||||
WallpaperBuilder().build(images, monitors,
|
||||
config.passphrase_file,
|
||||
config.wallpaper_dir)
|
||||
|
||||
wallpaper_path = config.wallpaper_dir / "wallpaper.jpg"
|
||||
@@ -123,17 +123,17 @@ class ImageRepositoryTests(unittest.TestCase):
|
||||
img_folder = image_root / "Desktop1920x1080"
|
||||
args = self.parse_args("--conf-dir", str(image_root),
|
||||
"--folder", str(image_root / "Desktop1920x1080"),
|
||||
"--private-key", str(assets / "test_private_key.pem"))
|
||||
"--passphrase-file", str(assets / "test_passphrase.txt"))
|
||||
config = Config.load(args)
|
||||
img_folder.mkdir(parents=True, exist_ok=True)
|
||||
shutil.copy(
|
||||
assets / "images" /"half-white-half-black.jpg",
|
||||
img_folder / "Foto_Right_WhiteBlack.jpg",
|
||||
)
|
||||
images = ImageRepository(config.img_dir).scan(config.private_key)
|
||||
images = ImageRepository(config.img_dir).scan(config.passphrase_file)
|
||||
monitors = ScreenLayout((Monitor(1920, 1080,0,set("TestMon1")),))
|
||||
WallpaperBuilder().build(images, monitors,
|
||||
config.private_key,
|
||||
WallpaperBuilder().build(images, monitors,
|
||||
config.passphrase_file,
|
||||
config.wallpaper_dir)
|
||||
|
||||
wallpaper_path = config.wallpaper_dir / "wallpaper.jpg"
|
||||
@@ -146,23 +146,23 @@ class ImageRepositoryTests(unittest.TestCase):
|
||||
class WallpaperBuilderEdgeCaseTests(unittest.TestCase):
|
||||
def parse_args(self, *arguments: str):
|
||||
return parse_args(list(arguments))
|
||||
|
||||
|
||||
def test_build_rejects_empty_monitor_layouts(self) -> None:
|
||||
with TemporaryDirectory() as temporary_directory:
|
||||
image_root = Path(temporary_directory)
|
||||
assets = Path(__file__).resolve().parents[3] / "test" / "assets"
|
||||
args = self.parse_args("--conf-dir", str(image_root),
|
||||
"--folder", str(image_root / "Desktop1920x1080"),
|
||||
"--private-key", str(assets / "test_private_key.pem"))
|
||||
"--passphrase-file", str(assets / "test_passphrase.txt"))
|
||||
config = Config.load(args)
|
||||
shutil.copytree(assets / "Desktop1920x1080", config.img_dir)
|
||||
images = ImageRepository(config.img_dir).scan(config.private_key)
|
||||
shutil.copytree(assets / "Desktop1920x1080", config.img_dir)
|
||||
images = ImageRepository(config.img_dir).scan(config.passphrase_file)
|
||||
monitors = ScreenLayout((Monitor(0,0,0,set("WrongLayout")),))
|
||||
try:
|
||||
WallpaperBuilder().build(images, monitors, config.private_key, config.wallpaper_dir)
|
||||
WallpaperBuilder().build(images, monitors, config.passphrase_file, config.wallpaper_dir)
|
||||
except ValueError as e:
|
||||
self.assertEqual("Monitor 0 has no dimensions", str(e))
|
||||
|
||||
|
||||
def test_build_ignores_search_term_when_no_candidates_remain(self) -> None:
|
||||
"""Verify building reports when exclusions leave no eligible image."""
|
||||
with TemporaryDirectory() as temporary_directory:
|
||||
@@ -171,17 +171,17 @@ class WallpaperBuilderEdgeCaseTests(unittest.TestCase):
|
||||
args = self.parse_args("--conf-dir", str(image_root),
|
||||
"--folder", str(image_root / "Desktop1920x1080"),
|
||||
"-s", "Tal",
|
||||
"--private-key", str(assets / "test_private_key.pem"))
|
||||
"--passphrase-file", str(assets / "test_passphrase.txt"))
|
||||
config = Config.load(args)
|
||||
shutil.copytree(assets / "Desktop1920x1080", config.img_dir)
|
||||
images = ImageRepository(config.img_dir).scan(config.private_key)
|
||||
shutil.copytree(assets / "Desktop1920x1080", config.img_dir)
|
||||
images = ImageRepository(config.img_dir).scan(config.passphrase_file)
|
||||
candidates = WallpaperBuilder().find_candidates(images, None,args.search,"")
|
||||
self.assertEqual(len(candidates),8)
|
||||
monitors = ScreenLayout((Monitor(1920, 1080,0,set("TestMon1")),))
|
||||
WallpaperBuilder().build(images, monitors, config.private_key,
|
||||
WallpaperBuilder().build(images, monitors, config.passphrase_file,
|
||||
config.wallpaper_dir, search_terms_monitor=args.search)
|
||||
self.assertTrue((config.wallpaper_dir/"wallpaper.jpg").exists())
|
||||
|
||||
|
||||
def test_build_can_handle_multiple_search_terms(self) -> None:
|
||||
"""Verify building reports when exclusions leave no eligible image."""
|
||||
with TemporaryDirectory() as temporary_directory:
|
||||
@@ -190,14 +190,14 @@ class WallpaperBuilderEdgeCaseTests(unittest.TestCase):
|
||||
args = self.parse_args("--conf-dir", str(image_root),
|
||||
"--folder", str(image_root / "Desktop1920x1080"),
|
||||
"-s", "Tal", "See",
|
||||
"--private-key", str(assets / "test_private_key.pem"))
|
||||
"--passphrase-file", str(assets / "test_passphrase.txt"))
|
||||
config = Config.load(args)
|
||||
shutil.copytree(assets / "Desktop1920x1080", config.img_dir)
|
||||
shutil.move(config.img_dir / "Foto_Berg01.jpg" , config.img_dir / "Foto_Tal_Hügel_See01.jpg")
|
||||
shutil.move(config.img_dir / "Foto_Berg03.jpg" , config.img_dir / "Foto_Tal_Hügel_Meer01.jpg")
|
||||
images = ImageRepository(config.img_dir).scan(config.private_key)
|
||||
shutil.copytree(assets / "Desktop1920x1080", config.img_dir)
|
||||
shutil.move(config.img_dir / "Foto_Berg01.jpg" , config.img_dir / "Foto_Tal_Hügel_See01.jpg")
|
||||
shutil.move(config.img_dir / "Foto_Berg03.jpg" , config.img_dir / "Foto_Tal_Hügel_Meer01.jpg")
|
||||
images = ImageRepository(config.img_dir).scan(config.passphrase_file)
|
||||
monitors = ScreenLayout((Monitor(1920, 1080,0,set("TestMon1")),))
|
||||
_,image_list = WallpaperBuilder().build(images, monitors, config.private_key,
|
||||
_,image_list = WallpaperBuilder().build(images, monitors, config.passphrase_file,
|
||||
config.wallpaper_dir, search_terms_gen=args.search)
|
||||
self.assertTrue((config.wallpaper_dir/"wallpaper.jpg").exists())
|
||||
self.assertTrue("Tal" in image_list[0] and "See" in image_list[0])
|
||||
@@ -210,15 +210,15 @@ class WallpaperBuilderEdgeCaseTests(unittest.TestCase):
|
||||
args = self.parse_args("--conf-dir", str(image_root),
|
||||
"--folder", str(image_root / "Desktop1920x1080"),
|
||||
"--search-mon", "See", "Meer",
|
||||
"--private-key", str(assets / "test_private_key.pem"))
|
||||
"--passphrase-file", str(assets / "test_passphrase.txt"))
|
||||
config = Config.load(args)
|
||||
shutil.copytree(assets / "Desktop1920x1080", config.img_dir)
|
||||
shutil.move(config.img_dir / "Foto_Berg01.jpg" , config.img_dir / "Foto_Tal_Hügel_See01.jpg")
|
||||
shutil.move(config.img_dir / "Foto_Berg03.jpg" , config.img_dir / "Foto_Tal_Hügel_Meer01.jpg")
|
||||
images = ImageRepository(config.img_dir).scan(config.private_key)
|
||||
shutil.copytree(assets / "Desktop1920x1080", config.img_dir)
|
||||
shutil.move(config.img_dir / "Foto_Berg01.jpg" , config.img_dir / "Foto_Tal_Hügel_See01.jpg")
|
||||
shutil.move(config.img_dir / "Foto_Berg03.jpg" , config.img_dir / "Foto_Tal_Hügel_Meer01.jpg")
|
||||
images = ImageRepository(config.img_dir).scan(config.passphrase_file)
|
||||
monitors = ScreenLayout((Monitor(1920, 1080,0,set("TestMon1")),
|
||||
Monitor(1920, 1080,1920,set("TestMon2")),))
|
||||
_,image_list = WallpaperBuilder().build(images, monitors, config.private_key,
|
||||
_,image_list = WallpaperBuilder().build(images, monitors, config.passphrase_file,
|
||||
config.wallpaper_dir, search_terms_monitor=args.search_mon)
|
||||
self.assertTrue((config.wallpaper_dir/"wallpaper.jpg").exists())
|
||||
self.assertTrue("Foto_Tal_Hügel_See01.jpg" in image_list and "Foto_Tal_Hügel_Meer01.jpg" in image_list)
|
||||
@@ -231,18 +231,18 @@ class WallpaperBuilderEdgeCaseTests(unittest.TestCase):
|
||||
args = self.parse_args("--conf-dir", str(image_root),
|
||||
"--folder", str(image_root / "Desktop1920x1080"),
|
||||
"--exclude", "Berg",
|
||||
"--private-key", str(assets / "test_private_key.pem"))
|
||||
"--passphrase-file", str(assets / "test_passphrase.txt"))
|
||||
config = Config.load(args)
|
||||
shutil.copytree(assets / "Desktop1920x1080", config.img_dir)
|
||||
images = ImageRepository(config.img_dir).scan(config.private_key)
|
||||
shutil.copytree(assets / "Desktop1920x1080", config.img_dir)
|
||||
images = ImageRepository(config.img_dir).scan(config.passphrase_file)
|
||||
candidates = WallpaperBuilder().find_candidates(images, None,None,args.exclude)
|
||||
self.assertEqual(len(candidates),0)
|
||||
monitors = ScreenLayout((Monitor(1920, 1080,0,set("TestMon1")),))
|
||||
try:
|
||||
WallpaperBuilder().build(images, monitors, config.private_key,
|
||||
WallpaperBuilder().build(images, monitors, config.passphrase_file,
|
||||
config.wallpaper_dir,exclude=args.exclude)
|
||||
except ValueError as error:
|
||||
self.assertEqual(str(error),
|
||||
self.assertEqual(str(error),
|
||||
f"No images left after excluding {args.exclude}")
|
||||
|
||||
def test_search_matching_and_exclusion_are_case_insensitive(self) -> None:
|
||||
@@ -254,29 +254,29 @@ class WallpaperBuilderEdgeCaseTests(unittest.TestCase):
|
||||
"--folder", str(image_root / "Desktop1920x1080"),
|
||||
"--search", "gÖller",
|
||||
"--exclude", "HOCHSCHWAB",
|
||||
"--private-key", str(assets / "test_private_key.pem"))
|
||||
"--passphrase-file", str(assets / "test_passphrase.txt"))
|
||||
config = Config.load(args)
|
||||
shutil.copytree(assets / "Desktop1920x1080", config.img_dir)
|
||||
images = ImageRepository(config.img_dir).scan(config.private_key)
|
||||
shutil.copytree(assets / "Desktop1920x1080", config.img_dir)
|
||||
images = ImageRepository(config.img_dir).scan(config.passphrase_file)
|
||||
candidates = WallpaperBuilder().find_candidates(images, None,None,args.exclude)
|
||||
self.assertEqual(len(candidates),7)
|
||||
candidates = WallpaperBuilder().find_candidates(images, None,args.search,())
|
||||
self.assertEqual(len(candidates),2)
|
||||
|
||||
|
||||
def test_build_creates_custom_output_parent_directory(self) -> None:
|
||||
"""Verify a custom output path is created along with missing parent folders."""
|
||||
"""Verify a custom output path is created along with missing parent folders."""
|
||||
with TemporaryDirectory() as temporary_directory:
|
||||
image_root = Path(temporary_directory)
|
||||
assets = Path(__file__).resolve().parents[3] / "test" / "assets"
|
||||
args = self.parse_args("--conf-dir", str(image_root / "wallpaper" / "custom"),
|
||||
"--folder", str(image_root / "Desktop1920x1080"),
|
||||
"--private-key", str(assets / "test_private_key.pem"))
|
||||
"--passphrase-file", str(assets / "test_passphrase.txt"))
|
||||
config = Config.load(args)
|
||||
shutil.copytree(assets / "Desktop1920x1080", config.img_dir)
|
||||
images = ImageRepository(config.img_dir).scan(config.private_key)
|
||||
shutil.copytree(assets / "Desktop1920x1080", config.img_dir)
|
||||
images = ImageRepository(config.img_dir).scan(config.passphrase_file)
|
||||
monitors = ScreenLayout((Monitor(1920, 1080,0,set("TestMon1")),))
|
||||
WallpaperBuilder().build(images, monitors, config.private_key, config.wallpaper_dir)
|
||||
|
||||
WallpaperBuilder().build(images, monitors, config.passphrase_file, config.wallpaper_dir)
|
||||
|
||||
self.assertTrue((config.wallpaper_dir/"wallpaper.jpg").exists())
|
||||
|
||||
def test_build_creates_correct_canvas_for_multiple_monitors(self) -> None:
|
||||
@@ -286,16 +286,16 @@ class WallpaperBuilderEdgeCaseTests(unittest.TestCase):
|
||||
assets = Path(__file__).resolve().parents[3] / "test" / "assets"
|
||||
args = self.parse_args("--conf-dir", str(image_root),
|
||||
"--folder", str(image_root / "Desktop1920x1080"),
|
||||
"--private-key", str(assets / "test_private_key.pem"))
|
||||
"--passphrase-file", str(assets / "test_passphrase.txt"))
|
||||
config = Config.load(args)
|
||||
shutil.copytree(assets / "Desktop1920x1080", config.img_dir)
|
||||
images = ImageRepository(config.img_dir).scan(config.private_key)
|
||||
monitors = ScreenLayout((Monitor(1920, 1080,0,set("TestMon1")),
|
||||
shutil.copytree(assets / "Desktop1920x1080", config.img_dir)
|
||||
images = ImageRepository(config.img_dir).scan(config.passphrase_file)
|
||||
monitors = ScreenLayout((Monitor(1920, 1080,0,set("TestMon1")),
|
||||
Monitor(1920, 1080,1920,set("TestMon1")),))
|
||||
WallpaperBuilder().build(images, monitors, config.private_key, config.wallpaper_dir)
|
||||
WallpaperBuilder().build(images, monitors, config.passphrase_file, config.wallpaper_dir)
|
||||
image = Image.open(config.wallpaper_dir/"wallpaper.jpg")
|
||||
|
||||
self.assertTrue((config.wallpaper_dir/"wallpaper.jpg").exists())
|
||||
self.assertTrue((config.wallpaper_dir/"wallpaper.jpg").exists())
|
||||
self.assertTrue(image.size == (3840, 1080))
|
||||
|
||||
def test_build_surfaces_image_decode_errors_empty_file(self) -> None:
|
||||
@@ -305,22 +305,22 @@ class WallpaperBuilderEdgeCaseTests(unittest.TestCase):
|
||||
assets = Path(__file__).resolve().parents[3] / "test" / "assets"
|
||||
args = self.parse_args("--conf-dir", str(image_root),
|
||||
"--folder", str(image_root / "Desktop1920x1080"),
|
||||
"--private-key", str(assets / "test_private_key.pem"))
|
||||
"--passphrase-file", str(assets / "test_passphrase.txt"))
|
||||
config = Config.load(args)
|
||||
(image_root / "Desktop1920x1080" ).mkdir()
|
||||
iamge_path = image_root / "Desktop1920x1080" / "faulty.jpg"
|
||||
iamge_path.touch()
|
||||
images = ImageRepository(config.img_dir).scan(config.private_key)
|
||||
images = ImageRepository(config.img_dir).scan(config.passphrase_file)
|
||||
candidates = WallpaperBuilder().find_candidates(images, None,None,args.exclude)
|
||||
self.assertEqual(len(candidates),1)
|
||||
monitors = ScreenLayout((Monitor(1920, 1080,0,set("TestMon1")),))
|
||||
try:
|
||||
WallpaperBuilder().build(images, monitors, config.private_key,
|
||||
WallpaperBuilder().build(images, monitors, config.passphrase_file,
|
||||
config.wallpaper_dir,exclude=args.exclude)
|
||||
except UnidentifiedImageError as error:
|
||||
self.assertTrue(
|
||||
self.assertTrue(
|
||||
"cannot identify image file" in str(error))
|
||||
|
||||
|
||||
def test_build_surfaces_image_decode_errors_corrupt_file_header(self) -> None:
|
||||
"""Verify unreadable or undecryptable selected images produce an explicit error."""
|
||||
with TemporaryDirectory() as temporary_directory:
|
||||
@@ -328,23 +328,23 @@ class WallpaperBuilderEdgeCaseTests(unittest.TestCase):
|
||||
assets = Path(__file__).resolve().parents[3] / "test" / "assets"
|
||||
args = self.parse_args("--conf-dir", str(image_root),
|
||||
"--folder", str(image_root / "Desktop1920x1080"),
|
||||
"--private-key", str(assets / "test_private_key.pem"))
|
||||
"--passphrase-file", str(assets / "test_passphrase.txt"))
|
||||
config = Config.load(args)
|
||||
(image_root / "Desktop1920x1080" ).mkdir()
|
||||
shutil.copy(assets / "Desktop1920x1080" / "Foto_Berg01.jpg", config.img_dir/"Foto_Berg01.jpg")
|
||||
# Overwrite the first 3 bytes with null bytes
|
||||
with open( config.img_dir / "Foto_Berg01.jpg", "r+b") as file:
|
||||
file.write(b"\x00\x00\x00")
|
||||
images = ImageRepository(config.img_dir).scan(config.private_key)
|
||||
images = ImageRepository(config.img_dir).scan(config.passphrase_file)
|
||||
candidates = WallpaperBuilder().find_candidates(images, None,None,args.exclude)
|
||||
self.assertEqual(len(candidates),1)
|
||||
monitors = ScreenLayout((Monitor(1920, 1080,0,set("TestMon1")),))
|
||||
try:
|
||||
WallpaperBuilder().build(images, monitors, config.private_key,
|
||||
WallpaperBuilder().build(images, monitors, config.passphrase_file,
|
||||
config.wallpaper_dir,exclude=args.exclude)
|
||||
except UnidentifiedImageError as error:
|
||||
self.assertTrue(
|
||||
"cannot identify image file" in str(error))
|
||||
self.assertTrue(
|
||||
"cannot identify image file" in str(error))
|
||||
|
||||
def test_build_surfaces_image_decode_errors_corrupt_file_tail(self) -> None:
|
||||
"""Verify unreadable or undecryptable selected images produce an explicit error."""
|
||||
@@ -353,24 +353,24 @@ class WallpaperBuilderEdgeCaseTests(unittest.TestCase):
|
||||
assets = Path(__file__).resolve().parents[3] / "test" / "assets"
|
||||
args = self.parse_args("--conf-dir", str(image_root),
|
||||
"--folder", str(image_root / "Desktop1920x1080"),
|
||||
"--private-key", str(assets / "test_private_key.pem"))
|
||||
"--passphrase-file", str(assets / "test_passphrase.txt"))
|
||||
config = Config.load(args)
|
||||
(image_root / "Desktop1920x1080" ).mkdir()
|
||||
shutil.copy(assets / "Desktop1920x1080" / "Foto_Berg01.jpg",
|
||||
shutil.copy(assets / "Desktop1920x1080" / "Foto_Berg01.jpg",
|
||||
config.img_dir/"Foto_Berg01.jpg")
|
||||
# Get file size
|
||||
size = os.path.getsize(config.img_dir/"Foto_Berg01.jpg")
|
||||
# Remove the last 50 bytes
|
||||
os.truncate( config.img_dir/"Foto_Berg01.jpg", size - 50)
|
||||
images = ImageRepository(config.img_dir).scan(config.private_key)
|
||||
images = ImageRepository(config.img_dir).scan(config.passphrase_file)
|
||||
candidates = WallpaperBuilder().find_candidates(images, None,None,args.exclude)
|
||||
self.assertEqual(len(candidates),1)
|
||||
monitors = ScreenLayout((Monitor(1920, 1080,0,set("TestMon1")),))
|
||||
try:
|
||||
WallpaperBuilder().build(images, monitors, config.private_key,
|
||||
WallpaperBuilder().build(images, monitors, config.passphrase_file,
|
||||
config.wallpaper_dir,exclude=args.exclude)
|
||||
except OSError as error:
|
||||
self.assertTrue(
|
||||
self.assertTrue(
|
||||
"image file is truncated" in str(error))
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
Reference in new issue
Block a user