From fef676f312c52b80c375e53f1c9dcc2424268aaa Mon Sep 17 00:00:00 2001 From: Athitheya Gobinathan Date: Wed, 17 Jul 2024 18:20:16 -0400 Subject: [PATCH 1/7] test: add move_files tests --- tests/decryption/test_decrypt.py | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/tests/decryption/test_decrypt.py b/tests/decryption/test_decrypt.py index 5fc4af6..8cba38d 100644 --- a/tests/decryption/test_decrypt.py +++ b/tests/decryption/test_decrypt.py @@ -5,7 +5,12 @@ 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, + remove_files +) INPUT_DIR = Path(__file__).parents[2]/"inputs" INPUT_TEXT = "hello world from the input!" @@ -97,3 +102,26 @@ def file_contents_are_valid(): assert files_exist() assert file_contents_are_valid() + + +class TestMoveFiles: + """Test move_files.""" + + @pytest.fixture() + def files(self): + """Returns list of input file paths.""" + return [INPUT_DIR/"hello.txt", INPUT_DIR/"hello.c4gh", INPUT_DIR/"alice.sec"] + + 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) + + def test_move_files(self, files, tmp_path): + """Test that a list of unique files are moved successfully.""" + move_files(file_paths=files, output_dir=tmp_path) + assert all((tmp_path/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) From d12002d57005027c66c11a13fcc49defeb08708b Mon Sep 17 00:00:00 2001 From: Athitheya Gobinathan Date: Wed, 17 Jul 2024 18:42:52 -0400 Subject: [PATCH 2/7] test: fix temp file issues --- tests/decryption/test_decrypt.py | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/tests/decryption/test_decrypt.py b/tests/decryption/test_decrypt.py index 8cba38d..f4e716c 100644 --- a/tests/decryption/test_decrypt.py +++ b/tests/decryption/test_decrypt.py @@ -108,9 +108,13 @@ class TestMoveFiles: """Test move_files.""" @pytest.fixture() - def files(self): + def files(self, tmp_path): """Returns list of input file paths.""" - return [INPUT_DIR/"hello.txt", INPUT_DIR/"hello.c4gh", INPUT_DIR/"alice.sec"] + 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.""" @@ -118,10 +122,18 @@ def test_empty_list(self, tmp_path): def test_move_files(self, files, tmp_path): """Test that a list of unique files are moved successfully.""" - move_files(file_paths=files, output_dir=tmp_path) - assert all((tmp_path/file.name).exists() for file in files) + 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): + with pytest.raises(FileNotFoundError): + move_files(file_paths=files, output_dir=INPUT_DIR/"bad_dir") + From f4500b53e76a461be1a2bc4d8d8fe7d8a4338ab8 Mon Sep 17 00:00:00 2001 From: Athitheya Gobinathan Date: Wed, 17 Jul 2024 18:44:46 -0400 Subject: [PATCH 3/7] test: fix minor issues --- tests/decryption/test_decrypt.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/tests/decryption/test_decrypt.py b/tests/decryption/test_decrypt.py index f4e716c..9061859 100644 --- a/tests/decryption/test_decrypt.py +++ b/tests/decryption/test_decrypt.py @@ -8,8 +8,7 @@ from crypt4gh_middleware.decrypt import ( get_private_keys, decrypt_files, - move_files, - remove_files + move_files ) INPUT_DIR = Path(__file__).parents[2]/"inputs" @@ -134,6 +133,6 @@ def test_duplicate_file_names(self, tmp_path): 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") - From d976373115afb3d04f2f2cc0616d102299281b25 Mon Sep 17 00:00:00 2001 From: Athitheya Gobinathan Date: Wed, 17 Jul 2024 18:53:29 -0400 Subject: [PATCH 4/7] test: add sorcery suggestions --- tests/decryption/test_decrypt.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/tests/decryption/test_decrypt.py b/tests/decryption/test_decrypt.py index 9061859..4eb3aa1 100644 --- a/tests/decryption/test_decrypt.py +++ b/tests/decryption/test_decrypt.py @@ -118,6 +118,7 @@ def files(self, tmp_path): 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.""" @@ -136,3 +137,11 @@ 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, files, 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) From 6d9d0e454839f5059253211d6c07b5da73042385 Mon Sep 17 00:00:00 2001 From: Athitheya Gobinathan Date: Wed, 17 Jul 2024 18:54:17 -0400 Subject: [PATCH 5/7] fix: remove unused argument --- tests/decryption/test_decrypt.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/decryption/test_decrypt.py b/tests/decryption/test_decrypt.py index 4eb3aa1..8780bc9 100644 --- a/tests/decryption/test_decrypt.py +++ b/tests/decryption/test_decrypt.py @@ -138,7 +138,7 @@ def test_dir_does_not_exist(self, files): with pytest.raises(FileNotFoundError): move_files(file_paths=files, output_dir=INPUT_DIR/"bad_dir") - def test_permission_error(self, files, tmp_path): + 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() From 38ba2fb182133464598f380fa2df65dd4d5244d8 Mon Sep 17 00:00:00 2001 From: Athitheya Gobinathan Date: Wed, 17 Jul 2024 18:54:54 -0400 Subject: [PATCH 6/7] refactor: make style consistent --- tests/decryption/test_decrypt.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/decryption/test_decrypt.py b/tests/decryption/test_decrypt.py index 8780bc9..b5cf9d6 100644 --- a/tests/decryption/test_decrypt.py +++ b/tests/decryption/test_decrypt.py @@ -140,7 +140,7 @@ def test_dir_does_not_exist(self, files): 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 = tmp_path/"forbidden_dir" output_dir.mkdir() output_dir.chmod(0o400) with pytest.raises(PermissionError): From 736ae79be2ba0fdf63c646fe2d0aff929e6827c5 Mon Sep 17 00:00:00 2001 From: Athitheya Gobinathan Date: Thu, 25 Jul 2024 12:36:12 -0400 Subject: [PATCH 7/7] refactor: add fixture prefix --- tests/decryption/test_decrypt.py | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/tests/decryption/test_decrypt.py b/tests/decryption/test_decrypt.py index b5cf9d6..e502718 100644 --- a/tests/decryption/test_decrypt.py +++ b/tests/decryption/test_decrypt.py @@ -47,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"] @@ -69,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"] @@ -106,8 +106,8 @@ def file_contents_are_valid(): class TestMoveFiles: """Test move_files.""" - @pytest.fixture() - def files(self, tmp_path): + @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]