Create api changeset comment visibility resource

This commit is contained in:
Anton Khorev 2025-02-23 04:59:26 +03:00
parent 457cc99349
commit 84a0c76ab2
13 changed files with 356 additions and 299 deletions

View file

@ -0,0 +1,255 @@
require "test_helper"
module Api
module ChangesetComments
class VisibilitiesControllerTest < ActionDispatch::IntegrationTest
##
# test all routes which lead to this controller
def test_routes
assert_routing(
{ :path => "/api/0.6/changeset_comments/1/visibility", :method => :post },
{ :controller => "api/changeset_comments/visibilities", :action => "create", :changeset_comment_id => "1" }
)
assert_routing(
{ :path => "/api/0.6/changeset_comments/1/visibility.json", :method => :post },
{ :controller => "api/changeset_comments/visibilities", :action => "create", :changeset_comment_id => "1", :format => "json" }
)
assert_routing(
{ :path => "/api/0.6/changeset_comments/1/visibility", :method => :delete },
{ :controller => "api/changeset_comments/visibilities", :action => "destroy", :changeset_comment_id => "1" }
)
assert_routing(
{ :path => "/api/0.6/changeset_comments/1/visibility.json", :method => :delete },
{ :controller => "api/changeset_comments/visibilities", :action => "destroy", :changeset_comment_id => "1", :format => "json" }
)
assert_recognizes(
{ :controller => "api/changeset_comments/visibilities", :action => "create", :changeset_comment_id => "1" },
{ :path => "/api/0.6/changeset/comment/1/unhide", :method => :post }
)
assert_recognizes(
{ :controller => "api/changeset_comments/visibilities", :action => "create", :changeset_comment_id => "1", :format => "json" },
{ :path => "/api/0.6/changeset/comment/1/unhide.json", :method => :post }
)
assert_recognizes(
{ :controller => "api/changeset_comments/visibilities", :action => "destroy", :changeset_comment_id => "1" },
{ :path => "/api/0.6/changeset/comment/1/hide", :method => :post }
)
assert_recognizes(
{ :controller => "api/changeset_comments/visibilities", :action => "destroy", :changeset_comment_id => "1", :format => "json" },
{ :path => "/api/0.6/changeset/comment/1/hide.json", :method => :post }
)
end
def test_create_by_unauthorized
comment = create(:changeset_comment, :visible => false)
post api_changeset_comment_visibility_path(comment)
assert_response :unauthorized
assert_not comment.reload.visible
end
def test_create_by_normal_user
comment = create(:changeset_comment, :visible => false)
auth_header = bearer_authorization_header
post api_changeset_comment_visibility_path(comment), :headers => auth_header
assert_response :forbidden
assert_not comment.reload.visible
end
def test_create_on_missing_comment
auth_header = bearer_authorization_header create(:moderator_user)
post api_changeset_comment_visibility_path(999111), :headers => auth_header
assert_response :not_found
end
def test_create_without_required_scope
comment = create(:changeset_comment, :visible => false)
auth_header = bearer_authorization_header create(:moderator_user), :scopes => %w[read_prefs]
post api_changeset_comment_visibility_path(comment), :headers => auth_header
assert_response :forbidden
assert_not comment.reload.visible
end
def test_create_with_write_changeset_comments_scope
comment = create(:changeset_comment, :visible => false)
auth_header = bearer_authorization_header create(:moderator_user), :scopes => %w[write_changeset_comments]
post api_changeset_comment_visibility_path(comment), :headers => auth_header
assert_response :success
assert_equal "application/xml", response.media_type
assert_dom "osm", 1 do
assert_dom "> changeset", 1 do
assert_dom "> @id", comment.changeset_id.to_s
assert_dom "> @comments_count", "1"
end
end
assert comment.reload.visible
end
def test_create_with_write_changeset_comments_scope_json
comment = create(:changeset_comment, :visible => false)
auth_header = bearer_authorization_header create(:moderator_user), :scopes => %w[write_changeset_comments]
post api_changeset_comment_visibility_path(comment, :format => "json"), :headers => auth_header
assert_response :success
assert_equal "application/json", response.media_type
js = ActiveSupport::JSON.decode(@response.body)
assert_not_nil js["changeset"]
assert_equal comment.changeset_id, js["changeset"]["id"]
assert_equal 1, js["changeset"]["comments_count"]
assert comment.reload.visible
end
def test_create_with_write_api_scope
comment = create(:changeset_comment, :visible => false)
auth_header = bearer_authorization_header create(:moderator_user), :scopes => %w[write_api]
post api_changeset_comment_visibility_path(comment), :headers => auth_header
assert_response :success
assert_equal "application/xml", response.media_type
assert_dom "osm", 1 do
assert_dom "> changeset", 1 do
assert_dom "> @id", comment.changeset_id.to_s
assert_dom "> @comments_count", "1"
end
end
assert comment.reload.visible
end
def test_create_with_write_api_scope_json
comment = create(:changeset_comment, :visible => false)
auth_header = bearer_authorization_header create(:moderator_user), :scopes => %w[write_api]
post api_changeset_comment_visibility_path(comment, :format => "json"), :headers => auth_header
assert_response :success
js = ActiveSupport::JSON.decode(@response.body)
assert_equal "application/json", response.media_type
assert_not_nil js["changeset"]
assert_equal comment.changeset_id, js["changeset"]["id"]
assert_equal 1, js["changeset"]["comments_count"]
assert comment.reload.visible
end
def test_destroy_by_unauthorized
comment = create(:changeset_comment)
delete api_changeset_comment_visibility_path(comment)
assert_response :unauthorized
assert comment.reload.visible
end
def test_destroy_by_normal_user
comment = create(:changeset_comment)
auth_header = bearer_authorization_header
delete api_changeset_comment_visibility_path(comment), :headers => auth_header
assert_response :forbidden
assert comment.reload.visible
end
def test_destroy_on_missing_comment
auth_header = bearer_authorization_header create(:moderator_user)
delete api_changeset_comment_visibility_path(999111), :headers => auth_header
assert_response :not_found
end
def test_destroy_without_required_scope
comment = create(:changeset_comment)
auth_header = bearer_authorization_header create(:moderator_user), :scopes => %w[read_prefs]
delete api_changeset_comment_visibility_path(comment), :headers => auth_header
assert_response :forbidden
assert comment.reload.visible
end
def test_destroy_with_write_changeset_comments_scope
comment = create(:changeset_comment)
auth_header = bearer_authorization_header create(:moderator_user), :scopes => %w[write_changeset_comments]
delete api_changeset_comment_visibility_path(comment), :headers => auth_header
assert_response :success
assert_equal "application/xml", response.media_type
assert_dom "osm", 1 do
assert_dom "> changeset", 1 do
assert_dom "> @id", comment.changeset_id.to_s
assert_dom "> @comments_count", "0"
end
end
assert_not comment.reload.visible
end
def test_destroy_with_write_changeset_comments_scope_json
comment = create(:changeset_comment)
auth_header = bearer_authorization_header create(:moderator_user), :scopes => %w[write_changeset_comments]
delete api_changeset_comment_visibility_path(comment, :format => "json"), :headers => auth_header
assert_response :success
assert_equal "application/json", response.media_type
js = ActiveSupport::JSON.decode(@response.body)
assert_not_nil js["changeset"]
assert_equal comment.changeset_id, js["changeset"]["id"]
assert_equal 0, js["changeset"]["comments_count"]
assert_not comment.reload.visible
end
def test_destroy_with_write_api_scope
comment = create(:changeset_comment)
auth_header = bearer_authorization_header create(:moderator_user), :scopes => %w[write_api]
delete api_changeset_comment_visibility_path(comment), :headers => auth_header
assert_response :success
assert_equal "application/xml", response.media_type
assert_dom "osm", 1 do
assert_dom "> changeset", 1 do
assert_dom "> @id", comment.changeset_id.to_s
assert_dom "> @comments_count", "0"
end
end
assert_not comment.reload.visible
end
def test_destroy_with_write_api_scope_json
comment = create(:changeset_comment)
auth_header = bearer_authorization_header create(:moderator_user), :scopes => %w[write_api]
delete api_changeset_comment_visibility_path(comment, :format => "json"), :headers => auth_header
assert_response :success
assert_equal "application/json", response.media_type
js = ActiveSupport::JSON.decode(@response.body)
assert_not_nil js["changeset"]
assert_equal comment.changeset_id, js["changeset"]["id"]
assert_equal 0, js["changeset"]["comments_count"]
assert_not comment.reload.visible
end
end
end
end

