From b4d35439e69c6974c794b547c76d85cd94bd4b5f Mon Sep 17 00:00:00 2001 From: Victor Costa Date: Mon, 11 Apr 2016 14:42:26 -0300 Subject: [PATCH] api: not return comments marked as spam --- lib/noosfero/api/v1/comments.rb | 1 + plugins/comment_paragraph/lib/comment_paragraph_plugin/api.rb | 1 + plugins/comment_paragraph/test/unit/api_test.rb | 12 ++++++++++++ test/api/comments_test.rb | 10 ++++++++++ 4 files changed, 24 insertions(+), 0 deletions(-) diff --git a/lib/noosfero/api/v1/comments.rb b/lib/noosfero/api/v1/comments.rb index a61569a..84a264a 100644 --- a/lib/noosfero/api/v1/comments.rb +++ b/lib/noosfero/api/v1/comments.rb @@ -20,6 +20,7 @@ module Noosfero get ":id/comments" do article = find_article(environment.articles, params[:id]) comments = select_filtered_collection_of(article, :comments, params) + comments = comments.without_spam comments = comments.without_reply if(params[:without_reply].present?) comments = plugins.filter(:unavailable_comments, comments) present paginate(comments), :with => Entities::Comment, :current_person => current_person diff --git a/plugins/comment_paragraph/lib/comment_paragraph_plugin/api.rb b/plugins/comment_paragraph/lib/comment_paragraph_plugin/api.rb index 547125b..b2689fa 100644 --- a/plugins/comment_paragraph/lib/comment_paragraph_plugin/api.rb +++ b/plugins/comment_paragraph/lib/comment_paragraph_plugin/api.rb @@ -6,6 +6,7 @@ class CommentParagraphPlugin::API < Grape::API get ':id/comment_paragraph_plugin/comments' do article = find_article(environment.articles, params[:id]) comments = select_filtered_collection_of(article, :comments, params) + comments = comments.without_spam comments = comments.in_paragraph(params[:paragraph_uuid]) comments = comments.without_reply if(params[:without_reply].present?) present paginate(comments), :with => Noosfero::API::Entities::Comment, :current_person => current_person diff --git a/plugins/comment_paragraph/test/unit/api_test.rb b/plugins/comment_paragraph/test/unit/api_test.rb index 12a29a9..9daaac2 100644 --- a/plugins/comment_paragraph/test/unit/api_test.rb +++ b/plugins/comment_paragraph/test/unit/api_test.rb @@ -68,4 +68,16 @@ class APITest < ActiveSupport::TestCase json = JSON.parse(last_response.body) assert_equal({"1"=>1, ""=>1, "2"=>2}, json) end + + should 'filter comments marked as spam' do + article = fast_create(TextArticle, :profile_id => person.id, :name => "Some thing", :published => false) + comment1 = fast_create(Comment, :paragraph_uuid => '1', :source_id => article.id) + comment2 = fast_create(Comment, :paragraph_uuid => nil, :source_id => article.id, spam: true) + comment3 = fast_create(Comment, :paragraph_uuid => '2', :source_id => article.id, spam: true) + params[:paragraph_uuid] = '1' + get "/api/v1/articles/#{article.id}/comment_paragraph_plugin/comments?#{params.to_query}" + + json = JSON.parse(last_response.body) + assert_equivalent [comment1.id], json['comments'].map {|c| c['id']} + end end diff --git a/test/api/comments_test.rb b/test/api/comments_test.rb index 6252b7f..9ab0f20 100644 --- a/test/api/comments_test.rb +++ b/test/api/comments_test.rb @@ -118,4 +118,14 @@ class CommentsTest < ActiveSupport::TestCase json = JSON.parse(last_response.body) assert_equal ["comment 2"], json["comments"].map {|c| c["body"]} end + + should 'do not return comments marked as spam' do + article = fast_create(Article, :profile_id => user.person.id, :name => "Some thing") + c1 = fast_create(Comment, source_id: article.id, body: "comment 1", spam: true) + c2 = fast_create(Comment, source_id: article.id, body: "comment 2") + + get "/api/v1/articles/#{article.id}/comments?#{params.to_query}" + json = JSON.parse(last_response.body) + assert_equal ["comment 2"], json["comments"].map {|c| c["body"]} + end end -- libgit2 0.21.2