flash_test.rb 9.4 KB
Newer Older
1
require 'abstract_unit'
2
require 'active_support/key_generator'
D
Initial  
David Heinemeier Hansson 已提交
3

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

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

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

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

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

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

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

55 56
    # methods for test_sweep_after_halted_action_chain
    before_action :halt_and_redir, only: 'filter_halting_action'
57 58 59

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

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

77 78 79
    def redirect_with_notice
      redirect_to '/somewhere', :notice => "Good luck in the somewheres!"
    end
80

81 82 83 84 85 86 87 88 89 90
    def render_with_flash_now_alert
      flash.now.alert = "Beware the nowheres now!"
      render :inline => "hello"
    end

    def render_with_flash_now_notice
      flash.now.notice = "Good luck in the somewheres now!"
      render :inline => "hello"
    end

91 92 93
    def redirect_with_other_flashes
      redirect_to '/wonderland', :flash => { :joyride => "Horses!" }
    end
K
kennyj 已提交
94 95 96 97

    def redirect_with_foo_flash
      redirect_to "/wonderland", :foo => 'for great justice'
    end
D
Initial  
David Heinemeier Hansson 已提交
98 99
  end

100
  tests TestController
D
Initial  
David Heinemeier Hansson 已提交
101 102

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

D
David Heinemeier Hansson 已提交
105
    get :use_flash
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_nil assigns["flash_copy"]["that"], "On second flash"
D
Initial  
David Heinemeier Hansson 已提交
111 112 113
  end

  def test_keep_flash
D
David Heinemeier Hansson 已提交
114
    get :set_flash
115

116
    get :use_flash_and_keep_it
117 118
    assert_equal "hello", assigns["flash_copy"]["that"]
    assert_equal "hello", assigns["flashy"]
D
Initial  
David Heinemeier Hansson 已提交
119

D
David Heinemeier Hansson 已提交
120
    get :use_flash
121
    assert_equal "hello", assigns["flash_copy"]["that"], "On second flash"
D
Initial  
David Heinemeier Hansson 已提交
122

D
David Heinemeier Hansson 已提交
123
    get :use_flash
124
    assert_nil assigns["flash_copy"]["that"], "On third flash"
D
Initial  
David Heinemeier Hansson 已提交
125
  end
126

127
  def test_flash_now
D
David Heinemeier Hansson 已提交
128
    get :set_flash_now
129 130 131
    assert_equal "hello", assigns["flash_copy"]["that"]
    assert_equal "bar"  , assigns["flash_copy"]["foo"]
    assert_equal "hello", assigns["flashy"]
132

D
David Heinemeier Hansson 已提交
133
    get :attempt_to_use_flash_now
134 135 136
    assert_nil assigns["flash_copy"]["that"]
    assert_nil assigns["flash_copy"]["foo"]
    assert_nil assigns["flashy"]
137 138
  end

139 140 141
  def test_update_flash
    get :set_flash
    get :use_flash_and_update_it
142 143
    assert_equal "hello",       assigns["flash_copy"]["that"]
    assert_equal "hello again", assigns["flash_copy"]["this"]
144
    get :use_flash
145 146
    assert_nil                  assigns["flash_copy"]["that"], "On second flash"
    assert_equal "hello again", assigns["flash_copy"]["this"], "On second flash"
147
  end
148

149 150
  def test_flash_after_reset_session
    get :use_flash_after_reset_session
151 152 153
    assert_equal "hello",    assigns["flashy_that"]
    assert_equal "good-bye", assigns["flashy_this"]
    assert_nil   assigns["flashy_that_reset"]
154
  end
155

156 157 158 159 160
  def test_does_not_set_the_session_if_the_flash_is_empty
    get :std_action
    assert_nil session["flash"]
  end

161
  def test_sweep_after_halted_action_chain
162
    get :std_action
163
    assert_nil assigns["flash_copy"]["foo"]
164
    get :filter_halting_action
165
    assert_equal "bar", assigns["flash_copy"]["foo"]
166
    get :std_action # follow redirection
167
    assert_equal "bar", assigns["flash_copy"]["foo"]
168
    get :std_action
169
    assert_nil assigns["flash_copy"]["foo"]
170
  end
171 172

  def test_keep_and_discard_return_values
J
Joshua Peek 已提交
173
    flash = ActionDispatch::Flash::FlashHash.new
174 175 176
    flash.update(:foo => :foo_indeed, :bar => :bar_indeed)

    assert_equal(:foo_indeed, flash.discard(:foo)) # valid key passed
A
Akshay Vishnoi 已提交
177
    assert_nil flash.discard(:unknown) # non existent key passed
G
Godfrey Chan 已提交
178 179
    assert_equal({"foo" => :foo_indeed, "bar" => :bar_indeed}, flash.discard().to_hash) # nothing passed
    assert_equal({"foo" => :foo_indeed, "bar" => :bar_indeed}, flash.discard(nil).to_hash) # nothing passed
180 181

    assert_equal(:foo_indeed, flash.keep(:foo)) # valid key passed
A
Akshay Vishnoi 已提交
182
    assert_nil flash.keep(:unknown) # non existent key passed
