From 36592f72bca5e5670c01c2d259c3139e6eae54ad Mon Sep 17 00:00:00 2001 From: Tom Hughes Date: Tue, 2 Dec 2014 21:29:12 +0000 Subject: [PATCH] Fix exception when message limit is hit --- app/controllers/message_controller.rb | 6 ++-- test/controllers/message_controller_test.rb | 33 +++++++++++++++++++++ 2 files changed, 36 insertions(+), 3 deletions(-) diff --git a/app/controllers/message_controller.rb b/app/controllers/message_controller.rb index fd638e4e7..4d53943ed 100644 --- a/app/controllers/message_controller.rb +++ b/app/controllers/message_controller.rb @@ -28,10 +28,10 @@ class MessageController < ApplicationController redirect_to :controller => 'message', :action => 'inbox', :display_name => @user.display_name end end - else - @message = Message.new(:recipient => @this_user) - @title = t 'message.new.title' end + + @message ||= Message.new(:recipient => @this_user) + @title = t 'message.new.title' end # Allow the user to reply to another message. diff --git a/test/controllers/message_controller_test.rb b/test/controllers/message_controller_test.rb index 44271f5dd..1d8416880 100644 --- a/test/controllers/message_controller_test.rb +++ b/test/controllers/message_controller_test.rb @@ -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 "p.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 -- 2.43.2