4 module ChangesetComments
5 class VisibilitiesControllerTest < ActionDispatch::IntegrationTest
7 # test all routes which lead to this controller
10 { :path => "/api/0.6/changeset_comments/1/visibility", :method => :post },
11 { :controller => "api/changeset_comments/visibilities", :action => "create", :changeset_comment_id => "1" }
14 { :path => "/api/0.6/changeset_comments/1/visibility.json", :method => :post },
15 { :controller => "api/changeset_comments/visibilities", :action => "create", :changeset_comment_id => "1", :format => "json" }
18 { :path => "/api/0.6/changeset_comments/1/visibility", :method => :delete },
19 { :controller => "api/changeset_comments/visibilities", :action => "destroy", :changeset_comment_id => "1" }
22 { :path => "/api/0.6/changeset_comments/1/visibility.json", :method => :delete },
23 { :controller => "api/changeset_comments/visibilities", :action => "destroy", :changeset_comment_id => "1", :format => "json" }
27 { :controller => "api/changeset_comments/visibilities", :action => "create", :changeset_comment_id => "1" },
28 { :path => "/api/0.6/changeset/comment/1/unhide", :method => :post }
31 { :controller => "api/changeset_comments/visibilities", :action => "create", :changeset_comment_id => "1", :format => "json" },
32 { :path => "/api/0.6/changeset/comment/1/unhide.json", :method => :post }
35 { :controller => "api/changeset_comments/visibilities", :action => "destroy", :changeset_comment_id => "1" },
36 { :path => "/api/0.6/changeset/comment/1/hide", :method => :post }
39 { :controller => "api/changeset_comments/visibilities", :action => "destroy", :changeset_comment_id => "1", :format => "json" },
40 { :path => "/api/0.6/changeset/comment/1/hide.json", :method => :post }
44 def test_create_by_unauthorized
45 comment = create(:changeset_comment, :visible => false)
47 post api_changeset_comment_visibility_path(comment)
49 assert_response :unauthorized
50 assert_not comment.reload.visible
53 def test_create_by_normal_user
54 comment = create(:changeset_comment, :visible => false)
55 auth_header = bearer_authorization_header
57 post api_changeset_comment_visibility_path(comment), :headers => auth_header
59 assert_response :forbidden
60 assert_not comment.reload.visible
63 def test_create_on_missing_comment
64 auth_header = bearer_authorization_header create(:moderator_user)
66 post api_changeset_comment_visibility_path(999111), :headers => auth_header
68 assert_response :not_found
71 def test_create_without_required_scope
72 comment = create(:changeset_comment, :visible => false)
73 auth_header = bearer_authorization_header create(:moderator_user), :scopes => %w[read_prefs]
75 post api_changeset_comment_visibility_path(comment), :headers => auth_header
77 assert_response :forbidden
78 assert_not comment.reload.visible
81 def test_create_with_write_changeset_comments_scope
82 comment = create(:changeset_comment, :visible => false)
83 auth_header = bearer_authorization_header create(:moderator_user), :scopes => %w[write_changeset_comments]
85 post api_changeset_comment_visibility_path(comment), :headers => auth_header
87 assert_response :success
88 assert_equal "application/xml", response.media_type
89 assert_dom "osm", 1 do
90 assert_dom "> changeset", 1 do
91 assert_dom "> @id", comment.changeset_id.to_s
92 assert_dom "> @comments_count", "1"
96 assert comment.reload.visible
99 def test_create_with_write_changeset_comments_scope_json
100 comment = create(:changeset_comment, :visible => false)
101 auth_header = bearer_authorization_header create(:moderator_user), :scopes => %w[write_changeset_comments]
103 post api_changeset_comment_visibility_path(comment, :format => "json"), :headers => auth_header
105 assert_response :success
106 assert_equal "application/json", response.media_type
107 js = ActiveSupport::JSON.decode(@response.body)
108 assert_not_nil js["changeset"]
109 assert_equal comment.changeset_id, js["changeset"]["id"]
110 assert_equal 1, js["changeset"]["comments_count"]
112 assert comment.reload.visible
115 def test_create_with_write_api_scope
116 comment = create(:changeset_comment, :visible => false)
117 auth_header = bearer_authorization_header create(:moderator_user), :scopes => %w[write_api]
119 post api_changeset_comment_visibility_path(comment), :headers => auth_header
121 assert_response :success
122 assert_equal "application/xml", response.media_type
123 assert_dom "osm", 1 do
124 assert_dom "> changeset", 1 do
125 assert_dom "> @id", comment.changeset_id.to_s
126 assert_dom "> @comments_count", "1"
130 assert comment.reload.visible
133 def test_create_with_write_api_scope_json
134 comment = create(:changeset_comment, :visible => false)
135 auth_header = bearer_authorization_header create(:moderator_user), :scopes => %w[write_api]
137 post api_changeset_comment_visibility_path(comment, :format => "json"), :headers => auth_header
139 assert_response :success
140 js = ActiveSupport::JSON.decode(@response.body)
141 assert_equal "application/json", response.media_type
142 assert_not_nil js["changeset"]
143 assert_equal comment.changeset_id, js["changeset"]["id"]
144 assert_equal 1, js["changeset"]["comments_count"]
146 assert comment.reload.visible
149 def test_destroy_by_unauthorized
150 comment = create(:changeset_comment)
152 delete api_changeset_comment_visibility_path(comment)
154 assert_response :unauthorized
155 assert comment.reload.visible
158 def test_destroy_by_normal_user
159 comment = create(:changeset_comment)
160 auth_header = bearer_authorization_header
162 delete api_changeset_comment_visibility_path(comment), :headers => auth_header
164 assert_response :forbidden
165 assert comment.reload.visible
168 def test_destroy_on_missing_comment
169 auth_header = bearer_authorization_header create(:moderator_user)
171 delete api_changeset_comment_visibility_path(999111), :headers => auth_header
173 assert_response :not_found
176 def test_destroy_without_required_scope
177 comment = create(:changeset_comment)
178 auth_header = bearer_authorization_header create(:moderator_user), :scopes => %w[read_prefs]
180 delete api_changeset_comment_visibility_path(comment), :headers => auth_header
182 assert_response :forbidden
183 assert comment.reload.visible
186 def test_destroy_with_write_changeset_comments_scope
187 comment = create(:changeset_comment)
188 auth_header = bearer_authorization_header create(:moderator_user), :scopes => %w[write_changeset_comments]
190 delete api_changeset_comment_visibility_path(comment), :headers => auth_header
192 assert_response :success
193 assert_equal "application/xml", response.media_type
194 assert_dom "osm", 1 do
195 assert_dom "> changeset", 1 do
196 assert_dom "> @id", comment.changeset_id.to_s
197 assert_dom "> @comments_count", "0"
201 assert_not comment.reload.visible
204 def test_destroy_with_write_changeset_comments_scope_json
205 comment = create(:changeset_comment)
206 auth_header = bearer_authorization_header create(:moderator_user), :scopes => %w[write_changeset_comments]
208 delete api_changeset_comment_visibility_path(comment, :format => "json"), :headers => auth_header
210 assert_response :success
211 assert_equal "application/json", response.media_type
212 js = ActiveSupport::JSON.decode(@response.body)
213 assert_not_nil js["changeset"]
214 assert_equal comment.changeset_id, js["changeset"]["id"]
215 assert_equal 0, js["changeset"]["comments_count"]
217 assert_not comment.reload.visible
220 def test_destroy_with_write_api_scope
221 comment = create(:changeset_comment)
222 auth_header = bearer_authorization_header create(:moderator_user), :scopes => %w[write_api]
224 delete api_changeset_comment_visibility_path(comment), :headers => auth_header
226 assert_response :success
227 assert_equal "application/xml", response.media_type
228 assert_dom "osm", 1 do
229 assert_dom "> changeset", 1 do
230 assert_dom "> @id", comment.changeset_id.to_s
231 assert_dom "> @comments_count", "0"
235 assert_not comment.reload.visible
238 def test_destroy_with_write_api_scope_json
239 comment = create(:changeset_comment)
240 auth_header = bearer_authorization_header create(:moderator_user), :scopes => %w[write_api]
242 delete api_changeset_comment_visibility_path(comment, :format => "json"), :headers => auth_header
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 0, js["changeset"]["comments_count"]
251 assert_not comment.reload.visible