mirror of
https://github.com/PR0M3TH3AN/SeedPass.git
synced 2025-09-08 07:18:47 +00:00
Merge pull request #37 from PR0M3TH3AN/codex/add-encryption-file-tests
Add encryption file tests
This commit is contained in:
41
src/tests/test_encryption_files.py
Normal file
41
src/tests/test_encryption_files.py
Normal file
@@ -0,0 +1,41 @@
|
||||
import json
|
||||
import sys
|
||||
from pathlib import Path
|
||||
from tempfile import TemporaryDirectory
|
||||
|
||||
from cryptography.fernet import Fernet
|
||||
|
||||
sys.path.append(str(Path(__file__).resolve().parents[1]))
|
||||
|
||||
from password_manager.encryption import EncryptionManager
|
||||
|
||||
|
||||
def test_json_save_and_load_round_trip():
|
||||
with TemporaryDirectory() as tmpdir:
|
||||
key = Fernet.generate_key()
|
||||
manager = EncryptionManager(key, Path(tmpdir))
|
||||
|
||||
data = {"hello": "world", "nums": [1, 2, 3]}
|
||||
manager.save_json_data(data)
|
||||
loaded = manager.load_json_data()
|
||||
assert loaded == data
|
||||
|
||||
file_path = Path(tmpdir) / "seedpass_passwords_db.json.enc"
|
||||
raw = file_path.read_bytes()
|
||||
assert raw != json.dumps(data, indent=4).encode("utf-8")
|
||||
|
||||
|
||||
def test_encrypt_and_decrypt_file_binary_round_trip():
|
||||
with TemporaryDirectory() as tmpdir:
|
||||
key = Fernet.generate_key()
|
||||
manager = EncryptionManager(key, Path(tmpdir))
|
||||
|
||||
payload = b"binary secret"
|
||||
rel = Path("payload.bin.enc")
|
||||
manager.encrypt_and_save_file(payload, rel)
|
||||
decrypted = manager.decrypt_file(rel)
|
||||
assert decrypted == payload
|
||||
|
||||
file_path = Path(tmpdir) / rel
|
||||
raw = file_path.read_bytes()
|
||||
assert raw != payload
|
Reference in New Issue
Block a user