From b82caded294b8aec78efb37bce18ee6a1ca7a4f4 Mon Sep 17 00:00:00 2001 From: Dhwani Patel Date: Thu, 9 May 2024 12:33:46 -0600 Subject: [PATCH] Create test for fixity check signals --- storage_service/locations/models/package.py | 10 +-- tests/locations/test_package.py | 83 +++++++++++++++++++++ 2 files changed, 88 insertions(+), 5 deletions(-) diff --git a/storage_service/locations/models/package.py b/storage_service/locations/models/package.py index 32d0acc04..512402516 100644 --- a/storage_service/locations/models/package.py +++ b/storage_service/locations/models/package.py @@ -2062,13 +2062,13 @@ def get_fixity_check_report_send_signals( # Build the response (to be a JSON object) response = { "success": success, - "message": message, + "message": str(message), "failures": {"files": {"missing": [], "changed": [], "untracked": []}}, "timestamp": timestamp, } for failure in failures: if isinstance(failure, bagit.FileMissing): - info = {"path": failure.path, "message": str(failure)} + info = {"path": failure.path, "message": failure} response["failures"]["files"]["missing"].append(info) if isinstance(failure, bagit.ChecksumMismatch): info = { @@ -2076,13 +2076,13 @@ def get_fixity_check_report_send_signals( "expected": failure.expected, "actual": failure.found, "hash_type": failure.algorithm, - "message": str(failure), + "message": failure, } response["failures"]["files"]["changed"].append(info) if isinstance(failure, bagit.UnexpectedFile): - info = {"path": failure.path, "message": str(failure)} + info = {"path": failure.path, "message": failure} response["failures"]["files"]["untracked"].append(info) - report = json.dumps(response, default=str) + report = json.dumps(response) # Trigger the signals (so ``FixityLog`` instances are created) if success is False: diff --git a/tests/locations/test_package.py b/tests/locations/test_package.py index 1031ecd68..6ac89d7f1 100644 --- a/tests/locations/test_package.py +++ b/tests/locations/test_package.py @@ -1370,6 +1370,89 @@ def test_move_uncompressed_aip(self): assert pkg.current_location == dst_location +@pytest.fixture +@pytest.mark.django_db +def space(tmp_path): + space_dir = tmp_path / "space" + space_dir.mkdir() + + staging_dir = tmp_path / "staging" + staging_dir.mkdir() + + space = models.Space.objects.create( + uuid=uuid.uuid4(), + access_protocol=models.Space.LOCAL_FILESYSTEM, + path=space_dir, + staging_path=staging_dir, + ) + models.LocalFilesystem.objects.create(space=space) + return space + + +@pytest.fixture +@pytest.mark.django_db +def location(space): + aipstore = models.Location.objects.create( + uuid=uuid.uuid4(), + space=space, + relative_path="fs-aips", + purpose="AS", + ) + models.Location.objects.create( + space=space, purpose=models.Location.STORAGE_SERVICE_INTERNAL, relative_path="" + ) + return aipstore + + +@pytest.fixture +@pytest.mark.django +def bag_fixture(tmp_path): + tmp_dir = tmp_path / "dir" + tmp_dir.mkdir() + src = os.path.join(FIXTURES_DIR, "working_bag.zip") + dst = os.path.join(tmp_dir, "") + return shutil.copy(src, dst) + + +@pytest.fixture +@pytest.mark.django_db +def package(location, bag_fixture): + return models.Package.objects.create( + uuid=uuid.uuid4(), + current_location=location, + current_path=bag_fixture, + package_type="AIP", + status="Uploaded", + ) + + +@pytest.mark.django_db +@mock.patch( + "common.utils.generate_checksum", + return_value=mock.Mock( + **{ + "hexdigest.return_value": "myhash", + } + ), +) +def test_get_fixity_check_report_send_signals(generate_checksum, package): + """ + It verifies report of fixity check + """ + package.checksum = "incorrect" + package.save() + + report, response = package.get_fixity_check_report_send_signals() + + assert response == { + "success": False, + "message": "Incorrect package checksum", + "failures": {"files": {"missing": [], "changed": [], "untracked": []}}, + "timestamp": None, + } + assert response["message"] == "Incorrect package checksum" + + class TestTransferPackage(TestCase): """Test integration of transfer reading and indexing.