| 
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")  | 
@@ -65,22 +67,121 @@ def test_fetch_with_scheme_not_present():  | 
65 | 67 |         assert "Not a supported/known scheme." == e_info  | 
66 | 68 | 
 
  | 
67 | 69 | 
 
  | 
68 |  | -@mock.patch("fetchcode.resolve_url_from_purl")  | 
 | 70 | +@mock.patch("fetchcode._http_exists")  | 
69 | 71 | @mock.patch("fetchcode.fetch_http")  | 
70 |  | -def test_fetch_purl(mock_fetch_http, mock_resolve):  | 
 | 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):  | 
71 | 74 |     mock_fetch_http.return_value = "mocked_purl_response"  | 
72 |  | -    mock_resolve.return_value = ("http://resolved.com/file.tar.gz", "http")  | 
 | 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 | +    }  | 
73 | 79 | 
 
  | 
74 | 80 |     response = fetch( "pkg:pypi/[email protected]")   | 
75 | 81 | 
 
  | 
76 | 82 |     assert response == "mocked_purl_response"  | 
77 |  | -    mock_resolve.assert_called_once()  | 
 | 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/[email protected]")   | 
 | 94 | + | 
 | 95 | +    assert response == "mocked_purl_response"  | 
 | 96 | +    mock_http_exists.assert_called_once()  | 
78 | 97 |     mock_fetch_http.assert_called_once()  | 
79 | 98 | 
 
  | 
80 | 99 | 
 
  | 
81 |  | -@mock.patch("fetchcode.get_url_scheme")  | 
82 |  | -def test_fetch_unsupported_scheme(mock_get_scheme):  | 
83 |  | -    mock_get_scheme.return_value = "s3"  | 
 | 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 = {}  | 
84 | 103 | 
 
  | 
 | 104 | +    with pytest.raises(Exception, match="No download URL found for invalid-package version 1.0.0"):  | 
 | 105 | +        fetch( "pkg:pypi/[email protected]")   | 
 | 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/[email protected]")   | 
 | 114 | + | 
 | 115 | + | 
 | 116 | +def test_fetch_unsupported_scheme():  | 
85 | 117 |     with pytest.raises(Exception, match="Not a supported/known scheme"):  | 
86 | 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/[email protected]")   | 
 | 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/[email protected]")   | 
 | 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/[email protected]")   | 
 | 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/[email protected]")  is None  | 
 | 152 | + | 
 | 153 | + | 
 | 154 | +def test_resolve_purl_using_purl2url():  | 
 | 155 | +    url = resolve_purl( "pkg:pub/[email protected]")   | 
 | 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/[email protected]")   | 
 | 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/[email protected]")   | 
 | 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/[email protected]")   | 
 | 187 | +    assert url is None  | 
0 commit comments