]> git.openstreetmap.org Git - rails.git/blob - test/controllers/api/changeset_comments_controller_test.rb
Merge remote-tracking branch 'upstream/pull/3523'
[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_comment_success
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 = basic_authorization_header user.email, "test"
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(:id => 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(:id => 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 = basic_authorization_header user2.email, "test"
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(:id => 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     ##
102     # create comment fail
103     def test_create_comment_fail
104       # unauthorized
105       post changeset_comment_path(:id => create(:changeset, :closed), :text => "This is a comment")
106       assert_response :unauthorized
107
108       auth_header = basic_authorization_header create(:user).email, "test"
109
110       # bad changeset id
111       assert_no_difference "ChangesetComment.count" do
112         post changeset_comment_path(:id => 999111, :text => "This is a comment"), :headers => auth_header
113       end
114       assert_response :not_found
115
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
119       end
120       assert_response :conflict
121
122       # no text
123       assert_no_difference "ChangesetComment.count" do
124         post changeset_comment_path(:id => create(:changeset, :closed)), :headers => auth_header
125       end
126       assert_response :bad_request
127
128       # empty text
129       assert_no_difference "ChangesetComment.count" do
130         post changeset_comment_path(:id => create(:changeset, :closed), :text => ""), :headers => auth_header
131       end
132       assert_response :bad_request
133     end
134
135     ##
136     # test hide comment fail
137     def test_destroy_comment_fail
138       # unauthorized
139       comment = create(:changeset_comment)
140       assert comment.visible
141
142       post changeset_comment_hide_path(:id => comment)
143       assert_response :unauthorized
144       assert comment.reload.visible
145
146       auth_header = basic_authorization_header create(:user).email, "test"
147
148       # not a moderator
149       post changeset_comment_hide_path(:id => comment), :headers => auth_header
150       assert_response :forbidden
151       assert comment.reload.visible
152
153       auth_header = basic_authorization_header create(:moderator_user).email, "test"
154
155       # bad comment id
156       post changeset_comment_hide_path(:id => 999111), :headers => auth_header
157       assert_response :not_found
158       assert comment.reload.visible
159     end
160
161     ##
162     # test hide comment succes
163     def test_hide_comment_success
164       comment = create(:changeset_comment)
165       assert comment.visible
166
167       auth_header = basic_authorization_header create(:moderator_user).email, "test"
168
169       post changeset_comment_hide_path(:id => comment), :headers => auth_header
170       assert_response :success
171       assert_not comment.reload.visible
172     end
173
174     ##
175     # test unhide comment fail
176     def test_restore_comment_fail
177       # unauthorized
178       comment = create(:changeset_comment, :visible => false)
179       assert_not comment.visible
180
181       post changeset_comment_unhide_path(:id => comment)
182       assert_response :unauthorized
183       assert_not comment.reload.visible
184
185       auth_header = basic_authorization_header create(:user).email, "test"
186
187       # not a moderator
188       post changeset_comment_unhide_path(:id => comment), :headers => auth_header
189       assert_response :forbidden
190       assert_not comment.reload.visible
191
192       auth_header = basic_authorization_header create(:moderator_user).email, "test"
193
194       # bad comment id
195       post changeset_comment_unhide_path(:id => 999111), :headers => auth_header
196       assert_response :not_found
197       assert_not comment.reload.visible
198     end
199
200     ##
201     # test unhide comment succes
202     def test_unhide_comment_success
203       comment = create(:changeset_comment, :visible => false)
204       assert_not comment.visible
205
206       auth_header = basic_authorization_header create(:moderator_user).email, "test"
207
208       post changeset_comment_unhide_path(:id => comment), :headers => auth_header
209       assert_response :success
210       assert comment.reload.visible
211     end
212
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)
222
223       assert_difference "ChangesetComment.count", 0 do
224         signed_post changeset_comment_path(:id => changeset), :params => { :text => "This is a comment" }, :oauth => { :token => token }
225       end
226       assert_response :forbidden
227
228       # Try again, after agreement with the terms
229       user.terms_agreed = Time.now.utc
230       user.save!
231
232       assert_difference "ChangesetComment.count", 1 do
233         signed_post changeset_comment_path(:id => changeset), :params => { :text => "This is a comment" }, :oauth => { :token => token }
234       end
235       assert_response :success
236     end
237
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)
243
244       auth_header = basic_authorization_header user.email, "test"
245
246       assert_difference "ChangesetComment.count", 0 do
247         post changeset_comment_path(:id => changeset, :text => "This is a comment"), :headers => auth_header
248       end
249       assert_response :forbidden
250
251       # Try again, after agreement with the terms
252       user.terms_agreed = Time.now.utc
253       user.save!
254
255       assert_difference "ChangesetComment.count", 1 do
256         post changeset_comment_path(:id => changeset, :text => "This is a comment"), :headers => auth_header
257       end
258       assert_response :success
259     end
260   end
261 end