Skip to content

Commit

Permalink
Add support to filter releases by state in the JSON API
Browse files Browse the repository at this point in the history
This commit add the possibility to request releases according to
their state. This is available only to the JSON release endpoint.

Signed-off-by: Clement Verna <cverna@tutanota.com>
  • Loading branch information
cverna authored and mergify[bot] committed Jul 26, 2018
1 parent 4d1c284 commit beb69a0
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 0 deletions.
6 changes: 6 additions & 0 deletions bodhi/server/schemas.py
Expand Up @@ -339,6 +339,12 @@ class ListReleaseSchema(PaginatedSchema):
preparer=[util.splitter],
)

state = colander.SchemaNode(
colander.String(),
validator=colander.OneOf(list(ReleaseState.values())),
missing=None,
)


class SaveReleaseSchema(CSRFProtectedSchema, colander.MappingSchema):
"""An API schema for bodhi.server.services.releases.save_release()."""
Expand Down
5 changes: 5 additions & 0 deletions bodhi/server/services/releases.py
Expand Up @@ -35,6 +35,7 @@
BuildrootOverride,
Package,
Release,
ReleaseState,
)
from bodhi.server.validators import (
validate_tags,
Expand Down Expand Up @@ -259,6 +260,10 @@ def query_releases_json(request):
query = query.join(Release.builds).join(Build.package)
query = query.filter(or_(*[Package.id == p.id for p in packages]))

state = data.get('state')
if state is not None:
query = query.filter(Release.state == ReleaseState.from_string(state))

# We can't use ``query.count()`` here because it is naive with respect to
# all the joins that we're doing above.
count_query = query.with_labels().statement\
Expand Down
28 changes: 28 additions & 0 deletions bodhi/tests/server/services/test_releases.py
Expand Up @@ -195,6 +195,34 @@ def test_new_release(self):

self.assertEquals(r.state, ReleaseState.disabled)

def test_list_releases_by_current_state(self):
""" Test that we can filter releases using the 'current' state """
res = self.app.get('/releases/', {"state": 'current'})
body = res.json_body
self.assertEquals(len(body['releases']), 1)
self.assertEquals(body['releases'][0]['name'], 'F17')

def test_list_releases_by_disabled_state(self):
""" Test that we can filter releases using the 'disabled' state """
res = self.app.get('/releases/', {"state": 'disabled'})
body = res.json_body
self.assertEquals(len(body['releases']), 1)
self.assertEquals(body['releases'][0]['name'], 'F22')

def test_list_releases_by_pending_state(self):
""" Test that we can filter releases using the 'pending' state """
res = self.app.get('/releases/', {"state": 'pending'})
body = res.json_body
self.assertEquals(len(body['releases']), 0)

def test_list_releases_with_a_wrong_state(self):
""" Test that we get a 400 error when we use an undefined state """
res = self.app.get('/releases/', {"state": 'active'}, status=400)
body = res.json_body
# the error description is not the same in Python2 and Python3
# so let's just make sure we have an error.
self.assertEquals(body['status'], 'error')

@mock.patch('bodhi.server.services.releases.log.info', side_effect=IOError('BOOM!'))
def test_save_release_exception_handler(self, info):
"""Test the exception handler in save_release()."""
Expand Down

0 comments on commit beb69a0

Please sign in to comment.