4 class ChangesetCommentsControllerTest < ActionDispatch::IntegrationTest
6 # test all routes which lead to this controller
9 { :path => "/api/0.6/changeset/1/comment", :method => :post },
10 { :controller => "api/changeset_comments", :action => "create", :id => "1" }
13 { :path => "/api/0.6/changeset/1/comment.json", :method => :post },
14 { :controller => "api/changeset_comments", :action => "create", :id => "1", :format => "json" }
17 { :path => "/api/0.6/changeset/comment/1/hide", :method => :post },
18 { :controller => "api/changeset_comments", :action => "destroy", :id => "1" }
21 { :path => "/api/0.6/changeset/comment/1/hide.json", :method => :post },
22 { :controller => "api/changeset_comments", :action => "destroy", :id => "1", :format => "json" }
25 { :path => "/api/0.6/changeset/comment/1/unhide", :method => :post },
26 { :controller => "api/changeset_comments", :action => "restore", :id => "1" }
29 { :path => "/api/0.6/changeset/comment/1/unhide.json", :method => :post },
30 { :controller => "api/changeset_comments", :action => "restore", :id => "1", :format => "json" }
35 # create comment success
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)
44 auth_header = bearer_authorization_header user
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
53 assert_response :success
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)
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
68 assert_response :success
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
75 ActionMailer::Base.deliveries.clear
77 auth_header = bearer_authorization_header user2
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
86 assert_response :success
88 email = ActionMailer::Base.deliveries.find { |e| e.to.first == private_user.email }
90 assert_equal 1, email.to.length
91 assert_equal "[OpenStreetMap] #{user2.display_name} has commented on one of your changesets", email.subject
93 email = ActionMailer::Base.deliveries.find { |e| e.to.first == user.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
98 ActionMailer::Base.deliveries.clear
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
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
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
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
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
136 def test_create_when_not_agreed_to_terms
137 user = create(:user, :terms_agreed => nil)
138 auth_header = bearer_authorization_header user
139 changeset = create(:changeset, :closed)
141 assert_difference "ChangesetComment.count", 0 do
142 post changeset_comment_path(changeset), :params => { :text => "This is a comment" }, :headers => auth_header
143 assert_response :forbidden
147 def test_create_with_write_api_scope
149 auth_header = bearer_authorization_header user, :scopes => %w[write_api]
150 changeset = create(:changeset, :closed)
152 assert_difference "ChangesetComment.count", 1 do
153 post changeset_comment_path(changeset), :params => { :text => "This is a comment" }, :headers => auth_header
154 assert_response :success
157 comment = ChangesetComment.last
158 assert_equal changeset.id, comment.changeset_id
159 assert_equal user.id, comment.author_id
160 assert_equal "This is a comment", comment.body
161 assert comment.visible
165 # create comment rate limit for new users
166 def test_create_by_new_user_with_rate_limit
167 changeset = create(:changeset, :closed)
170 auth_header = bearer_authorization_header user
172 assert_difference "ChangesetComment.count", Settings.initial_changeset_comments_per_hour do
173 1.upto(Settings.initial_changeset_comments_per_hour) do |count|
174 post changeset_comment_path(changeset, :text => "Comment #{count}"), :headers => auth_header
175 assert_response :success
179 assert_no_difference "ChangesetComment.count" do
180 post changeset_comment_path(changeset, :text => "One comment too many"), :headers => auth_header
181 assert_response :too_many_requests
186 # create comment rate limit for experienced users
187 def test_create_by_experienced_user_with_rate_limit
188 changeset = create(:changeset, :closed)
190 create_list(:changeset_comment, Settings.comments_to_max_changeset_comments, :author_id => user.id, :created_at => Time.now.utc - 1.day)
192 auth_header = bearer_authorization_header user
194 assert_difference "ChangesetComment.count", Settings.max_changeset_comments_per_hour do
195 1.upto(Settings.max_changeset_comments_per_hour) do |count|
196 post changeset_comment_path(changeset, :text => "Comment #{count}"), :headers => auth_header
197 assert_response :success
201 assert_no_difference "ChangesetComment.count" do
202 post changeset_comment_path(changeset, :text => "One comment too many"), :headers => auth_header
203 assert_response :too_many_requests
208 # create comment rate limit for reported users
209 def test_create_by_reported_user_with_rate_limit
210 changeset = create(:changeset, :closed)
212 create(:issue_with_reports, :reportable => user, :reported_user => user)
214 auth_header = bearer_authorization_header user
216 assert_difference "ChangesetComment.count", Settings.initial_changeset_comments_per_hour / 2 do
217 1.upto(Settings.initial_changeset_comments_per_hour / 2) do |count|
218 post changeset_comment_path(changeset, :text => "Comment #{count}"), :headers => auth_header
219 assert_response :success
223 assert_no_difference "ChangesetComment.count" do
224 post changeset_comment_path(changeset, :text => "One comment too many"), :headers => auth_header
225 assert_response :too_many_requests
230 # create comment rate limit for moderator users
231 def test_create_by_moderator_user_with_rate_limit
232 changeset = create(:changeset, :closed)
233 user = create(:moderator_user)
235 auth_header = bearer_authorization_header user
237 assert_difference "ChangesetComment.count", Settings.moderator_changeset_comments_per_hour do
238 1.upto(Settings.moderator_changeset_comments_per_hour) do |count|
239 post changeset_comment_path(changeset, :text => "Comment #{count}"), :headers => auth_header
240 assert_response :success
244 assert_no_difference "ChangesetComment.count" do
245 post changeset_comment_path(changeset, :text => "One comment too many"), :headers => auth_header
246 assert_response :too_many_requests
251 # test hide comment fail
254 comment = create(:changeset_comment)
255 assert comment.visible
257 post changeset_comment_hide_path(comment)
258 assert_response :unauthorized
259 assert comment.reload.visible
261 auth_header = bearer_authorization_header
264 post changeset_comment_hide_path(comment), :headers => auth_header
265 assert_response :forbidden
266 assert comment.reload.visible
268 auth_header = bearer_authorization_header create(:moderator_user)
271 post changeset_comment_hide_path(999111), :headers => auth_header
272 assert_response :not_found
273 assert comment.reload.visible
277 # test hide comment succes
279 comment = create(:changeset_comment)
280 assert comment.visible
282 auth_header = bearer_authorization_header create(:moderator_user)
284 post changeset_comment_hide_path(comment), :headers => auth_header
285 assert_response :success
286 assert_not comment.reload.visible
290 # test unhide comment fail
293 comment = create(:changeset_comment, :visible => false)
294 assert_not comment.visible
296 post changeset_comment_unhide_path(comment)
297 assert_response :unauthorized
298 assert_not comment.reload.visible
300 auth_header = bearer_authorization_header
303 post changeset_comment_unhide_path(comment), :headers => auth_header
304 assert_response :forbidden
305 assert_not comment.reload.visible
307 auth_header = bearer_authorization_header create(:moderator_user)
310 post changeset_comment_unhide_path(999111), :headers => auth_header
311 assert_response :not_found
312 assert_not comment.reload.visible
316 # test unhide comment succes
318 comment = create(:changeset_comment, :visible => false)
319 assert_not comment.visible
321 auth_header = bearer_authorization_header create(:moderator_user)
323 post changeset_comment_unhide_path(comment), :headers => auth_header
324 assert_response :success
325 assert comment.reload.visible