|
| 1 | +# |
| 2 | +# Copyright (c) nexB Inc. and others. All rights reserved. |
| 3 | +# purldb is a trademark of nexB Inc. |
| 4 | +# SPDX-License-Identifier: Apache-2.0 |
| 5 | +# See http://www.apache.org/licenses/LICENSE-2.0 for the license text. |
| 6 | +# See https://github.com/aboutcode-org/purldb for support or download. |
| 7 | +# See https://aboutcode.org for more information about nexB OSS projects. |
| 8 | +# |
| 9 | + |
| 10 | +import unittest |
| 11 | +from unittest.mock import patch |
| 12 | + |
| 13 | +from packageurl import PackageURL |
| 14 | + |
| 15 | +from packagedb.purl_url_utils import derive_download_url |
| 16 | +from packagedb.purl_url_utils import generate_synthetic_download_url |
| 17 | + |
| 18 | + |
| 19 | +class TestDeriveDownloadURL(unittest.TestCase): |
| 20 | + def test_provided_url_takes_precedence(self): |
| 21 | + provided = "https://example.com/lodash-4.17.21.tgz" |
| 22 | + result = derive_download_url("pkg:npm/lodash@4.17.21", provided) |
| 23 | + self.assertEqual(result, provided) |
| 24 | + |
| 25 | + @patch("packagedb.purl_url_utils.purl2url.get_download_url") |
| 26 | + def test_infers_url_from_purl(self, mock_get_download): |
| 27 | + expected = "https://rubygems.org/downloads/bundler-2.3.23.gem" |
| 28 | + mock_get_download.return_value = expected |
| 29 | + |
| 30 | + result = derive_download_url("pkg:gem/bundler@2.3.23") |
| 31 | + |
| 32 | + mock_get_download.assert_called_once_with("pkg:gem/bundler@2.3.23") |
| 33 | + self.assertEqual(result, expected) |
| 34 | + |
| 35 | + @patch("packagedb.purl_url_utils.purl2url.get_download_url") |
| 36 | + def test_falls_back_to_synthetic_url(self, mock_get_download): |
| 37 | + mock_get_download.side_effect = Exception("cannot infer") |
| 38 | + |
| 39 | + result = derive_download_url("pkg:generic/some-package@1.0.0") |
| 40 | + |
| 41 | + self.assertTrue(result.startswith("purl://")) |
| 42 | + self.assertIn("generic/some-package@1.0.0", result) |
| 43 | + |
| 44 | + def test_invalid_purl_does_not_raise(self): |
| 45 | + # Last-resort fallback: returns a purl:-prefixed string |
| 46 | + result = derive_download_url("not-a-valid-purl") |
| 47 | + self.assertIsNotNone(result) |
| 48 | + self.assertIn("purl:", result) |
| 49 | + |
| 50 | + |
| 51 | +class TestGenerateSyntheticDownloadURL(unittest.TestCase): |
| 52 | + def test_basic(self): |
| 53 | + purl = PackageURL.from_string("pkg:npm/express@4.17.1") |
| 54 | + self.assertEqual(generate_synthetic_download_url(purl), "purl://npm/express@4.17.1") |
| 55 | + |
| 56 | + def test_includes_namespace(self): |
| 57 | + purl = PackageURL.from_string("pkg:maven/org.apache.commons/commons-lang3@3.12.0") |
| 58 | + url = generate_synthetic_download_url(purl) |
| 59 | + self.assertTrue(url.startswith("purl://maven/org.apache.commons/")) |
| 60 | + self.assertIn("commons-lang3@3.12.0", url) |
| 61 | + |
| 62 | + def test_qualifiers_differentiate_packages(self): |
| 63 | + # Maven JARs with different classifiers must produce different URLs |
| 64 | + purl1 = PackageURL.from_string("pkg:maven/com.example/lib@1.0.0") |
| 65 | + purl2 = PackageURL.from_string("pkg:maven/com.example/lib@1.0.0?classifier=sources") |
| 66 | + self.assertNotEqual( |
| 67 | + generate_synthetic_download_url(purl1), |
| 68 | + generate_synthetic_download_url(purl2), |
| 69 | + ) |
| 70 | + |
| 71 | + def test_no_version(self): |
| 72 | + purl = PackageURL.from_string("pkg:npm/express") |
| 73 | + url = generate_synthetic_download_url(purl) |
| 74 | + self.assertNotIn("@", url) |
| 75 | + |
| 76 | + def test_includes_subpath(self): |
| 77 | + purl = PackageURL.from_string("pkg:github/user/repo@v1.0#path/to/file") |
| 78 | + url = generate_synthetic_download_url(purl) |
| 79 | + self.assertIn("#path/to/file", url) |
| 80 | + |
| 81 | + |
| 82 | +if __name__ == "__main__": |
| 83 | + unittest.main(verbosity=2) |
0 commit comments