|
| 1 | +# fetchcode is a free software tool from nexB Inc. and others. |
| 2 | +# Visit https://github.com/aboutcode-org/fetchcode for support and download. |
| 3 | +# |
| 4 | +# Copyright (c) nexB Inc. and others. All rights reserved. |
| 5 | +# http://nexb.com and http://aboutcode.org |
| 6 | +# |
| 7 | +# This software is licensed under the Apache License version 2.0. |
| 8 | +# |
| 9 | +# You may not use this software except in compliance with the License. |
| 10 | +# You may obtain a copy of the License at: |
| 11 | +# http://apache.org/licenses/LICENSE-2.0 |
| 12 | +# Unless required by applicable law or agreed to in writing, software distributed |
| 13 | +# under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR |
| 14 | +# CONDITIONS OF ANY KIND, either express or implied. See the License for the |
| 15 | +# specific language governing permissions and limitations under the License. |
| 16 | + |
| 17 | +from unittest.mock import patch |
| 18 | + |
| 19 | +import pytest |
| 20 | + |
| 21 | +from fetchcode.composer import Composer |
| 22 | + |
| 23 | + |
| 24 | +def test_valid_composer_package_with_namespace(): |
| 25 | + purl = "pkg:composer/laravel/[email protected]" |
| 26 | + name = "laravel/framework" |
| 27 | + expected_url = f"https://repo.packagist.org/p2/{name}.json " |
| 28 | + download_url = "https://github.com/laravel/framework/archive/refs/tags/v10.0.0.zip" |
| 29 | + |
| 30 | + mock_data = {"packages": {name: [{"version": "10.0.0", "dist": {"url": download_url}}]}} |
| 31 | + |
| 32 | + with patch("fetchcode.composer.fetch_json_response", return_value=mock_data) as mock_fetch: |
| 33 | + result = Composer.get_download_url(purl) |
| 34 | + assert result == download_url |
| 35 | + mock_fetch.assert_called_once_with(expected_url) |
| 36 | + |
| 37 | + |
| 38 | +def test_valid_composer_package_without_namespace(): |
| 39 | + purl = "pkg:composer/[email protected]" |
| 40 | + name = "some-package" |
| 41 | + expected_url = f"https://repo.packagist.org/p2/{name}.json " |
| 42 | + download_url = "https://example.org/some-package-1.0.0.zip" |
| 43 | + |
| 44 | + mock_data = {"packages": {name: [{"version": "1.0.0", "dist": {"url": download_url}}]}} |
| 45 | + |
| 46 | + with patch("fetchcode.composer.fetch_json_response", return_value=mock_data) as mock_fetch: |
| 47 | + result = Composer.get_download_url(purl) |
| 48 | + assert result == download_url |
| 49 | + mock_fetch.assert_called_once_with(expected_url) |
| 50 | + |
| 51 | + |
| 52 | +def test_version_not_found_returns_none(): |
| 53 | + purl = "pkg:composer/laravel/[email protected]" |
| 54 | + name = "laravel/framework" |
| 55 | + mock_data = {"packages": {name: [{"version": "9.0.0", "dist": {"url": "https://old.zip"}}]}} |
| 56 | + |
| 57 | + with patch("fetchcode.composer.fetch_json_response", return_value=mock_data): |
| 58 | + result = Composer.get_download_url(purl) |
| 59 | + assert result is None |
| 60 | + |
| 61 | + |
| 62 | +def test_missing_packages_key_returns_none(): |
| 63 | + purl = "pkg:composer/laravel/[email protected]" |
| 64 | + with patch("fetchcode.composer.fetch_json_response", return_value={}): |
| 65 | + result = Composer.get_download_url(purl) |
| 66 | + assert result is None |
| 67 | + |
| 68 | + |
| 69 | +def test_missing_package_name_in_data_returns_none(): |
| 70 | + purl = "pkg:composer/laravel/[email protected]" |
| 71 | + mock_data = {"packages": {"some/other": []}} |
| 72 | + |
| 73 | + with patch("fetchcode.composer.fetch_json_response", return_value=mock_data): |
| 74 | + result = Composer.get_download_url(purl) |
| 75 | + assert result is None |
| 76 | + |
| 77 | + |
| 78 | +def test_missing_version_raises(): |
| 79 | + purl = "pkg:composer/laravel/framework" |
| 80 | + with pytest.raises(ValueError, match="Composer PURL must specify a name and version"): |
| 81 | + Composer.get_download_url(purl) |
0 commit comments