X-Git-Url: https://git.openstreetmap.org./rails.git/blobdiff_plain/7e1a4b68849c5972fc0f86fdff988ffdce60f765..cebda5ffb9bc45171ddc6b4c4a25f14912fdad51:/test/controllers/api/changeset_comments_controller_test.rb diff --git a/test/controllers/api/changeset_comments_controller_test.rb b/test/controllers/api/changeset_comments_controller_test.rb index 64871baa3..e25926c78 100644 --- a/test/controllers/api/changeset_comments_controller_test.rb +++ b/test/controllers/api/changeset_comments_controller_test.rb @@ -9,14 +9,26 @@ module Api { :path => "/api/0.6/changeset/1/comment", :method => :post }, { :controller => "api/changeset_comments", :action => "create", :id => "1" } ) + assert_routing( + { :path => "/api/0.6/changeset/1/comment.json", :method => :post }, + { :controller => "api/changeset_comments", :action => "create", :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 ## @@ -120,6 +132,92 @@ module Api assert_response :bad_request end + ## + # create comment rate limit for new users + def test_create_comment_new_user_rate_limit + changeset = create(:changeset, :closed) + user = create(:user) + + auth_header = basic_authorization_header user.email, "test" + + assert_difference "ChangesetComment.count", Settings.initial_changeset_comments_per_hour do + 1.upto(Settings.initial_changeset_comments_per_hour) do |count| + post changeset_comment_path(:id => changeset, :text => "Comment #{count}"), :headers => auth_header + assert_response :success + end + end + + assert_no_difference "ChangesetComment.count" do + post changeset_comment_path(:id => changeset, :text => "One comment too many"), :headers => auth_header + assert_response :too_many_requests + end + end + + ## + # create comment rate limit for experienced users + def test_create_comment_experienced_user_rate_limit + changeset = create(:changeset, :closed) + user = create(:user) + create_list(:changeset_comment, 200, :author_id => user.id, :created_at => Time.now.utc - 1.day) + + auth_header = basic_authorization_header user.email, "test" + + assert_difference "ChangesetComment.count", Settings.max_changeset_comments_per_hour do + 1.upto(Settings.max_changeset_comments_per_hour) do |count| + post changeset_comment_path(:id => changeset, :text => "Comment #{count}"), :headers => auth_header + assert_response :success + end + end + + assert_no_difference "ChangesetComment.count" do + post changeset_comment_path(:id => changeset, :text => "One comment too many"), :headers => auth_header + assert_response :too_many_requests + end + end + + ## + # create comment rate limit for reported users + def test_create_comment_reported_user_rate_limit + changeset = create(:changeset, :closed) + user = create(:user) + create(:issue_with_reports, :reportable => user, :reported_user => user) + + auth_header = basic_authorization_header user.email, "test" + + assert_difference "ChangesetComment.count", Settings.initial_changeset_comments_per_hour / 2 do + 1.upto(Settings.initial_changeset_comments_per_hour / 2) do |count| + post changeset_comment_path(:id => changeset, :text => "Comment #{count}"), :headers => auth_header + assert_response :success + end + end + + assert_no_difference "ChangesetComment.count" do + post changeset_comment_path(:id => changeset, :text => "One comment too many"), :headers => auth_header + assert_response :too_many_requests + end + end + + ## + # create comment rate limit for moderator users + def test_create_comment_moderator_user_rate_limit + changeset = create(:changeset, :closed) + user = create(:moderator_user) + + auth_header = basic_authorization_header user.email, "test" + + assert_difference "ChangesetComment.count", Settings.moderator_changeset_comments_per_hour do + 1.upto(Settings.moderator_changeset_comments_per_hour) do |count| + post changeset_comment_path(:id => changeset, :text => "Comment #{count}"), :headers => auth_header + assert_response :success + end + end + + assert_no_difference "ChangesetComment.count" do + post changeset_comment_path(:id => changeset, :text => "One comment too many"), :headers => auth_header + assert_response :too_many_requests + end + end + ## # test hide comment fail def test_destroy_comment_fail @@ -214,7 +312,7 @@ module Api assert_response :forbidden # Try again, after agreement with the terms - user.terms_agreed = Time.now + user.terms_agreed = Time.now.utc user.save! assert_difference "ChangesetComment.count", 1 do @@ -237,7 +335,7 @@ module Api assert_response :forbidden # Try again, after agreement with the terms - user.terms_agreed = Time.now + user.terms_agreed = Time.now.utc user.save! assert_difference "ChangesetComment.count", 1 do