Ticket #5408: 0001-Fix-5408-ignore-non-int-offset-in-api-feed.patch

File 0001-Fix-5408-ignore-non-int-offset-in-api-feed.patch, 3.7 KB (added by Loic Dachary, 9 years ago)
  • mediagoblin/api/views.py

    From 1ba57d3f039495d0799d545c96002313f8b2cd94 Mon Sep 17 00:00:00 2001
    From: Loic Dachary <loic@dachary.org>
    Date: Mon, 25 Jan 2016 17:26:33 +0700
    Subject: [PATCH] Fix #5408 - ignore non-int offset in api feed
    
    In the same fashion limit=BAD fallsback to the default value,
    fallback to zero when offset=WORSE.
    
    Also add test coverage verifying limit/offset do the right thing.
    
    Signed-off-by: Loic Dachary <loic@dachary.org>
    ---
     mediagoblin/api/views.py      |  7 ++++++-
     mediagoblin/tests/test_api.py | 46 +++++++++++++++++++++++++++++++++++++++++--
     2 files changed, 50 insertions(+), 3 deletions(-)
    
    diff --git a/mediagoblin/api/views.py b/mediagoblin/api/views.py
    index dcd04cd..74181fd 100644
    a b def feed_endpoint(request, outbox=None):  
    587587    outbox = outbox.limit(limit)
    588588
    589589    # Offset (default: no offset - first <count>  result)
    590     outbox = outbox.offset(request.args.get("offset", 0))
     590    offset = request.args.get("offset", 0)
     591    try:
     592        offset = int(offset)
     593    except ValueError:
     594        offset = 0
     595    outbox = outbox.offset(offset)
    591596
    592597    # Build feed.
    593598    for activity in outbox:
  • mediagoblin/tests/test_api.py

    diff --git a/mediagoblin/tests/test_api.py b/mediagoblin/tests/test_api.py
    index 10bf08f..33b9320 100644
    a b class TestAPI(object):  
    438438
    439439    def test_read_feed(self, test_app):
    440440        """ Test able to read objects from the feed """
    441         response, data = self._upload_image(test_app, GOOD_JPG)
    442         response, data = self._post_image_to_feed(test_app, data)
     441        response, image_data = self._upload_image(test_app, GOOD_JPG)
     442        response, data = self._post_image_to_feed(test_app, image_data)
    443443
    444444        uri = "/api/user/{0}/feed".format(self.active_user.username)
    445445        with self.mock_oauth():
    class TestAPI(object):  
    462462            assert feed["items"][0]["object"]["objectType"] == "image"
    463463            assert feed["items"][0]["object"]["id"] == data["object"]["id"]
    464464
     465        default_limit = 20
     466        items_count = default_limit * 2
     467        for i in range(items_count):
     468            response, image_data = self._upload_image(test_app, GOOD_JPG)
     469            self._post_image_to_feed(test_app, image_data)
     470        items_count += 1  # because there already is one
     471
     472        #
     473        # default returns default_limit items
     474        #
     475        with self.mock_oauth():
     476            response = test_app.get(uri)
     477            feed = json.loads(response.body.decode())
     478            assert len(feed["items"]) == default_limit
     479
     480        #
     481        # silentely ignore count and offset that that are
     482        # not a number
     483        #
     484        with self.mock_oauth():
     485            response = test_app.get(uri + "?count=BAD&offset=WORSE")
     486            feed = json.loads(response.body.decode())
     487            assert len(feed["items"]) == default_limit
     488
     489        #
     490        # if offset is less than default_limit items
     491        # from the end of the feed, return less than
     492        # default_limit
     493        #
     494        with self.mock_oauth():
     495            near_the_end = items_count - default_limit / 2
     496            response = test_app.get(uri + "?offset=%d" % near_the_end)
     497            feed = json.loads(response.body.decode())
     498            assert len(feed["items"]) < default_limit
     499
     500        #
     501        # count=5 returns 5 items
     502        #
     503        with self.mock_oauth():
     504            response = test_app.get(uri + "?count=5")
     505            feed = json.loads(response.body.decode())
     506            assert len(feed["items"]) == 5
    465507
    466508    def test_read_another_feed(self, test_app):
    467509        """ Test able to read objects from someone else's feed """