Skip to content

Commit

Permalink
Add unit test forcing ValueError when using MD5 (#9120)
Browse files Browse the repository at this point in the history
  • Loading branch information
PabloAlexis611 committed Mar 17, 2024
1 parent 19c4d25 commit 9700541
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions tests/repositories/test_http_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,3 +183,41 @@ def test_calculate_sha256(
calculated_hash
== "sha256:e216b70f013c47b82a72540d34347632c5bfe59fd54f5fe5d51f6a68b19aaf84"
)


def test_calculate_sha256_defaults_to_sha256_on_md5_errors(
mocker: MockerFixture,
) -> None:
raised_value_error = False

def mock_hashlib_md5_error() -> None:
nonlocal raised_value_error
raised_value_error = True
raise ValueError(
"[digital envelope routines: EVP_DigestInit_ex] disabled for FIPS"
)

filename = "poetry_core-1.5.0-py3-none-any.whl"
filepath = MockRepository.DIST_FIXTURES / filename
mock_download = mocker.patch(
"poetry.repositories.http_repository.download_file",
side_effect=lambda _, dest, *args, **kwargs: shutil.copy(filepath, dest),
)
mock_hashlib_md5 = mocker.patch("hashlib.md5", side_effect=mock_hashlib_md5_error)

domain = "foo.com"
link = Link(
f"https://{domain}/{filename}",
hashes={"md5": "be7589b4902793e66d7d979bd8581591"},
)
repo = MockRepository()

calculated_hash = repo.calculate_sha256(link)

assert raised_value_error
assert mock_download.call_count == 1
assert mock_hashlib_md5.call_count == 1
assert (
calculated_hash
== "sha256:e216b70f013c47b82a72540d34347632c5bfe59fd54f5fe5d51f6a68b19aaf84"
)

0 comments on commit 9700541

Please sign in to comment.