flash_test.rb 11.4 KB
Newer Older
1 2
# frozen_string_literal: true

3
require "abstract_unit"
4
require "active_support/messages/rotation_configuration"
D
Initial  
David Heinemeier Hansson 已提交
5

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

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

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

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

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

41 42 43
    def use_flash_and_update_it
      flash.update("this" => "hello again")
      @flash_copy = {}.update flash
44
      render inline: "hello"
45
    end
D
Initial  
David Heinemeier Hansson 已提交
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"]
54
      render inline: "hello"
55 56
    end

57
    # methods for test_sweep_after_halted_action_chain
58
    before_action :halt_and_redir, only: "filter_halting_action"
59 60 61

    def std_action
      @flash_copy = {}.update(flash)
62
      head :ok
63 64 65 66 67 68 69 70
    end

    def filter_halting_action
      @flash_copy = {}.update(flash)
    end

    def halt_and_redir
      flash["foo"] = "bar"
71
      redirect_to action: "std_action"
72 73
      @flash_copy = {}.update(flash)
    end
74 75

    def redirect_with_alert
76
      redirect_to "/nowhere", alert: "Beware the nowheres!"
77
    end
78

79
    def redirect_with_notice
80
      redirect_to "/somewhere", notice: "Good luck in the somewheres!"
81
    end
82

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

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

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

    def redirect_with_foo_flash
98
      redirect_to "/wonderland", foo: "for great justice"
K
kennyj 已提交
99
    end
D
Initial  
David Heinemeier Hansson 已提交
100 101
  end

102
  tests TestController
D
Initial  
David Heinemeier Hansson 已提交
103 104

  def test_flash
D
David Heinemeier Hansson 已提交
105
    get :set_flash
D
Initial  
David Heinemeier Hansson 已提交
106

D
David Heinemeier Hansson 已提交
107
    get :use_flash
108 109
    assert_equal "hello", @controller.instance_variable_get(:@flash_copy)["that"]
    assert_equal "hello", @controller.instance_variable_get(:@flashy)
D
Initial  
David Heinemeier Hansson 已提交
110

D
David Heinemeier Hansson 已提交
111
    get :use_flash
112
    assert_nil @controller.instance_variable_get(:@flash_copy)["that"], "On second flash"
D
Initial  
David Heinemeier Hansson 已提交
113 114 115
  end

  def test_keep_flash
D
David Heinemeier Hansson 已提交
116
    get :set_flash
117

118
    get :use_flash_and_keep_it
119 120
    assert_equal "hello", @controller.instance_variable_get(:@flash_copy)["that"]
    assert_equal "hello", @controller.instance_variable_get(:@flashy)
D
Initial  
David Heinemeier Hansson 已提交
121

D
David Heinemeier Hansson 已提交
122
    get :use_flash
123
    assert_equal "hello", @controller.instance_variable_get(:@flash_copy)["that"], "On second flash"
D
Initial  
David Heinemeier Hansson 已提交
124

D
David Heinemeier Hansson 已提交
125
    get :use_flash
126
    assert_nil @controller.instance_variable_get(:@flash_copy)["that"], "On third flash"
D
Initial  
David Heinemeier Hansson 已提交
127
  end
128

129
  def test_flash_now
D
David Heinemeier Hansson 已提交
130
    get :set_flash_now
131 132 133
    assert_equal "hello", @controller.instance_variable_get(:@flash_copy)["that"]
    assert_equal "bar", @controller.instance_variable_get(:@flash_copy)["foo"]
    assert_equal "hello", @controller.instance_variable_get(:@flashy)
134

D
David Heinemeier Hansson 已提交
135
    get :attempt_to_use_flash_now
136 137 138
    assert_nil @controller.instance_variable_get(:@flash_copy)["that"]
    assert_nil @controller.instance_variable_get(:@flash_copy)["foo"]
    assert_nil @controller.instance_variable_get(:@flashy)
139 140
  end

141 142 143
  def test_update_flash
    get :set_flash
    get :use_flash_and_update_it
144 145
    assert_equal "hello", @controller.instance_variable_get(:@flash_copy)["that"]
    assert_equal "hello again", @controller.instance_variable_get(:@flash_copy)["this"]
146
    get :use_flash
