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 2  
3 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 11 should 'logged user not list comments if user has no permission to view the source article' do
6 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 14 assert !article.published?
10 15  
11 16 get "/api/v1/articles/#{article.id}/comments?#{params.to_query}"
... ... @@ -14,9 +19,8 @@ class CommentsTest &lt; ActiveSupport::TestCase
14 19  
15 20 should 'logged user not return comment if user has no permission to view the source article' do
16 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 24 assert !article.published?
21 25  
22 26 get "/api/v1/articles/#{article.id}/comments/#{comment.id}?#{params.to_query}"
... ... @@ -25,8 +29,7 @@ class CommentsTest &lt; ActiveSupport::TestCase
25 29  
26 30 should 'logged user not comment an article if user has no permission to view it' do
27 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 33 assert !article.published?
31 34  
32 35 post "/api/v1/articles/#{article.id}/comments?#{params.to_query}"
... ... @@ -35,9 +38,9 @@ class CommentsTest &lt; ActiveSupport::TestCase
35 38  
36 39 should 'logged user return comments of an article' do
37 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 45 get "/api/v1/articles/#{article.id}/comments?#{params.to_query}"
43 46 json = JSON.parse(last_response.body)
... ... @@ -47,8 +50,8 @@ class CommentsTest &lt; ActiveSupport::TestCase
47 50  
48 51 should 'logged user return comment of an article' do
49 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 56 get "/api/v1/articles/#{article.id}/comments/#{comment.id}?#{params.to_query}"
54 57 json = JSON.parse(last_response.body)
... ... @@ -58,7 +61,7 @@ class CommentsTest &lt; ActiveSupport::TestCase
58 61  
59 62 should 'logged user comment an article' do
60 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 65 body = 'My comment'
63 66 params.merge!({:body => body})
64 67  
... ... @@ -77,46 +80,10 @@ class CommentsTest &lt; ActiveSupport::TestCase
77 80 assert_equal 400, last_response.status
78 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 83 should 'logged user comment creation define the source' do
117 84 login_api
118 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 87 body = 'My comment'
121 88 params.merge!({:body => body})
122 89  
... ... @@ -135,7 +102,7 @@ class CommentsTest &lt; ActiveSupport::TestCase
135 102 Noosfero::Plugin.stubs(:all).returns([Plugin1.name])
136 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 106 c1 = fast_create(Comment, source_id: article.id, body: "comment 1")
140 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 111 assert_equal ["comment 2"], json["comments"].map {|c| c["body"]}
145 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 116 c1 = fast_create(Comment, source_id: article.id, body: "comment 1", spam: true)
150 117 c2 = fast_create(Comment, source_id: article.id, body: "comment 2")
151   -
152 118 get "/api/v1/articles/#{article.id}/comments?#{params.to_query}"
153 119 json = JSON.parse(last_response.body)
154 120 assert_equal ["comment 2"], json["comments"].map {|c| c["body"]}
155 121 end
156 122  
157 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 125 assert !article.published?
162 126  
163 127 get "/api/v1/articles/#{article.id}/comments?#{params.to_query}"
164 128 assert_equal 403, last_response.status
165 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 136 get "/api/v1/articles/#{article.id}/comments?#{params.to_query}"
175 137 json = JSON.parse(last_response.body)
... ... @@ -177,11 +139,9 @@ class CommentsTest &lt; ActiveSupport::TestCase
177 139 assert_equal 2, json["comments"].length
178 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 146 get "/api/v1/articles/#{article.id}/comments/#{comment.id}?#{params.to_query}"
187 147 json = JSON.parse(last_response.body)
... ... @@ -190,9 +150,7 @@ class CommentsTest &lt; ActiveSupport::TestCase
190 150 end
191 151  
192 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 154 body = 'My comment'
197 155 name = "John Doe"
198 156 email = "JohnDoe@gmail.com"
... ... @@ -202,10 +160,10 @@ class CommentsTest &lt; ActiveSupport::TestCase
202 160 assert_equal 401, last_response.status
203 161 end
204 162  
205   - should 'paginate comments' do
  163 + should 'logged user paginate comments' do
206 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 167 params[:per_page] = 3
210 168  
211 169 get "/api/v1/articles/#{article.id}/comments?#{params.to_query}"
... ... @@ -214,11 +172,11 @@ class CommentsTest &lt; ActiveSupport::TestCase
214 172 assert_equal 3, json["comments"].length
215 173 end
216 174  
217   - should 'return only root comments' do
  175 + should 'logged user return only root comments' do
218 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 180 params[:without_reply] = true
223 181  
224 182 get "/api/v1/articles/#{article.id}/comments?#{params.to_query}"
... ...