flash_test.rb 6.3 KB
Newer Older
1
require 'abstract_unit'
D
Initial  
David Heinemeier Hansson 已提交
2

3
class FlashTest < ActionController::TestCase
D
Initial  
David Heinemeier Hansson 已提交
4 5 6
  class TestController < ActionController::Base
    def set_flash
      flash["that"] = "hello"
7
      render :inline => "hello"
D
Initial  
David Heinemeier Hansson 已提交
8 9
    end

10 11
    def set_flash_now
      flash.now["that"] = "hello"
12 13 14
      flash.now["foo"] ||= "bar"
      flash.now["foo"] ||= "err"
      @flashy = flash.now["that"]
15
      @flash_copy = {}.update flash
16
      render :inline => "hello"
17 18 19 20 21
    end

    def attempt_to_use_flash_now
      @flash_copy = {}.update flash
      @flashy = flash["that"]
22
      render :inline => "hello"
23 24
    end

D
Initial  
David Heinemeier Hansson 已提交
25
    def use_flash
26
      @flash_copy = {}.update flash
D
Initial  
David Heinemeier Hansson 已提交
27
      @flashy = flash["that"]
28
      render :inline => "hello"
D
Initial  
David Heinemeier Hansson 已提交
29 30 31
    end

    def use_flash_and_keep_it
32
      @flash_copy = {}.update flash
D
Initial  
David Heinemeier Hansson 已提交
33
      @flashy = flash["that"]
34
      flash.keep
35
      render :inline => "hello"
D
Initial  
David Heinemeier Hansson 已提交
36
    end
37

38 39 40 41 42
    def use_flash_and_update_it
      flash.update("this" => "hello again")
      @flash_copy = {}.update flash
      render :inline => "hello"
    end
D
Initial  
David Heinemeier Hansson 已提交
43

44 45 46 47 48 49 50 51 52 53
    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

D
Initial  
David Heinemeier Hansson 已提交
54
    def rescue_action(e)
55
      raise unless ActionView::MissingTemplate === e
D
Initial  
David Heinemeier Hansson 已提交
56
    end
57 58 59 60 61 62

    # methods for test_sweep_after_halted_filter_chain
    before_filter :halt_and_redir, :only => "filter_halting_action"

    def std_action
      @flash_copy = {}.update(flash)
63
      render :nothing => true
64 65 66 67 68 69 70 71 72 73 74
    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
75 76 77 78

    def redirect_with_alert
      redirect_to '/nowhere', :alert => "Beware the nowheres!"
    end
79

80 81 82
    def redirect_with_notice
      redirect_to '/somewhere', :notice => "Good luck in the somewheres!"
    end
83

84 85 86
    def redirect_with_other_flashes
      redirect_to '/wonderland', :flash => { :joyride => "Horses!" }
    end
D
Initial  
David Heinemeier Hansson 已提交
87 88
  end

89
  tests TestController
D
Initial  
David Heinemeier Hansson 已提交
90 91

  def test_flash
D
David Heinemeier Hansson 已提交
92
    get :set_flash
D
Initial  
David Heinemeier Hansson 已提交
93

D
David Heinemeier Hansson 已提交
94
    get :use_flash
95 96
    assert_equal "hello", assigns["flash_copy"]["that"]
    assert_equal "hello", assigns["flashy"]
D
Initial  
David Heinemeier Hansson 已提交
97

D
David Heinemeier Hansson 已提交
98
    get :use_flash
99
    assert_nil assigns["flash_copy"]["that"], "On second flash"
D
Initial  
David Heinemeier Hansson 已提交
100 101 102
  end

  def test_keep_flash
D
David Heinemeier Hansson 已提交
103
    get :set_flash
104

105
    get :use_flash_and_keep_it
106 107
    assert_equal "hello", assigns["flash_copy"]["that"]
    assert_equal "hello", assigns["flashy"]
D
Initial  
David Heinemeier Hansson 已提交
108

D
David Heinemeier Hansson 已提交
109
    get :use_flash
110
    assert_equal "hello", assigns["flash_copy"]["that"], "On second flash"
D
Initial  
David Heinemeier Hansson 已提交
111

D
David Heinemeier Hansson 已提交
112
    get :use_flash
113
    assert_nil assigns["flash_copy"]["that"], "On third flash"
D
Initial  
David Heinemeier Hansson 已提交
114
  end
115

116
  def test_flash_now
D
David Heinemeier Hansson 已提交
117
    get :set_flash_now
118 119 120
    assert_equal "hello", assigns["flash_copy"]["that"]
    assert_equal "bar"  , assigns["flash_copy"]["foo"]
    assert_equal "hello", assigns["flashy"]
121

D
David Heinemeier Hansson 已提交
122
    get :attempt_to_use_flash_now
123 124 125
    assert_nil assigns["flash_copy"]["that"]
    assert_nil assigns["flash_copy"]["foo"]
    assert_nil assigns["flashy"]
126 127
  end

128 129 130
  def test_update_flash
    get :set_flash
    get :use_flash_and_update_it
131 132
    assert_equal "hello",       assigns["flash_copy"]["that"]
    assert_equal "hello again", assigns["flash_copy"]["this"]
133
    get :use_flash
134 135
    assert_nil                  assigns["flash_copy"]["that"], "On second flash"
    assert_equal "hello again", assigns["flash_copy"]["this"], "On second flash"
136
  end
137

138 139
  def test_flash_after_reset_session
    get :use_flash_after_reset_session
140 141 142
    assert_equal "hello",    assigns["flashy_that"]
    assert_equal "good-bye", assigns["flashy_this"]
    assert_nil   assigns["flashy_that_reset"]
143
  end
144

145 146 147 148 149
  def test_does_not_set_the_session_if_the_flash_is_empty
    get :std_action
    assert_nil session["flash"]
  end

150 151
  def test_sweep_after_halted_filter_chain
    get :std_action
152
    assert_nil assigns["flash_copy"]["foo"]
153
    get :filter_halting_action
154
    assert_equal "bar", assigns["flash_copy"]["foo"]
155
    get :std_action # follow redirection
156
    assert_equal "bar", assigns["flash_copy"]["foo"]
157
    get :std_action
158
    assert_nil assigns["flash_copy"]["foo"]
159
  end
160 161

  def test_keep_and_discard_return_values
J
Joshua Peek 已提交
162
    flash = ActionDispatch::Flash::FlashHash.new
163 164 165 166 167
    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
168
    assert_equal({:foo => :foo_indeed, :bar => :bar_indeed}, flash.discard(nil)) # nothing passed
169 170 171 172

    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
173
    assert_equal({:foo => :foo_indeed, :bar => :bar_indeed}, flash.keep(nil)) # nothing passed
174
  end
175 176 177 178 179

  def test_redirect_to_with_alert
    get :redirect_with_alert
    assert_equal "Beware the nowheres!", @controller.send(:flash)[:alert]
  end
180

181 182 183 184
  def test_redirect_to_with_notice
    get :redirect_with_notice
    assert_equal "Good luck in the somewheres!", @controller.send(:flash)[:notice]
  end
185

186 187 188 189
  def test_redirect_to_with_other_flashes
    get :redirect_with_other_flashes
    assert_equal "Horses!", @controller.send(:flash)[:joyride]
  end
J
Joshua Peek 已提交
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
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|
223
          match ':action', :to => ActionDispatch::Session::CookieStore.new(FlashIntegrationTest::TestController, :key => FlashIntegrationTest::SessionKey, :secret => FlashIntegrationTest::SessionSecret)
J
Joshua Peek 已提交
224 225 226 227 228
        end
        yield
      end
    end
end