Merge "Fix 500 error when filtering by invalid version string"

This commit is contained in:
Jenkins 2016-01-12 07:07:33 +00:00 committed by Gerrit Code Review
commit f4d46d1c34
2 changed files with 16 additions and 0 deletions

View File

@ -114,6 +114,13 @@ class ArtifactsController(object):
filter['value'] = self._get_latest_version(
req, filters['name'][0]['value'], type_name,
type_version)
else:
try:
semantic_version.Version(filter['value'], partial=True)
except ValueError:
msg = (_('The format of the version %s is not valid. '
'Use semver notation') % filter['value'])
raise webob.exc.HTTPBadRequest(explanation=msg)
res = artifact_repo.list(filters=filters,
show_level=Showlevel.BASIC,

View File

@ -1956,3 +1956,12 @@ paste.filter_factory = glance.tests.utils:FakeAuthMiddleware.factory
result = self._check_artifact_get(url=url)['artifacts']
self.assertEqual(1, len(result))
def test_filter_by_bad_version(self):
bad_versions = ['kkk', '1.k', 'h.0', '1.3.hf', 's.9.2s2']
response_string = ('The format of the version %s is not valid. '
'Use semver notation')
for bad_version in bad_versions:
url = '/withprops/v1.0/drafts?version=gt:%s' % bad_version
result = self._check_artifact_get(url=url, status=400)
self.assertIn(response_string % bad_version, result)