Commit d7b67378c6d0e161e0ccc6164ae644d28c87d87d

Authored by Leandro Santos
1 parent 085aaaa9

refactoring comment test

Showing 1 changed file with 36 additions and 78 deletions   Show diff stats
test/api/comments_test.rb
@@ -2,10 +2,15 @@ require_relative 'test_helper' @@ -2,10 +2,15 @@ require_relative 'test_helper'
2 2
3 class CommentsTest < ActiveSupport::TestCase 3 class CommentsTest < ActiveSupport::TestCase
4 4
  5 + def setup
  6 + @local_person = fast_create(Person)
  7 + anonymous_setup
  8 + end
  9 + attr_reader :local_person
  10 +
5 should 'logged user not list comments if user has no permission to view the source article' do 11 should 'logged user not list comments if user has no permission to view the source article' do
6 login_api 12 login_api
7 - person = fast_create(Person)  
8 - article = fast_create(Article, :profile_id => person.id, :name => "Some thing", :published => false) 13 + article = fast_create(Article, :profile_id => local_person.id, :name => "Some thing", :published => false)
9 assert !article.published? 14 assert !article.published?
10 15
11 get "/api/v1/articles/#{article.id}/comments?#{params.to_query}" 16 get "/api/v1/articles/#{article.id}/comments?#{params.to_query}"
@@ -14,9 +19,8 @@ class CommentsTest &lt; ActiveSupport::TestCase @@ -14,9 +19,8 @@ class CommentsTest &lt; ActiveSupport::TestCase
14 19
15 should 'logged user not return comment if user has no permission to view the source article' do 20 should 'logged user not return comment if user has no permission to view the source article' do
16 login_api 21 login_api
17 - person = fast_create(Person)  
18 - article = fast_create(Article, :profile_id => person.id, :name => "Some thing", :published => false)  
19 - comment = article.comments.create!(:body => "another comment", :author => user.person) 22 + article = fast_create(Article, :profile_id => local_person.id, :name => "Some thing", :published => false)
  23 + comment = article.comments.create!(:body => "another comment", :author => local_person)
20 assert !article.published? 24 assert !article.published?
21 25
22 get "/api/v1/articles/#{article.id}/comments/#{comment.id}?#{params.to_query}" 26 get "/api/v1/articles/#{article.id}/comments/#{comment.id}?#{params.to_query}"
@@ -25,8 +29,7 @@ class CommentsTest &lt; ActiveSupport::TestCase @@ -25,8 +29,7 @@ class CommentsTest &lt; ActiveSupport::TestCase
25 29
26 should 'logged user not comment an article if user has no permission to view it' do 30 should 'logged user not comment an article if user has no permission to view it' do
27 login_api 31 login_api
28 - person = fast_create(Person)  
29 - article = fast_create(Article, :profile_id => person.id, :name => "Some thing", :published => false) 32 + article = fast_create(Article, :profile_id => local_person.id, :name => "Some thing", :published => false)
30 assert !article.published? 33 assert !article.published?
31 34
32 post "/api/v1/articles/#{article.id}/comments?#{params.to_query}" 35 post "/api/v1/articles/#{article.id}/comments?#{params.to_query}"
@@ -35,9 +38,9 @@ class CommentsTest &lt; ActiveSupport::TestCase @@ -35,9 +38,9 @@ class CommentsTest &lt; ActiveSupport::TestCase
35 38
36 should 'logged user return comments of an article' do 39 should 'logged user return comments of an article' do
37 login_api 40 login_api
38 - article = fast_create(Article, :profile_id => user.person.id, :name => "Some thing")  
39 - article.comments.create!(:body => "some comment", :author => user.person)  
40 - article.comments.create!(:body => "another comment", :author => user.person) 41 + article = fast_create(Article, :profile_id => local_person.id, :name => "Some thing")
  42 + article.comments.create!(:body => "some comment", :author => local_person)
  43 + article.comments.create!(:body => "another comment", :author => local_person)
