|
| 1 | +import base64 |
| 2 | +import pytest |
| 3 | +from pathlib import Path |
| 4 | +from tests import MockResponse |
| 5 | +from unittest.mock import patch, mock_open, MagicMock |
| 6 | +from deluge_web_client import DelugeWebClientError |
| 7 | + |
| 8 | + |
| 9 | +def test_upload_torrent(client_mock): |
| 10 | + client, _ = client_mock |
| 11 | + |
| 12 | + # Mock parameters for the test |
| 13 | + torrent_path = "mocked_torrent_file.torrent" |
| 14 | + # Mocked content of the torrent file |
| 15 | + mocked_file_content = b"mocked torrent file content" |
| 16 | + base64_encoded_content = base64.b64encode(mocked_file_content).decode("utf-8") |
| 17 | + |
| 18 | + # Mock the open function to simulate reading a torrent file |
| 19 | + with patch("builtins.open", mock_open(read_data=mocked_file_content)), patch.object( |
| 20 | + client, "_upload_helper", return_value=MagicMock(result=True, error=None) |
| 21 | + ) as mock_upload_helper: |
| 22 | + # Call the upload_torrent method |
| 23 | + response = client.upload_torrent( |
| 24 | + torrent_path, add_paused=True, save_directory="/downloads" |
| 25 | + ) |
| 26 | + |
| 27 | + # Verify the response indicates success |
| 28 | + assert response.result is True |
| 29 | + assert response.error is None |
| 30 | + |
| 31 | + # Prepare expected payload for the upload_helper |
| 32 | + expected_payload = { |
| 33 | + "method": "core.add_torrent_file", |
| 34 | + "params": [ |
| 35 | + str(torrent_path), |
| 36 | + base64_encoded_content, |
| 37 | + { |
| 38 | + "add_paused": True, |
| 39 | + "seed_mode": False, |
| 40 | + "auto_managed": False, |
| 41 | + "download_location": "/downloads", |
| 42 | + }, |
| 43 | + ], |
| 44 | + "id": client.ID, |
| 45 | + } |
| 46 | + |
| 47 | + # Verify that the correct payload was sent to _upload_helper |
| 48 | + mock_upload_helper.assert_called_once_with(expected_payload, None, 30) |
| 49 | + |
| 50 | + |
| 51 | +def test_upload_torrents(client_mock): |
| 52 | + client, _ = client_mock |
| 53 | + |
| 54 | + # Mock the responses for upload_torrent |
| 55 | + mock_responses = { |
| 56 | + "torrent1": MagicMock(result=True, error=None), |
| 57 | + "torrent2": MagicMock(result=True, error=None), |
| 58 | + } |
| 59 | + |
| 60 | + # Patch the upload_torrent method to return mocked responses |
| 61 | + with patch.object(client, "upload_torrent") as mock_upload_torrent: |
| 62 | + # Set side effects for multiple calls |
| 63 | + mock_upload_torrent.side_effect = [ |
| 64 | + mock_responses["torrent1"], |
| 65 | + mock_responses["torrent2"], |
| 66 | + ] |
| 67 | + |
| 68 | + # Define the torrent paths to upload |
| 69 | + torrents = ["path/to/torrent1.torrent", "path/to/torrent2.torrent"] |
| 70 | + |
| 71 | + # Call the upload_torrents method |
| 72 | + results = client.upload_torrents(torrents, save_directory="/downloads") |
| 73 | + |
| 74 | + # Assertions to check that the responses are correct |
| 75 | + assert len(results) == 2 |
| 76 | + assert results["torrent1"].result is True |
| 77 | + assert results["torrent2"].result is True |
| 78 | + assert results["torrent1"].error is None |
| 79 | + assert results["torrent2"].error is None |
| 80 | + |
| 81 | + # Verify that upload_torrent was called with the correct arguments |
| 82 | + mock_upload_torrent.assert_any_call( |
| 83 | + Path("path/to/torrent1.torrent"), |
| 84 | + save_directory="/downloads", |
| 85 | + label=None, |
| 86 | + timeout=30, |
| 87 | + ) |
| 88 | + mock_upload_torrent.assert_any_call( |
| 89 | + Path("path/to/torrent2.torrent"), |
| 90 | + save_directory="/downloads", |
| 91 | + label=None, |
| 92 | + timeout=30, |
| 93 | + ) |
| 94 | + |
| 95 | + |
| 96 | +def test_upload_torrents_failure(client_mock): |
| 97 | + client, _ = client_mock |
| 98 | + |
| 99 | + # Mock the upload_torrent method to raise an exception for one of the torrents |
| 100 | + with patch.object(client, "upload_torrent") as mock_upload_torrent: |
| 101 | + mock_upload_torrent.side_effect = [ |
| 102 | + MagicMock(result=True, error=None), # First upload succeeds |
| 103 | + Exception("Upload failed"), # Second upload fails |
| 104 | + ] |
| 105 | + |
| 106 | + torrents = ["path/to/torrent1.torrent", "path/to/torrent2.torrent"] |
| 107 | + |
| 108 | + # Expecting a DelugeWebClientError to be raised |
| 109 | + with pytest.raises( |
| 110 | + DelugeWebClientError, match="Failed to upload torrent2.torrent:" |
| 111 | + ): |
| 112 | + client.upload_torrents(torrents) |
| 113 | + |
| 114 | + # Verify that upload_torrent was called for both torrents |
| 115 | + mock_upload_torrent.assert_any_call( |
| 116 | + Path("path/to/torrent1.torrent"), |
| 117 | + save_directory=None, |
| 118 | + label=None, |
| 119 | + timeout=30, |
| 120 | + ) |
| 121 | + mock_upload_torrent.assert_any_call( |
| 122 | + Path("path/to/torrent2.torrent"), |
| 123 | + save_directory=None, |
| 124 | + label=None, |
| 125 | + timeout=30, |
| 126 | + ) |
| 127 | + |
| 128 | + |
| 129 | +def test_add_torrent_magnet(client_mock): |
| 130 | + client, _ = client_mock |
| 131 | + magnet_uri = "magnet:?xt=urn:btih:...&dn=example" |
| 132 | + |
| 133 | + # Mock the response for _upload_helper |
| 134 | + mock_response = MagicMock() |
| 135 | + mock_response.json.return_value = {"result": "Ok", "id": client.ID} |
| 136 | + |
| 137 | + with patch.object( |
| 138 | + client, "_upload_helper", return_value=mock_response |
| 139 | + ) as mock_upload_helper: |
| 140 | + response = client.add_torrent_magnet( |
| 141 | + magnet_uri, add_paused=True, save_directory="/downloads" |
| 142 | + ) |
| 143 | + |
| 144 | + # Assertions to check the response is as expected |
| 145 | + assert response == mock_response |
| 146 | + expected_payload = { |
| 147 | + "method": "core.add_torrent_magnet", |
| 148 | + "params": [ |
| 149 | + str(magnet_uri), |
| 150 | + { |
| 151 | + "add_paused": True, |
| 152 | + "seed_mode": False, |
| 153 | + "auto_managed": False, |
| 154 | + "download_location": "/downloads", |
| 155 | + }, |
| 156 | + ], |
| 157 | + "id": client.ID, |
| 158 | + } |
| 159 | + |
| 160 | + # Verify that the correct payload was sent to _upload_helper |
| 161 | + mock_upload_helper.assert_called_once_with(expected_payload, None, 30) |
| 162 | + |
| 163 | + |
| 164 | +def test_add_torrent_magnet_failure(client_mock): |
| 165 | + client, _ = client_mock |
| 166 | + magnet_uri = "magnet:?xt=urn:btih:..." |
| 167 | + |
| 168 | + # Mock the _upload_helper to raise an exception |
| 169 | + with patch.object( |
| 170 | + client, "_upload_helper", side_effect=DelugeWebClientError("Upload failed") |
| 171 | + ): |
| 172 | + with pytest.raises(DelugeWebClientError, match=r".+"): |
| 173 | + client.add_torrent_magnet(magnet_uri) |
| 174 | + |
| 175 | + |
| 176 | +def test_add_torrent_url(client_mock): |
| 177 | + client, _ = client_mock |
| 178 | + torrent_url = "http://example.com/torrent" |
| 179 | + |
| 180 | + # Mock the response for _upload_helper |
| 181 | + mock_response = MagicMock() |
| 182 | + mock_response.json.return_value = {"result": "Ok", "id": client.ID} |
| 183 | + |
| 184 | + with patch.object( |
| 185 | + client, "_upload_helper", return_value=mock_response |
| 186 | + ) as mock_upload_helper: |
| 187 | + response = client.add_torrent_url( |
| 188 | + torrent_url, add_paused=False, save_directory="/downloads" |
| 189 | + ) |
| 190 | + |
| 191 | + # Assertions to check the response is as expected |
| 192 | + assert response == mock_response |
| 193 | + expected_payload = { |
| 194 | + "method": "core.add_torrent_url", |
| 195 | + "params": [ |
| 196 | + str(torrent_url), |
| 197 | + { |
| 198 | + "add_paused": False, |
| 199 | + "seed_mode": False, |
| 200 | + "auto_managed": False, |
| 201 | + "download_location": "/downloads", |
| 202 | + }, |
| 203 | + ], |
| 204 | + "id": client.ID, |
| 205 | + } |
| 206 | + |
| 207 | + # Verify that the correct payload was sent to _upload_helper |
| 208 | + mock_upload_helper.assert_called_once_with(expected_payload, None, 30) |
| 209 | + |
| 210 | + |
| 211 | +def test_add_torrent_url_failure(client_mock): |
| 212 | + client, _ = client_mock |
| 213 | + torrent_url = "http://example.com/torrent" |
| 214 | + |
| 215 | + # Mock the _upload_helper to raise an exception |
| 216 | + with patch.object( |
| 217 | + client, "_upload_helper", side_effect=DelugeWebClientError("Upload failed") |
| 218 | + ): |
| 219 | + with pytest.raises(DelugeWebClientError, match=r".+"): |
| 220 | + client.add_torrent_url(torrent_url) |
| 221 | + |
| 222 | + |
| 223 | +def test_upload_helper_success(client_mock): |
| 224 | + client, _ = client_mock |
| 225 | + payload = {"method": "core.add_torrent_file", "params": [], "id": 0} |
| 226 | + label = "Test Label" |
| 227 | + |
| 228 | + with patch.object( |
| 229 | + client.session, |
| 230 | + "post", |
| 231 | + return_value=MockResponse( |
| 232 | + json_data={"result": "info_hash"}, ok=True, status_code=200 |
| 233 | + ), |
| 234 | + ): |
| 235 | + response = client._upload_helper(payload, label, timeout=30) |
| 236 | + |
| 237 | + assert response.result == "info_hash" |
| 238 | + assert response.error is None |
| 239 | + assert client.ID == 4 |
| 240 | + |
| 241 | + |
| 242 | +def test_upload_helper_failure(client_mock): |
| 243 | + client, _ = client_mock |
| 244 | + payload = {"method": "core.add_torrent_file", "params": [], "id": 0} |
| 245 | + label = "Test Label" |
| 246 | + |
| 247 | + with patch.object( |
| 248 | + client.session, |
| 249 | + "post", |
| 250 | + return_value=MockResponse( |
| 251 | + json_data={"result": "info_hash"}, ok=False, status_code=500 |
| 252 | + ), |
| 253 | + ): |
| 254 | + with pytest.raises(DelugeWebClientError) as error_info: |
| 255 | + client._upload_helper(payload, label, timeout=30) |
| 256 | + assert ( |
| 257 | + "Failed to upload file. Status code: 500, Reason: Internal Server Error" |
| 258 | + in str(error_info.value) |
| 259 | + ) |
0 commit comments