|
14 | 14 | # CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
15 | 15 | # specific language governing permissions and limitations under the License.
|
16 | 16 |
|
| 17 | +from fetchcode import fetch |
| 18 | +from pathlib import Path |
17 | 19 | from unittest import mock
|
18 | 20 |
|
19 | 21 | import pytest
|
20 | 22 |
|
21 |
| -from fetchcode import fetch |
22 | 23 |
|
| 24 | +FILENAMES = ["a.ext", "b.ext", "c.ext"] |
| 25 | +HTTP_URL = "http://example.com/" |
| 26 | +FTP_URL = "ftp://example.com/" |
| 27 | + |
| 28 | + |
| 29 | +@mock.patch("fetchcode.requests.get") |
| 30 | +def test_fetch_http_with_filename_provided(mock_get): |
| 31 | + with mock.patch("fetchcode.open", mock.mock_open()) as mocked_file: |
| 32 | + location = Path.home() / FILENAMES[0] |
| 33 | + response = fetch(HTTP_URL, location) |
| 34 | + assert response is not None |
| 35 | + assert response.location == location |
| 36 | + |
| 37 | + |
| 38 | +@pytest.mark.parametrize( |
| 39 | + "parameters, expected_filename", |
| 40 | + [ |
| 41 | + pytest.param(f'filename*="{FILENAMES[0]}"; filename=""', FILENAMES[0]), |
| 42 | + pytest.param(f'filename*=""; filename="{FILENAMES[1]}"', FILENAMES[1]), |
| 43 | + pytest.param(f'filename*=""; filename=""', FILENAMES[2]), |
| 44 | + ], |
| 45 | +) |
| 46 | +@mock.patch("fetchcode.requests.get") |
| 47 | +def test_fetch_http_with_filename_deduction(mock_get, parameters, expected_filename): |
| 48 | + mock_get.return_value.headers = {"content-disposition": f"attachment; {parameters}"} |
| 49 | + with mock.patch("fetchcode.open", mock.mock_open()) as mocked_file: |
| 50 | + location = Path.home() |
| 51 | + response = fetch(HTTP_URL + FILENAMES[2], location) |
| 52 | + assert response is not None |
| 53 | + assert response.location == (location / expected_filename) |
23 | 54 |
|
24 |
| -@mock.patch('fetchcode.requests.get') |
| 55 | + |
| 56 | +@mock.patch("fetchcode.tempfile.NamedTemporaryFile") |
| 57 | +@mock.patch("fetchcode.requests.get") |
| 58 | +def test_fetch_http_filename_deduction_failed(mock_get, mock_tmp): |
| 59 | + location = Path.home() |
| 60 | + mock_get.return_value.headers = {} |
| 61 | + mock_tmp.return_value.name = location / FILENAMES[0] |
| 62 | + with mock.patch("fetchcode.open", mock.mock_open()) as mocked_file: |
| 63 | + response = fetch(HTTP_URL, location) |
| 64 | + assert response is not None |
| 65 | + assert response.location == (location / FILENAMES[0]) |
| 66 | + |
| 67 | + |
| 68 | +@mock.patch("fetchcode.requests.get") |
25 | 69 | def test_fetch_http_with_tempfile(mock_get):
|
26 | 70 | mock_get.return_value.headers = {
|
27 |
| - 'content-type': 'image/png', |
28 |
| - 'content-length': '1000999', |
| 71 | + "content-type": "image/png", |
| 72 | + "content-length": "1000999", |
29 | 73 | }
|
30 |
| - |
31 |
| - with mock.patch('fetchcode.open', mock.mock_open()) as mocked_file: |
32 |
| - url = 'https://raw.githubusercontent.com/TG1999/converge/master/assets/Group%2022.png' |
| 74 | + with mock.patch("fetchcode.open", mock.mock_open()) as mocked_file: |
| 75 | + url = "https://raw.githubusercontent.com/TG1999/converge/master/assets/Group%2022.png" |
33 | 76 | response = fetch(url=url)
|
34 | 77 | assert response is not None
|
35 | 78 | assert 1000999 == response.size
|
36 | 79 | assert url == response.url
|
37 |
| - assert 'image/png' == response.content_type |
| 80 | + assert "image/png" == response.content_type |
38 | 81 |
|
39 | 82 |
|
40 |
| -@mock.patch('fetchcode.FTP') |
| 83 | +@mock.patch("fetchcode.FTP") |
41 | 84 | def test_fetch_with_wrong_url(mock_get):
|
42 | 85 | with pytest.raises(Exception) as e_info:
|
43 |
| - url = 'ftp://speedtest/1KB.zip' |
| 86 | + url = "ftp://speedtest/1KB.zip" |
44 | 87 | response = fetch(url=url)
|
45 |
| - assert 'Not a valid URL' == e_info |
| 88 | + assert "Not a valid URL" == e_info |
46 | 89 |
|
47 | 90 |
|
48 |
| -@mock.patch('fetchcode.FTP', autospec=True) |
| 91 | +@mock.patch("fetchcode.FTP", autospec=True) |
49 | 92 | def test_fetch_ftp_with_tempfile(mock_ftp_constructor):
|
50 | 93 | mock_ftp = mock_ftp_constructor.return_value
|
51 | 94 | mock_ftp_constructor.return_value.size.return_value = 1024
|
52 |
| - with mock.patch('fetchcode.open', mock.mock_open()) as mocked_file: |
53 |
| - response = fetch('ftp://speedtest.tele2.net/1KB.zip') |
| 95 | + with mock.patch("fetchcode.open", mock.mock_open()) as mocked_file: |
| 96 | + response = fetch("ftp://speedtest.tele2.net/1KB.zip") |
54 | 97 | assert 1024 == response.size
|
55 |
| - mock_ftp_constructor.assert_called_with('speedtest.tele2.net') |
| 98 | + mock_ftp_constructor.assert_called_with("speedtest.tele2.net") |
56 | 99 | assert mock_ftp.login.called == True
|
57 |
| - mock_ftp.cwd.assert_called_with('/') |
| 100 | + mock_ftp.cwd.assert_called_with("/") |
58 | 101 | assert mock_ftp.retrbinary.called
|
59 | 102 |
|
60 | 103 |
|
| 104 | +@mock.patch("fetchcode.FTP") |
| 105 | +def test_fetch_ftp_with_filename_deduction(mock_ftp): |
| 106 | + with mock.patch("fetchcode.open", mock.mock_open()) as mocked_file: |
| 107 | + location = Path.home() |
| 108 | + response = fetch(FTP_URL + FILENAMES[0], location) |
| 109 | + assert response.location == (location / FILENAMES[0]) |
| 110 | + |
| 111 | + |
61 | 112 | def test_fetch_with_scheme_not_present():
|
62 | 113 | with pytest.raises(Exception) as e_info:
|
63 |
| - url = 'abc://speedtest/1KB.zip' |
| 114 | + url = "abc://speedtest/1KB.zip" |
64 | 115 | response = fetch(url=url)
|
65 |
| - assert 'Not a supported/known scheme.' == e_info |
| 116 | + assert "Not a supported/known scheme." == e_info |
0 commit comments