147 148 149
    assert_nil @controller.instance_variable_get(:@flash_copy)["that"], "On second flash"
    assert_equal "hello again",
      @controller.instance_variable_get(:@flash_copy)["this"], "On second flash"
150
  end
151

152 153
  def test_flash_after_reset_session
    get :use_flash_after_reset_session
154 155 156
    assert_equal "hello", @controller.instance_variable_get(:@flashy_that)
    assert_equal "good-bye", @controller.instance_variable_get(:@flashy_this)
    assert_nil @controller.instance_variable_get(:@flashy_that_reset)
157
  end
158

159 160 161 162 163
  def test_does_not_set_the_session_if_the_flash_is_empty
    get :std_action
    assert_nil session["flash"]
  end

164
  def test_sweep_after_halted_action_chain
165
    get :std_action
166
    assert_nil @controller.instance_variable_get(:@flash_copy)["foo"]
167
    get :filter_halting_action
168
    assert_equal "bar", @controller.instance_variable_get(:@flash_copy)["foo"]
169
    get :std_action # follow redirection
170
    assert_equal "bar", @controller.instance_variable_get(:@flash_copy)["foo"]
171
    get :std_action
172
    assert_nil @controller.instance_variable_get(:@flash_copy)["foo"]
173
  end
174 175

  def test_keep_and_discard_return_values
J
Joshua Peek 已提交
176
    flash = ActionDispatch::Flash::FlashHash.new
177
    flash.update(foo: :foo_indeed, bar: :bar_indeed)
178 179

    assert_equal(:foo_indeed, flash.discard(:foo)) # valid key passed
A
Akshay Vishnoi 已提交
180
    assert_nil flash.discard(:unknown) # non existent key passed
181 182
    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
183 184

    assert_equal(:foo_indeed, flash.keep(:foo)) # valid key passed
A
Akshay Vishnoi 已提交
185
    assert_nil flash.keep(:unknown) # non existent key passed
186 187
    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
188
  end
189 190 191

  def test_redirect_to_with_alert
    get :redirect_with_alert
192
    assert_equal "Beware the nowheres!", @controller.flash[:alert]
193
  end
194

195 196
  def test_redirect_to_with_notice
    get :redirect_with_notice
197
    assert_equal "Good luck in the somewheres!", @controller.flash[:notice]
198
  end
199

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

  def test_render_with_flash_now_notice
    get :render_with_flash_now_notice
207
    assert_equal "Good luck in the somewheres now!", @controller.flash[:notice]
208 209
  end

210 211
  def test_redirect_to_with_other_flashes
    get :redirect_with_other_flashes
212
    assert_equal "Horses!", @controller.flash[:joyride]
213
  end
K
kennyj 已提交
214 215

  def test_redirect_to_with_adding_flash_types
216 217 218 219 220
    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 已提交
221
    get :redirect_with_foo_flash
222
    assert_equal "for great justice", @controller.flash[:foo]
223 224
  ensure
    @controller = original_controller
K
kennyj 已提交
225
  end
226 227

  def test_add_flash_type_to_subclasses
228 229 230 231
    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)
232
    assert_includes subclass_controller_with_no_flash_type._flash_types, :foo
233 234
  end

235 236
  def test_does_not_add_flash_type_to_parent_class
    Class.new(TestController) do
237 238
      add_flash_types :bar
    end
239 240
    assert_not TestController._flash_types.include?(:bar)
  end
J
Joshua Peek 已提交
241 242
end

243
class FlashIntegrationTest < ActionDispatch::IntegrationTest
244
  SessionKey = "_myapp_session"
245 246 247 248 249
  Generator = ActiveSupport::CachingKeyGenerator.new(
    ActiveSupport::KeyGenerator.new("b3c631c314c0bbca50c1b2843150fe33", iterations: 1000)
 )
  Rotations = ActiveSupport::Messages::RotationConfiguration.new
  SIGNED_COOKIE_SALT = "signed cookie"
J
Joshua Peek 已提交
250 251

  class TestController < ActionController::Base
K
kennyj 已提交
252 253
    add_flash_types :bar

J
Joshua Peek 已提交
254 255 256 257 258
    def set_flash
      flash["that"] = "hello"
      head :ok
    end

259 260 261 262 263
    def set_flash_now
      flash.now["that"] = "hello"
      head :ok
    end

J
Joshua Peek 已提交
264
    def use_flash
