def test_authenticator_uses_provided_certs_instead_of_config_certs( config, mock_remote, http, mocker): config.merge({ "repositories": { "foo": { "url": "https://foo.bar/simple/" } }, "http-basic": { "foo": { "username": "******", "password": "******" } }, "certificates": { "foo": { "cert": "/path/to/cert", "client-cert": "/path/to/client-cert", } }, }) authenticator = Authenticator(config, NullIO()) session_send = mocker.patch.object(authenticator.session, "send") authenticator.request( "get", "https://foo.bar/files/foo-0.1.0.tar.gz", verify="/path/to/provided/cert", cert="/path/to/provided/client-cert", ) call_args = session_send.call_args call_args.kwargs["verify"] == pathlib.Path("/path/to/provided/cert") call_args.kwargs["cert"] == pathlib.Path("/path/to/provided/client-cert")
def test_authenticator_uses_env_provided_credentials( config, environ, mock_remote, http, environment_repository_credentials): config.merge({"repositories": {"foo": {"url": "https://foo.bar/simple/"}}}) authenticator = Authenticator(config, NullIO()) authenticator.request("get", "https://foo.bar/files/foo-0.1.0.tar.gz") request = http.last_request() assert "Basic YmFyOmJheg==" == request.headers["Authorization"]
def test_authenticator_request_raises_exception_when_attempts_exhausted( mocker, config, http): sleep = mocker.patch("time.sleep") sdist_uri = "https://foo.bar/files/{}/foo-0.1.0.tar.gz".format( str(uuid.uuid4())) def callback(*_, **__): raise requests.exceptions.ConnectionError(str(uuid.uuid4())) httpretty.register_uri(httpretty.GET, sdist_uri, body=callback) authenticator = Authenticator(config, NullIO()) with pytest.raises(requests.exceptions.ConnectionError): authenticator.request("get", sdist_uri) assert sleep.call_count == 5
def test_authenticator_request_retries_on_exception(mocker, config, http): sleep = mocker.patch("time.sleep") sdist_uri = "https://foo.bar/files/{}/foo-0.1.0.tar.gz".format( str(uuid.uuid4())) content = str(uuid.uuid4()) seen = list() def callback(request, uri, response_headers): if seen.count(uri) < 2: seen.append(uri) raise requests.exceptions.ConnectionError("Disconnected") return [200, response_headers, content] httpretty.register_uri(httpretty.GET, sdist_uri, body=callback) authenticator = Authenticator(config, NullIO()) response = authenticator.request("get", sdist_uri) assert response.text == content assert sleep.call_count == 2
def test_authenticator_request_retries_on_status_code(mocker, config, http, status, attempts): sleep = mocker.patch("time.sleep") sdist_uri = "https://foo.bar/files/{}/foo-0.1.0.tar.gz".format( str(uuid.uuid4())) content = str(uuid.uuid4()) def callback(request, uri, response_headers): return [status, response_headers, content] httpretty.register_uri(httpretty.GET, sdist_uri, body=callback) authenticator = Authenticator(config, NullIO()) with pytest.raises(requests.exceptions.HTTPError) as excinfo: authenticator.request("get", sdist_uri) assert excinfo.value.response.status_code == status assert excinfo.value.response.text == content assert sleep.call_count == attempts
def test_authenticator_uses_empty_strings_as_default_password( config, mock_remote, http): config.merge({ "repositories": { "foo": { "url": "https://foo.bar/simple/" } }, "http-basic": { "foo": { "username": "******" } }, }) authenticator = Authenticator(config, NullIO()) authenticator.request("get", "https://foo.bar/files/foo-0.1.0.tar.gz") request = http.last_request() assert "Basic YmFyOg==" == request.headers["Authorization"]
def test_authenticator_uses_credentials_from_config_if_not_provided( config, mock_remote, http): config.merge({ "repositories": { "foo": { "url": "https://foo.bar/simple/" } }, "http-basic": { "foo": { "username": "******", "password": "******" } }, }) authenticator = Authenticator(config, NullIO()) authenticator.request("get", "https://foo.bar/files/foo-0.1.0.tar.gz") request = http.last_request() assert "Basic YmFyOmJheg==" == request.headers["Authorization"]
def test_authenticator_uses_username_only_credentials(config, mock_remote, http): config.merge({ "repositories": { "foo": { "url": "https://foo.bar/simple/" } }, "http-basic": { "foo": { "username": "******", "password": "******" } }, }) authenticator = Authenticator(config, NullIO()) authenticator.request("get", "https://[email protected]/files/foo-0.1.0.tar.gz") request = http.last_request() assert "Basic Zm9vMDAxOg==" == request.headers["Authorization"]