@@ -153,3 +153,71 @@ def test_get_info_from_wheel_state_sequence(mocker: MockerFixture) -> None:
153
153
repo ._get_info_from_wheel (link )
154
154
assert mock_metadata_from_wheel_url .call_count == 5
155
155
assert mock_download .call_count == 4
156
+
157
+
158
+ @pytest .mark .parametrize (
159
+ "mock_hashes" ,
160
+ [
161
+ None ,
162
+ {"sha256" : "e216b70f013c47b82a72540d34347632c5bfe59fd54f5fe5d51f6a68b19aaf84" },
163
+ {"md5" : "be7589b4902793e66d7d979bd8581591" },
164
+ ],
165
+ )
166
+ def test_calculate_sha256 (
167
+ mocker : MockerFixture , mock_hashes : dict [str , Any ] | None
168
+ ) -> None :
169
+ filename = "poetry_core-1.5.0-py3-none-any.whl"
170
+ filepath = MockRepository .DIST_FIXTURES / filename
171
+ mock_download = mocker .patch (
172
+ "poetry.repositories.http_repository.download_file" ,
173
+ side_effect = lambda _ , dest , * args , ** kwargs : shutil .copy (filepath , dest ),
174
+ )
175
+ domain = "foo.com"
176
+ link = Link (f"https://{ domain } /{ filename } " , hashes = mock_hashes )
177
+ repo = MockRepository ()
178
+
179
+ calculated_hash = repo .calculate_sha256 (link )
180
+
181
+ assert mock_download .call_count == 1
182
+ assert (
183
+ calculated_hash
184
+ == "sha256:e216b70f013c47b82a72540d34347632c5bfe59fd54f5fe5d51f6a68b19aaf84"
185
+ )
186
+
187
+
188
+ def test_calculate_sha256_defaults_to_sha256_on_md5_errors (
189
+ mocker : MockerFixture ,
190
+ ) -> None :
191
+ raised_value_error = False
192
+
193
+ def mock_hashlib_md5_error () -> None :
194
+ nonlocal raised_value_error
195
+ raised_value_error = True
196
+ raise ValueError (
197
+ "[digital envelope routines: EVP_DigestInit_ex] disabled for FIPS"
198
+ )
199
+
200
+ filename = "poetry_core-1.5.0-py3-none-any.whl"
201
+ filepath = MockRepository .DIST_FIXTURES / filename
202
+ mock_download = mocker .patch (
203
+ "poetry.repositories.http_repository.download_file" ,
204
+ side_effect = lambda _ , dest , * args , ** kwargs : shutil .copy (filepath , dest ),
205
+ )
206
+ mock_hashlib_md5 = mocker .patch ("hashlib.md5" , side_effect = mock_hashlib_md5_error )
207
+
208
+ domain = "foo.com"
209
+ link = Link (
210
+ f"https://{ domain } /{ filename } " ,
211
+ hashes = {"md5" : "be7589b4902793e66d7d979bd8581591" },
212
+ )
213
+ repo = MockRepository ()
214
+
215
+ calculated_hash = repo .calculate_sha256 (link )
216
+
217
+ assert raised_value_error
218
+ assert mock_download .call_count == 1
219
+ assert mock_hashlib_md5 .call_count == 1
220
+ assert (
221
+ calculated_hash
222
+ == "sha256:e216b70f013c47b82a72540d34347632c5bfe59fd54f5fe5d51f6a68b19aaf84"
223
+ )
0 commit comments