From 6119a837ea103276025faaec1feb1f49429271a3 Mon Sep 17 00:00:00 2001 From: Devesh Kumar Singh Date: Thu, 30 Apr 2020 21:59:20 +0530 Subject: [PATCH 1/3] Add unit tests for --no-input flag --- news/7688.doc | 1 + tests/functional/test_install_config.py | 66 ++++++++++++++++++++++++- tests/lib/server.py | 16 ++++++ 3 files changed, 82 insertions(+), 1 deletion(-) create mode 100644 news/7688.doc diff --git a/news/7688.doc b/news/7688.doc new file mode 100644 index 00000000000..e891c7e8c29 --- /dev/null +++ b/news/7688.doc @@ -0,0 +1 @@ +Add ``--no-input`` option to pip docs diff --git a/tests/functional/test_install_config.py b/tests/functional/test_install_config.py index 6cd283f077f..8e9b04a37d9 100644 --- a/tests/functional/test_install_config.py +++ b/tests/functional/test_install_config.py @@ -1,10 +1,17 @@ import os +import ssl import tempfile import textwrap import pytest -from tests.lib.server import file_response, package_page +from tests.lib.server import ( + authorization_response, + file_response, + make_mock_server, + package_page, + server_running, +) def test_options_from_env_vars(script): @@ -209,3 +216,60 @@ def test_install_no_binary_via_config_disables_cached_wheels( assert "Building wheel for upper" not in str(res), str(res) # Must have used source, not a cached wheel to install upper. assert "Running setup.py install for upper" in str(res), str(res) + + +def test_prompt_for_authentication(script, data, cert_factory): + """Test behaviour while installing from a index url + requiring authentication + """ + cert_path = cert_factory() + ctx = ssl.SSLContext(ssl.PROTOCOL_SSLv23) + ctx.load_cert_chain(cert_path, cert_path) + ctx.load_verify_locations(cafile=cert_path) + ctx.verify_mode = ssl.CERT_REQUIRED + + server = make_mock_server(ssl_context=ctx) + server.mock.side_effect = [ + package_page({ + "simple-3.0.tar.gz": "/files/simple-3.0.tar.gz", + }), + authorization_response(str(data.packages / "simple-3.0.tar.gz")), + ] + + url = "https://{}:{}/simple".format(server.host, server.port) + + with server_running(server): + result = script.pip('install', "--index-url", url, + "--cert", cert_path, "--client-cert", cert_path, + 'simple', expect_error=True) + print(result) + assert 'User for {}:{}'.format(server.host, server.port) in result.stdout + + +def test_do_not_prompt_for_authentication(script, data, cert_factory): + """Test behaviour if --no-input option is given while installing + from a index url requiring authentication + """ + cert_path = cert_factory() + ctx = ssl.SSLContext(ssl.PROTOCOL_SSLv23) + ctx.load_cert_chain(cert_path, cert_path) + ctx.load_verify_locations(cafile=cert_path) + ctx.verify_mode = ssl.CERT_REQUIRED + + server = make_mock_server(ssl_context=ctx) + + server.mock.side_effect = [ + package_page({ + "simple-3.0.tar.gz": "/files/simple-3.0.tar.gz", + }), + authorization_response(str(data.packages / "simple-3.0.tar.gz")), + ] + + url = "https://{}:{}/simple".format(server.host, server.port) + + with server_running(server): + result = script.pip('install', "--index-url", url, + "--cert", cert_path, "--client-cert", cert_path, + '--no-input', 'simple', expect_error=True) + + assert "ERROR: HTTP error 401" in result.stderr diff --git a/tests/lib/server.py b/tests/lib/server.py index bb423a2d867..6cf891d0d5d 100644 --- a/tests/lib/server.py +++ b/tests/lib/server.py @@ -210,3 +210,19 @@ def responder(environ, start_response): return [f.read()] return responder + + +def authorization_response(path): + def responder(environ, start_response): + # type: (Environ, StartResponse) -> Body + + start_response( + "401 Unauthorized", [ + ("WWW-Authenticate", "Basic"), + ], + ) + + with open(path, 'rb') as f: + return [f.read()] + + return responder From a833914387386734ad9706982890d3031d18dd12 Mon Sep 17 00:00:00 2001 From: Devesh Kumar Singh Date: Sat, 2 May 2020 01:26:27 +0530 Subject: [PATCH 2/3] Add help text to --no-input option --- src/pip/_internal/cli/cmdoptions.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/pip/_internal/cli/cmdoptions.py b/src/pip/_internal/cli/cmdoptions.py index 4c557efa80f..643b11280e3 100644 --- a/src/pip/_internal/cli/cmdoptions.py +++ b/src/pip/_internal/cli/cmdoptions.py @@ -243,7 +243,7 @@ class PipOption(Option): dest='no_input', action='store_true', default=False, - help=SUPPRESS_HELP + help="Disable prompting for input." ) # type: Callable[..., Option] proxy = partial( From 4ecd7ecbc705db7ad9953b1cab2e3b8a15c9d748 Mon Sep 17 00:00:00 2001 From: Devesh Kumar Singh Date: Sun, 17 May 2020 01:10:31 +0530 Subject: [PATCH 3/3] Assert result string in test_prompt_for_authentication --- tests/functional/test_install_config.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tests/functional/test_install_config.py b/tests/functional/test_install_config.py index 8e9b04a37d9..dcc9c66d5a4 100644 --- a/tests/functional/test_install_config.py +++ b/tests/functional/test_install_config.py @@ -242,8 +242,9 @@ def test_prompt_for_authentication(script, data, cert_factory): result = script.pip('install', "--index-url", url, "--cert", cert_path, "--client-cert", cert_path, 'simple', expect_error=True) - print(result) - assert 'User for {}:{}'.format(server.host, server.port) in result.stdout + + assert 'User for {}:{}'.format(server.host, server.port) in \ + result.stdout, str(result) def test_do_not_prompt_for_authentication(script, data, cert_factory):