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_create_at_legacy_route
 
 118         comment = create(:changeset_comment, :visible => false)
 
 119         auth_header = bearer_authorization_header create(:moderator_user), :scopes => %w[write_api]
 
 121         post "/api/0.6/changeset/comment/#{comment.id}/unhide", :headers => auth_header
 
 123         check_successful_response_xml(comment, :comment_visible => true)
 
 126       def test_create_at_legacy_route_json
 
 127         comment = create(:changeset_comment, :visible => false)
 
 128         auth_header = bearer_authorization_header create(:moderator_user), :scopes => %w[write_api]
 
 130         post "/api/0.6/changeset/comment/#{comment.id}/unhide.json", :headers => auth_header
 
 132         check_successful_response_json(comment, :comment_visible => true)
 
 135       def test_destroy_by_unauthorized
 
 136         comment = create(:changeset_comment)
 
 138         delete api_changeset_comment_visibility_path(comment)
 
 140         assert_response :unauthorized
 
 141         assert comment.reload.visible
 
 144       def test_destroy_by_normal_user
 
 145         comment = create(:changeset_comment)
 
 146         auth_header = bearer_authorization_header
 
 148         delete api_changeset_comment_visibility_path(comment), :headers => auth_header
 
 150         assert_response :forbidden
 
 151         assert comment.reload.visible
 
 154       def test_destroy_on_missing_comment
 
 155         auth_header = bearer_authorization_header create(:moderator_user)
 
 157         delete api_changeset_comment_visibility_path(999111), :headers => auth_header
 
 159         assert_response :not_found
 
 162       def test_destroy_without_required_scope
 
 163         comment = create(:changeset_comment)
 
 164         auth_header = bearer_authorization_header create(:moderator_user), :scopes => %w[read_prefs]
 
 166         delete api_changeset_comment_visibility_path(comment), :headers => auth_header
 
 168         assert_response :forbidden
 
 169         assert comment.reload.visible
 
 172       def test_destroy_with_write_changeset_comments_scope
 
 173         comment = create(:changeset_comment)
 
 174         auth_header = bearer_authorization_header create(:moderator_user), :scopes => %w[write_changeset_comments]
 
 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_changeset_comments_scope_json
 
 182         comment = create(:changeset_comment)
 
 183         auth_header = bearer_authorization_header create(:moderator_user), :scopes => %w[write_changeset_comments]
 
 185         delete api_changeset_comment_visibility_path(comment, :format => "json"), :headers => auth_header
 
 187         check_successful_response_json(comment, :comment_visible => false)
 
 190       def test_destroy_with_write_api_scope
 
 191         comment = create(:changeset_comment)
 
 192         auth_header = bearer_authorization_header create(:moderator_user), :scopes => %w[write_api]
 
 194         delete api_changeset_comment_visibility_path(comment), :headers => auth_header
 
 196         check_successful_response_xml(comment, :comment_visible => false)
 
 199       def test_destroy_with_write_api_scope_json
 
 200         comment = create(:changeset_comment)
 
 201         auth_header = bearer_authorization_header create(:moderator_user), :scopes => %w[write_api]
 
 203         delete api_changeset_comment_visibility_path(comment, :format => "json"), :headers => auth_header
 
 205         check_successful_response_json(comment, :comment_visible => false)
 
 208       def test_destroy_at_legacy_route
 
 209         comment = create(:changeset_comment)
 
 210         auth_header = bearer_authorization_header create(:moderator_user), :scopes => %w[write_api]
 
 212         post "/api/0.6/changeset/comment/#{comment.id}/hide", :headers => auth_header
 
 214         check_successful_response_xml(comment, :comment_visible => false)
 
 217       def test_destroy_at_legacy_route_json
 
 218         comment = create(:changeset_comment)
 
 219         auth_header = bearer_authorization_header create(:moderator_user), :scopes => %w[write_api]
 
 221         post "/api/0.6/changeset/comment/#{comment.id}/hide.json", :headers => auth_header
 
 223         check_successful_response_json(comment, :comment_visible => false)
 
 228       def check_successful_response_xml(comment, comment_visible:)
 
 229         assert_response :success
 
 230         assert_equal "application/xml", response.media_type
 
 231         assert_dom "osm", 1 do
 
 232           assert_dom "> changeset", 1 do
 
 233             assert_dom "> @id", comment.changeset_id.to_s
 
 234             assert_dom "> @comments_count", comment_visible ? "1" : "0"
 
 238         assert_equal comment_visible, comment.reload.visible
 
 241       def check_successful_response_json(comment, comment_visible:)
 
 242         assert_response :success
 
 243         assert_equal "application/json", response.media_type
 
 244         js = ActiveSupport::JSON.decode(@response.body)
 
 245         assert_not_nil js["changeset"]
 
 246         assert_equal comment.changeset_id, js["changeset"]["id"]
 
 247         assert_equal comment_visible ? 1 : 0, js["changeset"]["comments_count"]
 
 249         assert_equal comment_visible, comment.reload.visible