From 7c83fd474ab1512c3824e0b89c4d59420e292873 Mon Sep 17 00:00:00 2001 From: Victor Costa Date: Mon, 11 Apr 2016 10:56:26 -0300 Subject: [PATCH] comment_paragraph: add endpoit to activate/deactivate paragraph comments --- plugins/comment_paragraph/lib/comment_paragraph_plugin/api.rb | 11 +++++++++++ plugins/comment_paragraph/test/unit/api_test.rb | 26 +++++++++++++++++++++++--- 2 files changed, 34 insertions(+), 3 deletions(-) diff --git a/plugins/comment_paragraph/lib/comment_paragraph_plugin/api.rb b/plugins/comment_paragraph/lib/comment_paragraph_plugin/api.rb index a269d1b..c0b69ee 100644 --- a/plugins/comment_paragraph/lib/comment_paragraph_plugin/api.rb +++ b/plugins/comment_paragraph/lib/comment_paragraph_plugin/api.rb @@ -10,5 +10,16 @@ class CommentParagraphPlugin::API < Grape::API comments = comments.without_reply if(params[:without_reply].present?) present paginate(comments), :with => Noosfero::API::Entities::Comment, :current_person => current_person end + + {activate: true, deactivate: false}.each do |method, value| + post ":id/comment_paragraph_plugin/#{method}" do + authenticate! + article = find_article(environment.articles, params[:id]) + return forbidden! unless article.comment_paragraph_plugin_enabled? && article.allow_edit?(current_person) + article.comment_paragraph_plugin_activate = value + article.save! + present_partial article, :with => Noosfero::API::Entities::Article + end + end end end diff --git a/plugins/comment_paragraph/test/unit/api_test.rb b/plugins/comment_paragraph/test/unit/api_test.rb index f68f6b8..363b499 100644 --- a/plugins/comment_paragraph/test/unit/api_test.rb +++ b/plugins/comment_paragraph/test/unit/api_test.rb @@ -5,12 +5,11 @@ class APITest < ActiveSupport::TestCase def setup login_api - environment = Environment.default environment.enable_plugin(CommentParagraphPlugin) end should 'return custom parameters for each comment' do - article = fast_create(Article, :profile_id => person.id, :name => "Some thing", :published => false) + article = fast_create(TextArticle, :profile_id => person.id, :name => "Some thing", :published => false) comment = fast_create(Comment, paragraph_uuid: '1', source_id: article.id, author_id: fast_create(Person).id) comment.comment_paragraph_selected_area = 'area' comment.comment_paragraph_selected_content = 'content' @@ -25,7 +24,7 @@ class APITest < ActiveSupport::TestCase end should 'return comments that belongs to a paragraph' do - article = fast_create(Article, :profile_id => person.id, :name => "Some thing", :published => false) + 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) comment3 = fast_create(Comment, :paragraph_uuid => '2', :source_id => article.id) @@ -36,4 +35,25 @@ class APITest < ActiveSupport::TestCase assert_equivalent [comment1.id], json['comments'].map {|c| c['id']} end + {activate: true, deactivate: false}.each do |method, value| + should "#{method} paragraph comment in an article" do + article = fast_create(TextArticle, :profile_id => person.id, :name => "Some thing", :author_id => person.id) + post "/api/v1/articles/#{article.id}/comment_paragraph_plugin/#{method}?#{params.to_query}" + json = JSON.parse(last_response.body) + assert_equal value, json["article"]["setting"]["comment_paragraph_plugin_activate"] + end + + should "not allow #{method} paragraph comment when not logged in" do + article = fast_create(TextArticle, :profile_id => person.id, :name => "Some thing") + post "/api/v1/articles/#{article.id}/comment_paragraph_plugin/#{method}" + assert_equal 401, last_response.status + end + + should "not allow #{method} paragraph comment when user does not have permission to edit article" do + author = create_user.person + article = fast_create(TextArticle, :profile_id => author.id, :name => "Some thing", :author_id => author.id) + post "/api/v1/articles/#{article.id}/comment_paragraph_plugin/#{method}?#{params.to_query}" + assert_equal 403, last_response.status + end + end end -- libgit2 0.21.2