| 1 |
# Copyright 2015-2026 Facundo Batista, Nicolás Demarchi |
| 2 |
# |
| 3 |
# This program is free software: you can redistribute it and/or modify it |
| 4 |
# under the terms of the GNU General Public License version 3, as published |
| 5 |
# by the Free Software Foundation. |
| 6 |
# |
| 7 |
# This program is distributed in the hope that it will be useful, but |
| 8 |
# WITHOUT ANY WARRANTY; without even the implied warranties of |
| 9 |
# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR |
| 10 |
# PURPOSE. See the GNU General Public License for more details. |
| 11 |
# |
| 12 |
# You should have received a copy of the GNU General Public License along |
| 13 |
# with this program. If not, see <http://www.gnu.org/licenses/>. |
| 14 |
# |
| 15 |
# For further info, check https://github.com/PyAr/fades |
| 16 |
|
| 17 |
"""Tests for functions in helpers.""" |
| 18 |
|
| 19 |
import io |
| 20 |
import json |
| 21 |
import os |
| 22 |
import sys |
| 23 |
import tempfile |
| 24 |
import unittest |
| 25 |
from http.server import HTTPStatus |
| 26 |
from pathlib import Path |
| 27 |
from unittest.mock import patch |
| 28 |
from urllib.error import HTTPError |
| 29 |
from urllib.request import Request |
| 30 |
|
| 31 |
import logassert |
| 32 |
import pytest |
| 33 |
from xdg import BaseDirectory |
| 34 |
|
| 35 |
from fades import helpers, parsing |
| 36 |
|
| 37 |
|
| 38 |
PATH_TO_EXAMPLES = "tests/examples/" |
| 39 |
|
| 40 |
|
| 41 |
class GetInterpreterVersionTestCase(unittest.TestCase): |
| 42 |
"""Some tests for get_interpreter_version.""" |
| 43 |
|
| 44 |
def test_current_version(self): |
| 45 |
values = {None: ('/path/to/python1.0'), "/path/to/python": ('/path/to/python1.0')} |
| 46 |
|
| 47 |
def side_effect(arg=None): |
| 48 |
return values[arg] |
| 49 |
|
| 50 |
with patch.object(helpers, '_get_interpreter_info') as mock: |
| 51 |
mock.side_effect = side_effect |
| 52 |
interpreter, is_current = helpers.get_interpreter_version('/path/to/python') |
| 53 |
self.assertEqual(is_current, True) |
| 54 |
|
| 55 |
def test_other_version(self): |
| 56 |
values = {None: ('/path/to/python1.0'), "/path/to/python": ('/path/to/python9.8')} |
| 57 |
|
| 58 |
def side_effect(arg=None): |
| 59 |
return values[arg] |
| 60 |
|
| 61 |
with patch.object(helpers, '_get_interpreter_info') as mock: |
| 62 |
mock.side_effect = side_effect |
| 63 |
interpreter, is_current = helpers.get_interpreter_version('/path/to/python') |
| 64 |
self.assertEqual(is_current, False) |
| 65 |
|
| 66 |
def test_none_requested(self): |
| 67 |
values = {None: ('/path/to/python1.0'), "/path/to/python": ('/path/to/python9.8')} |
| 68 |
|
| 69 |
def side_effect(arg=None): |
| 70 |
return values[arg] |
| 71 |
|
| 72 |
with patch.object(helpers, '_get_interpreter_info') as mock: |
| 73 |
mock.side_effect = side_effect |
| 74 |
interpreter, is_current = helpers.get_interpreter_version(requested_interpreter=None) |
| 75 |
self.assertEqual(is_current, True) |
| 76 |
self.assertTrue(mock.call_count, 1) |
| 77 |
|
| 78 |
|
| 79 |
class GetInterpreterInfoTestCase(unittest.TestCase): |
| 80 |
"""Some tests for _get_interpreter_info.""" |
| 81 |
|
| 82 |
def setUp(self): |
| 83 |
logassert.setup(self, 'fades.helpers') |
| 84 |
|
| 85 |
def test_none_requested(self): |
| 86 |
with patch.object(sys, 'version_info', (9, 8)), patch.object(sys, |
| 87 |
'executable', |
| 88 |
'/path/to/python'): |
| 89 |
interpreter = helpers._get_interpreter_info(None) |
| 90 |
self.assertEqual(interpreter, '/path/to/python9.8') |
| 91 |
|
| 92 |
def test_requested_fullpath_nodigit(self): |
| 93 |
response = [('{"serial": 0,"path": "/path/to/python","minor": 8,"major": 9,"micro": 0,' |
| 94 |
'"releaselevel": "ultimate"}')] |
| 95 |
with patch.object(helpers, 'logged_exec', return_value=response): |
| 96 |
interpreter = helpers._get_interpreter_info('/path/to/python') |
| 97 |
self.assertEqual(interpreter, '/path/to/python9.8') |
| 98 |
|
| 99 |
def test_requested_fullpath_with_major(self): |
| 100 |
response = [('{"serial": 0,"path": "/path/to/python9","minor": 8,"major": 9,"micro": 0,' |
| 101 |
'"releaselevel": "ultimate"}')] |
| 102 |
with patch.object(helpers, 'logged_exec', return_value=response): |
| 103 |
interpreter = helpers._get_interpreter_info('/path/to/python9') |
| 104 |
self.assertEqual(interpreter, '/path/to/python9.8') |
| 105 |
|
| 106 |
def test_requested_fullpath_with_minor(self): |
| 107 |
response = [('{"serial": 0,"path": "/path/to/python9.8","minor": 8,"major": 9,"micro": 0,' |
| 108 |
'"releaselevel": "ultimate"}')] |
| 109 |
with patch.object(helpers, 'logged_exec', return_value=response): |
| 110 |
interpreter = helpers._get_interpreter_info('/path/to/python9.8') |
| 111 |
self.assertEqual(interpreter, '/path/to/python9.8') |
| 112 |
|
| 113 |
def test_requested_nodigit(self): |
| 114 |
response = [('{"serial": 0,"path": "/path/to/python","minor": 8,"major": 9,"micro": 0,' |
| 115 |
'"releaselevel": "ultimate"}')] |
| 116 |
with patch.object(helpers, 'logged_exec', return_value=response): |
| 117 |
interpreter = helpers._get_interpreter_info('python') |
| 118 |
self.assertEqual(interpreter, '/path/to/python9.8') |
| 119 |
|
| 120 |
def test_requested_with_major(self): |
| 121 |
response = [('{"serial": 0,"path": "/path/to/python9","minor": 8,"major": 9,"micro": 0,' |
| 122 |
'"releaselevel": "ultimate"}')] |
| 123 |
with patch.object(helpers, 'logged_exec', return_value=response): |
| 124 |
interpreter = helpers._get_interpreter_info('python9') |
| 125 |
self.assertEqual(interpreter, '/path/to/python9.8') |
| 126 |
|
| 127 |
def test_requested_with_minor(self): |
| 128 |
response = [('{"serial": 0,"path": "/path/to/python9.8","minor": 8,"major": 9,"micro": 0,' |
| 129 |
'"releaselevel": "ultimate"}')] |
| 130 |
with patch.object(helpers, 'logged_exec', return_value=response): |
| 131 |
interpreter = helpers._get_interpreter_info('python9.8') |
| 132 |
self.assertEqual(interpreter, '/path/to/python9.8') |
| 133 |
|
| 134 |
def test_requested_not_exists(self): |
| 135 |
side_effect = IOError("[Errno 2] No such file or directory: 'pythonME'") |
| 136 |
|
| 137 |
with patch('fades.helpers.logged_exec') as mock_lexec: |
| 138 |
mock_lexec.side_effect = side_effect |
| 139 |
|
| 140 |
with self.assertRaises(Exception): |
| 141 |
helpers._get_interpreter_info('pythonME') |
| 142 |
|
| 143 |
self.assertLoggedError("Error getting requested interpreter version:" |
| 144 |
" [Errno 2] No such file or directory: 'pythonME'") |
| 145 |
|
| 146 |
|
| 147 |
class GetLatestVersionNumberTestCase(unittest.TestCase): |
| 148 |
"""Some tests for get_latest_version_number.""" |
| 149 |
|
| 150 |
def setUp(self): |
| 151 |
logassert.setup(self, 'fades.helpers') |
| 152 |
|
| 153 |
def test_get_version_correct(self): |
| 154 |
with open(os.path.join(PATH_TO_EXAMPLES, 'pypi_get_version_ok.json'), "rb") as fh: |
| 155 |
with patch('urllib.request.urlopen') as mock_urlopen: |
| 156 |
mock_urlopen.return_value = fh |
| 157 |
last_version = helpers.get_latest_version_number("some_package") |
| 158 |
mock_urlopen.assert_called_once_with(helpers.BASE_PYPI_URL.format(name="some_package")) |
| 159 |
self.assertEqual(last_version, '2.8.1') |
| 160 |
|
| 161 |
def test_get_version_wrong(self): |
| 162 |
with patch('urllib.request.urlopen') as mock_urlopen: |
| 163 |
with patch('http.client.HTTPResponse') as mock_http_response: |
| 164 |
mock_http_response.read.side_effect = HTTPError("url", |
| 165 |
500, |
| 166 |
"mgs", |
| 167 |
{}, |
| 168 |
io.BytesIO()) |
| 169 |
mock_urlopen.return_value = mock_http_response |
| 170 |
self.assertRaises(Exception, helpers.get_latest_version_number, "some_package") |
| 171 |
self.assertLoggedWarning("Network error.") |
| 172 |
|
| 173 |
def test_get_version_fail(self): |
| 174 |
with open(os.path.join(PATH_TO_EXAMPLES, 'pypi_get_version_fail.json'), "rb") as fh: |
| 175 |
with patch('urllib.request.urlopen') as mock_urlopen: |
| 176 |
mock_urlopen.return_value = fh |
| 177 |
self.assertRaises(KeyError, helpers.get_latest_version_number, "some_package") |
| 178 |
self.assertLoggedError("Could not get the version of the package. Error:") |
| 179 |
|
| 180 |
|
| 181 |
class CheckPyPIUpdatesTestCase(unittest.TestCase): |
| 182 |
"""Some tests for check_pypi_updates.""" |
| 183 |
|
| 184 |
def setUp(self): |
| 185 |
logassert.setup(self, 'fades.helpers') |
| 186 |
|
| 187 |
def test_check_pypi_updates_with_and_without_version(self): |
| 188 |
with patch('urllib.request.urlopen') as mock_urlopen: |
| 189 |
with patch('http.client.HTTPResponse') as mock_http_response: |
| 190 |
mock_http_response.read.side_effect = [b'{"info": {"version": "1.9"}}', |
| 191 |
b'{"info": {"version": "2.1"}}'] |
| 192 |
mock_urlopen.return_value = mock_http_response |
| 193 |
requested = parsing.parse_manual(["django==1.7.5", "requests"]) |
| 194 |
dependencies = helpers.check_pypi_updates(requested) |
| 195 |
dep_django = dependencies['pypi'][0] |
| 196 |
dep_request = dependencies['pypi'][1] |
| 197 |
self.assertLoggedInfo('There is a new version of django: 1.9') |
| 198 |
self.assertEqual(str(dep_request.specifier), "==2.1") |
| 199 |
self.assertEqual(str(dep_django.specifier), "==1.7.5") |
| 200 |
self.assertLoggedInfo("The latest version of 'requests' is 2.1 and will use it.") |
| 201 |
|
| 202 |
def test_check_pypi_updates_with_a_higher_version_of_a_package_simple(self): |
| 203 |
with patch('urllib.request.urlopen') as mock_urlopen: |
| 204 |
with patch('http.client.HTTPResponse') as mock_http_response: |
| 205 |
mock_http_response.read.side_effect = [b'{"info": {"version": "1.9"}}'] |
| 206 |
mock_urlopen.return_value = mock_http_response |
| 207 |
helpers.check_pypi_updates(parsing.parse_manual(["django==100.1.1"])) |
| 208 |
self.assertLoggedWarning( |
| 209 |
"The requested version for django is greater than latest found in PyPI: 1.9") |
| 210 |
|
| 211 |
def test_check_pypi_updates_with_a_higher_version_of_a_package_real_order(self): |
| 212 |
with patch('urllib.request.urlopen') as mock_urlopen: |
| 213 |
with patch('http.client.HTTPResponse') as mock_http_response: |
| 214 |
mock_http_response.read.side_effect = [b'{"info": {"version": "2.9"}}'] |
| 215 |
mock_urlopen.return_value = mock_http_response |
| 216 |
helpers.check_pypi_updates(parsing.parse_manual(["django==10.1"])) |
| 217 |
self.assertLoggedWarning( |
| 218 |
"The requested version for django is greater than latest found in PyPI: 2.9") |
| 219 |
|
| 220 |
def test_check_pypi_updates_with_the_latest_version_of_a_package(self): |
| 221 |
with patch('urllib.request.urlopen') as mock_urlopen: |
| 222 |
with patch('http.client.HTTPResponse') as mock_http_response: |
| 223 |
mock_http_response.read.side_effect = [b'{"info": {"version": "1.9"}}'] |
| 224 |
mock_urlopen.return_value = mock_http_response |
| 225 |
helpers.check_pypi_updates(parsing.parse_manual(["django==1.9"])) |
| 226 |
self.assertLoggedInfo( |
| 227 |
"The requested version for django is the latest one in PyPI: 1.9") |
| 228 |
|
| 229 |
|
| 230 |
class GetDirsTestCase(unittest.TestCase): |
| 231 |
"""Utilities to get dir.""" |
| 232 |
|
| 233 |
_home = os.path.expanduser("~") |
| 234 |
|
| 235 |
def test_basedir_xdg(self): |
| 236 |
direct = helpers.get_basedir() |
| 237 |
self.assertEqual(direct, Path(BaseDirectory.xdg_data_home) / 'fades') |
| 238 |
|
| 239 |
def _fake_snap_env_dir(self, direct): |
| 240 |
"""Fake Snap's environment variable.""" |
| 241 |
os.environ[helpers.SNAP_BASEDIR_NAME] = direct |
| 242 |
self.addCleanup(os.environ.pop, helpers.SNAP_BASEDIR_NAME) |
| 243 |
|
| 244 |
def test_basedir_snap(self): |
| 245 |
with tempfile.TemporaryDirectory() as dirname: |
| 246 |
self._fake_snap_env_dir(dirname) |
| 247 |
direct = helpers.get_basedir() |
| 248 |
self.assertEqual(direct, Path(dirname) / 'data') |
| 249 |
|
| 250 |
def test_basedir_default(self): |
| 251 |
with patch.object(helpers, "_get_basedirectory") as mock: |
| 252 |
mock.side_effect = ImportError() |
| 253 |
direct = helpers.get_basedir() |
| 254 |
self.assertEqual(direct, Path(self._home) / '.fades') |
| 255 |
|
| 256 |
def test_basedir_xdg_nonexistant(self): |
| 257 |
with patch("xdg.BaseDirectory") as mock: |
| 258 |
with tempfile.TemporaryDirectory() as dirname: |
| 259 |
mock.xdg_data_home = dirname |
| 260 |
direct = helpers.get_basedir() |
| 261 |
self.assertTrue(os.path.exists(direct)) |
| 262 |
|
| 263 |
def test_basedir_snap_nonexistant(self): |
| 264 |
with tempfile.TemporaryDirectory() as dirname: |
| 265 |
self._fake_snap_env_dir(dirname) |
| 266 |
direct = helpers.get_basedir() |
| 267 |
self.assertTrue(os.path.exists(direct)) |
| 268 |
|
| 269 |
def test_confdir_xdg(self): |
| 270 |
direct = helpers.get_confdir() |
| 271 |
self.assertEqual(direct, Path(BaseDirectory.xdg_config_home) / 'fades') |
| 272 |
|
| 273 |
def test_confdir_snap(self): |
| 274 |
with tempfile.TemporaryDirectory() as dirname: |
| 275 |
self._fake_snap_env_dir(dirname) |
| 276 |
direct = helpers.get_confdir() |
| 277 |
self.assertEqual(direct, Path(dirname) / 'config') |
| 278 |
|
| 279 |
def test_confdir_default(self): |
| 280 |
with patch.object(helpers, "_get_basedirectory") as mock: |
| 281 |
mock.side_effect = ImportError() |
| 282 |
direct = helpers.get_confdir() |
| 283 |
self.assertEqual(direct, Path(self._home) / '.fades') |
| 284 |
|
| 285 |
def test_confdir_xdg_nonexistant(self): |
| 286 |
with patch("xdg.BaseDirectory") as mock: |
| 287 |
with tempfile.TemporaryDirectory() as dirname: |
| 288 |
mock.xdg_config_home = dirname |
| 289 |
direct = helpers.get_confdir() |
| 290 |
self.assertTrue(os.path.exists(direct)) |
| 291 |
|
| 292 |
def test_confdir_snap_nonexistant(self): |
| 293 |
with tempfile.TemporaryDirectory() as dirname: |
| 294 |
self._fake_snap_env_dir(dirname) |
| 295 |
direct = helpers.get_confdir() |
| 296 |
self.assertTrue(os.path.exists(direct)) |
| 297 |
|
| 298 |
|
| 299 |
class CheckPackageExistenceTestCase(unittest.TestCase): |
| 300 |
"""Test for check_pypi_exists.""" |
| 301 |
|
| 302 |
def setUp(self): |
| 303 |
logassert.setup(self, 'fades.helpers') |
| 304 |
|
| 305 |
def test_exists(self): |
| 306 |
deps = parsing.parse_manual(["foo"]) |
| 307 |
|
| 308 |
with patch('urllib.request.urlopen') as mock_urlopen: |
| 309 |
with patch('http.client.HTTPResponse') as mock_http_response: |
| 310 |
mock_http_response.status = HTTPStatus.OK |
| 311 |
mock_urlopen.return_value = mock_http_response |
| 312 |
|
| 313 |
exists = helpers.check_pypi_exists(deps) |
| 314 |
self.assertTrue(exists) |
| 315 |
self.assertLogged("exists in PyPI") |
| 316 |
|
| 317 |
def test_all_exists(self): |
| 318 |
dependencies = parsing.parse_manual(['foo', 'bar', 'baz']) |
| 319 |
|
| 320 |
with patch('urllib.request.urlopen') as mock_urlopen: |
| 321 |
with patch('http.client.HTTPResponse') as mock_http_response: |
| 322 |
mock_http_response.status = HTTPStatus.OK |
| 323 |
mock_urlopen.side_effect = [mock_http_response] * 3 |
| 324 |
|
| 325 |
exists = helpers.check_pypi_exists(dependencies) |
| 326 |
self.assertTrue(exists) |
| 327 |
self.assertLogged("exists in PyPI") |
| 328 |
|
| 329 |
def test_doesnt_exists(self): |
| 330 |
dependency = parsing.parse_manual(["foo"]) |
| 331 |
|
| 332 |
with patch('urllib.request.urlopen') as mock_urlopen: |
| 333 |
mock_http_error = HTTPError("url", HTTPStatus.NOT_FOUND, "mgs", {}, io.BytesIO()) |
| 334 |
mock_urlopen.side_effect = mock_http_error |
| 335 |
|
| 336 |
exists = helpers.check_pypi_exists(dependency) |
| 337 |
|
| 338 |
self.assertFalse(exists) |
| 339 |
self.assertLoggedError("foo doesn't exists in PyPI.") |
| 340 |
|
| 341 |
def test_one_doesnt_exists(self): |
| 342 |
dependencies = parsing.parse_manual(["foo", "bar"]) |
| 343 |
|
| 344 |
with patch('urllib.request.urlopen') as mock_urlopen: |
| 345 |
with patch('http.client.HTTPResponse') as mock_http_response: |
| 346 |
mock_http_error = HTTPError("url", HTTPStatus.NOT_FOUND, "mgs", {}, io.BytesIO()) |
| 347 |
mock_http_response.status = HTTPStatus.OK |
| 348 |
mock_urlopen.side_effect = [mock_http_response, mock_http_error] |
| 349 |
|
| 350 |
exists = helpers.check_pypi_exists(dependencies) |
| 351 |
|
| 352 |
self.assertFalse(exists) |
| 353 |
self.assertLoggedError("bar doesn't exists in PyPI.") |
| 354 |
|
| 355 |
def test_error_hitting_pypi(self): |
| 356 |
dependency = parsing.parse_manual(["foo"]) |
| 357 |
|
| 358 |
with self.assertRaises(Exception): |
| 359 |
with patch('urllib.request.urlopen') as mock_urlopen: |
| 360 |
mock_urlopen.side_effect = ValueError("cabum!!") |
| 361 |
|
| 362 |
helpers.check_pypi_exists(dependency) |
| 363 |
|
| 364 |
def test_status_code_error(self): |
| 365 |
dependency = parsing.parse_manual(["foo"]) |
| 366 |
|
| 367 |
with self.assertRaises(Exception): |
| 368 |
with patch('urllib.request.urlopen') as mock_urlopen: |
| 369 |
mock_http_error = HTTPError("url", 400, "mgs", {}, io.BytesIO()) |
| 370 |
mock_urlopen.side_effect = mock_http_error |
| 371 |
|
| 372 |
helpers.check_pypi_exists(dependency) |
| 373 |
|
| 374 |
def test_redirect_response(self): |
| 375 |
deps = parsing.parse_manual(["foo"]) |
| 376 |
|
| 377 |
with patch('urllib.request.urlopen') as mock_urlopen: |
| 378 |
with patch('http.client.HTTPResponse') as mock_http_response: |
| 379 |
mock_http_response.status = 302 # redirect |
| 380 |
mock_urlopen.return_value = mock_http_response |
| 381 |
|
| 382 |
exists = helpers.check_pypi_exists(deps) |
| 383 |
self.assertTrue(exists) |
| 384 |
self.assertLoggedWarning("Got a (unexpected) HTTP_STATUS") |
| 385 |
|
| 386 |
|
| 387 |
class ScriptDownloaderTestCase(unittest.TestCase): |
| 388 |
"""Check the script downloader.""" |
| 389 |
|
| 390 |
def setUp(self): |
| 391 |
logassert.setup(self, 'fades.helpers') |
| 392 |
|
| 393 |
def test_external_public_function(self): |
| 394 |
test_url = "http://scripts.com/foobar.py" |
| 395 |
test_content = "test content of the remote script ññ" |
| 396 |
with patch('fades.helpers._ScriptDownloader') as mock_downloader_class: |
| 397 |
mock_downloader = mock_downloader_class() |
| 398 |
mock_downloader.get.return_value = test_content |
| 399 |
mock_downloader.name = 'mock downloader' |
| 400 |
filepath = helpers.download_remote_script(test_url) |
| 401 |
|
| 402 |
# plan to remove the downloaded content (so test remains clean) |
| 403 |
self.addCleanup(os.unlink, filepath) |
| 404 |
|
| 405 |
# checks |
| 406 |
mock_downloader_class.assert_called_with(test_url) |
| 407 |
self.assertLoggedInfo( |
| 408 |
"Downloading remote script from {!r}".format(test_url), |
| 409 |
str(filepath), "(using 'mock downloader' downloader)") |
| 410 |
with open(filepath, "rt", encoding='utf8') as fh: |
| 411 |
self.assertEqual(fh.read(), test_content) |
| 412 |
|
| 413 |
def test_decide_linkode(self): |
| 414 |
url = "http://linkode.org/#02c5nESQBLEjgBRhUwJK74" |
| 415 |
downloader = helpers._ScriptDownloader(url) |
| 416 |
name = downloader._decide() |
| 417 |
self.assertEqual(name, 'linkode') |
| 418 |
|
| 419 |
def test_decide_pastebin(self): |
| 420 |
url = "https://pastebin.com/sZGwz7SL" |
| 421 |
downloader = helpers._ScriptDownloader(url) |
| 422 |
name = downloader._decide() |
| 423 |
self.assertEqual(name, 'pastebin') |
| 424 |
|
| 425 |
def test_decide_gist(self): |
| 426 |
url = "https://gist.github.com/facundobatista/6ff4f75760a9acc35e68bae8c1d7da1c" |
| 427 |
downloader = helpers._ScriptDownloader(url) |
| 428 |
name = downloader._decide() |
| 429 |
self.assertEqual(name, 'gist') |
| 430 |
|
| 431 |
def test_downloader_raw(self): |
| 432 |
test_url = "http://scripts.com/foobar.py" |
| 433 |
raw_service_response = b"test content of the remote script" |
| 434 |
downloader = helpers._ScriptDownloader(test_url) |
| 435 |
with patch('urllib.request.urlopen') as mock_urlopen: |
| 436 |
with patch('http.client.HTTPResponse') as mock_http_response: |
| 437 |
mock_http_response.read.return_value = raw_service_response |
| 438 |
mock_urlopen.return_value = mock_http_response |
| 439 |
mock_http_response.geturl.return_value = test_url |
| 440 |
|
| 441 |
content = downloader.get() |
| 442 |
|
| 443 |
# check urlopen was called with the proper url, and passing correct headers |
| 444 |
headers = { |
| 445 |
'Accept': 'text/plain', |
| 446 |
'User-agent': helpers._ScriptDownloader.USER_AGENT, |
| 447 |
} |
| 448 |
(call,) = mock_urlopen.mock_calls |
| 449 |
(called_request,) = call[1] |
| 450 |
self.assertIsInstance(called_request, Request) |
| 451 |
self.assertEqual(called_request.full_url, test_url) |
| 452 |
self.assertEqual(called_request.headers, headers) |
| 453 |
self.assertEqual(content, raw_service_response.decode("utf8")) |
| 454 |
|
| 455 |
def test_downloader_linkode(self): |
| 456 |
test_url = "http://linkode.org/#02c5nESQBLEjgBRhUwJK74" |
| 457 |
test_content = "test content of the remote script áéíóú" |
| 458 |
raw_service_response = json.dumps({ |
| 459 |
'content': test_content, |
| 460 |
'morestuff': 'whocares', |
| 461 |
}).encode("utf8") |
| 462 |
|
| 463 |
downloader = helpers._ScriptDownloader(test_url) |
| 464 |
with patch('urllib.request.urlopen') as mock_urlopen: |
| 465 |
with patch('http.client.HTTPResponse') as mock_http_response: |
| 466 |
mock_http_response.read.return_value = raw_service_response |
| 467 |
mock_urlopen.return_value = mock_http_response |
| 468 |
|
| 469 |
content = downloader.get() |
| 470 |
|
| 471 |
# check urlopen was called with the proper url, and passing correct headers |
| 472 |
headers = { |
| 473 |
'Accept': 'application/json', |
| 474 |
'User-agent': helpers._ScriptDownloader.USER_AGENT, |
| 475 |
} |
| 476 |
(call,) = mock_urlopen.mock_calls |
| 477 |
(called_request,) = call[1] |
| 478 |
self.assertIsInstance(called_request, Request) |
| 479 |
self.assertEqual( |
| 480 |
called_request.full_url, "https://linkode.org/api/1/linkodes/02c5nESQBLEjgBRhUwJK74") |
| 481 |
self.assertEqual(called_request.headers, headers) |
| 482 |
self.assertEqual(content, test_content) |
| 483 |
|
| 484 |
def test_downloader_pastebin(self): |
| 485 |
test_url = "http://pastebin.com/sZGwz7SL" |
| 486 |
real_url = "https://pastebin.com/raw/sZGwz7SL" |
| 487 |
test_content = "test content of the remote script áéíóú" |
| 488 |
raw_service_response = test_content.encode("utf8") |
| 489 |
|
| 490 |
downloader = helpers._ScriptDownloader(test_url) |
| 491 |
with patch('urllib.request.urlopen') as mock_urlopen: |
| 492 |
with patch('http.client.HTTPResponse') as mock_http_response: |
| 493 |
mock_http_response.read.return_value = raw_service_response |
| 494 |
mock_urlopen.return_value = mock_http_response |
| 495 |
mock_http_response.geturl.return_value = real_url |
| 496 |
|
| 497 |
content = downloader.get() |
| 498 |
|
| 499 |
# check urlopen was called with the proper url, and passing correct headers |
| 500 |
headers = { |
| 501 |
'Accept': 'text/plain', |
| 502 |
'User-agent': helpers._ScriptDownloader.USER_AGENT, |
| 503 |
} |
| 504 |
(call,) = mock_urlopen.mock_calls |
| 505 |
(called_request,) = call[1] |
| 506 |
self.assertIsInstance(called_request, Request) |
| 507 |
self.assertEqual(called_request.full_url, real_url) |
| 508 |
self.assertEqual(called_request.headers, headers) |
| 509 |
self.assertEqual(content, test_content) |
| 510 |
|
| 511 |
def test_downloader_gist(self): |
| 512 |
test_url = "http://gist.github.com/facundobatista/6ff4f75760a9acc35e68bae8c1d7da1c" |
| 513 |
real_url = "https://gist.github.com/facundobatista/6ff4f75760a9acc35e68bae8c1d7da1c/raw" |
| 514 |
test_content = "test content of the remote script áéíóú" |
| 515 |
raw_service_response = test_content.encode("utf8") |
| 516 |
|
| 517 |
downloader = helpers._ScriptDownloader(test_url) |
| 518 |
with patch('urllib.request.urlopen') as mock_urlopen: |
| 519 |
with patch('http.client.HTTPResponse') as mock_http_response: |
| 520 |
mock_http_response.read.return_value = raw_service_response |
| 521 |
mock_urlopen.return_value = mock_http_response |
| 522 |
mock_http_response.geturl.return_value = real_url |
| 523 |
|
| 524 |
content = downloader.get() |
| 525 |
|
| 526 |
# check urlopen was called with the proper url, and passing correct headers |
| 527 |
headers = { |
| 528 |
'Accept': 'text/plain', |
| 529 |
'User-agent': helpers._ScriptDownloader.USER_AGENT, |
| 530 |
} |
| 531 |
(call,) = mock_urlopen.mock_calls |
| 532 |
(called_request,) = call[1] |
| 533 |
self.assertIsInstance(called_request, Request) |
| 534 |
self.assertEqual(called_request.full_url, real_url) |
| 535 |
self.assertEqual(called_request.headers, headers) |
| 536 |
self.assertEqual(content, test_content) |
| 537 |
|
| 538 |
def test_downloader_raw_with_redirection(self): |
| 539 |
test_url = "http://bit.ly/will-redirect" |
| 540 |
final_url = "http://real-service.com/" |
| 541 |
raw_service_response = b"test content of the remote script" |
| 542 |
downloader = helpers._ScriptDownloader(test_url) |
| 543 |
response_contents = [ |
| 544 |
b"whatever; we don't care as we are redirectect", |
| 545 |
raw_service_response, |
| 546 |
] |
| 547 |
with patch('urllib.request.urlopen') as mock_urlopen: |
| 548 |
with patch('http.client.HTTPResponse') as mock_http_response: |
| 549 |
mock_http_response.read.side_effect = lambda: response_contents.pop() |
| 550 |
mock_http_response.geturl.return_value = final_url |
| 551 |
mock_urlopen.return_value = mock_http_response |
| 552 |
|
| 553 |
content = downloader.get() |
| 554 |
|
| 555 |
# two calls, first to the service that will redirect us, second to the final one |
| 556 |
call1, call2 = mock_urlopen.mock_calls |
| 557 |
|
| 558 |
(called_request,) = call1[1] |
| 559 |
self.assertEqual(called_request.full_url, test_url) |
| 560 |
|
| 561 |
(called_request,) = call2[1] |
| 562 |
self.assertEqual(called_request.full_url, final_url) |
| 563 |
self.assertEqual(content, raw_service_response.decode("utf8")) |
| 564 |
|
| 565 |
self.assertLoggedInfo("Download redirect detect, now downloading from", final_url) |
| 566 |
|
| 567 |
|
| 568 |
def test_getbinpath_posix(tmp_path): |
| 569 |
realbin = tmp_path / "bin" |
| 570 |
realbin.mkdir() |
| 571 |
path = helpers.get_env_bin_path(tmp_path) |
| 572 |
assert path == realbin |
| 573 |
|
| 574 |
|
| 575 |
def test_getbinpath_windows(tmp_path): |
| 576 |
realbin = tmp_path / "Scripts" |
| 577 |
realbin.mkdir() |
| 578 |
path = helpers.get_env_bin_path(tmp_path) |
| 579 |
assert path == realbin |
| 580 |
|
| 581 |
|
| 582 |
def test_getbinpath_missing(tmp_path): |
| 583 |
with pytest.raises(ValueError): |
| 584 |
helpers.get_env_bin_path(tmp_path) |