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 check_successful_response_xml(comment, :comment_visible => true)
90 def test_create_with_write_changeset_comments_scope_json
91 comment = create(:changeset_comment, :visible => false)
92 auth_header = bearer_authorization_header create(:moderator_user), :scopes => %w[write_changeset_comments]
94 post api_changeset_comment_visibility_path(comment, :format => "json"), :headers => auth_header
96 check_successful_response_json(comment, :comment_visible => true)
99 def test_create_with_write_api_scope
100 comment = create(:changeset_comment, :visible => false)
101 auth_header = bearer_authorization_header create(:moderator_user), :scopes => %w[write_api]
103 post api_changeset_comment_visibility_path(comment), :headers => auth_header
105 check_successful_response_xml(comment, :comment_visible => true)
108 def test_create_with_write_api_scope_json
109 comment = create(:changeset_comment, :visible => false)
110 auth_header = bearer_authorization_header create(:moderator_user), :scopes => %w[write_api]
112 post api_changeset_comment_visibility_path(comment, :format => "json"), :headers => auth_header
114 check_successful_response_json(comment, :comment_visible => true)
117 def test_destroy_by_unauthorized
118 comment = create(:changeset_comment)
120 delete api_changeset_comment_visibility_path(comment)
122 assert_response :unauthorized
123 assert comment.reload.visible
126 def test_destroy_by_normal_user
127 comment = create(:changeset_comment)
128 auth_header = bearer_authorization_header
130 delete api_changeset_comment_visibility_path(comment), :headers => auth_header
132 assert_response :forbidden
133 assert comment.reload.visible
136 def test_destroy_on_missing_comment
137 auth_header = bearer_authorization_header create(:moderator_user)
139 delete api_changeset_comment_visibility_path(999111), :headers => auth_header
141 assert_response :not_found
144 def test_destroy_without_required_scope
145 comment = create(:changeset_comment)
146 auth_header = bearer_authorization_header create(:moderator_user), :scopes => %w[read_prefs]
148 delete api_changeset_comment_visibility_path(comment), :headers => auth_header
150 assert_response :forbidden
151 assert comment.reload.visible
154 def test_destroy_with_write_changeset_comments_scope
155 comment = create(:changeset_comment)
156 auth_header = bearer_authorization_header create(:moderator_user), :scopes => %w[write_changeset_comments]
158 delete api_changeset_comment_visibility_path(comment), :headers => auth_header
160 check_successful_response_xml(comment, :comment_visible => false)
163 def test_destroy_with_write_changeset_comments_scope_json
164 comment = create(:changeset_comment)
165 auth_header = bearer_authorization_header create(:moderator_user), :scopes => %w[write_changeset_comments]
167 delete api_changeset_comment_visibility_path(comment, :format => "json"), :headers => auth_header
169 check_successful_response_json(comment, :comment_visible => false)
172 def test_destroy_with_write_api_scope
173 comment = create(:changeset_comment)
174 auth_header = bearer_authorization_header create(:moderator_user), :scopes => %w[write_api]
176 delete api_changeset_comment_visibility_path(comment), :headers => auth_header
178 check_successful_response_xml(comment, :comment_visible => false)
181 def test_destroy_with_write_api_scope_json
182 comment = create(:changeset_comment)
183 auth_header = bearer_authorization_header create(:moderator_user), :scopes => %w[write_api]
185 delete api_changeset_comment_visibility_path(comment, :format => "json"), :headers => auth_header
187 check_successful_response_json(comment, :comment_visible => false)
192 def check_successful_response_xml(comment, comment_visible:)
193 assert_response :success
194 assert_equal "application/xml", response.media_type
195 assert_dom "osm", 1 do
196 assert_dom "> changeset", 1 do
197 assert_dom "> @id", comment.changeset_id.to_s
198 assert_dom "> @comments_count", comment_visible ? "1" : "0"
202 assert_equal comment_visible, comment.reload.visible
205 def check_successful_response_json(comment, comment_visible:)
206 assert_response :success
207 assert_equal "application/json", response.media_type
208 js = ActiveSupport::JSON.decode(@response.body)
209 assert_not_nil js["changeset"]
210 assert_equal comment.changeset_id, js["changeset"]["id"]
211 assert_equal comment_visible ? 1 : 0, js["changeset"]["comments_count"]
213 assert_equal comment_visible, comment.reload.visible