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:
parent
25d83c8978
commit
4527573a50
@ -96,7 +96,7 @@ def update():
|
||||
try:
|
||||
action = request.forms[':action']
|
||||
except KeyError:
|
||||
raise HTTPError(400, ":action field not found")
|
||||
raise HTTPError(400, "Missing ':action' field!")
|
||||
|
||||
if action in ("verify", "submit"):
|
||||
return ""
|
||||
@ -105,7 +105,7 @@ def update():
|
||||
try:
|
||||
content = request.files['content']
|
||||
except KeyError:
|
||||
raise HTTPError(400, "content file field not found")
|
||||
raise HTTPError(400, "Missing 'content' file-field!")
|
||||
zip_data = content.file.read()
|
||||
try:
|
||||
zf = zipfile.ZipFile(BytesIO(zip_data))
|
||||
@ -118,7 +118,8 @@ def update():
|
||||
name = request.forms.get("name")
|
||||
version = request.forms.get("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
|
||||
for pkg in core.find_packages(packages()):
|
||||
if pkg.pkgname == name and pkg.version == version:
|
||||
@ -130,21 +131,21 @@ def update():
|
||||
return ""
|
||||
|
||||
if action != "file_upload":
|
||||
raise HTTPError(400, "action not supported: %s" % action)
|
||||
raise HTTPError(400, "Unsupported ':action' field: %s" % action)
|
||||
|
||||
try:
|
||||
content = request.files['content']
|
||||
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):
|
||||
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):
|
||||
log.warn("Cannot upload package(%s) since it already exists! \n" +
|
||||
" You may use `--overwrite` option when starting server to disable this check. ",
|
||||
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)
|
||||
|
||||
core.store(packages.root, content.raw_filename, content.save)
|
||||
|
@ -12,10 +12,16 @@ import webtest
|
||||
|
||||
import test_core
|
||||
|
||||
try:
|
||||
from html.parser import HTMLParser
|
||||
except ImportError:
|
||||
from HTMLParser import HTMLParser
|
||||
|
||||
|
||||
# Enable logging to detect any problems with it
|
||||
##
|
||||
__main__.init_logging(level=logging.NOTSET)
|
||||
hp = HTMLParser()
|
||||
|
||||
|
||||
@pytest.fixture()
|
||||
@ -310,12 +316,12 @@ def test_cache_control_set(root):
|
||||
def test_upload_noAction(root, testapp):
|
||||
resp = testapp.post("/", expect_errors=1)
|
||||
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):
|
||||
resp = testapp.post("/", params={':action': 'BAD'}, expect_errors=1)
|
||||
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]])
|
||||
def test_upload(package, root, testapp):
|
||||
@ -334,31 +340,26 @@ def test_upload_badFilename(package, root, testapp):
|
||||
upload_files=[('content', package, b'')],
|
||||
expect_errors=1)
|
||||
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,
|
||||
params={
|
||||
':action': 'remove_pkg',
|
||||
'name': '',
|
||||
})
|
||||
assert resp.status == '400 Bad Request'
|
||||
assert "Name or version not specified" in resp.text
|
||||
@pytest.mark.parametrize(("name", "version"), [
|
||||
(None, None),
|
||||
(None, ''),
|
||||
('', None),
|
||||
(None, '1'),
|
||||
('pkg', None),
|
||||
('', '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 "Name or version not specified" in resp.text
|
||||
assert msg %(name, version) in hp.unescape(resp.text)
|
||||
|
||||
def test_remove_pkg_notFound(root, testapp):
|
||||
resp = testapp.post("/", expect_errors=1,
|
||||
@ -368,7 +369,7 @@ def test_remove_pkg_notFound(root, testapp):
|
||||
'version': '123',
|
||||
})
|
||||
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()
|
||||
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
|
||||
|
||||
("a-γρεεκ-package-1.0", None, None),
|
||||
("a-%-package-1.0", None, None),
|
||||
("some/pkg-1.0", None, None),
|
||||
]
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user