41 44
42 get "/api/v1/articles/#{article.id}/comments?#{params.to_query}" 45 get "/api/v1/articles/#{article.id}/comments?#{params.to_query}"
43 json = JSON.parse(last_response.body) 46 json = JSON.parse(last_response.body)
@@ -47,8 +50,8 @@ class CommentsTest &lt; ActiveSupport::TestCase @@ -47,8 +50,8 @@ class CommentsTest &lt; ActiveSupport::TestCase
47 50
48 should 'logged user return comment of an article' do 51 should 'logged user return comment of an article' do
49 login_api 52 login_api
50 - article = fast_create(Article, :profile_id => user.person.id, :name => "Some thing")  
51 - comment = article.comments.create!(:body => "another comment", :author => user.person) 53 + article = fast_create(Article, :profile_id => local_person.id, :name => "Some thing")
  54 + comment = article.comments.create!(:body => "another comment", :author => local_person)
52 55
53 get "/api/v1/articles/#{article.id}/comments/#{comment.id}?#{params.to_query}" 56 get "/api/v1/articles/#{article.id}/comments/#{comment.id}?#{params.to_query}"
54 json = JSON.parse(last_response.body) 57 json = JSON.parse(last_response.body)
@@ -58,7 +61,7 @@ class CommentsTest &lt; ActiveSupport::TestCase @@ -58,7 +61,7 @@ class CommentsTest &lt; ActiveSupport::TestCase
58 61
59 should 'logged user comment an article' do 62 should 'logged user comment an article' do
60 login_api 63 login_api
61 - article = fast_create(Article, :profile_id => user.person.id, :name => "Some thing") 64 + article = fast_create(Article, :profile_id => local_person.id, :name => "Some thing")
62 body = 'My comment' 65 body = 'My comment'
63 params.merge!({:body => body}) 66 params.merge!({:body => body})
64 67
@@ -77,46 +80,10 @@ class CommentsTest &lt; ActiveSupport::TestCase @@ -77,46 +80,10 @@ class CommentsTest &lt; ActiveSupport::TestCase
77 assert_equal 400, last_response.status 80 assert_equal 400, last_response.status
78 end 81 end
79 82
80 - should 'comment creation define the source' do  
81 - login_api  
82 - amount = Comment.count  
83 - article = fast_create(Article, :profile_id => user.person.id, :name => "Some thing")  
84 - body = 'My comment'  
85 - params.merge!({:body => body})  
86 -  
87 - post "/api/v1/articles/#{article.id}/comments?#{params.to_query}"  
88 - assert_equal amount + 1, Comment.count  
89 - comment = Comment.last  
90 - assert_not_nil comment.source  
91 - end  
92 -  
93 - should 'paginate comments' do  
94 - article = fast_create(Article, :profile_id => user.person.id, :name => "Some thing")  
95 - 5.times { article.comments.create!(:body => "some comment", :author => user.person) }  
96 - params[:per_page] = 3  
97 -  
98 - get "/api/v1/articles/#{article.id}/comments?#{params.to_query}"  
99 - json = JSON.parse(last_response.body)  
100 - assert_equal 200, last_response.status  
101 - assert_equal 3, json["comments"].length  
102 - end  
103 -  
104 - should 'return only root comments' do  
105 - article = fast_create(Article, :profile_id => user.person.id, :name => "Some thing")  
106 - comment1 = article.comments.create!(:body => "some comment", :author => user.person)  
107 - comment2 = article.comments.create!(:body => "another comment", :author => user.person, :reply_of_id => comment1.id)  
108 - params[:without_reply] = true  
109 -  
110 - get "/api/v1/articles/#{article.id}/comments?#{params.to_query}"  
111 - json = JSON.parse(last_response.body)  
112 - assert_equal 200, last_response.status  
113 - assert_equal [comment1.id], json["comments"].map { |c| c['id'] }  
114 - end  
115 -  
116 should 'logged user comment creation define the source' do 83 should 'logged user comment creation define the source' do
117 login_api 84 login_api
118 amount = Comment.count 85 amount = Comment.count
119 - article = fast_create(Article, :profile_id => user.person.id, :name => "Some thing") 86 + article = fast_create(Article, :profile_id => local_person.id, :name => "Some thing")
120 body = 'My comment' 87 body = 'My comment'
121 params.merge!({:body => body}) 88 params.merge!({:body => body})
122 89
@@ -135,7 +102,7 @@ class CommentsTest &lt; ActiveSupport::TestCase @@ -135,7 +102,7 @@ class CommentsTest &lt; ActiveSupport::TestCase
135 Noosfero::Plugin.stubs(:all).returns([Plugin1.name]) 102 Noosfero::Plugin.stubs(:all).returns([Plugin1.name])
136 Environment.default.enable_plugin(Plugin1) 103 Environment.default.enable_plugin(Plugin1)
137 104
138 - article = fast_create(Article, :profile_id => user.person.id, :name => "Some thing") 105 + article = fast_create(Article, :profile_id => local_person.id, :name => "Some thing")
139 c1 = fast_create(Comment, source_id: article.id, body: "comment 1") 106 c1 = fast_create(Comment, source_id: article.id, body: "comment 1")
140 c2 = fast_create(Comment, source_id: article.id, body: "comment 2", :user_agent => 'Jack') 107 c2 = fast_create(Comment, source_id: article.id, body: "comment 2", :user_agent => 'Jack')
141 108
@@ -144,32 +111,27 @@ class CommentsTest &lt; ActiveSupport::TestCase @@ -144,32 +111,27 @@ class CommentsTest &lt; ActiveSupport::TestCase
144 assert_equal ["comment 2"], json["comments"].map {|c| c["body"]} 111 assert_equal ["comment 2"], json["comments"].map {|c| c["body"]}
145 end 112 end
146 113
147 - should 'do not return comments marked as spam' do  
148 - article = fast_create(Article, :profile_id => user.person.id, :name => "Some thing") 114 + should 'anonymous do not return comments marked as spam' do
  115 + article = fast_create(Article, :profile_id => local_person.id, :name => "Some thing")
