]> git.openstreetmap.org Git - rails.git/blob - test/controllers/api/changeset_comments_controller_test.rb
Split api changeset comment create fail tests
[rails.git] / test / controllers / api / changeset_comments_controller_test.rb
1 require "test_helper"
2
3 module Api
4   class ChangesetCommentsControllerTest < ActionDispatch::IntegrationTest
5     ##
6     # test all routes which lead to this controller
7     def test_routes
8       assert_routing(
9         { :path => "/api/0.6/changeset/1/comment", :method => :post },
10         { :controller => "api/changeset_comments", :action => "create", :id => "1" }
11       )
12       assert_routing(
13         { :path => "/api/0.6/changeset/1/comment.json", :method => :post },
14         { :controller => "api/changeset_comments", :action => "create", :id => "1", :format => "json" }
15       )
16       assert_routing(
17         { :path => "/api/0.6/changeset/comment/1/hide", :method => :post },
18         { :controller => "api/changeset_comments", :action => "destroy", :id => "1" }
19       )
20       assert_routing(
21         { :path => "/api/0.6/changeset/comment/1/hide.json", :method => :post },
22         { :controller => "api/changeset_comments", :action => "destroy", :id => "1", :format => "json" }
23       )
24       assert_routing(
25         { :path => "/api/0.6/changeset/comment/1/unhide", :method => :post },
26         { :controller => "api/changeset_comments", :action => "restore", :id => "1" }
27       )
28       assert_routing(
29         { :path => "/api/0.6/changeset/comment/1/unhide.json", :method => :post },
30         { :controller => "api/changeset_comments", :action => "restore", :id => "1", :format => "json" }
31       )
32     end
33
34     ##
35     # create comment success
36     def test_create
37       user = create(:user)
38       user2 = create(:user)
39       private_user = create(:user, :data_public => false)
40       suspended_user = create(:user, :suspended)
41       deleted_user = create(:user, :deleted)
42       private_user_closed_changeset = create(:changeset, :closed, :user => private_user)
43
44       auth_header = bearer_authorization_header user
45
46       assert_difference "ChangesetComment.count", 1 do
47         assert_no_difference "ActionMailer::Base.deliveries.size" do
48           perform_enqueued_jobs do
49             post changeset_comment_path(private_user_closed_changeset, :text => "This is a comment"), :headers => auth_header
50           end
51         end
52       end
53       assert_response :success
54
55       changeset = create(:changeset, :closed, :user => private_user)
56       changeset.subscribers.push(private_user)
57       changeset.subscribers.push(user)
58       changeset.subscribers.push(suspended_user)
59       changeset.subscribers.push(deleted_user)
60
61       assert_difference "ChangesetComment.count", 1 do
62         assert_difference "ActionMailer::Base.deliveries.size", 1 do
63           perform_enqueued_jobs do
64             post changeset_comment_path(changeset, :text => "This is a comment"), :headers => auth_header
65           end
66         end
67       end
68       assert_response :success
69
70       email = ActionMailer::Base.deliveries.first
71       assert_equal 1, email.to.length
72       assert_equal "[OpenStreetMap] #{user.display_name} has commented on one of your changesets", email.subject
73       assert_equal private_user.email, email.to.first
74
75       ActionMailer::Base.deliveries.clear
76
77       auth_header = bearer_authorization_header user2
78
79       assert_difference "ChangesetComment.count", 1 do
80         assert_difference "ActionMailer::Base.deliveries.size", 2 do
81           perform_enqueued_jobs do
82             post changeset_comment_path(changeset, :text => "This is a comment"), :headers => auth_header
83           end
84         end
85       end
86       assert_response :success
87
88       email = ActionMailer::Base.deliveries.find { |e| e.to.first == private_user.email }
89       assert_not_nil email
90       assert_equal 1, email.to.length
91       assert_equal "[OpenStreetMap] #{user2.display_name} has commented on one of your changesets", email.subject
92
93       email = ActionMailer::Base.deliveries.find { |e| e.to.first == user.email }
94       assert_not_nil email
95       assert_equal 1, email.to.length
96       assert_equal "[OpenStreetMap] #{user2.display_name} has commented on a changeset you are interested in", email.subject
97
98       ActionMailer::Base.deliveries.clear
99     end
100
101     def test_create_by_unauthorized
102       assert_no_difference "ChangesetComment.count" do
103         post changeset_comment_path(create(:changeset, :closed), :text => "This is a comment")
104         assert_response :unauthorized
105       end
106     end
107
108     def test_create_on_missing_changeset
109       assert_no_difference "ChangesetComment.count" do
110         post changeset_comment_path(999111, :text => "This is a comment"), :headers => bearer_authorization_header
111         assert_response :not_found
112       end
113     end
114
115     def test_create_on_open_changeset
116       assert_no_difference "ChangesetComment.count" do
117         post changeset_comment_path(create(:changeset), :text => "This is a comment"), :headers => bearer_authorization_header
118         assert_response :conflict
119       end
120     end
121
122     def test_create_without_text
123       assert_no_difference "ChangesetComment.count" do
124         post changeset_comment_path(create(:changeset, :closed)), :headers => bearer_authorization_header
125         assert_response :bad_request
126       end
127     end
128
129     def test_create_with_empty_text
130       assert_no_difference "ChangesetComment.count" do
131         post changeset_comment_path(create(:changeset, :closed), :text => ""), :headers => bearer_authorization_header
132         assert_response :bad_request
133       end
134     end
135
136     ##
137     # create comment rate limit for new users
138     def test_create_by_new_user_with_rate_limit
139       changeset = create(:changeset, :closed)
140       user = create(:user)
141
142       auth_header = bearer_authorization_header user
143
144       assert_difference "ChangesetComment.count", Settings.initial_changeset_comments_per_hour do
145         1.upto(Settings.initial_changeset_comments_per_hour) do |count|
146           post changeset_comment_path(changeset, :text => "Comment #{count}"), :headers => auth_header
147           assert_response :success
148         end
149       end
150
151       assert_no_difference "ChangesetComment.count" do
152         post changeset_comment_path(changeset, :text => "One comment too many"), :headers => auth_header
153         assert_response :too_many_requests
154       end
155     end
156
157     ##
158     # create comment rate limit for experienced users
159     def test_create_by_experienced_user_with_rate_limit
160       changeset = create(:changeset, :closed)
161       user = create(:user)
162       create_list(:changeset_comment, Settings.comments_to_max_changeset_comments, :author_id => user.id, :created_at => Time.now.utc - 1.day)
163
164       auth_header = bearer_authorization_header user
165
166       assert_difference "ChangesetComment.count", Settings.max_changeset_comments_per_hour do
167         1.upto(Settings.max_changeset_comments_per_hour) do |count|
168           post changeset_comment_path(changeset, :text => "Comment #{count}"), :headers => auth_header
169           assert_response :success
170         end
171       end
172
173       assert_no_difference "ChangesetComment.count" do
174         post changeset_comment_path(changeset, :text => "One comment too many"), :headers => auth_header
175         assert_response :too_many_requests
176       end
177     end
178
179     ##
180     # create comment rate limit for reported users
181     def test_create_by_reported_user_with_rate_limit
182       changeset = create(:changeset, :closed)
183       user = create(:user)
184       create(:issue_with_reports, :reportable => user, :reported_user => user)
185
186       auth_header = bearer_authorization_header user
187
188       assert_difference "ChangesetComment.count", Settings.initial_changeset_comments_per_hour / 2 do
189         1.upto(Settings.initial_changeset_comments_per_hour / 2) do |count|
190           post changeset_comment_path(changeset, :text => "Comment #{count}"), :headers => auth_header
191           assert_response :success
192         end
193       end
194
195       assert_no_difference "ChangesetComment.count" do
196         post changeset_comment_path(changeset, :text => "One comment too many"), :headers => auth_header
197         assert_response :too_many_requests
198       end
199     end
200
201     ##
202     # create comment rate limit for moderator users
203     def test_create_by_moderator_user_with_rate_limit
204       changeset = create(:changeset, :closed)
205       user = create(:moderator_user)
206
207       auth_header = bearer_authorization_header user
208
209       assert_difference "ChangesetComment.count", Settings.moderator_changeset_comments_per_hour do
210         1.upto(Settings.moderator_changeset_comments_per_hour) do |count|
211           post changeset_comment_path(changeset, :text => "Comment #{count}"), :headers => auth_header
212           assert_response :success
213         end
214       end
215
216       assert_no_difference "ChangesetComment.count" do
217         post changeset_comment_path(changeset, :text => "One comment too many"), :headers => auth_header
218         assert_response :too_many_requests
219       end
220     end
221
222     ##
223     # test hide comment fail
224     def test_hide_fail
225       # unauthorized
226       comment = create(:changeset_comment)
227       assert comment.visible
228
229       post changeset_comment_hide_path(comment)
230       assert_response :unauthorized
231       assert comment.reload.visible
232
233       auth_header = bearer_authorization_header
234
235       # not a moderator
236       post changeset_comment_hide_path(comment), :headers => auth_header
237       assert_response :forbidden
238       assert comment.reload.visible
239
240       auth_header = bearer_authorization_header create(:moderator_user)
241
242       # bad comment id
243       post changeset_comment_hide_path(999111), :headers => auth_header
244       assert_response :not_found
245       assert comment.reload.visible
246     end
247
248     ##
249     # test hide comment succes
250     def test_hide
251       comment = create(:changeset_comment)
252       assert comment.visible
253
254       auth_header = bearer_authorization_header create(:moderator_user)
255
256       post changeset_comment_hide_path(comment), :headers => auth_header
257       assert_response :success
258       assert_not comment.reload.visible
259     end
260
261     ##
262     # test unhide comment fail
263     def test_unhide_fail
264       # unauthorized
265       comment = create(:changeset_comment, :visible => false)
266       assert_not comment.visible
267
268       post changeset_comment_unhide_path(comment)
269       assert_response :unauthorized
270       assert_not comment.reload.visible
271
272       auth_header = bearer_authorization_header
273
274       # not a moderator
275       post changeset_comment_unhide_path(comment), :headers => auth_header
276       assert_response :forbidden
277       assert_not comment.reload.visible
278
279       auth_header = bearer_authorization_header create(:moderator_user)
280
281       # bad comment id
282       post changeset_comment_unhide_path(999111), :headers => auth_header
283       assert_response :not_found
284       assert_not comment.reload.visible
285     end
286
287     ##
288     # test unhide comment succes
289     def test_unhide
290       comment = create(:changeset_comment, :visible => false)
291       assert_not comment.visible
292
293       auth_header = bearer_authorization_header create(:moderator_user)
294
295       post changeset_comment_unhide_path(comment), :headers => auth_header
296       assert_response :success
297       assert comment.reload.visible
298     end
299
300     # This test ensures that token capabilities behave correctly for a method that
301     # requires the terms to have been agreed.
302     # (This would be better as an integration or system testcase, since the changeset_comment
303     # create method is simply a stand-in for any method that requires terms agreement.
304     # But writing oauth tests is hard, and so it's easier to put in a controller test.)
305     def test_api_write_and_terms_agreed_via_token
306       user = create(:user, :terms_agreed => nil)
307       auth_header = bearer_authorization_header(user, :scopes => %w[write_api])
308       changeset = create(:changeset, :closed)
309
310       assert_difference "ChangesetComment.count", 0 do
311         post changeset_comment_path(changeset), :params => { :text => "This is a comment" }, :headers => auth_header
312       end
313       assert_response :forbidden
314
315       # Try again, after agreement with the terms
316       user.terms_agreed = Time.now.utc
317       user.save!
318
319       assert_difference "ChangesetComment.count", 1 do
320         post changeset_comment_path(changeset), :params => { :text => "This is a comment" }, :headers => auth_header
321       end
322       assert_response :success
323     end
324   end
325 end