Skip to content

Commit

Permalink
Catch the case where a non-existing update is submitted
Browse files Browse the repository at this point in the history
Fixes: #2236

Signed-off-by: Patrick Uiterwijk <[email protected]>
  • Loading branch information
puiterwijk authored and bowlofeggs committed Mar 21, 2018
1 parent 7509594 commit eba39b0
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 9 deletions.
16 changes: 13 additions & 3 deletions bodhi/server/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,8 @@ def cache_nvrs(request, build):
request (pyramid.util.Request): The current request.
build (basestring): The NVR of the build to cache.
Raises:
ValueError: If any of the name, version, or release in build are found to be ''.
ValueError: If the build could not be found in koji.
koji.GenericError: If an error was thrown by koji's getBuild() call.
"""
if build in request.buildinfo and 'nvr' in request.buildinfo[build]:
return
Expand All @@ -228,6 +229,10 @@ def cache_nvrs(request, build):
# We use Koji's information to get the NVR split, because modules can have dashes in their
# stream.
kbinfo = request.koji.getBuild(build)
if not kbinfo:
request.buildinfo[build]['info'] = None
request.buildinfo[build]['nvr'] = None
raise ValueError('Build %s did not exist' % build)
request.buildinfo[build]['info'] = kbinfo
request.buildinfo[build]['nvr'] = kbinfo['name'], kbinfo['version'], kbinfo['release']

Expand All @@ -245,8 +250,13 @@ def validate_nvrs(request, **kwargs):
cache_nvrs(request, build)
except ValueError:
request.validated['builds'] = []
request.errors.add('body', 'builds', 'Build not in '
'name-version-release format: %s' % build)
request.errors.add('body', 'builds', 'Build does not exist: %s' % build)
return
except koji.GenericError:
log.exception("Error retrieving koji build for %s" % build)
request.validated['builds'] = []
request.errors.add('body', 'builds',
'Koji error getting build: %s' % build)
return


Expand Down
36 changes: 30 additions & 6 deletions bodhi/tests/server/services/test_updates.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,6 @@ class TestNewUpdate(base.BaseTestCase):
"""
This class contains tests for the new_update() function.
"""
@mock.patch(**mock_valid_requirements)
def test_invalid_build_name(self, *args):
res = self.app.post_json('/updates/', self.get_update(u'bodhi-2.0-1.fc17,invalidbuild-1.0'),
status=400)
assert 'Build not in name-version-release format' in res, res

@mock.patch(**mock_valid_requirements)
def test_empty_build_name(self, *args):
res = self.app.post_json('/updates/', self.get_update([u'']), status=400)
Expand Down Expand Up @@ -265,6 +259,36 @@ def test_new_rpm_update(self, publish, *args):
publish.assert_called_once_with(
topic='update.request.testing', msg=mock.ANY)

@mock.patch.dict('bodhi.server.validators.config', {'acl_system': u'dummy'})
@mock.patch(**mock_uuid4_version1)
@mock.patch(**mock_valid_requirements)
@mock.patch('bodhi.server.notifications.publish')
def test_new_rpm_update_unknown_build(self, publish, *args):
with mock.patch('bodhi.server.buildsys.DevBuildsys.getBuild',
return_value=None):
r = self.app.post_json('/updates/', self.get_update('bodhi-2.0.0-2.fc17'),
status=400)
up = r.json_body

self.assertEquals(up['status'], 'error')
self.assertEquals(up['errors'][0]['description'],
"Build does not exist: bodhi-2.0.0-2.fc17")

@mock.patch.dict('bodhi.server.validators.config', {'acl_system': u'dummy'})
@mock.patch(**mock_uuid4_version1)
@mock.patch(**mock_valid_requirements)
@mock.patch('bodhi.server.notifications.publish')
def test_new_rpm_update_koji_error(self, publish, *args):
with mock.patch('bodhi.server.buildsys.DevBuildsys.getBuild',
side_effect=koji.GenericError()):
r = self.app.post_json('/updates/', self.get_update('bodhi-2.0.0-2.fc17'),
status=400)
up = r.json_body

self.assertEquals(up['status'], 'error')
self.assertEquals(up['errors'][0]['description'],
"Koji error getting build: bodhi-2.0.0-2.fc17")

@mock.patch.dict('bodhi.server.validators.config', {'acl_system': u'dummy'})
@mock.patch(**mock_uuid4_version1)
@mock.patch(**mock_valid_requirements)
Expand Down

0 comments on commit eba39b0

Please sign in to comment.