149 c1 = fast_create(Comment, source_id: article.id, body: "comment 1", spam: true) 116 c1 = fast_create(Comment, source_id: article.id, body: "comment 1", spam: true)
150 c2 = fast_create(Comment, source_id: article.id, body: "comment 2") 117 c2 = fast_create(Comment, source_id: article.id, body: "comment 2")
151 -  
152 get "/api/v1/articles/#{article.id}/comments?#{params.to_query}" 118 get "/api/v1/articles/#{article.id}/comments?#{params.to_query}"
153 json = JSON.parse(last_response.body) 119 json = JSON.parse(last_response.body)
154 assert_equal ["comment 2"], json["comments"].map {|c| c["body"]} 120 assert_equal ["comment 2"], json["comments"].map {|c| c["body"]}
155 end 121 end
156 122
157 should 'not, anonymous list comments if has no permission to view the source article' do 123 should 'not, anonymous list comments if has no permission to view the source article' do
158 - anonymous_setup  
159 - person = fast_create(Person)  
160 - article = fast_create(Article, :profile_id => person.id, :name => "Some thing", :published => false) 124 + article = fast_create(Article, :profile_id => local_person.id, :name => "Some thing", :published => false)
161 assert !article.published? 125 assert !article.published?
162 126
163 get "/api/v1/articles/#{article.id}/comments?#{params.to_query}" 127 get "/api/v1/articles/#{article.id}/comments?#{params.to_query}"
164 assert_equal 403, last_response.status 128 assert_equal 403, last_response.status
165 end 129 end
166 130
167 - should 'visitor return comments of an article' do  
168 - visitor_setup  
169 - person = fast_create(Person)  
170 - article = fast_create(Article, :profile_id => person.id, :name => "Some thing")  
171 - article.comments.create!(:body => "some comment", :author => person)  
172 - article.comments.create!(:body => "another comment", :author => person) 131 + should 'anonymous return comments of an article' do
  132 + article = fast_create(Article, :profile_id => local_person.id, :name => "Some thing")
  133 + article.comments.create!(:body => "some comment", :author => local_person)
  134 + article.comments.create!(:body => "another comment", :author => local_person)
