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

Add checksums into UI JSON, and verify them #219

Merged
merged 1 commit into from
Jul 13, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
25 changes: 25 additions & 0 deletions sacro/views.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import getpass
import hashlib
import html
import json
import logging
Expand Down Expand Up @@ -44,6 +45,10 @@ def __post_init__(self):

self.version = self.raw_metadata["version"]
self.update(self.raw_metadata["results"])
self.annotate()

def annotate(self):
"""Add various useful annotations to the JSON data"""

# add urls to JSON data
for output, metadata in self.items():
Expand All @@ -57,6 +62,26 @@ def __post_init__(self):
"contents",
)

# add and check checksum data
checksums_dir = self.path.parent / "checksums"
for output, metadata in self.items():
for filedata in metadata["files"]:
filedata["checksum_valid"] = False
filedata["checksum"] = None

path = checksums_dir / (filedata["name"] + ".txt")
if not path.exists():
continue

filedata["checksum"] = path.read_text(encoding="utf8")
actual_file = self.get_file_path(output, filedata["name"])

if not actual_file.exists(): # pragma: nocover
continue

checksum = hashlib.sha256(actual_file.read_bytes()).hexdigest()
filedata["checksum_valid"] = checksum == filedata["checksum"]

def get_file_path(self, output, filename):
"""Return absolute path to output file"""
if filename not in {
Expand Down
22 changes: 22 additions & 0 deletions tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,28 @@ def test_outputs(tmp_path):
return views.Outputs(tmp_path / TEST_PATH.name)


def test_outputs_annotation(test_outputs):
assert test_outputs.version == "0.4.0"
for metadata in test_outputs.values():
for filedata in metadata["files"]:
assert filedata["checksum"] is not None
assert filedata["checksum_valid"] is True
assert filedata["url"].startswith("/contents/?path=")


def test_outputs_annotation_checksum_failed(test_outputs):
first_output = list(test_outputs)[0]
first_file = test_outputs[first_output]["files"][0]["name"]
checksum = test_outputs.path.parent / "checksums" / (first_file + ".txt")
checksum.write_text("bad checksum")

# re-annotate
test_outputs.annotate()

assert test_outputs[first_output]["files"][0]["checksum"] == "bad checksum"
assert test_outputs[first_output]["files"][0]["checksum_valid"] is False


def test_index(test_outputs):
request = RequestFactory().get(path="/", data={"path": str(test_outputs.path)})

Expand Down