1
0
mirror of https://github.com/pypiserver/pypiserver synced 2025-02-22 19:19:37 +01:00

app: Improve server-response error-messages.

+ test: Parametrize remove-pkg TCs.
This commit is contained in:
Kostis Anagnostopoulos 2016-01-19 12:52:34 +01:00
parent 25d83c8978
commit 4527573a50
3 changed files with 35 additions and 33 deletions

@ -96,7 +96,7 @@ def update():
try: try:
action = request.forms[':action'] action = request.forms[':action']
except KeyError: except KeyError:
raise HTTPError(400, ":action field not found") raise HTTPError(400, "Missing ':action' field!")
if action in ("verify", "submit"): if action in ("verify", "submit"):
return "" return ""
@ -105,7 +105,7 @@ def update():
try: try:
content = request.files['content'] content = request.files['content']
except KeyError: except KeyError:
raise HTTPError(400, "content file field not found") raise HTTPError(400, "Missing 'content' file-field!")
zip_data = content.file.read() zip_data = content.file.read()
try: try:
zf = zipfile.ZipFile(BytesIO(zip_data)) zf = zipfile.ZipFile(BytesIO(zip_data))
@ -118,7 +118,8 @@ def update():
name = request.forms.get("name") name = request.forms.get("name")
version = request.forms.get("version") version = request.forms.get("version")
if not name or not version: if not name or not version:
raise HTTPError(400, "Name or version not specified") msg = "Missing 'name'/'version' fields: name=%s, version=%s"
raise HTTPError(400, msg % (name, version))
found = None found = None
for pkg in core.find_packages(packages()): for pkg in core.find_packages(packages()):
if pkg.pkgname == name and pkg.version == version: if pkg.pkgname == name and pkg.version == version:
@ -130,21 +131,21 @@ def update():
return "" return ""
if action != "file_upload": if action != "file_upload":
raise HTTPError(400, "action not supported: %s" % action) raise HTTPError(400, "Unsupported ':action' field: %s" % action)
try: try:
content = request.files['content'] content = request.files['content']
except KeyError: except KeyError:
raise HTTPError(400, "content file field not found") raise HTTPError(400, "Missing 'content' file-field!")
if not core.is_valid_pkg_filename(content.raw_filename): if not core.is_valid_pkg_filename(content.raw_filename):
raise HTTPError(400, "bad filename") raise HTTPError(400, "Bad filename: %s" % content.raw_filename)
if not config.overwrite and core.exists(packages.root, content.raw_filename): if not config.overwrite and core.exists(packages.root, content.raw_filename):
log.warn("Cannot upload package(%s) since it already exists! \n" + log.warn("Cannot upload package(%s) since it already exists! \n" +
" You may use `--overwrite` option when starting server to disable this check. ", " You may use `--overwrite` option when starting server to disable this check. ",
content.raw_filename) content.raw_filename)
msg = "Package already exists! Use `--overwrite` option on server." msg = "Package already exists! Start server with `--overwrite` option?"
raise HTTPError(409, msg) raise HTTPError(409, msg)
core.store(packages.root, content.raw_filename, content.save) core.store(packages.root, content.raw_filename, content.save)

