From bac4c76146f743783c60c7ca00fdb8a279dcc4ef Mon Sep 17 00:00:00 2001 From: Victor Costa Date: Mon, 4 Apr 2016 17:34:59 -0300 Subject: [PATCH] api: paginate comments --- lib/noosfero/api/v1/comments.rb | 3 +++ test/api/comments_test.rb | 10 ++++++++++ 2 files changed, 13 insertions(+), 0 deletions(-) diff --git a/lib/noosfero/api/v1/comments.rb b/lib/noosfero/api/v1/comments.rb index 3510c42..aef92b6 100644 --- a/lib/noosfero/api/v1/comments.rb +++ b/lib/noosfero/api/v1/comments.rb @@ -2,9 +2,12 @@ module Noosfero module API module V1 class Comments < Grape::API + MAX_PER_PAGE = 20 + before { authenticate! } resource :articles do + paginate max_per_page: MAX_PER_PAGE # Collect comments from articles # # Parameters: diff --git a/test/api/comments_test.rb b/test/api/comments_test.rb index d68c4b6..ef22a42 100644 --- a/test/api/comments_test.rb +++ b/test/api/comments_test.rb @@ -78,4 +78,14 @@ class CommentsTest < ActiveSupport::TestCase assert_not_nil comment.source end + should 'paginate comments' do + article = fast_create(Article, :profile_id => user.person.id, :name => "Some thing") + 5.times { article.comments.create!(:body => "some comment", :author => user.person) } + params[:per_page] = 3 + + get "/api/v1/articles/#{article.id}/comments?#{params.to_query}" + json = JSON.parse(last_response.body) + assert_equal 200, last_response.status + assert_equal 3, json["comments"].length + end end -- libgit2 0.21.2