X-Git-Url: https://git.openstreetmap.org./rails.git/blobdiff_plain/eab2eeaa47b2aa8c7f52371f9bfdd60711a41fb6..dc2a2c8ebd1a11e4a64555fda22c6859a51defff:/test/controllers/message_controller_test.rb?ds=sidebyside diff --git a/test/controllers/message_controller_test.rb b/test/controllers/message_controller_test.rb index 44271f5dd..eb4222bd5 100644 --- a/test/controllers/message_controller_test.rb +++ b/test/controllers/message_controller_test.rb @@ -1,4 +1,4 @@ -require 'test_helper' +require "test_helper" class MessageControllerTest < ActionController::TestCase fixtures :users, :messages @@ -69,8 +69,8 @@ class MessageControllerTest < ActionController::TestCase assert_difference "ActionMailer::Base.deliveries.size", 0 do assert_difference "Message.count", 0 do post :new, - :display_name => users(:public_user).display_name, - :message => { :title => "Test Message", :body => "" } + :display_name => users(:public_user).display_name, + :message => { :title => "Test Message", :body => "" } end end assert_response :success @@ -78,7 +78,7 @@ class MessageControllerTest < ActionController::TestCase assert_select "title", "OpenStreetMap | Send message" assert_select "form[action='#{new_message_path(:display_name => users(:public_user).display_name)}']", :count => 1 do assert_select "input#message_title", :count => 1 do - assert_select "[value=Test Message]" + assert_select "[value='Test Message']" end assert_select "textarea#message_body", :text => "", :count => 1 assert_select "input[type='submit'][value='Send']", :count => 1 @@ -88,8 +88,8 @@ class MessageControllerTest < ActionController::TestCase assert_difference "ActionMailer::Base.deliveries.size", 0 do assert_difference "Message.count", 0 do post :new, - :display_name => users(:public_user).display_name, - :message => { :title => "", :body => "Test message body" } + :display_name => users(:public_user).display_name, + :message => { :title => "", :body => "Test message body" } end end assert_response :success @@ -97,7 +97,7 @@ class MessageControllerTest < ActionController::TestCase assert_select "title", "OpenStreetMap | Send message" assert_select "form[action='#{new_message_path(:display_name => users(:public_user).display_name)}']", :count => 1 do assert_select "input#message_title", :count => 1 do - assert_select "[value=]" + assert_select "[value='']" end assert_select "textarea#message_body", :text => "Test message body", :count => 1 assert_select "input[type='submit'][value='Send']", :count => 1 @@ -107,14 +107,14 @@ class MessageControllerTest < ActionController::TestCase assert_difference "ActionMailer::Base.deliveries.size", 1 do assert_difference "Message.count", 1 do post :new, - :display_name => users(:public_user).display_name, - :message => { :title => "Test Message", :body => "Test message body" } + :display_name => users(:public_user).display_name, + :message => { :title => "Test Message", :body => "Test message body" } end end assert_redirected_to inbox_path(:display_name => users(:normal_user).display_name) assert_equal "Message sent", flash[:notice] e = ActionMailer::Base.deliveries.first - assert_equal [ users(:public_user).email ], e.to + assert_equal [users(:public_user).email], e.to assert_equal "[OpenStreetMap] Test Message", e.subject assert_match /Test message body/, e.text_part.decoded assert_match /Test message body/, e.html_part.decoded @@ -134,6 +134,27 @@ class MessageControllerTest < ActionController::TestCase assert_select "h1", "The user non_existent_user does not exist" end + ## + # test the new action message limit + def test_new_limit + # Login as a normal user + session[:user] = users(:normal_user).id + + # Check that sending a message fails when the message limit is hit + assert_no_difference "ActionMailer::Base.deliveries.size" do + assert_no_difference "Message.count" do + with_message_limit(0) do + post :new, + :display_name => users(:public_user).display_name, + :message => { :title => "Test Message", :body => "Test message body" } + assert_response :success + assert_template "new" + assert_select ".error", /wait a while/ + end + end + end + end + ## # test the reply action def test_reply @@ -362,4 +383,16 @@ class MessageControllerTest < ActionController::TestCase assert_response :not_found assert_template "no_such_message" end + + private + + def with_message_limit(value) + max_messages_per_hour = Object.send("remove_const", "MAX_MESSAGES_PER_HOUR") + Object.const_set("MAX_MESSAGES_PER_HOUR", value) + + yield + + Object.send("remove_const", "MAX_MESSAGES_PER_HOUR") + Object.const_set("MAX_MESSAGES_PER_HOUR", max_messages_per_hour) + end end