cirandas.net

ref: master

plugins/comment_paragraph/test/unit/api_test.rb


  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
require_relative '../test_helper'
require_relative '../../../../test/api/test_helper'

class APITest <  ActiveSupport::TestCase

  def setup
    create_and_activate_user
    login_api
    environment.enable_plugin(CommentParagraphPlugin)
  end

  should 'return custom parameters for each comment' do
    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'
    comment.save!
    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 ['1'], json.map {|c| c['paragraph_uuid']}
    assert_equivalent ['area'], json.map {|c| c['comment_paragraph_selected_area']}
    assert_equivalent ['content'], json.map {|c| c['comment_paragraph_selected_content']}
  end

  should 'return comments that belongs to a paragraph' 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)
    comment3 = fast_create(Comment, :paragraph_uuid => '2', :source_id => article.id)
    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.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["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

  should 'return comment counts grouped by paragraph' do
    article = fast_create(TextArticle, :profile_id => person.id, :name => "Some thing", :published => false)
    fast_create(Comment, :paragraph_uuid => '1', :source_id => article.id)
    fast_create(Comment, :paragraph_uuid => nil, :source_id => article.id)
    fast_create(Comment, :paragraph_uuid => '2', :source_id => article.id)
    fast_create(Comment, :paragraph_uuid => '2', :source_id => article.id)
    get "/api/v1/articles/#{article.id}/comment_paragraph_plugin/comments/count?#{params.to_query}"

    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.map {|c| c['id']}
  end

  should "create discussion article" do
    article = fast_create(Article, :profile_id => person.id)
    params[:article] = {name: "Title", type: "CommentParagraphPlugin::Discussion"}
    post "/api/v1/articles/#{article.id}/children?#{params.to_query}"
    json = JSON.parse(last_response.body)
    assert_equal "CommentParagraphPlugin::Discussion", json["type"]
    assert json["setting"]["comment_paragraph_plugin_activate"]
  end

  should 'export comments' do
    login_api
    article = fast_create(Article, :profile_id => person.id, :name => "Some thing")
    comment1 = fast_create(Comment, :created_at => Time.now - 1.days, :source_id => article, :title => 'a comment', :body => 'a comment', :paragraph_uuid => nil)
    comment2 = fast_create(Comment, :created_at => Time.now - 2.days, :source_id => article, :title => 'b comment', :body => 'b comment', :paragraph_uuid => nil)
    get "/api/v1/articles/#{article.id}/comment_paragraph_plugin/export?#{params.to_query}"
    assert_equal 200, last_response.status
    assert_equal 'text/csv; charset=UTF-8; header=present', last_response.content_type
    lines = last_response.body.split("\n")
    assert_equal '"paragraph_id","paragraph_text","comment_id","comment_reply_to","comment_title","comment_content","comment_author_name","comment_author_email"', lines.first
    assert_equal "\"\",\"\",\"#{comment2.id}\",\"\",\"b comment\",\"b comment\",\"#{comment2.author_name}\",\"#{comment2.author_email}\"", lines.second
    assert_match /#{article.slug}/, last_response.original_headers["Content-Disposition"]
  end

end