|
19 | 19 | import pytest |
20 | 20 |
|
21 | 21 | from fetchcode import fetch |
| 22 | +from fetchcode import resolve_purl |
| 23 | +from fetchcode import resolve_url_from_purl |
22 | 24 |
|
23 | 25 |
|
24 | 26 | @mock.patch("fetchcode.requests.get") |
@@ -63,3 +65,123 @@ def test_fetch_with_scheme_not_present(): |
63 | 65 | url = "abc://speedtest/1KB.zip" |
64 | 66 | response = fetch(url=url) |
65 | 67 | assert "Not a supported/known scheme." == e_info |
| 68 | + |
| 69 | + |
| 70 | +@mock.patch("fetchcode._http_exists") |
| 71 | +@mock.patch("fetchcode.fetch_http") |
| 72 | +@mock.patch("fetchcode.pypi.fetch_json_response") |
| 73 | +def test_fetch_purl_with_fetchcode(mock_fetch_json_response, mock_fetch_http, mock_http_exists): |
| 74 | + mock_fetch_http.return_value = "mocked_purl_response" |
| 75 | + mock_http_exists.return_value = True |
| 76 | + mock_fetch_json_response.return_value = { |
| 77 | + "urls": [{"url": "https://example.com/sample-1.0.0.zip"}] |
| 78 | + } |
| 79 | + |
| 80 | + response = fetch("pkg:pypi/sample@1.0.0") |
| 81 | + |
| 82 | + assert response == "mocked_purl_response" |
| 83 | + mock_http_exists.assert_called_once() |
| 84 | + mock_fetch_http.assert_called_once() |
| 85 | + |
| 86 | + |
| 87 | +@mock.patch("fetchcode._http_exists") |
| 88 | +@mock.patch("fetchcode.fetch_http") |
| 89 | +def test_fetch_purl_with_purl2url(mock_fetch_http, mock_http_exists): |
| 90 | + mock_fetch_http.return_value = "mocked_purl_response" |
| 91 | + mock_http_exists.return_value = True |
| 92 | + |
| 93 | + response = fetch("pkg:alpm/sample@1.0.0") |
| 94 | + |
| 95 | + assert response == "mocked_purl_response" |
| 96 | + mock_http_exists.assert_called_once() |
| 97 | + mock_fetch_http.assert_called_once() |
| 98 | + |
| 99 | + |
| 100 | +@mock.patch("fetchcode.pypi.fetch_json_response") |
| 101 | +def test_fetch_invalid_purl(mock_fetch_json_response): |
| 102 | + mock_fetch_json_response.return_value = {} |
| 103 | + |
| 104 | + with pytest.raises(Exception, match="No download URL found for invalid-package version 1.0.0"): |
| 105 | + fetch("pkg:pypi/invalid-package@1.0.0") |
| 106 | + |
| 107 | + |
| 108 | +@mock.patch("fetchcode.pypi.fetch_json_response") |
| 109 | +def test_fetch_invalid_purl(mock_fetch_json_response): |
| 110 | + mock_fetch_json_response.return_value = {} |
| 111 | + |
| 112 | + with pytest.raises(Exception, match="No download URL found for invalid-package version 1.0.0"): |
| 113 | + fetch("pkg:pypi/invalid-package@1.0.0") |
| 114 | + |
| 115 | + |
| 116 | +def test_fetch_unsupported_scheme(): |
| 117 | + with pytest.raises(Exception, match="Not a supported/known scheme"): |
| 118 | + fetch("s3://bucket/object") |
| 119 | + |
| 120 | + |
| 121 | +def test_resolve_url_from_purl_invalid(): |
| 122 | + with pytest.raises(ValueError, match="Could not resolve PURL to a valid URL."): |
| 123 | + fetch("pkg:invalid/invalid-package@1.0.0") |
| 124 | + |
| 125 | + |
| 126 | +@mock.patch("fetchcode._http_exists") |
| 127 | +def test_resolve_url_from_purl_using_purl2url(mock_http_exists): |
| 128 | + mock_http_exists.return_value = True |
| 129 | + |
| 130 | + url, _ = resolve_url_from_purl("pkg:swift/github.com/Alamofire/Alamofire@5.4.3") |
| 131 | + assert url == "https://github.com/Alamofire/Alamofire/archive/5.4.3.zip" |
| 132 | + mock_http_exists.assert_called_once_with( |
| 133 | + "https://github.com/Alamofire/Alamofire/archive/5.4.3.zip" |
| 134 | + ) |
| 135 | + |
| 136 | + |
| 137 | +@mock.patch("fetchcode._http_exists") |
| 138 | +@mock.patch("fetchcode.pypi.fetch_json_response") |
| 139 | +def test_resolve_url_from_purl_using_fetchcode(mock_fetch_json_response, mock_http_exists): |
| 140 | + mock_http_exists.return_value = True |
| 141 | + mock_fetch_json_response.return_value = { |
| 142 | + "urls": [{"url": "https://example.com/sample-1.0.0.zip"}] |
| 143 | + } |
| 144 | + |
| 145 | + url, _ = resolve_url_from_purl("pkg:pypi/example@1.0.0") |
| 146 | + assert url == "https://example.com/sample-1.0.0.zip" |
| 147 | + mock_http_exists.assert_called_once_with("https://example.com/sample-1.0.0.zip") |
| 148 | + |
| 149 | + |
| 150 | +def test_resolve_purl_invalid(): |
| 151 | + assert resolve_purl("pkg:invalid/invalid-package@1.0.0") is None |
| 152 | + |
| 153 | + |
| 154 | +def test_resolve_purl_using_purl2url(): |
| 155 | + url = resolve_purl("pkg:pub/http@0.13.3") |
| 156 | + assert url == "https://pub.dev/api/archives/http-0.13.3.tar.gz" |
| 157 | + |
| 158 | + |
| 159 | +@mock.patch("fetchcode._http_exists") |
| 160 | +def test_resolve_purl_using_purl2url_url_does_not_exists(mock_http_exists): |
| 161 | + mock_http_exists.return_value = False |
| 162 | + url = resolve_purl("pkg:pub/http@0.13.3") |
| 163 | + assert url is None |
| 164 | + |
| 165 | + |
| 166 | +@mock.patch("fetchcode._http_exists") |
| 167 | +@mock.patch("fetchcode.pypi.fetch_json_response") |
| 168 | +def test_resolve_purl_using_fetchcode(mock_fetch_json_response, mock_http_exists): |
| 169 | + mock_fetch_json_response.return_value = { |
| 170 | + "urls": [{"url": "https://example.com/sample-1.0.0.zip"}] |
| 171 | + } |
| 172 | + mock_http_exists.return_value = True |
| 173 | + url = resolve_purl("pkg:pypi/example@1.0.0") |
| 174 | + assert url == "https://example.com/sample-1.0.0.zip" |
| 175 | + |
| 176 | + |
| 177 | +@mock.patch("fetchcode._http_exists") |
| 178 | +@mock.patch("fetchcode.pypi.fetch_json_response") |
| 179 | +def test_resolve_purl_using_fetchcode_url_does_not_exists( |
| 180 | + mock_fetch_json_response, mock_http_exists |
| 181 | +): |
| 182 | + mock_fetch_json_response.return_value = { |
| 183 | + "urls": [{"url": "https://example.com/sample-1.0.0.zip"}] |
| 184 | + } |
| 185 | + mock_http_exists.return_value = False |
| 186 | + url = resolve_purl("pkg:pypi/example@1.0.0") |
| 187 | + assert url is None |
0 commit comments