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):
|
| 587 | 587 | outbox = outbox.limit(limit) |
| 588 | 588 | |
| 589 | 589 | # 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) |
| 591 | 596 | |
| 592 | 597 | # Build feed. |
| 593 | 598 | for activity in outbox: |
diff --git a/mediagoblin/tests/test_api.py b/mediagoblin/tests/test_api.py
index 10bf08f..33b9320 100644
|
a
|
b
|
class TestAPI(object):
|
| 438 | 438 | |
| 439 | 439 | def test_read_feed(self, test_app): |
| 440 | 440 | """ 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) |
| 443 | 443 | |
| 444 | 444 | uri = "/api/user/{0}/feed".format(self.active_user.username) |
| 445 | 445 | with self.mock_oauth(): |
| … |
… |
class TestAPI(object):
|
| 462 | 462 | assert feed["items"][0]["object"]["objectType"] == "image" |
| 463 | 463 | assert feed["items"][0]["object"]["id"] == data["object"]["id"] |
| 464 | 464 | |
| | 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 |
| 465 | 507 | |
| 466 | 508 | def test_read_another_feed(self, test_app): |
| 467 | 509 | """ Test able to read objects from someone else's feed """ |