G
Godfrey Chan 已提交
183 184
    assert_equal({"foo" => :foo_indeed, "bar" => :bar_indeed}, flash.keep().to_hash) # nothing passed
    assert_equal({"foo" => :foo_indeed, "bar" => :bar_indeed}, flash.keep(nil).to_hash) # nothing passed
185
  end
186 187 188 189 190

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

192 193 194 195
  def test_redirect_to_with_notice
    get :redirect_with_notice
    assert_equal "Good luck in the somewheres!", @controller.send(:flash)[:notice]
  end
196

197 198 199 200 201 202 203 204 205 206
  def test_render_with_flash_now_alert
    get :render_with_flash_now_alert
    assert_equal "Beware the nowheres now!", @controller.send(:flash)[:alert]
  end

  def test_render_with_flash_now_notice
    get :render_with_flash_now_notice
    assert_equal "Good luck in the somewheres now!", @controller.send(:flash)[:notice]
  end

207 208 209 210
  def test_redirect_to_with_other_flashes
    get :redirect_with_other_flashes
    assert_equal "Horses!", @controller.send(:flash)[:joyride]
  end
K
kennyj 已提交
211 212

  def test_redirect_to_with_adding_flash_types
213 214 215 216 217
    original_controller = @controller
    test_controller_with_flash_type_foo = Class.new(TestController) do
      add_flash_types :foo
    end
    @controller = test_controller_with_flash_type_foo.new
K
kennyj 已提交
218 219
    get :redirect_with_foo_flash
    assert_equal "for great justice", @controller.send(:flash)[:foo]
220 221
  ensure
    @controller = original_controller
K
kennyj 已提交
222
  end
223 224

  def test_add_flash_type_to_subclasses
225 226 227 228 229
    test_controller_with_flash_type_foo = Class.new(TestController) do
      add_flash_types :foo
    end
    subclass_controller_with_no_flash_type = Class.new(test_controller_with_flash_type_foo)
    assert subclass_controller_with_no_flash_type._flash_types.include?(:foo)
230 231 232
  end

  def test_do_not_add_flash_type_to_parent_class
233 234 235
    subclass_controller_with_flash_type_bar = Class.new(TestController) do
      add_flash_types :bar
    end
236 237
    assert_not TestController._flash_types.include?(:bar)
  end
J
Joshua Peek 已提交
238 239
end

240
class FlashIntegrationTest < ActionDispatch::IntegrationTest
J
Joshua Peek 已提交
241
  SessionKey = '_myapp_session'
242
  Generator  = ActiveSupport::LegacyKeyGenerator.new('b3c631c314c0bbca50c1b2843150fe33')
J
Joshua Peek 已提交
243 244

  class TestController < ActionController::Base
K
kennyj 已提交
245 246
    add_flash_types :bar

J
Joshua Peek 已提交
247 248 249 250 251
    def set_flash
      flash["that"] = "hello"
      head :ok
    end

252 253 254 255 256
    def set_flash_now
      flash.now["that"] = "hello"
      head :ok
    end

J
Joshua Peek 已提交
257 258 259
    def use_flash
      render :inline => "flash: #{flash["that"]}"
    end
K
kennyj 已提交
260 261 262 263 264

    def set_bar
      flash[:bar] = "for great justice"
      head :ok
    end
J
Joshua Peek 已提交
265 266 267 268 269 270 271 272 273 274 275 276 277 278
  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

279 280 281 282 283 284 285 286 287
  def test_just_using_flash_does_not_stream_a_cookie_back
    with_test_route_set do
      get '/use_flash'
      assert_response :success
      assert_nil @response.headers["Set-Cookie"]
      assert_equal "flash: ", @response.body
    end
  end

288 289 290 291 292 293 294 295
  def test_setting_flash_does_not_raise_in_following_requests
    with_test_route_set do
      env = { 'action_dispatch.request.flash_hash' => ActionDispatch::Flash::FlashHash.new }
      get '/set_flash', nil, env
      get '/set_flash', nil, env
    end
  end

296 297 298 299 300 301 302 303
  def test_setting_flash_now_does_not_raise_in_following_requests
    with_test_route_set do
      env = { 'action_dispatch.request.flash_hash' => ActionDispatch::Flash::FlashHash.new }
      get '/set_flash_now', nil, env
      get '/set_flash_now', nil, env
    end
  end

K
kennyj 已提交
304 305 306 307 308 309 310 311
  def test_added_flash_types_method
    with_test_route_set do
      get '/set_bar'
      assert_response :success
      assert_equal 'for great justice', @controller.bar
    end
  end

J
Joshua Peek 已提交
312
  private
313 314 315

    # Overwrite get to send SessionSecret in env hash
    def get(path, parameters = nil, env = {})
316
      env["action_dispatch.key_generator"] ||= Generator
317 318 319
      super
    end

J
Joshua Peek 已提交
320 321
    def with_test_route_set
      with_routing do |set|
322
        set.draw do
323
          get ':action', :to => FlashIntegrationTest::TestController
324 325 326 327 328 329
        end

        @app = self.class.build_app(set) do |middleware|
          middleware.use ActionDispatch::Session::CookieStore, :key => SessionKey
          middleware.use ActionDispatch::Flash
          middleware.delete "ActionDispatch::ShowExceptions"
J
Joshua Peek 已提交
330
        end
331

J
Joshua Peek 已提交
332 333 334 335
        yield
      end
    end
end