265
      render inline: "flash: #{flash["that"]}"
J
Joshua Peek 已提交
266
    end
K
kennyj 已提交
267 268 269 270 271

    def set_bar
      flash[:bar] = "for great justice"
      head :ok
    end
272 273 274 275 276 277 278

    def set_flash_optionally
      flash.now.notice = params[:flash]
      if stale? etag: "abe"
        render inline: "maybe flash"
      end
    end
J
Joshua Peek 已提交
279 280 281 282
  end

  def test_flash
    with_test_route_set do
283
      get "/set_flash"
J
Joshua Peek 已提交
284 285 286
      assert_response :success
      assert_equal "hello", @request.flash["that"]

287
      get "/use_flash"
J
Joshua Peek 已提交
288 289 290 291 292
      assert_response :success
      assert_equal "flash: hello", @response.body
    end
  end

293 294
  def test_just_using_flash_does_not_stream_a_cookie_back
    with_test_route_set do
295
      get "/use_flash"
296 297 298 299 300 301
      assert_response :success
      assert_nil @response.headers["Set-Cookie"]
      assert_equal "flash: ", @response.body
    end
  end

302 303
  def test_setting_flash_does_not_raise_in_following_requests
    with_test_route_set do
304 305 306
      env = { "action_dispatch.request.flash_hash" => ActionDispatch::Flash::FlashHash.new }
      get "/set_flash", env: env
      get "/set_flash", env: env
307 308 309
    end
  end

310 311
  def test_setting_flash_now_does_not_raise_in_following_requests
    with_test_route_set do
312 313 314
      env = { "action_dispatch.request.flash_hash" => ActionDispatch::Flash::FlashHash.new }
      get "/set_flash_now", env: env
      get "/set_flash_now", env: env
315 316 317
    end
  end

K
kennyj 已提交
318 319
  def test_added_flash_types_method
    with_test_route_set do
320
      get "/set_bar"
K
kennyj 已提交
321
      assert_response :success
322
      assert_equal "for great justice", @controller.bar
K
kennyj 已提交
323 324 325
    end
  end

326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347
  def test_flash_factored_into_etag
    with_test_route_set do
      get "/set_flash_optionally"
      no_flash_etag = response.etag

      get "/set_flash_optionally", params: { flash: "hello!" }
      hello_flash_etag = response.etag

      assert_not_equal no_flash_etag, hello_flash_etag

      get "/set_flash_optionally", params: { flash: "hello!" }
      another_hello_flash_etag = response.etag

      assert_equal another_hello_flash_etag, hello_flash_etag

      get "/set_flash_optionally", params: { flash: "goodbye!" }
      goodbye_flash_etag = response.etag

      assert_not_equal another_hello_flash_etag, goodbye_flash_etag
    end
  end

348
  def test_flash_usable_in_metal_without_helper
349 350
    controller_class = nil

351
    assert_nothing_raised do
352
      controller_class = Class.new(ActionController::Metal) do
353 354 355
        include ActionController::Flash
      end
    end
356 357 358 359 360

    controller = controller_class.new

    assert_respond_to controller, :alert
    assert_respond_to controller, :notice
361 362
  end

J
Joshua Peek 已提交
363
  private
364
    # Overwrite get to send SessionSecret in env hash
A
Akira Matsuda 已提交
365 366 367 368 369 370
    def get(path, **options)
      options[:env] ||= {}
      options[:env]["action_dispatch.key_generator"] ||= Generator
      options[:env]["action_dispatch.cookies_rotations"] = Rotations
      options[:env]["action_dispatch.signed_cookie_salt"] = SIGNED_COOKIE_SALT
      super(path, **options)
371 372
    end

J
Joshua Peek 已提交
373 374
    def with_test_route_set
      with_routing do |set|
375
        set.draw do
376
          ActiveSupport::Deprecation.silence do
377
            get ":action", to: FlashIntegrationTest::TestController
378
          end
379 380 381
        end

        @app = self.class.build_app(set) do |middleware|
382
          middleware.use ActionDispatch::Session::CookieStore, key: SessionKey
383
          middleware.use ActionDispatch::Flash
384
          middleware.delete ActionDispatch::ShowExceptions
J
Joshua Peek 已提交
385
        end
386

J
Joshua Peek 已提交
387 388 389 390
        yield
      end
    end
end