2008-01-05 08:32:06 -05:00
|
|
|
require 'abstract_unit'
|
2004-11-23 20:04:44 -05:00
|
|
|
|
2009-01-07 16:23:10 -05:00
|
|
|
class FlashTest < ActionController::TestCase
|
2004-11-23 20:04:44 -05:00
|
|
|
class TestController < ActionController::Base
|
|
|
|
def set_flash
|
|
|
|
flash["that"] = "hello"
|
2005-07-24 12:45:39 -04:00
|
|
|
render :inline => "hello"
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
|
|
|
|
2005-03-20 14:12:53 -05:00
|
|
|
def set_flash_now
|
|
|
|
flash.now["that"] = "hello"
|
2005-10-26 09:14:10 -04:00
|
|
|
flash.now["foo"] ||= "bar"
|
|
|
|
flash.now["foo"] ||= "err"
|
|
|
|
@flashy = flash.now["that"]
|
2005-03-20 14:12:53 -05:00
|
|
|
@flash_copy = {}.update flash
|
2005-07-24 12:45:39 -04:00
|
|
|
render :inline => "hello"
|
2005-03-20 14:12:53 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def attempt_to_use_flash_now
|
|
|
|
@flash_copy = {}.update flash
|
|
|
|
@flashy = flash["that"]
|
2005-07-24 12:45:39 -04:00
|
|
|
render :inline => "hello"
|
2005-03-20 14:12:53 -05:00
|
|
|
end
|
|
|
|
|
2004-11-23 20:04:44 -05:00
|
|
|
def use_flash
|
2005-03-20 14:12:53 -05:00
|
|
|
@flash_copy = {}.update flash
|
2004-11-23 20:04:44 -05:00
|
|
|
@flashy = flash["that"]
|
2005-07-24 12:45:39 -04:00
|
|
|
render :inline => "hello"
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def use_flash_and_keep_it
|
2005-03-20 14:12:53 -05:00
|
|
|
@flash_copy = {}.update flash
|
2004-11-23 20:04:44 -05:00
|
|
|
@flashy = flash["that"]
|
2007-09-09 17:54:59 -04:00
|
|
|
flash.keep
|
2005-07-24 12:45:39 -04:00
|
|
|
render :inline => "hello"
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
2009-12-21 18:29:59 -05:00
|
|
|
|
2007-05-06 00:17:01 -04:00
|
|
|
def use_flash_and_update_it
|
|
|
|
flash.update("this" => "hello again")
|
|
|
|
@flash_copy = {}.update flash
|
|
|
|
render :inline => "hello"
|
|
|
|
end
|
2004-11-23 20:04:44 -05:00
|
|
|
|
2006-07-21 22:31:21 -04:00
|
|
|
def use_flash_after_reset_session
|
|
|
|
flash["that"] = "hello"
|
|
|
|
@flashy_that = flash["that"]
|
|
|
|
reset_session
|
|
|
|
@flashy_that_reset = flash["that"]
|
|
|
|
flash["this"] = "good-bye"
|
|
|
|
@flashy_this = flash["this"]
|
|
|
|
render :inline => "hello"
|
|
|
|
end
|
|
|
|
|
2004-11-23 20:04:44 -05:00
|
|
|
def rescue_action(e)
|
2008-04-19 13:52:14 -04:00
|
|
|
raise unless ActionView::MissingTemplate === e
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
2007-05-06 00:17:01 -04:00
|
|
|
|
|
|
|
# methods for test_sweep_after_halted_filter_chain
|
|
|
|
before_filter :halt_and_redir, :only => "filter_halting_action"
|
|
|
|
|
|
|
|
def std_action
|
|
|
|
@flash_copy = {}.update(flash)
|
2009-05-20 19:38:48 -04:00
|
|
|
render :nothing => true
|
2007-05-06 00:17:01 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def filter_halting_action
|
|
|
|
@flash_copy = {}.update(flash)
|
|
|
|
end
|
|
|
|
|
|
|
|
def halt_and_redir
|
|
|
|
flash["foo"] = "bar"
|
|
|
|
redirect_to :action => "std_action"
|
|
|
|
@flash_copy = {}.update(flash)
|
|
|
|
end
|
2009-12-17 19:37:11 -05:00
|
|
|
|
|
|
|
def redirect_with_alert
|
|
|
|
redirect_to '/nowhere', :alert => "Beware the nowheres!"
|
|
|
|
end
|
2009-12-21 18:29:59 -05:00
|
|
|
|
2009-12-17 19:37:11 -05:00
|
|
|
def redirect_with_notice
|
|
|
|
redirect_to '/somewhere', :notice => "Good luck in the somewheres!"
|
|
|
|
end
|
2009-12-21 18:29:59 -05:00
|
|
|
|
2009-12-17 19:37:11 -05:00
|
|
|
def redirect_with_other_flashes
|
|
|
|
redirect_to '/wonderland', :flash => { :joyride => "Horses!" }
|
|
|
|
end
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
|
|
|
|
2009-01-07 16:23:10 -05:00
|
|
|
tests TestController
|
2004-11-23 20:04:44 -05:00
|
|
|
|
|
|
|
def test_flash
|
2006-02-12 11:29:21 -05:00
|
|
|
get :set_flash
|
2004-11-23 20:04:44 -05:00
|
|
|
|
2006-02-12 11:29:21 -05:00
|
|
|
get :use_flash
|
2009-05-20 19:38:48 -04:00
|
|
|
assert_equal "hello", assigns["flash_copy"]["that"]
|
|
|
|
assert_equal "hello", assigns["flashy"]
|
2004-11-23 20:04:44 -05:00
|
|
|
|
2006-02-12 11:29:21 -05:00
|
|
|
get :use_flash
|
2009-05-20 19:38:48 -04:00
|
|
|
assert_nil assigns["flash_copy"]["that"], "On second flash"
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_keep_flash
|
2006-02-12 11:29:21 -05:00
|
|
|
get :set_flash
|
2009-12-21 18:29:59 -05:00
|
|
|
|
2007-09-09 17:54:59 -04:00
|
|
|
get :use_flash_and_keep_it
|
2009-05-20 19:38:48 -04:00
|
|
|
assert_equal "hello", assigns["flash_copy"]["that"]
|
|
|
|
assert_equal "hello", assigns["flashy"]
|
2004-11-23 20:04:44 -05:00
|
|
|
|
2006-02-12 11:29:21 -05:00
|
|
|
get :use_flash
|
2009-05-20 19:38:48 -04:00
|
|
|
assert_equal "hello", assigns["flash_copy"]["that"], "On second flash"
|
2004-11-23 20:04:44 -05:00
|
|
|
|
2006-02-12 11:29:21 -05:00
|
|
|
get :use_flash
|
2009-05-20 19:38:48 -04:00
|
|
|
assert_nil assigns["flash_copy"]["that"], "On third flash"
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
2009-12-21 18:29:59 -05:00
|
|
|
|
2005-03-20 14:12:53 -05:00
|
|
|
def test_flash_now
|
2006-02-12 11:29:21 -05:00
|
|
|
get :set_flash_now
|
2009-05-20 19:38:48 -04:00
|
|
|
assert_equal "hello", assigns["flash_copy"]["that"]
|
|
|
|
assert_equal "bar" , assigns["flash_copy"]["foo"]
|
|
|
|
assert_equal "hello", assigns["flashy"]
|
2005-03-20 14:12:53 -05:00
|
|
|
|
2006-02-12 11:29:21 -05:00
|
|
|
get :attempt_to_use_flash_now
|
2009-05-20 19:38:48 -04:00
|
|
|
assert_nil assigns["flash_copy"]["that"]
|
|
|
|
assert_nil assigns["flash_copy"]["foo"]
|
|
|
|
assert_nil assigns["flashy"]
|
2009-12-21 18:29:59 -05:00
|
|
|
end
|
|
|
|
|
2007-05-06 00:17:01 -04:00
|
|
|
def test_update_flash
|
|
|
|
get :set_flash
|
|
|
|
get :use_flash_and_update_it
|
2009-05-20 19:38:48 -04:00
|
|
|
assert_equal "hello", assigns["flash_copy"]["that"]
|
|
|
|
assert_equal "hello again", assigns["flash_copy"]["this"]
|
2007-05-06 00:17:01 -04:00
|
|
|
get :use_flash
|
2009-05-20 19:38:48 -04:00
|
|
|
assert_nil assigns["flash_copy"]["that"], "On second flash"
|
|
|
|
assert_equal "hello again", assigns["flash_copy"]["this"], "On second flash"
|
2007-05-06 00:17:01 -04:00
|
|
|
end
|
2009-05-28 10:30:49 -04:00
|
|
|
|
2006-07-21 22:31:21 -04:00
|
|
|
def test_flash_after_reset_session
|
|
|
|
get :use_flash_after_reset_session
|
2009-05-20 19:38:48 -04:00
|
|
|
assert_equal "hello", assigns["flashy_that"]
|
|
|
|
assert_equal "good-bye", assigns["flashy_this"]
|
|
|
|
assert_nil assigns["flashy_that_reset"]
|
2009-12-21 18:29:59 -05:00
|
|
|
end
|
2007-05-06 00:17:01 -04:00
|
|
|
|
2009-05-28 10:30:49 -04:00
|
|
|
def test_does_not_set_the_session_if_the_flash_is_empty
|
|
|
|
get :std_action
|
|
|
|
assert_nil session["flash"]
|
|
|
|
end
|
|
|
|
|
2007-05-06 00:17:01 -04:00
|
|
|
def test_sweep_after_halted_filter_chain
|
|
|
|
get :std_action
|
2009-05-20 19:38:48 -04:00
|
|
|
assert_nil assigns["flash_copy"]["foo"]
|
2007-05-06 00:17:01 -04:00
|
|
|
get :filter_halting_action
|
2009-05-20 19:38:48 -04:00
|
|
|
assert_equal "bar", assigns["flash_copy"]["foo"]
|
2007-05-06 00:17:01 -04:00
|
|
|
get :std_action # follow redirection
|
2009-05-20 19:38:48 -04:00
|
|
|
assert_equal "bar", assigns["flash_copy"]["foo"]
|
2007-05-06 00:17:01 -04:00
|
|
|
get :std_action
|
2009-05-20 19:38:48 -04:00
|
|
|
assert_nil assigns["flash_copy"]["foo"]
|
2007-05-06 00:17:01 -04:00
|
|
|
end
|
2009-05-27 15:51:33 -04:00
|
|
|
|
|
|
|
def test_keep_and_discard_return_values
|
2010-01-15 15:44:27 -05:00
|
|
|
flash = ActionDispatch::Flash::FlashHash.new
|
2009-05-27 15:51:33 -04:00
|
|
|
flash.update(:foo => :foo_indeed, :bar => :bar_indeed)
|
|
|
|
|
|
|
|
assert_equal(:foo_indeed, flash.discard(:foo)) # valid key passed
|
|
|
|
assert_nil flash.discard(:unknown) # non existant key passed
|
|
|
|
assert_equal({:foo => :foo_indeed, :bar => :bar_indeed}, flash.discard()) # nothing passed
|
2009-12-21 18:29:59 -05:00
|
|
|
assert_equal({:foo => :foo_indeed, :bar => :bar_indeed}, flash.discard(nil)) # nothing passed
|
2009-05-27 15:51:33 -04:00
|
|
|
|
|
|
|
assert_equal(:foo_indeed, flash.keep(:foo)) # valid key passed
|
|
|
|
assert_nil flash.keep(:unknown) # non existant key passed
|
|
|
|
assert_equal({:foo => :foo_indeed, :bar => :bar_indeed}, flash.keep()) # nothing passed
|
2009-12-21 18:29:59 -05:00
|
|
|
assert_equal({:foo => :foo_indeed, :bar => :bar_indeed}, flash.keep(nil)) # nothing passed
|
2009-05-27 15:51:33 -04:00
|
|
|
end
|
2009-12-17 19:37:11 -05:00
|
|
|
|
|
|
|
def test_redirect_to_with_alert
|
|
|
|
get :redirect_with_alert
|
|
|
|
assert_equal "Beware the nowheres!", @controller.send(:flash)[:alert]
|
|
|
|
end
|
2009-12-21 18:29:59 -05:00
|
|
|
|
2009-12-17 19:37:11 -05:00
|
|
|
def test_redirect_to_with_notice
|
|
|
|
get :redirect_with_notice
|
|
|
|
assert_equal "Good luck in the somewheres!", @controller.send(:flash)[:notice]
|
|
|
|
end
|
2009-12-21 18:29:59 -05:00
|
|
|
|
2009-12-17 19:37:11 -05:00
|
|
|
def test_redirect_to_with_other_flashes
|
|
|
|
get :redirect_with_other_flashes
|
|
|
|
assert_equal "Horses!", @controller.send(:flash)[:joyride]
|
|
|
|
end
|
2010-01-15 15:44:27 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
class FlashIntegrationTest < ActionController::IntegrationTest
|
|
|
|
SessionKey = '_myapp_session'
|
|
|
|
SessionSecret = 'b3c631c314c0bbca50c1b2843150fe33'
|
|
|
|
|
|
|
|
class TestController < ActionController::Base
|
|
|
|
def set_flash
|
|
|
|
flash["that"] = "hello"
|
|
|
|
head :ok
|
|
|
|
end
|
|
|
|
|
|
|
|
def use_flash
|
|
|
|
render :inline => "flash: #{flash["that"]}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_flash
|
|
|
|
with_test_route_set do
|
|
|
|
get '/set_flash'
|
|
|
|
assert_response :success
|
|
|
|
assert_equal "hello", @request.flash["that"]
|
|
|
|
|
|
|
|
get '/use_flash'
|
|
|
|
assert_response :success
|
|
|
|
assert_equal "flash: hello", @response.body
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
def with_test_route_set
|
|
|
|
with_routing do |set|
|
|
|
|
set.draw do |map|
|
2010-01-16 22:39:30 -05:00
|
|
|
match ':action', :to => ActionDispatch::Session::CookieStore.new(FlashIntegrationTest::TestController, :key => FlashIntegrationTest::SessionKey, :secret => FlashIntegrationTest::SessionSecret)
|
2010-01-15 15:44:27 -05:00
|
|
|
end
|
|
|
|
yield
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|