Commit 7c83fd474ab1512c3824e0b89c4d59420e292873

Authored by Victor Costa
1 parent a339de44

comment_paragraph: add endpoit to activate/deactivate paragraph comments

plugins/comment_paragraph/lib/comment_paragraph_plugin/api.rb
... ... @@ -10,5 +10,16 @@ class CommentParagraphPlugin::API < Grape::API
10 10 comments = comments.without_reply if(params[:without_reply].present?)
11 11 present paginate(comments), :with => Noosfero::API::Entities::Comment, :current_person => current_person
12 12 end
  13 +
  14 + {activate: true, deactivate: false}.each do |method, value|
  15 + post ":id/comment_paragraph_plugin/#{method}" do
  16 + authenticate!
  17 + article = find_article(environment.articles, params[:id])
  18 + return forbidden! unless article.comment_paragraph_plugin_enabled? && article.allow_edit?(current_person)
  19 + article.comment_paragraph_plugin_activate = value
  20 + article.save!
  21 + present_partial article, :with => Noosfero::API::Entities::Article
  22 + end
  23 + end
13 24 end
14 25 end
... ...
plugins/comment_paragraph/test/unit/api_test.rb
... ... @@ -5,12 +5,11 @@ class APITest < ActiveSupport::TestCase
5 5  
6 6 def setup
7 7 login_api
8   - environment = Environment.default
9 8 environment.enable_plugin(CommentParagraphPlugin)
10 9 end
11 10  
12 11 should 'return custom parameters for each comment' do
13   - article = fast_create(Article, :profile_id => person.id, :name => "Some thing", :published => false)
  12 + article = fast_create(TextArticle, :profile_id => person.id, :name => "Some thing", :published => false)
14 13 comment = fast_create(Comment, paragraph_uuid: '1', source_id: article.id, author_id: fast_create(Person).id)
15 14 comment.comment_paragraph_selected_area = 'area'
16 15 comment.comment_paragraph_selected_content = 'content'
... ... @@ -25,7 +24,7 @@ class APITest < ActiveSupport::TestCase
25 24 end
26 25  
27 26 should 'return comments that belongs to a paragraph' do
28   - article = fast_create(Article, :profile_id => person.id, :name => "Some thing", :published => false)
  27 + article = fast_create(TextArticle, :profile_id => person.id, :name => "Some thing", :published => false)
29 28 comment1 = fast_create(Comment, :paragraph_uuid => '1', :source_id => article.id)
30 29 comment2 = fast_create(Comment, :paragraph_uuid => nil, :source_id => article.id)
31 30 comment3 = fast_create(Comment, :paragraph_uuid => '2', :source_id => article.id)
... ... @@ -36,4 +35,25 @@ class APITest < ActiveSupport::TestCase
36 35 assert_equivalent [comment1.id], json['comments'].map {|c| c['id']}
37 36 end
38 37  
  38 + {activate: true, deactivate: false}.each do |method, value|
  39 + should "#{method} paragraph comment in an article" do
  40 + article = fast_create(TextArticle, :profile_id => person.id, :name => "Some thing", :author_id => person.id)
  41 + post "/api/v1/articles/#{article.id}/comment_paragraph_plugin/#{method}?#{params.to_query}"
  42 + json = JSON.parse(last_response.body)
  43 + assert_equal value, json["article"]["setting"]["comment_paragraph_plugin_activate"]
  44 + end
  45 +
  46 + should "not allow #{method} paragraph comment when not logged in" do
  47 + article = fast_create(TextArticle, :profile_id => person.id, :name => "Some thing")
  48 + post "/api/v1/articles/#{article.id}/comment_paragraph_plugin/#{method}"
  49 + assert_equal 401, last_response.status
  50 + end
  51 +
  52 + should "not allow #{method} paragraph comment when user does not have permission to edit article" do
  53 + author = create_user.person
  54 + article = fast_create(TextArticle, :profile_id => author.id, :name => "Some thing", :author_id => author.id)
  55 + post "/api/v1/articles/#{article.id}/comment_paragraph_plugin/#{method}?#{params.to_query}"
  56 + assert_equal 403, last_response.status
  57 + end
  58 + end
39 59 end
... ...