diff --git a/tests/decryption/test_decrypt.py b/tests/decryption/test_decrypt.py index 5fc4af6..e502718 100644 --- a/tests/decryption/test_decrypt.py +++ b/tests/decryption/test_decrypt.py @@ -5,7 +5,11 @@ from crypt4gh.keys import get_private_key as get_sk_bytes, get_public_key as get_pk_bytes import pytest -from crypt4gh_middleware.decrypt import get_private_keys, decrypt_files +from crypt4gh_middleware.decrypt import ( + get_private_keys, + decrypt_files, + move_files +) INPUT_DIR = Path(__file__).parents[2]/"inputs" INPUT_TEXT = "hello world from the input!" @@ -43,21 +47,21 @@ def get_expected_bytes(): class TestDecryptFiles: """Test decrypt_files.""" - @pytest.fixture() - def key_pair(self): + @pytest.fixture(name="key_pair") + def fixture_key_pair(self): """Returns the key pair used to encrypt the input files.""" return INPUT_DIR/"alice.sec", INPUT_DIR/"alice.pub" - @pytest.fixture() - def key_pair_bytes(self, key_pair): + @pytest.fixture(name="key_pair_bytes") + def fixture_key_pair_bytes(self, key_pair): """Returns the bytes of the key pair.""" sk, pk = key_pair sk_bytes = get_sk_bytes(filepath=sk, callback=lambda x: '') pk_bytes = get_pk_bytes(filepath=pk) return sk_bytes, pk_bytes - @pytest.fixture() - def encrypted_files(self, tmp_path): + @pytest.fixture(name="encrypted_files") + def fixture_encrypted_files(self, tmp_path): """Returns temporary copies of encrypted files.""" encrypted_files = [INPUT_DIR/"hello.c4gh", INPUT_DIR/"hello2.c4gh"] temp_files = [tmp_path/"hello.c4gh", tmp_path/"hello2.c4gh"] @@ -65,8 +69,8 @@ def encrypted_files(self, tmp_path): shutil.copy(src, dest) return temp_files - @pytest.fixture() - def unencrypted_files(self): + @pytest.fixture(name="unencrypted_files") + def fixture_unencrypted_files(self): """Returns the unencrypted file paths""" return [INPUT_DIR/"hello.txt"] @@ -97,3 +101,47 @@ def file_contents_are_valid(): assert files_exist() assert file_contents_are_valid() + + +class TestMoveFiles: + """Test move_files.""" + + @pytest.fixture(name="files") + def fixture_files(self, tmp_path): + """Returns list of input file paths.""" + files = [INPUT_DIR/"hello.txt", INPUT_DIR/"hello.c4gh", INPUT_DIR/"alice.sec"] + temp_files = [tmp_path/file.name for file in files] + for src, dest in zip(files, temp_files): + shutil.copy(src, dest) + return temp_files + + def test_empty_list(self, tmp_path): + """Test that no error is thrown with an empty list.""" + move_files(file_paths=[], output_dir=tmp_path) + assert not any(tmp_path.iterdir()) + + def test_move_files(self, files, tmp_path): + """Test that a list of unique files are moved successfully.""" + dest = tmp_path/"new_location" + dest.mkdir() + move_files(file_paths=files, output_dir=dest) + assert not any(file.exists() for file in files) + assert all((dest/file.name).exists() for file in files) + + def test_duplicate_file_names(self, tmp_path): + """Test that a value error is raised when a duplicate file name is present.""" + with pytest.raises(ValueError): + move_files(file_paths=[INPUT_DIR/"hello.txt"]*2, output_dir=tmp_path) + + def test_dir_does_not_exist(self, files): + """Test that a file not found error is raised with a non-existent directory.""" + with pytest.raises(FileNotFoundError): + move_files(file_paths=files, output_dir=INPUT_DIR/"bad_dir") + + def test_permission_error(self, tmp_path): + """Test that a permission error is raised when the output directory is not writable.""" + output_dir = tmp_path/"forbidden_dir" + output_dir.mkdir() + output_dir.chmod(0o400) + with pytest.raises(PermissionError): + move_files(file_paths=[INPUT_DIR/"hello.txt"], output_dir=output_dir)