]> git.openstreetmap.org Git - rails.git/blob - test/controllers/api/changeset_comments_controller_test.rb
Fix rubocop Rails/TimeZone warnings
[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/comment/1/hide", :method => :post },
14         { :controller => "api/changeset_comments", :action => "destroy", :id => "1" }
15       )
16       assert_routing(
17         { :path => "/api/0.6/changeset/comment/1/unhide", :method => :post },
18         { :controller => "api/changeset_comments", :action => "restore", :id => "1" }
19       )
20     end
21
22     ##
23     # create comment success
24     def test_create_comment_success
25       user = create(:user)
26       user2 = create(:user)
27       private_user = create(:user, :data_public => false)
28       suspended_user = create(:user, :suspended)
29       deleted_user = create(:user, :deleted)
30       private_user_closed_changeset = create(:changeset, :closed, :user => private_user)
31
32       auth_header = basic_authorization_header user.email, "test"
33
34       assert_difference "ChangesetComment.count", 1 do
35         assert_no_difference "ActionMailer::Base.deliveries.size" do
36           perform_enqueued_jobs do
37             post changeset_comment_path(:id => private_user_closed_changeset, :text => "This is a comment"), :headers => auth_header
38           end
39         end
40       end
41       assert_response :success
42
43       changeset = create(:changeset, :closed, :user => private_user)
44       changeset.subscribers.push(private_user)
45       changeset.subscribers.push(user)
46       changeset.subscribers.push(suspended_user)
47       changeset.subscribers.push(deleted_user)
48
49       assert_difference "ChangesetComment.count", 1 do
50         assert_difference "ActionMailer::Base.deliveries.size", 1 do
51           perform_enqueued_jobs do
52             post changeset_comment_path(:id => changeset, :text => "This is a comment"), :headers => auth_header
53           end
54         end
55       end
56       assert_response :success
57
58       email = ActionMailer::Base.deliveries.first
59       assert_equal 1, email.to.length
60       assert_equal "[OpenStreetMap] #{user.display_name} has commented on one of your changesets", email.subject
61       assert_equal private_user.email, email.to.first
62
63       ActionMailer::Base.deliveries.clear
64
65       auth_header = basic_authorization_header user2.email, "test"
66
67       assert_difference "ChangesetComment.count", 1 do
68         assert_difference "ActionMailer::Base.deliveries.size", 2 do
69           perform_enqueued_jobs do
70             post changeset_comment_path(:id => changeset, :text => "This is a comment"), :headers => auth_header
71           end
72         end
73       end
74       assert_response :success
75
76       email = ActionMailer::Base.deliveries.find { |e| e.to.first == private_user.email }
77       assert_not_nil email
78       assert_equal 1, email.to.length
79       assert_equal "[OpenStreetMap] #{user2.display_name} has commented on one of your changesets", email.subject
80
81       email = ActionMailer::Base.deliveries.find { |e| e.to.first == user.email }
82       assert_not_nil email
83       assert_equal 1, email.to.length
84       assert_equal "[OpenStreetMap] #{user2.display_name} has commented on a changeset you are interested in", email.subject
85
86       ActionMailer::Base.deliveries.clear
87     end
88
89     ##
90     # create comment fail
91     def test_create_comment_fail
92       # unauthorized
93       post changeset_comment_path(:id => create(:changeset, :closed), :text => "This is a comment")
94       assert_response :unauthorized
95
96       auth_header = basic_authorization_header create(:user).email, "test"
97
98       # bad changeset id
99       assert_no_difference "ChangesetComment.count" do
100         post changeset_comment_path(:id => 999111, :text => "This is a comment"), :headers => auth_header
101       end
102       assert_response :not_found
103
104       # not closed changeset
105       assert_no_difference "ChangesetComment.count" do
106         post changeset_comment_path(:id => create(:changeset), :text => "This is a comment"), :headers => auth_header
107       end
108       assert_response :conflict
109
110       # no text
111       assert_no_difference "ChangesetComment.count" do
112         post changeset_comment_path(:id => create(:changeset, :closed)), :headers => auth_header
113       end
114       assert_response :bad_request
115
116       # empty text
117       assert_no_difference "ChangesetComment.count" do
118         post changeset_comment_path(:id => create(:changeset, :closed), :text => ""), :headers => auth_header
119       end
120       assert_response :bad_request
121     end
122
123     ##
124     # test hide comment fail
125     def test_destroy_comment_fail
126       # unauthorized
127       comment = create(:changeset_comment)
128       assert comment.visible
129
130       post changeset_comment_hide_path(:id => comment)
131       assert_response :unauthorized
132       assert comment.reload.visible
133
134       auth_header = basic_authorization_header create(:user).email, "test"
135
136       # not a moderator
137       post changeset_comment_hide_path(:id => comment), :headers => auth_header
138       assert_response :forbidden
139       assert comment.reload.visible
140
141       auth_header = basic_authorization_header create(:moderator_user).email, "test"
142
143       # bad comment id
144       post changeset_comment_hide_path(:id => 999111), :headers => auth_header
145       assert_response :not_found
146       assert comment.reload.visible
147     end
148
149     ##
150     # test hide comment succes
151     def test_hide_comment_success
152       comment = create(:changeset_comment)
153       assert comment.visible
154
155       auth_header = basic_authorization_header create(:moderator_user).email, "test"
156
157       post changeset_comment_hide_path(:id => comment), :headers => auth_header
158       assert_response :success
159       assert_not comment.reload.visible
160     end
161
162     ##
163     # test unhide comment fail
164     def test_restore_comment_fail
165       # unauthorized
166       comment = create(:changeset_comment, :visible => false)
167       assert_not comment.visible
168
169       post changeset_comment_unhide_path(:id => comment)
170       assert_response :unauthorized
171       assert_not comment.reload.visible
172
173       auth_header = basic_authorization_header create(:user).email, "test"
174
175       # not a moderator
176       post changeset_comment_unhide_path(:id => comment), :headers => auth_header
177       assert_response :forbidden
178       assert_not comment.reload.visible
179
180       auth_header = basic_authorization_header create(:moderator_user).email, "test"
181
182       # bad comment id
183       post changeset_comment_unhide_path(:id => 999111), :headers => auth_header
184       assert_response :not_found
185       assert_not comment.reload.visible
186     end
187
188     ##
189     # test unhide comment succes
190     def test_unhide_comment_success
191       comment = create(:changeset_comment, :visible => false)
192       assert_not comment.visible
193
194       auth_header = basic_authorization_header create(:moderator_user).email, "test"
195
196       post changeset_comment_unhide_path(:id => comment), :headers => auth_header
197       assert_response :success
198       assert comment.reload.visible
199     end
200
201     # This test ensures that token capabilities behave correctly for a method that
202     # requires the terms to have been agreed.
203     # (This would be better as an integration or system testcase, since the changeset_comment
204     # create method is simply a stand-in for any method that requires terms agreement.
205     # But writing oauth tests is hard, and so it's easier to put in a controller test.)
206     def test_api_write_and_terms_agreed_via_token
207       user = create(:user, :terms_agreed => nil)
208       token = create(:access_token, :user => user, :allow_write_api => true)
209       changeset = create(:changeset, :closed)
210
211       assert_difference "ChangesetComment.count", 0 do
212         signed_post changeset_comment_path(:id => changeset), :params => { :text => "This is a comment" }, :oauth => { :token => token }
213       end
214       assert_response :forbidden
215
216       # Try again, after agreement with the terms
217       user.terms_agreed = Time.now.utc
218       user.save!
219
220       assert_difference "ChangesetComment.count", 1 do
221         signed_post changeset_comment_path(:id => changeset), :params => { :text => "This is a comment" }, :oauth => { :token => token }
222       end
223       assert_response :success
224     end
225
226     # This test does the same as above, but with basic auth, to similarly test that the
227     # abilities take into account terms agreement too.
228     def test_api_write_and_terms_agreed_via_basic_auth
229       user = create(:user, :terms_agreed => nil)
230       changeset = create(:changeset, :closed)
231
232       auth_header = basic_authorization_header user.email, "test"
233
234       assert_difference "ChangesetComment.count", 0 do
235         post changeset_comment_path(:id => changeset, :text => "This is a comment"), :headers => auth_header
236       end
237       assert_response :forbidden
238
239       # Try again, after agreement with the terms
240       user.terms_agreed = Time.now.utc
241       user.save!
242
243       assert_difference "ChangesetComment.count", 1 do
244         post changeset_comment_path(:id => changeset, :text => "This is a comment"), :headers => auth_header
245       end
246       assert_response :success
247     end
248   end
249 end