api_test.rb
4.66 KB
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
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['comments'].map {|c| c['paragraph_uuid']}
assert_equivalent ['area'], json['comments'].map {|c| c['comment_paragraph_selected_area']}
assert_equivalent ['content'], json['comments'].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['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
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['comments'].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["article"]["type"]
assert json["article"]["setting"]["comment_paragraph_plugin_activate"]
end
end