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
36 def test_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 = basic_authorization_header user.email, "test"
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(:id => 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(:id => 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 = basic_authorization_header user2.email, "test"
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(:id => 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
102 # create comment fail
103 def test_create_comment_fail
105 post changeset_comment_path(:id => create(:changeset, :closed), :text => "This is a comment")
106 assert_response :unauthorized
108 auth_header = basic_authorization_header create(:user).email, "test"
111 assert_no_difference "ChangesetComment.count" do
112 post changeset_comment_path(:id => 999111, :text => "This is a comment"), :headers => auth_header
114 assert_response :not_found
116 # not closed changeset
117 assert_no_difference "ChangesetComment.count" do
118 post changeset_comment_path(:id => create(:changeset), :text => "This is a comment"), :headers => auth_header
120 assert_response :conflict
123 assert_no_difference "ChangesetComment.count" do
124 post changeset_comment_path(:id => create(:changeset, :closed)), :headers => auth_header
126 assert_response :bad_request
129 assert_no_difference "ChangesetComment.count" do
130 post changeset_comment_path(:id => create(:changeset, :closed), :text => ""), :headers => auth_header
132 assert_response :bad_request
136 # test hide comment fail
137 def test_destroy_comment_fail
139 comment = create(:changeset_comment)
140 assert comment.visible
142 post changeset_comment_hide_path(:id => comment)
143 assert_response :unauthorized
144 assert comment.reload.visible
146 auth_header = basic_authorization_header create(:user).email, "test"
149 post changeset_comment_hide_path(:id => comment), :headers => auth_header
150 assert_response :forbidden
151 assert comment.reload.visible
153 auth_header = basic_authorization_header create(:moderator_user).email, "test"
156 post changeset_comment_hide_path(:id => 999111), :headers => auth_header
157 assert_response :not_found
158 assert comment.reload.visible
162 # test hide comment succes
163 def test_hide_comment_success
164 comment = create(:changeset_comment)
165 assert comment.visible
167 auth_header = basic_authorization_header create(:moderator_user).email, "test"
169 post changeset_comment_hide_path(:id => comment), :headers => auth_header
170 assert_response :success
171 assert_not comment.reload.visible
175 # test unhide comment fail
176 def test_restore_comment_fail
178 comment = create(:changeset_comment, :visible => false)
179 assert_not comment.visible
181 post changeset_comment_unhide_path(:id => comment)
182 assert_response :unauthorized
183 assert_not comment.reload.visible
185 auth_header = basic_authorization_header create(:user).email, "test"
188 post changeset_comment_unhide_path(:id => comment), :headers => auth_header
189 assert_response :forbidden
190 assert_not comment.reload.visible
192 auth_header = basic_authorization_header create(:moderator_user).email, "test"
195 post changeset_comment_unhide_path(:id => 999111), :headers => auth_header
196 assert_response :not_found
197 assert_not comment.reload.visible
201 # test unhide comment succes
202 def test_unhide_comment_success
203 comment = create(:changeset_comment, :visible => false)
204 assert_not comment.visible
206 auth_header = basic_authorization_header create(:moderator_user).email, "test"
208 post changeset_comment_unhide_path(:id => comment), :headers => auth_header
209 assert_response :success
210 assert comment.reload.visible
213 # This test ensures that token capabilities behave correctly for a method that
214 # requires the terms to have been agreed.
215 # (This would be better as an integration or system testcase, since the changeset_comment
216 # create method is simply a stand-in for any method that requires terms agreement.
217 # But writing oauth tests is hard, and so it's easier to put in a controller test.)
218 def test_api_write_and_terms_agreed_via_token
219 user = create(:user, :terms_agreed => nil)
220 token = create(:access_token, :user => user, :allow_write_api => true)
221 changeset = create(:changeset, :closed)
223 assert_difference "ChangesetComment.count", 0 do
224 signed_post changeset_comment_path(:id => changeset), :params => { :text => "This is a comment" }, :oauth => { :token => token }
226 assert_response :forbidden
228 # Try again, after agreement with the terms
229 user.terms_agreed = Time.now.utc
232 assert_difference "ChangesetComment.count", 1 do
233 signed_post changeset_comment_path(:id => changeset), :params => { :text => "This is a comment" }, :oauth => { :token => token }
235 assert_response :success
238 # This test does the same as above, but with basic auth, to similarly test that the
239 # abilities take into account terms agreement too.
240 def test_api_write_and_terms_agreed_via_basic_auth
241 user = create(:user, :terms_agreed => nil)
242 changeset = create(:changeset, :closed)
244 auth_header = basic_authorization_header user.email, "test"
246 assert_difference "ChangesetComment.count", 0 do
247 post changeset_comment_path(:id => changeset, :text => "This is a comment"), :headers => auth_header
249 assert_response :forbidden
251 # Try again, after agreement with the terms
252 user.terms_agreed = Time.now.utc
255 assert_difference "ChangesetComment.count", 1 do
256 post changeset_comment_path(:id => changeset, :text => "This is a comment"), :headers => auth_header
258 assert_response :success