Skip to content

Commit 465e6ce

Browse files
committed
refactor: extract image validation to a separate function
Extracted the image validation logic to a separate function `_check_image` for better readability and maintainability. This change ensures that the image validation is more modular and easier to understand.
1 parent 87bf445 commit 465e6ce

File tree

1 file changed

+34
-27
lines changed

1 file changed

+34
-27
lines changed

cardano_node_tests/utils/dbsync_utils.py

Lines changed: 34 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1322,6 +1322,39 @@ def check_off_chain_drep_registration( # noqa: C901
13221322
db_metadata = drep_off_chain_metadata[0]
13231323
expected_metadata = metadata["body"]
13241324

1325+
# For the image rules refer to: https://cips.cardano.org/cip/CIP-0119
1326+
# off_chain_drep_data.image_url includes the image base64 with the data uri
1327+
# header prefix stripped and the off_chain_drep_data.image_sha256 remains empty.
1328+
def _check_image() -> None:
1329+
if "image" not in expected_metadata:
1330+
return
1331+
1332+
image_data = expected_metadata["image"]
1333+
1334+
match image_data.get("contentUrl"):
1335+
case None:
1336+
errors.append("Invalid metadata: image 'contentUrl' must be provided.")
1337+
case content_url if content_url.startswith("data:"):
1338+
if not content_url.startswith("data:image/"):
1339+
errors.append(
1340+
"Invalid metadata: base64 encoded image should start with 'data:image/'"
1341+
)
1342+
if "base64" in content_url:
1343+
base64_image_data = content_url.split("base64,")[1]
1344+
if db_metadata.image_url != base64_image_data:
1345+
errors.append(
1346+
"'image_url' value is different than expected Base64 'contentUrl';"
1347+
)
1348+
else:
1349+
errors.append("Invalid metadata: image is not Base64 encoded")
1350+
case content_url:
1351+
if db_metadata.image_url != content_url:
1352+
errors.append("'image_url' value is different than expected 'contentUrl';")
1353+
if "sha256" not in image_data:
1354+
errors.append("SHA256 hash is required for image URL.")
1355+
elif db_metadata.image_hash != image_data["sha256"]:
1356+
errors.append("'image_hash' value is different than expected;")
1357+
13251358
if db_metadata.payment_address != expected_metadata["paymentAddress"]:
13261359
errors.append("'paymentAddress' value is different than expected;")
13271360

@@ -1337,33 +1370,7 @@ def check_off_chain_drep_registration( # noqa: C901
13371370
if db_metadata.qualifications != expected_metadata["qualifications"]:
13381371
errors.append("'qualifications' value is different than expected;")
13391372

1340-
# For the image rules refer to: https://cips.cardano.org/cip/CIP-0119
1341-
# off_chain_drep_data.image_url includes the image base64 with the data uri
1342-
# header prefix stripped and the off_chain_drep_data.image_sha256 remains empty
1343-
if "image" in expected_metadata:
1344-
image_data = expected_metadata["image"]
1345-
1346-
if "contentUrl" in image_data:
1347-
content_url = image_data["contentUrl"]
1348-
if content_url.startswith("data:"):
1349-
if "base64" not in content_url:
1350-
errors.append("Image is not Base64 encoded")
1351-
elif not content_url.startswith("data:image/"):
1352-
errors.append("Base64 encoded image should start with 'data:image/'")
1353-
base64_image_data = content_url.split("base64,")[1]
1354-
if db_metadata.image_url != base64_image_data:
1355-
errors.append(
1356-
"'image_url' value is different than expected Base64 'contentUrl';"
1357-
)
1358-
else:
1359-
if db_metadata.image_url != content_url:
1360-
errors.append("'image_url' value is different than expected 'contentUrl';")
1361-
if "sha256" not in image_data:
1362-
errors.append("SHA256 hash is required for image URL.")
1363-
elif db_metadata.image_hash != image_data["sha256"]:
1364-
errors.append("'image_hash' value is different than expected;")
1365-
else:
1366-
errors.append("Image contentUrl must be provided.")
1373+
_check_image()
13671374

13681375
if errors:
13691376
raise AssertionError("\n".join(errors))

0 commit comments

Comments
 (0)