Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix JSON serialization for fixity failure report #713

Merged
merged 5 commits into from
May 13, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions storage_service/locations/models/package.py
Original file line number Diff line number Diff line change
Expand Up @@ -2062,25 +2062,25 @@
# 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}

Check warning on line 2071 in storage_service/locations/models/package.py

View check run for this annotation

Codecov / codecov/patch

storage_service/locations/models/package.py#L2071

Added line #L2071 was not covered by tests
replaceafill marked this conversation as resolved.
Show resolved Hide resolved
response["failures"]["files"]["missing"].append(info)
if isinstance(failure, bagit.ChecksumMismatch):
info = {
"path": failure.path,
"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}

Check warning on line 2083 in storage_service/locations/models/package.py

View check run for this annotation

Codecov / codecov/patch

storage_service/locations/models/package.py#L2083

Added line #L2083 was not covered by tests
response["failures"]["files"]["untracked"].append(info)
report = json.dumps(response)

Expand Down
83 changes: 83 additions & 0 deletions tests/locations/test_package.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
replaceafill marked this conversation as resolved.
Show resolved Hide resolved
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=""
)
replaceafill marked this conversation as resolved.
Show resolved Hide resolved
return aipstore


@pytest.fixture
@pytest.mark.django
replaceafill marked this conversation as resolved.
Show resolved Hide resolved
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,
replaceafill marked this conversation as resolved.
Show resolved Hide resolved
package_type="AIP",
status="Uploaded",
)


@pytest.mark.django_db
@mock.patch(
"common.utils.generate_checksum",
return_value=mock.Mock(
**{
"hexdigest.return_value": "myhash",
replaceafill marked this conversation as resolved.
Show resolved Hide resolved
}
),
)
def test_get_fixity_check_report_send_signals(generate_checksum, package):
"""
It verifies report of fixity check
"""
replaceafill marked this conversation as resolved.
Show resolved Hide resolved
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"
replaceafill marked this conversation as resolved.
Show resolved Hide resolved


class TestTransferPackage(TestCase):
"""Test integration of transfer reading and indexing.

Expand Down