173 135
174 get "/api/v1/articles/#{article.id}/comments?#{params.to_query}" 136 get "/api/v1/articles/#{article.id}/comments?#{params.to_query}"
175 json = JSON.parse(last_response.body) 137 json = JSON.parse(last_response.body)
@@ -177,11 +139,9 @@ class CommentsTest &lt; ActiveSupport::TestCase @@ -177,11 +139,9 @@ class CommentsTest &lt; ActiveSupport::TestCase
177 assert_equal 2, json["comments"].length 139 assert_equal 2, json["comments"].length
178 end 140 end
179 141
180 - should 'visitor return comment of an article' do  
181 - visitor_setup  
182 - person = fast_create(Person)  
183 - article = fast_create(Article, :profile_id => person.id, :name => "Some thing")  
184 - comment = article.comments.create!(:body => "another comment", :author => person) 142 + should 'anonymous return comment of an article' do
  143 + article = fast_create(Article, :profile_id => local_person.id, :name => "Some thing")
  144 + comment = article.comments.create!(:body => "another comment", :author => local_person)
185 145
186 get "/api/v1/articles/#{article.id}/comments/#{comment.id}?#{params.to_query}" 146 get "/api/v1/articles/#{article.id}/comments/#{comment.id}?#{params.to_query}"
187 json = JSON.parse(last_response.body) 147 json = JSON.parse(last_response.body)
@@ -190,9 +150,7 @@ class CommentsTest &lt; ActiveSupport::TestCase @@ -190,9 +150,7 @@ class CommentsTest &lt; ActiveSupport::TestCase
190 end 150 end
191 151
192 should 'not, anonymous comment an article (at least so far...)' do 152 should 'not, anonymous comment an article (at least so far...)' do
193 - anonymous_setup  
194 - person = fast_create(Person)  
195 - article = fast_create(Article, :profile_id => person.id, :name => "Some thing") 153 + article = fast_create(Article, :profile_id => local_person.id, :name => "Some thing")
196 body = 'My comment' 154 body = 'My comment'
197 name = "John Doe" 155 name = "John Doe"
198 email = "JohnDoe@gmail.com" 156 email = "JohnDoe@gmail.com"
@@ -202,10 +160,10 @@ class CommentsTest &lt; ActiveSupport::TestCase @@ -202,10 +160,10 @@ class CommentsTest &lt; ActiveSupport::TestCase
202 assert_equal 401, last_response.status 160 assert_equal 401, last_response.status
203 end 161 end
204 162
205 - should 'paginate comments' do 163 + should 'logged user paginate comments' do
206 login_api 164 login_api
207 - article = fast_create(Article, :profile_id => user.person.id, :name => "Some thing")  
208 - 5.times { article.comments.create!(:body => "some comment", :author => user.person) } 165 + article = fast_create(Article, :profile_id => local_person.id, :name => "Some thing")
  166 + 5.times { article.comments.create!(:body => "some comment", :author => local_person) }
209 params[:per_page] = 3 167 params[:per_page] = 3
210 168
211 get "/api/v1/articles/#{article.id}/comments?#{params.to_query}" 169 get "/api/v1/articles/#{article.id}/comments?#{params.to_query}"
@@ -214,11 +172,11 @@ class CommentsTest &lt; ActiveSupport::TestCase @@ -214,11 +172,11 @@ class CommentsTest &lt; ActiveSupport::TestCase
214 assert_equal 3, json["comments"].length 172 assert_equal 3, json["comments"].length
215 end 173 end
216 174
217 - should 'return only root comments' do 175 + should 'logged user return only root comments' do
218 login_api 176 login_api
219 - article = fast_create(Article, :profile_id => user.person.id, :name => "Some thing")  
220 - comment1 = article.comments.create!(:body => "some comment", :author => user.person)  
221 - comment2 = article.comments.create!(:body => "another comment", :author => user.person, :reply_of_id => comment1.id) 177 + article = fast_create(Article, :profile_id => local_person.id, :name => "Some thing")
  178 + comment1 = article.comments.create!(:body => "some comment", :author => local_person)
  179 + comment2 = article.comments.create!(:body => "another comment", :author => local_person, :reply_of_id => comment1.id)
222 params[:without_reply] = true 180 params[:without_reply] = true
223 181
224 get "/api/v1/articles/#{article.id}/comments?#{params.to_query}" 182 get "/api/v1/articles/#{article.id}/comments?#{params.to_query}"