@ -12,10 +12,16 @@ import webtest
import test_core import test_core
try:
from html.parser import HTMLParser
except ImportError:
from HTMLParser import HTMLParser
# Enable logging to detect any problems with it # Enable logging to detect any problems with it
## ##
__main__.init_logging(level=logging.NOTSET) __main__.init_logging(level=logging.NOTSET)
hp = HTMLParser()
@pytest.fixture() @pytest.fixture()
@ -310,12 +316,12 @@ def test_cache_control_set(root):
def test_upload_noAction(root, testapp): def test_upload_noAction(root, testapp):
resp = testapp.post("/", expect_errors=1) resp = testapp.post("/", expect_errors=1)
assert resp.status == '400 Bad Request' assert resp.status == '400 Bad Request'
assert ":action field not found" in resp.text assert "Missing ':action' field!" in hp.unescape(resp.text)
def test_upload_badAction(root, testapp): def test_upload_badAction(root, testapp):
resp = testapp.post("/", params={':action': 'BAD'}, expect_errors=1) resp = testapp.post("/", params={':action': 'BAD'}, expect_errors=1)
assert resp.status == '400 Bad Request' assert resp.status == '400 Bad Request'
assert "action not supported: BAD" in resp.text assert "Unsupported ':action' field: BAD" in hp.unescape(resp.text)
@pytest.mark.parametrize(("package"), [f[0] for f in test_core.files if f[1]]) @pytest.mark.parametrize(("package"), [f[0] for f in test_core.files if f[1]])
def test_upload(package, root, testapp): def test_upload(package, root, testapp):
@ -334,31 +340,26 @@ def test_upload_badFilename(package, root, testapp):
upload_files=[('content', package, b'')], upload_files=[('content', package, b'')],
expect_errors=1) expect_errors=1)
assert resp.status == '400 Bad Request' assert resp.status == '400 Bad Request'
assert "bad filename" in resp.text assert "Bad filename: %s" % package in resp.text
def test_remove_pkg_missingNaveVersion(root, testapp):
resp = testapp.post("/", expect_errors=1,
params={
':action': 'remove_pkg',
'version': '',
})
assert resp.status == '400 Bad Request'
assert "Name or version not specified" in resp.text
resp = testapp.post("/", expect_errors=1, @pytest.mark.parametrize(("name", "version"), [
params={ (None, None),
':action': 'remove_pkg', (None, ''),
'name': '', ('', None),
}) (None, '1'),
assert resp.status == '400 Bad Request' ('pkg', None),
assert "Name or version not specified" in resp.text ('', '1'),
('pkg', ''),
])
def test_remove_pkg_missingNaveVersion(name, version, root, testapp):
msg = "Missing 'name'/'version' fields: name=%s, version=%s"
params = {':action': 'remove_pkg', 'name': name, 'version': version}
params = dict((k, v) for k,v in params.items() if v is not None)
resp = testapp.post("/", expect_errors=1, params=params)
resp = testapp.post("/", expect_errors=1,
params={
':action': 'remove_pkg',
})
assert resp.status == '400 Bad Request' assert resp.status == '400 Bad Request'
assert "Name or version not specified" in resp.text assert msg %(name, version) in hp.unescape(resp.text)
def test_remove_pkg_notFound(root, testapp): def test_remove_pkg_notFound(root, testapp):
resp = testapp.post("/", expect_errors=1, resp = testapp.post("/", expect_errors=1,
@ -368,7 +369,7 @@ def test_remove_pkg_notFound(root, testapp):
'version': '123', 'version': '123',
}) })
assert resp.status == '404 Not Found' assert resp.status == '404 Not Found'
assert "foo (123) not found" in resp.text assert "foo (123) not found" in hp.unescape(resp.text)
@pytest.mark.xfail() @pytest.mark.xfail()
def test_remove_pkg(root, testapp): def test_remove_pkg(root, testapp):

@ -47,7 +47,7 @@ files = [
("pkg-3!1.0-0.1.tgz", 'pkg-3!1.0', '0.1'), # TO BE FIXED ("pkg-3!1.0-0.1.tgz", 'pkg-3!1.0', '0.1'), # TO BE FIXED
("pkg-3!1+.0-0.1.tgz", 'pkg-3!1+.0', '0.1'), # TO BE FIXED ("pkg-3!1+.0-0.1.tgz", 'pkg-3!1+.0', '0.1'), # TO BE FIXED
("a-γρεεκ-package-1.0", None, None), ("a-%-package-1.0", None, None),
("some/pkg-1.0", None, None), ("some/pkg-1.0", None, None),
] ]