|
| 1 | +import unittest |
| 2 | +from unittest.mock import patch |
| 3 | + |
| 4 | +from fetchcode.pypi import Pypi |
| 5 | + |
| 6 | + |
| 7 | +class TestGetDownloadURL(unittest.TestCase): |
| 8 | + @patch("fetchcode.pypi.fetch_json_response") |
| 9 | + def test_valid_purl_returns_download_url(self, mock_fetch_json_response): |
| 10 | + mock_response = { |
| 11 | + "urls": [ |
| 12 | + { |
| 13 | + "url": "https://files.pythonhosted.org/packages/source/r/requests/requests-2.31.0.tar.gz" |
| 14 | + } |
| 15 | + ] |
| 16 | + } |
| 17 | + mock_fetch_json_response.return_value = mock_response |
| 18 | + |
| 19 | + purl = "pkg:pypi/[email protected]" |
| 20 | + result = Pypi.get_download_url(purl) |
| 21 | + self.assertEqual( |
| 22 | + result, |
| 23 | + "https://files.pythonhosted.org/packages/source/r/requests/requests-2.31.0.tar.gz", |
| 24 | + ) |
| 25 | + |
| 26 | + @patch("fetchcode.pypi.fetch_json_response") |
| 27 | + def test_missing_version_raises_value_error(self, mock_fetch_json_response): |
| 28 | + purl = "pkg:pypi/requests" |
| 29 | + with self.assertRaises(ValueError) as context: |
| 30 | + Pypi.get_download_url(purl) |
| 31 | + self.assertIn("Pypi PURL must specify a name and version", str(context.exception)) |
| 32 | + |
| 33 | + @patch("fetchcode.pypi.fetch_json_response") |
| 34 | + def test_missing_name_raises_value_error(self, mock_fetch_json_response): |
| 35 | + purl = "pkg:pypi/@2.31.0" |
| 36 | + with self.assertRaises(ValueError) as context: |
| 37 | + Pypi.get_download_url(purl) |
| 38 | + self.assertIn("purl is missing the required name component", str(context.exception)) |
| 39 | + |
| 40 | + @patch("fetchcode.pypi.fetch_json_response") |
| 41 | + def test_missing_urls_field_raises_value_error(self, mock_fetch_json_response): |
| 42 | + mock_fetch_json_response.return_value = {} |
| 43 | + purl = "pkg:pypi/[email protected]" |
| 44 | + with self.assertRaises(ValueError) as context: |
| 45 | + Pypi.get_download_url(purl) |
| 46 | + self.assertIn("No download URL found", str(context.exception)) |
| 47 | + |
| 48 | + @patch("fetchcode.pypi.fetch_json_response") |
| 49 | + def test_empty_urls_list_raises_value_error(self, mock_fetch_json_response): |
| 50 | + mock_fetch_json_response.return_value = {"urls": []} |
| 51 | + purl = "pkg:pypi/[email protected]" |
| 52 | + with self.assertRaises(ValueError) as context: |
| 53 | + Pypi.get_download_url(purl) |
| 54 | + self.assertIn("No download URLs found", str(context.exception)) |
| 55 | + |
| 56 | + @patch("fetchcode.pypi.fetch_json_response") |
| 57 | + def test_first_url_object_missing_url_key(self, mock_fetch_json_response): |
| 58 | + mock_fetch_json_response.return_value = {"urls": [{}]} |
| 59 | + purl = "pkg:pypi/[email protected]" |
| 60 | + with self.assertRaises(ValueError) as context: |
| 61 | + Pypi.get_download_url(purl) |
| 62 | + self.assertIn("No download URL found", str(context.exception)) |
| 63 | + |
| 64 | + @patch("fetchcode.pypi.fetch_json_response") |
| 65 | + def test_url_fallback_when_multiple_urls_provided(self, mock_fetch_json_response): |
| 66 | + mock_fetch_json_response.return_value = { |
| 67 | + "urls": [{}, {"url": "https://example.com/fallback-url.tar.gz"}] |
| 68 | + } |
| 69 | + |
| 70 | + purl = "pkg:pypi/[email protected]" |
| 71 | + download_url = Pypi.get_download_url(purl) |
| 72 | + self.assertEqual(download_url, "https://example.com/fallback-url.tar.gz") |
| 73 | + |
| 74 | + def test_malformed_purl_raises_exception(self): |
| 75 | + with self.assertRaises(ValueError): |
| 76 | + Pypi.get_download_url("this-is-not-a-valid-purl") |
0 commit comments