def test_verify_checksum_unsupported_algorithm(tmpdir): file = tmpdir.join("spells.txt") file.write("Beetlejuice! Beetlejuice! Beetlejuice!") expected_error = "Cannot perform checksum on the file spells.txt,.*bacon.*" with pytest.raises(CachitoError, match=expected_error): verify_checksum(str(file), ChecksumInfo("bacon", "spam"))
def test_verify_checksum_invalid_hexdigest(tmpdir): file = tmpdir.join("spells.txt") file.write("Beetlejuice! Beetlejuice! Beetlejuice!") expected_error = "The file spells.txt has an unexpected checksum value" with pytest.raises(CachitoError, match=expected_error): verify_checksum(str(file), ChecksumInfo("sha512", "spam"))
def test_verify_checksum(tmpdir): file = tmpdir.join("spells.txt") file.write("Beetlejuice! Beetlejuice! Beetlejuice!") expected = { "sha512": ( "da518fe8b800b3325fe35ca680085fe37626414d0916937a01a25ef8f5d7aa769b7233073235fce85ee" "c717e02bb9d72062656cf2d79223792a784910c267b54" ), "sha256": "ed1f8cd69bfacf0528744b6a7084f36e8841b6128de0217503e215612a0ee835", "md5": "308764bc995153f7d853827a675e6731", } for algorithm, checksum in expected.items(): verify_checksum(str(file), ChecksumInfo(algorithm, checksum))
def convert_integrity_to_hex_checksum(integrity): """ Convert the input integrity value of a dependency to a hex checksum. The integrity is a key in an npm lock file that contains the checksum of the dependency in the format of ``<algorithm>-<base64 of the binary hash>``. :param str integrity: the integrity from the npm lock file :return: a tuple where the first item is the algorithm used and second is the hex value of the checksum :rtype: (str, str) """ algorithm, checksum = integrity.split("-", 1) return ChecksumInfo(algorithm, base64.b64decode(checksum).hex())
def test_convert_to_nexus_hosted_http(mock_unrd, mock_gncifn, exists): checksum = ( "325f07861e0ab888d90606b1074fde956fd3954dcc4c6e418dbff9d8aa8342b5507481408832bfaac8e48f344" "dc650c8df0f8182c0271ed9fa233aa32c329839") nexus_component_info = { "assets": [{ "checksum": { "sha512": checksum }, "downloadUrl": ("https://nexus.domain.local/repository/cachito-js-hosted/rxjs/-/" "rxjs-6.5.5-external-sha512-325f07861e0ab888d90606b1074fde956fd3954dcc4c6e418d" "bff9d8aa8342b5507481408832bfaac8e48f344.tgz"), }], "version": ("6.5.5-external-sha512-325f07861e0ab888d90606b1074fde956fd3954dcc4c6e418dbff9d8aa8342" "b5507481408832bfaac8e48f344"), } if exists: mock_gncifn.return_value = nexus_component_info else: mock_gncifn.side_effect = [None, nexus_component_info] dep_name = "rxjs" dep_info = { "version": "https://github.com/ReactiveX/rxjs/archive/6.5.5.tar.gz", "requires": { "tslib": "^1.9.0" }, "integrity": ("sha512-Ml8Hhh4KuIjZBgaxB0/elW/TlU3MTG5Bjb/52KqDQrVQdIFAiDK/qsjkjzRNxlDI3w+BgsAnHtn6I" "zqjLDKYOQ=="), } new_dep_info = npm.convert_to_nexus_hosted(dep_name, dep_info) assert new_dep_info == { "integrity": ("sha512-Ml8Hhh4KuIjZBgaxB0/elW/TlU3MTG5Bjb/52KqDQrVQdIFAiDK/qsjkjzRNxlDI3w+BgsAnHtn6I" "zqjLDKYOQ=="), "requires": { "tslib": "^1.9.0" }, "resolved": ("https://nexus.domain.local/repository/cachito-js-hosted/rxjs/-/rxjs-6.5.5-" "external-sha512-325f07861e0ab888d90606b1074fde956fd3954dcc4c6e418dbff9d8aa8342b55074" "81408832bfaac8e48f344.tgz"), "version": ("6.5.5-external-sha512-325f07861e0ab888d90606b1074fde956fd3954dcc4c6e418dbff9d8" "aa8342b5507481408832bfaac8e48f344"), } suffix = ( "-external-sha512-325f07861e0ab888d90606b1074fde956fd3954dcc4c6e418dbff9d8aa8342b5" "507481408832bfaac8e48f344dc650c8df0f8182c0271ed9fa233aa32c329839") suffix_search = f"*{suffix}" if exists: mock_gncifn.assert_called_once_with("rxjs", suffix_search) # Verify no upload occurs when the component already exists in Nexus mock_unrd.assert_not_called() else: assert mock_gncifn.call_count == 2 mock_gncifn.assert_has_calls([ mock.call("rxjs", suffix_search), mock.call("rxjs", suffix_search, max_attempts=5) ]) mock_unrd.assert_called_once_with( "https://github.com/ReactiveX/rxjs/archive/6.5.5.tar.gz", suffix, False, ChecksumInfo( "sha512", "325f07861e0ab888d90606b1074fde956fd3954dcc4c6e418dbff9d8aa8342b5507481408832bf" "aac8e48f344dc650c8df0f8182c0271ed9fa233aa32c329839", ), )