Esempio n. 1
0
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 = f"https://{server.host}:{server.port}/simple"

    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
Esempio n. 2
0
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)

    assert 'User for {}:{}'.format(server.host, server.port) in \
           result.stdout, str(result)
Esempio n. 3
0
def test_prompt_for_keyring_if_needed(
    script: PipTestEnvironment,
    data: TestData,
    cert_factory: CertFactory,
    auth_needed: bool,
) -> None:
    """Test behaviour while installing from a index url
    requiring authentication and keyring is possible.
    """
    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

    response = authorization_response if auth_needed else file_response

    server = make_mock_server(ssl_context=ctx)
    server.mock.side_effect = [
        package_page({
            "simple-3.0.tar.gz": "/files/simple-3.0.tar.gz",
        }),
        response(str(data.packages / "simple-3.0.tar.gz")),
        response(str(data.packages / "simple-3.0.tar.gz")),
    ]

    url = f"https://{server.host}:{server.port}/simple"

    keyring_content = textwrap.dedent("""\
        import os
        import sys
        from collections import namedtuple

        Cred = namedtuple("Cred", ["username", "password"])

        def get_credential(url, username):
            sys.stderr.write("get_credential was called" + os.linesep)
            return Cred("USERNAME", "PASSWORD")
    """)
    keyring_path = script.site_packages_path / "keyring.py"
    keyring_path.write_text(keyring_content)

    with server_running(server):
        result = script.pip(
            "install",
            "--index-url",
            url,
            "--cert",
            cert_path,
            "--client-cert",
            cert_path,
            "simple",
        )

    if auth_needed:
        assert "get_credential was called" in result.stderr
    else:
        assert "get_credential was called" not in result.stderr
Esempio n. 4
0
def test_prompt_for_authentication(
    script: PipTestEnvironment, data: TestData, cert_factory: CertFactory
) -> None:
    """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(data.packages / "simple-3.0.tar.gz"),
    ]

    url = f"https://{server.host}:{server.port}/simple"

    with server_running(server):
        result = script.pip(
            "install",
            "--index-url",
            url,
            "--cert",
            cert_path,
            "--client-cert",
            cert_path,
            "simple",
            expect_error=True,
        )

    assert f"User for {server.host}:{server.port}" in result.stdout, str(result)
Esempio n. 5
0
def test_prioritize_url_credentials_over_netrc(
    script: PipTestEnvironment,
    data: TestData,
    cert_factory: CertFactory,
) -> None:
    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 = f"https://*****:*****@{server.host}:{server.port}/simple"

    netrc = script.scratch_path / ".netrc"
    netrc.write_text(
        f"machine {server.host} login wrongusername password wrongpassword")
    with server_running(server):
        script.environ["NETRC"] = netrc
        script.pip(
            "install",
            "--no-cache-dir",
            "--index-url",
            url,
            "--cert",
            cert_path,
            "--client-cert",
            cert_path,
            "simple",
        )
        script.assert_installed(simple="3.0")
Esempio n. 6
0
 def start(self):
     # type: () -> None
     assert not self._running, "running server cannot be started"
     self.context.enter_context(server_running(self._server))
     self.context.enter_context(self._set_running())