1 # frozen_string_literal: true
6 module ChangesetComments
7 class VisibilitiesControllerTest < ActionDispatch::IntegrationTest
9 # test all routes which lead to this controller
12 { :path => "/api/0.6/changeset_comments/1/visibility", :method => :post },
13 { :controller => "api/changeset_comments/visibilities", :action => "create", :changeset_comment_id => "1" }
16 { :path => "/api/0.6/changeset_comments/1/visibility.json", :method => :post },
17 { :controller => "api/changeset_comments/visibilities", :action => "create", :changeset_comment_id => "1", :format => "json" }
20 { :path => "/api/0.6/changeset_comments/1/visibility", :method => :delete },
21 { :controller => "api/changeset_comments/visibilities", :action => "destroy", :changeset_comment_id => "1" }
24 { :path => "/api/0.6/changeset_comments/1/visibility.json", :method => :delete },
25 { :controller => "api/changeset_comments/visibilities", :action => "destroy", :changeset_comment_id => "1", :format => "json" }
29 { :controller => "api/changeset_comments/visibilities", :action => "create", :changeset_comment_id => "1" },
30 { :path => "/api/0.6/changeset/comment/1/unhide", :method => :post }
33 { :controller => "api/changeset_comments/visibilities", :action => "create", :changeset_comment_id => "1", :format => "json" },
34 { :path => "/api/0.6/changeset/comment/1/unhide.json", :method => :post }
37 { :controller => "api/changeset_comments/visibilities", :action => "destroy", :changeset_comment_id => "1" },
38 { :path => "/api/0.6/changeset/comment/1/hide", :method => :post }
41 { :controller => "api/changeset_comments/visibilities", :action => "destroy", :changeset_comment_id => "1", :format => "json" },
42 { :path => "/api/0.6/changeset/comment/1/hide.json", :method => :post }
46 def test_create_by_unauthorized
47 comment = create(:changeset_comment, :visible => false)
49 post api_changeset_comment_visibility_path(comment)
51 assert_response :unauthorized
52 assert_not comment.reload.visible
55 def test_create_by_normal_user
56 comment = create(:changeset_comment, :visible => false)
57 auth_header = bearer_authorization_header
59 post api_changeset_comment_visibility_path(comment), :headers => auth_header
61 assert_response :forbidden
62 assert_not comment.reload.visible
65 def test_create_on_missing_comment
66 auth_header = bearer_authorization_header create(:moderator_user)
68 post api_changeset_comment_visibility_path(999111), :headers => auth_header
70 assert_response :not_found
73 def test_create_without_required_scope
74 comment = create(:changeset_comment, :visible => false)
75 auth_header = bearer_authorization_header create(:moderator_user), :scopes => %w[read_prefs]
77 post api_changeset_comment_visibility_path(comment), :headers => auth_header
79 assert_response :forbidden
80 assert_not comment.reload.visible
83 def test_create_with_write_changeset_comments_scope
84 comment = create(:changeset_comment, :visible => false)
85 auth_header = bearer_authorization_header create(:moderator_user), :scopes => %w[write_changeset_comments]
87 post api_changeset_comment_visibility_path(comment), :headers => auth_header
89 check_successful_response_xml(comment, :comment_visible => true)
92 def test_create_with_write_changeset_comments_scope_json
93 comment = create(:changeset_comment, :visible => false)
94 auth_header = bearer_authorization_header create(:moderator_user), :scopes => %w[write_changeset_comments]
96 post api_changeset_comment_visibility_path(comment, :format => "json"), :headers => auth_header
98 check_successful_response_json(comment, :comment_visible => true)
101 def test_create_with_write_api_scope
102 comment = create(:changeset_comment, :visible => false)
103 auth_header = bearer_authorization_header create(:moderator_user), :scopes => %w[write_api]
105 post api_changeset_comment_visibility_path(comment), :headers => auth_header
107 check_successful_response_xml(comment, :comment_visible => true)
110 def test_create_with_write_api_scope_json
111 comment = create(:changeset_comment, :visible => false)
112 auth_header = bearer_authorization_header create(:moderator_user), :scopes => %w[write_api]
114 post api_changeset_comment_visibility_path(comment, :format => "json"), :headers => auth_header
116 check_successful_response_json(comment, :comment_visible => true)
119 def test_create_at_legacy_route
120 comment = create(:changeset_comment, :visible => false)
121 auth_header = bearer_authorization_header create(:moderator_user), :scopes => %w[write_api]
123 post "/api/0.6/changeset/comment/#{comment.id}/unhide", :headers => auth_header
125 check_successful_response_xml(comment, :comment_visible => true)
128 def test_create_at_legacy_route_json
129 comment = create(:changeset_comment, :visible => false)
130 auth_header = bearer_authorization_header create(:moderator_user), :scopes => %w[write_api]
132 post "/api/0.6/changeset/comment/#{comment.id}/unhide.json", :headers => auth_header
134 check_successful_response_json(comment, :comment_visible => true)
137 def test_destroy_by_unauthorized
138 comment = create(:changeset_comment)
140 delete api_changeset_comment_visibility_path(comment)
142 assert_response :unauthorized
143 assert comment.reload.visible
146 def test_destroy_by_normal_user
147 comment = create(:changeset_comment)
148 auth_header = bearer_authorization_header
150 delete api_changeset_comment_visibility_path(comment), :headers => auth_header
152 assert_response :forbidden
153 assert comment.reload.visible
156 def test_destroy_on_missing_comment
157 auth_header = bearer_authorization_header create(:moderator_user)
159 delete api_changeset_comment_visibility_path(999111), :headers => auth_header
161 assert_response :not_found
164 def test_destroy_without_required_scope
165 comment = create(:changeset_comment)
166 auth_header = bearer_authorization_header create(:moderator_user), :scopes => %w[read_prefs]
168 delete api_changeset_comment_visibility_path(comment), :headers => auth_header
170 assert_response :forbidden
171 assert comment.reload.visible
174 def test_destroy_with_write_changeset_comments_scope
175 comment = create(:changeset_comment)
176 auth_header = bearer_authorization_header create(:moderator_user), :scopes => %w[write_changeset_comments]
178 delete api_changeset_comment_visibility_path(comment), :headers => auth_header
180 check_successful_response_xml(comment, :comment_visible => false)
183 def test_destroy_with_write_changeset_comments_scope_json
184 comment = create(:changeset_comment)
185 auth_header = bearer_authorization_header create(:moderator_user), :scopes => %w[write_changeset_comments]
187 delete api_changeset_comment_visibility_path(comment, :format => "json"), :headers => auth_header
189 check_successful_response_json(comment, :comment_visible => false)
192 def test_destroy_with_write_api_scope
193 comment = create(:changeset_comment)
194 auth_header = bearer_authorization_header create(:moderator_user), :scopes => %w[write_api]
196 delete api_changeset_comment_visibility_path(comment), :headers => auth_header
198 check_successful_response_xml(comment, :comment_visible => false)
201 def test_destroy_with_write_api_scope_json
202 comment = create(:changeset_comment)
203 auth_header = bearer_authorization_header create(:moderator_user), :scopes => %w[write_api]
205 delete api_changeset_comment_visibility_path(comment, :format => "json"), :headers => auth_header
207 check_successful_response_json(comment, :comment_visible => false)
210 def test_destroy_at_legacy_route
211 comment = create(:changeset_comment)
212 auth_header = bearer_authorization_header create(:moderator_user), :scopes => %w[write_api]
214 post "/api/0.6/changeset/comment/#{comment.id}/hide", :headers => auth_header
216 check_successful_response_xml(comment, :comment_visible => false)
219 def test_destroy_at_legacy_route_json
220 comment = create(:changeset_comment)
221 auth_header = bearer_authorization_header create(:moderator_user), :scopes => %w[write_api]
223 post "/api/0.6/changeset/comment/#{comment.id}/hide.json", :headers => auth_header
225 check_successful_response_json(comment, :comment_visible => false)
230 def check_successful_response_xml(comment, comment_visible:)
231 assert_response :success
232 assert_equal "application/xml", response.media_type
233 assert_dom "osm", 1 do
234 assert_dom "> changeset", 1 do
235 assert_dom "> @id", comment.changeset_id.to_s
236 assert_dom "> @comments_count", comment_visible ? "1" : "0"
240 assert_equal comment_visible, comment.reload.visible
243 def check_successful_response_json(comment, comment_visible:)
244 assert_response :success
245 assert_equal "application/json", response.media_type
246 js = ActiveSupport::JSON.decode(@response.body)
247 assert_not_nil js["changeset"]
248 assert_equal comment.changeset_id, js["changeset"]["id"]
249 assert_equal comment_visible ? 1 : 0, js["changeset"]["comments_count"]
251 assert_equal comment_visible, comment.reload.visible