def test_exception_for_redirect(register_settings): """Raise an exception when repository URL results in a redirect.""" repository_url = register_settings.repository_config["repository"] redirect_url = "https://malicious.website.org/danger" stub_response = pretend.stub( is_redirect=True, status_code=301, headers={"location": redirect_url}, ) stub_repository = pretend.stub( register=lambda package: stub_response, close=lambda: None ) register_settings.create_repository = lambda: stub_repository err_msg = ( f"{repository_url} attempted to redirect to {redirect_url}.\n" f"If you trust these URLs, set {redirect_url} as your repository URL.\n" f"Aborting." ) with pytest.raises(exceptions.RedirectDetected, match=err_msg): register.register(register_settings, helpers.WHEEL_FIXTURE)
def register_package( package, repository, pypirc='~/.pypirc', username=None, password=None, certfile=None, client_certfile=None, comment=None ): """ :param package: - sdist/bdist etc artifact to register :param repository: """ cirrus_conf = load_configuration() if cirrus_conf.has_section('twine'): username = username or cirrus_conf.get_param('twine', 'username', None) password = password or cirrus_conf.get_param('twine', 'password', None) certfile = certfile or cirrus_conf.get_param('twine', 'certfile', None) client_certfile = client_certfile or cirrus_conf.get_param('twine', 'client_certfile', None) register( package, repository, username, password, comment, pypirc, certfile, client_certfile, None )
def test_non_existent_package(register_settings): """Raise an exception when package file doesn't exist.""" stub_repository = pretend.stub() register_settings.create_repository = lambda: stub_repository package = "/foo/bar/baz.whl" with pytest.raises( exceptions.PackageNotFound, match=f'"{package}" does not exist on the file system.', ): register.register(register_settings, package)
def test_exception_for_redirect(make_settings): register_settings = make_settings(""" [pypi] repository: https://test.pypi.org/legacy username:foo password:bar """) stub_response = pretend.stub( is_redirect=True, status_code=301, headers={'location': 'https://test.pypi.org/legacy/'}) stub_repository = pretend.stub(register=lambda package: stub_response, close=lambda: None) register_settings.create_repository = lambda: stub_repository with pytest.raises(exceptions.RedirectDetected) as err: register.register(register_settings, WHEEL_FIXTURE) assert "https://test.pypi.org/legacy/" in err.value.args[0]
def test_successful_register(register_settings): """Return a successful result for a valid repository url and package.""" stub_response = pretend.stub( is_redirect=False, status_code=200, headers={"location": "https://test.pypi.org/legacy/"}, raise_for_status=lambda: None, ) stub_repository = pretend.stub(register=lambda package: stub_response, close=lambda: None) register_settings.create_repository = lambda: stub_repository result = register.register(register_settings, helpers.WHEEL_FIXTURE) assert result is None
def _register(self, package): register(package, self.option('repository') or 'pypi', None, None, None, '~/.pypirc', None, None, None)