View file

@ -21,22 +21,6 @@ module Api
{ :path => "/api/0.6/changeset/1/comment.json", :method => :post },
{ :controller => "api/changeset_comments", :action => "create", :changeset_id => "1", :format => "json" }
)
assert_routing(
{ :path => "/api/0.6/changeset/comment/1/hide", :method => :post },
{ :controller => "api/changeset_comments", :action => "destroy", :id => "1" }
)
assert_routing(
{ :path => "/api/0.6/changeset/comment/1/hide.json", :method => :post },
{ :controller => "api/changeset_comments", :action => "destroy", :id => "1", :format => "json" }
)
assert_routing(
{ :path => "/api/0.6/changeset/comment/1/unhide", :method => :post },
{ :controller => "api/changeset_comments", :action => "restore", :id => "1" }
)
assert_routing(
{ :path => "/api/0.6/changeset/comment/1/unhide.json", :method => :post },
{ :controller => "api/changeset_comments", :action => "restore", :id => "1", :format => "json" }
)
end
def test_index
@ -353,216 +337,6 @@ module Api
end
end
def test_hide_by_unauthorized
comment = create(:changeset_comment)
post changeset_comment_hide_path(comment)
assert_response :unauthorized
assert comment.reload.visible
end
def test_hide_by_normal_user
comment = create(:changeset_comment)
auth_header = bearer_authorization_header
post changeset_comment_hide_path(comment), :headers => auth_header
assert_response :forbidden
assert comment.reload.visible
end
def test_hide_missing_comment
auth_header = bearer_authorization_header create(:moderator_user)
post changeset_comment_hide_path(999111), :headers => auth_header
assert_response :not_found
end
def test_hide_without_required_scope
comment = create(:changeset_comment)
auth_header = bearer_authorization_header create(:moderator_user), :scopes => %w[read_prefs]
post changeset_comment_hide_path(comment), :headers => auth_header
assert_response :forbidden
assert comment.reload.visible
end
def test_hide_with_write_changeset_comments_scope
comment = create(:changeset_comment)
auth_header = bearer_authorization_header create(:moderator_user), :scopes => %w[write_changeset_comments]
post changeset_comment_hide_path(comment), :headers => auth_header
assert_response :success
assert_equal "application/xml", response.media_type
assert_dom "osm", 1 do
assert_dom "> changeset", 1 do
assert_dom "> @id", comment.changeset_id.to_s
assert_dom "> @comments_count", "0"
end
end
assert_not comment.reload.visible
end
def test_hide_with_write_changeset_comments_scope_json
comment = create(:changeset_comment)
auth_header = bearer_authorization_header create(:moderator_user), :scopes => %w[write_changeset_comments]
post changeset_comment_hide_path(comment, :format => "json"), :headers => auth_header
assert_response :success
assert_equal "application/json", response.media_type
js = ActiveSupport::JSON.decode(@response.body)
assert_not_nil js["changeset"]
assert_equal comment.changeset_id, js["changeset"]["id"]
assert_equal 0, js["changeset"]["comments_count"]
assert_not comment.reload.visible
end
def test_hide_with_write_api_scope
comment = create(:changeset_comment)
auth_header = bearer_authorization_header create(:moderator_user), :scopes => %w[write_api]
post changeset_comment_hide_path(comment), :headers => auth_header
assert_response :success
assert_equal "application/xml", response.media_type
assert_dom "osm", 1 do
assert_dom "> changeset", 1 do
assert_dom "> @id", comment.changeset_id.to_s
assert_dom "> @comments_count", "0"
end
end
assert_not comment.reload.visible
end
def test_hide_with_write_api_scope_json
comment = create(:changeset_comment)
auth_header = bearer_authorization_header create(:moderator_user), :scopes => %w[write_api]
post changeset_comment_hide_path(comment, :format => "json"), :headers => auth_header
assert_response :success
assert_equal "application/json", response.media_type
js = ActiveSupport::JSON.decode(@response.body)
assert_not_nil js["changeset"]
assert_equal comment.changeset_id, js["changeset"]["id"]
assert_equal 0, js["changeset"]["comments_count"]
assert_not comment.reload.visible
end
def test_unhide_by_unauthorized
comment = create(:changeset_comment, :visible => false)
post changeset_comment_unhide_path(comment)
assert_response :unauthorized
assert_not comment.reload.visible
end
def test_unhide_by_normal_user
comment = create(:changeset_comment, :visible => false)
auth_header = bearer_authorization_header
post changeset_comment_unhide_path(comment), :headers => auth_header
assert_response :forbidden
assert_not comment.reload.visible
end
def test_unhide_missing_comment
auth_header = bearer_authorization_header create(:moderator_user)
post changeset_comment_unhide_path(999111), :headers => auth_header
assert_response :not_found
end
def test_unhide_without_required_scope
comment = create(:changeset_comment, :visible => false)
auth_header = bearer_authorization_header create(:moderator_user), :scopes => %w[read_prefs]
post changeset_comment_unhide_path(comment), :headers => auth_header
assert_response :forbidden
assert_not comment.reload.visible
end
def test_unhide_with_write_changeset_comments_scope
comment = create(:changeset_comment, :visible => false)
auth_header = bearer_authorization_header create(:moderator_user), :scopes => %w[write_changeset_comments]
post changeset_comment_unhide_path(comment), :headers => auth_header
assert_response :success
assert_equal "application/xml", response.media_type
assert_dom "osm", 1 do
assert_dom "> changeset", 1 do
assert_dom "> @id", comment.changeset_id.to_s
assert_dom "> @comments_count", "1"
end
end
assert comment.reload.visible
end
def test_unhide_with_write_changeset_comments_scope_json
comment = create(:changeset_comment, :visible => false)
auth_header = bearer_authorization_header create(:moderator_user), :scopes => %w[write_changeset_comments]
post changeset_comment_unhide_path(comment, :format => "json"), :headers => auth_header
assert_response :success
assert_equal "application/json", response.media_type
js = ActiveSupport::JSON.decode(@response.body)
assert_not_nil js["changeset"]
assert_equal comment.changeset_id, js["changeset"]["id"]
assert_equal 1, js["changeset"]["comments_count"]
assert comment.reload.visible
end
def test_unhide_with_write_api_scope
comment = create(:changeset_comment, :visible => false)
auth_header = bearer_authorization_header create(:moderator_user), :scopes => %w[write_api]
post changeset_comment_unhide_path(comment), :headers => auth_header
assert_response :success
assert_equal "application/xml", response.media_type
assert_dom "osm", 1 do
assert_dom "> changeset", 1 do
assert_dom "> @id", comment.changeset_id.to_s
assert_dom "> @comments_count", "1"
end
end
assert comment.reload.visible
end
def test_unhide_with_write_api_scope_json
comment = create(:changeset_comment, :visible => false)
auth_header = bearer_authorization_header create(:moderator_user), :scopes => %w[write_api]
post changeset_comment_unhide_path(comment, :format => "json"), :headers => auth_header
assert_response :success
js = ActiveSupport::JSON.decode(@response.body)
assert_equal "application/json", response.media_type
assert_not_nil js["changeset"]
assert_equal comment.changeset_id, js["changeset"]["id"]
assert_equal 1, js["changeset"]["comments_count"]
assert comment.reload.visible
end
private
##