Skip to content

Commit

Permalink
Move check_status_code back to utils; move tests
Browse files Browse the repository at this point in the history
Signed-off-by: Aditya Saky <aditya@saky.in>
  • Loading branch information
adityasaky committed Oct 28, 2019
1 parent 98c0d30 commit 4a51553
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 54 deletions.
19 changes: 2 additions & 17 deletions tests/test_upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from requests.exceptions import HTTPError

from twine.commands import upload
from twine import package, cli, exceptions
from twine import package, cli, exceptions, utils
import twine

import helpers
Expand Down Expand Up @@ -298,22 +298,7 @@ def test_check_status_code_for_wrong_repo_url(repo_url, make_settings):
# override defaults to use incorrect URL
upload_settings.repository_config['repository'] = repo_url

with pytest.raises(exceptions.InvalidPyPIUploadURL):
with pytest.raises(twine.exceptions.InvalidPyPIUploadURL):
upload.upload(upload_settings, [
WHEEL_FIXTURE, SDIST_FIXTURE, NEW_SDIST_FIXTURE, NEW_WHEEL_FIXTURE
])


@pytest.mark.parametrize('repo_url', [
"https://pypi.python.org",
"https://testpypi.python.org"
])
def test_check_status_code_for_deprecated_pypi_url(repo_url):
response = pretend.stub(
status_code=410,
url=repo_url
)

# value of Verbose doesn't matter for this check
with pytest.raises(exceptions.UploadToDeprecatedPyPIDetected):
upload.check_status_code(response, False)
18 changes: 17 additions & 1 deletion tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@
import textwrap

import pytest
import pretend

from twine import utils
from twine import utils, exceptions

import helpers

Expand Down Expand Up @@ -389,3 +390,18 @@ def t(foo=False):
t(1)

assert t(foo=True)


@pytest.mark.parametrize('repo_url', [
"https://pypi.python.org",
"https://testpypi.python.org"
])
def test_check_status_code_for_deprecated_pypi_url(repo_url):
response = pretend.stub(
status_code=410,
url=repo_url
)

# value of Verbose doesn't matter for this check
with pytest.raises(exceptions.UploadToDeprecatedPyPIDetected):
utils.check_status_code(response, False)
37 changes: 1 addition & 36 deletions twine/commands/upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,41 +50,6 @@ def skip_upload(response, skip_existing, package):
(response.status_code == 403 and msg_403 in response.text)))


def check_status_code(response, verbose):
"""Generate a helpful message based on the response from the repository.
Raise a custom exception for recognized errors. Otherwise, print the
response content (based on the verbose option) before re-raising the
HTTPError.
"""
if response.status_code == 410 and "pypi.python.org" in response.url:
raise exceptions.UploadToDeprecatedPyPIDetected(
f"It appears you're uploading to pypi.python.org (or "
f"testpypi.python.org). You've received a 410 error response. "
f"Uploading to those sites is deprecated. The new sites are "
f"pypi.org and test.pypi.org. Try using {utils.DEFAULT_REPOSITORY}"
f" (or {utils.TEST_REPOSITORY}) to upload your packages instead. "
f"These are the default URLs for Twine now. More at "
f"https://packaging.python.org/guides/migrating-to-pypi-org/.")
elif response.status_code == 405 and "pypi.org" in response.url:
raise exceptions.InvalidPyPIUploadURL(
f"It appears you're trying to upload to pypi.org but have an "
f"invalid URL. You probably want one of these two URLs: "
f"{utils.DEFAULT_REPOSITORY} or {utils.TEST_REPOSITORY}. Check "
f"your --repository-url value.")

try:
response.raise_for_status()
except requests.HTTPError as err:
if response.text:
if verbose:
print('Content received from server:\n{}'.format(
response.text))
else:
print('NOTE: Try --verbose to see response content.')
raise err


def upload(upload_settings, dists):
dists = _find_dists(dists)

Expand Down Expand Up @@ -136,7 +101,7 @@ def upload(upload_settings, dists):
print(skip_message)
continue

check_status_code(resp, upload_settings.verbose)
utils.check_status_code(resp, upload_settings.verbose)

uploaded_packages.append(package)

Expand Down
36 changes: 36 additions & 0 deletions twine/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import configparser
from urllib.parse import urlparse, urlunparse

import requests

try:
import keyring # noqa
Expand Down Expand Up @@ -128,6 +129,41 @@ def normalize_repository_url(url):
return urlunparse(parsed)


def check_status_code(response, verbose):
"""Generate a helpful message based on the response from the repository.
Raise a custom exception for recognized errors. Otherwise, print the
response content (based on the verbose option) before re-raising the
HTTPError.
"""
if response.status_code == 410 and "pypi.python.org" in response.url:
raise exceptions.UploadToDeprecatedPyPIDetected(
f"It appears you're uploading to pypi.python.org (or "
f"testpypi.python.org). You've received a 410 error response. "
f"Uploading to those sites is deprecated. The new sites are "
f"pypi.org and test.pypi.org. Try using {DEFAULT_REPOSITORY} (or "
f"{TEST_REPOSITORY}) to upload your packages instead. These are "
f"the default URLs for Twine now. More at "
f"https://packaging.python.org/guides/migrating-to-pypi-org/.")
elif response.status_code == 405 and "pypi.org" in response.url:
raise exceptions.InvalidPyPIUploadURL(
f"It appears you're trying to upload to pypi.org but have an "
f"invalid URL. You probably want one of these two URLs: "
f"{DEFAULT_REPOSITORY} or {TEST_REPOSITORY}. Check your "
f"--repository-url value.")

try:
response.raise_for_status()
except requests.HTTPError as err:
if response.text:
if verbose:
print('Content received from server:\n{}'.format(
response.text))
else:
print('NOTE: Try --verbose to see response content.')
raise err


def get_userpass_value(cli_value, config, key, prompt_strategy=None):
"""Gets the username / password from config.
Expand Down

0 comments on commit 4a51553

Please sign in to comment.