From 32b74fe2551ad7e4b5e592bf6efe9b377bce8bbb Mon Sep 17 00:00:00 2001 From: bogdanvlviv Date: Mon, 20 Feb 2017 00:35:14 +0200 Subject: Set correct "routes" in tests cases --- actionpack/test/controller/test_case_test.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'actionpack/test') diff --git a/actionpack/test/controller/test_case_test.rb b/actionpack/test/controller/test_case_test.rb index e084e45373..891ce0e905 100644 --- a/actionpack/test/controller/test_case_test.rb +++ b/actionpack/test/controller/test_case_test.rb @@ -134,7 +134,7 @@ XML end def create - head :created, location: "created resource" + head :created, location: "/resource" end def render_cookie @@ -893,12 +893,12 @@ XML assert_response :created # Redirect url doesn't care that it wasn't a :redirect response. - assert_equal "created resource", @response.redirect_url + assert_equal "/resource", @response.redirect_url assert_equal @response.redirect_url, redirect_to_url # Must be a :redirect response. assert_raise(ActiveSupport::TestCase::Assertion) do - assert_redirected_to "created resource" + assert_redirected_to "/resource" end end -- cgit v1.2.3 From ca7799861982e89d1dfc8a06f369b921edccddfa Mon Sep 17 00:00:00 2001 From: Mehmet Emin INAC Date: Mon, 20 Feb 2017 18:10:28 +0100 Subject: Use `response#location` instead of `#location` in redirect. Closes #28033 --- actionpack/test/controller/redirect_test.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'actionpack/test') diff --git a/actionpack/test/controller/redirect_test.rb b/actionpack/test/controller/redirect_test.rb index e4e968dfdb..f06a1f4d23 100644 --- a/actionpack/test/controller/redirect_test.rb +++ b/actionpack/test/controller/redirect_test.rb @@ -21,8 +21,8 @@ end class RedirectController < ActionController::Base # empty method not used anywhere to ensure methods like # `status` and `location` aren't called on `redirect_to` calls - def status; render plain: "called status"; end - def location; render plain: "called location"; end + def status; raise "Should not be called!"; end + def location; raise "Should not be called!"; end def simple_redirect redirect_to action: "hello_world" -- cgit v1.2.3 From 9730b1dba6d1bb9684a54915ac3735d9c0eade26 Mon Sep 17 00:00:00 2001 From: eileencodes Date: Sat, 27 Aug 2016 16:48:24 -0400 Subject: Add tests for system testing * Adds test case test * Adds driver adapter test * Adds tests for capybara seleium driver (testing the settings not actually opening the browser to test capybara w/ selenium because that would so so so slow) * Adds tests for rack test driver * Adds tests for generators --- actionpack/test/abstract_unit.rb | 1 + .../capybara_rack_test_driver_test.rb | 26 ++++++++++ .../capybara_selenium_driver_test.rb | 58 ++++++++++++++++++++++ .../test/system_testing/driver_adapter_test.rb | 19 +++++++ 4 files changed, 104 insertions(+) create mode 100644 actionpack/test/system_testing/capybara_rack_test_driver_test.rb create mode 100644 actionpack/test/system_testing/capybara_selenium_driver_test.rb create mode 100644 actionpack/test/system_testing/driver_adapter_test.rb (limited to 'actionpack/test') diff --git a/actionpack/test/abstract_unit.rb b/actionpack/test/abstract_unit.rb index 459b0d6c54..3b35af6d3a 100644 --- a/actionpack/test/abstract_unit.rb +++ b/actionpack/test/abstract_unit.rb @@ -33,6 +33,7 @@ require "action_view/testing/resolvers" require "action_dispatch" require "active_support/dependencies" require "active_model" +require "system_test_case" require "pp" # require 'pp' early to prevent hidden_methods from not picking up the pretty-print methods until too late diff --git a/actionpack/test/system_testing/capybara_rack_test_driver_test.rb b/actionpack/test/system_testing/capybara_rack_test_driver_test.rb new file mode 100644 index 0000000000..0f037760d3 --- /dev/null +++ b/actionpack/test/system_testing/capybara_rack_test_driver_test.rb @@ -0,0 +1,26 @@ +require 'abstract_unit' + +class CapybaraRackTestDriverTest < ActiveSupport::TestCase + def test_default_driver_adapter + assert_kind_of SystemTesting::DriverAdapters::CapybaraRackTestDriver, Rails::SystemTestCase.driver + end + + def test_default_settings + assert_equal 'Capybara', Rails::SystemTestCase.driver.useragent + end + + def test_setting_useragent + Rails::SystemTestCase.driver = SystemTesting::DriverAdapters::CapybaraRackTestDriver.new( + useragent: 'x' + ) + assert_equal 'x', Rails::SystemTestCase.driver.useragent + end + + def test_does_not_accept_nonsense_kwargs + assert_raises ArgumentError do + Rails::SystemTestCase.driver = SystemTesting::DriverAdapters::CapybaraRackTestDriver.new( + made_up_arg: 'x' + ) + end + end +end diff --git a/actionpack/test/system_testing/capybara_selenium_driver_test.rb b/actionpack/test/system_testing/capybara_selenium_driver_test.rb new file mode 100644 index 0000000000..6357b35aa6 --- /dev/null +++ b/actionpack/test/system_testing/capybara_selenium_driver_test.rb @@ -0,0 +1,58 @@ +require 'abstract_unit' + +class CapybaraSeleniumDriverTest < ActiveSupport::TestCase + def setup + Rails::SystemTestCase.driver = :capybara_selenium_driver + end + + def test_setting_driver_adapter_to_selenium + assert_kind_of SystemTesting::DriverAdapters::CapybaraSeleniumDriver, Rails::SystemTestCase.driver + end + + def test_default_settings + assert_equal :chrome, Rails::SystemTestCase.driver.browser + assert_equal :puma, Rails::SystemTestCase.driver.server + assert_equal 28100, Rails::SystemTestCase.driver.port + assert_equal [1400,1400], Rails::SystemTestCase.driver.screen_size + end + + def test_setting_browser + Rails::SystemTestCase.driver = SystemTesting::DriverAdapters::CapybaraSeleniumDriver.new( + browser: :firefox + ) + + assert_equal :firefox, Rails::SystemTestCase.driver.browser + end + + def test_setting_server + Rails::SystemTestCase.driver = SystemTesting::DriverAdapters::CapybaraSeleniumDriver.new( + server: :webrick + ) + + assert_equal :webrick, Rails::SystemTestCase.driver.server + end + + def test_setting_port + Rails::SystemTestCase.driver = SystemTesting::DriverAdapters::CapybaraSeleniumDriver.new( + port: 3000 + ) + + assert_equal 3000, Rails::SystemTestCase.driver.port + end + + def test_setting_screen_size + Rails::SystemTestCase.driver = SystemTesting::DriverAdapters::CapybaraSeleniumDriver.new( + screen_size: [ 800, 800 ] + ) + + assert_equal [ 800, 800 ], Rails::SystemTestCase.driver.screen_size + end + + def test_does_not_accept_nonsense_kwargs + assert_raises ArgumentError do + Rails::SystemTestCase.driver = SystemTesting::DriverAdapters::CapybaraSeleniumDriver.new( + made_up_arg: 'x' + ) + end + end +end diff --git a/actionpack/test/system_testing/driver_adapter_test.rb b/actionpack/test/system_testing/driver_adapter_test.rb new file mode 100644 index 0000000000..bfbeaa63a2 --- /dev/null +++ b/actionpack/test/system_testing/driver_adapter_test.rb @@ -0,0 +1,19 @@ +require 'abstract_unit' + +class DriverAdapterTest < ActiveSupport::TestCase + test 'only registered adapters are accepted' do + assert_raises(NameError) do + Rails::SystemTestCase.driver = :whatever + end + + assert_nothing_raised do + Rails::SystemTestCase.driver = :capybara_selenium_driver + end + end + + test 'settings can only be used for the appropriate adapter' do + assert_raises(ArgumentError) do + Rails::SystemTestCase.driver = SystemTesting::DriverAdapters::CapybaraRackTestDriver.new(something: 'test') + end + end +end -- cgit v1.2.3 From f482eddbeff9fe3d0dc6cdaa9a4b53df839f667c Mon Sep 17 00:00:00 2001 From: eileencodes Date: Sat, 1 Oct 2016 11:17:43 -0400 Subject: Reconfigure how the drivers work This removes the useless Rack Test Driver that Rails was providing and moves to a shim like approach for default adapters. If someone wants to use one of the default Capybara Drivers then we will initialize a new `CapybaraDriver` that simply sets the default driver. Rails though is much more opinionated than Capybara and to make system testing a "works out of the box" framework in Rails we have the `RailsSeleniumDriver`. This driver sets defaults that Rails deems important for selenium testing. The purpose of this is to simply add a test and it just works. --- .../test/system_testing/capybara_driver_test.rb | 10 ++++ .../capybara_rack_test_driver_test.rb | 26 ---------- .../capybara_selenium_driver_test.rb | 58 ---------------------- .../test/system_testing/driver_adapter_test.rb | 8 +-- .../system_testing/rails_selenium_driver_test.rb | 58 ++++++++++++++++++++++ 5 files changed, 69 insertions(+), 91 deletions(-) create mode 100644 actionpack/test/system_testing/capybara_driver_test.rb delete mode 100644 actionpack/test/system_testing/capybara_rack_test_driver_test.rb delete mode 100644 actionpack/test/system_testing/capybara_selenium_driver_test.rb create mode 100644 actionpack/test/system_testing/rails_selenium_driver_test.rb (limited to 'actionpack/test') diff --git a/actionpack/test/system_testing/capybara_driver_test.rb b/actionpack/test/system_testing/capybara_driver_test.rb new file mode 100644 index 0000000000..b409fdfab0 --- /dev/null +++ b/actionpack/test/system_testing/capybara_driver_test.rb @@ -0,0 +1,10 @@ +require 'abstract_unit' + +class CapybaraDriverTest < ActiveSupport::TestCase + def test_setting_useragent + Rails::SystemTestCase.driver = SystemTesting::DriverAdapters::CapybaraDriver.new( + :rack_test + ) + assert_equal :rack_test, Rails::SystemTestCase.driver.name + end +end diff --git a/actionpack/test/system_testing/capybara_rack_test_driver_test.rb b/actionpack/test/system_testing/capybara_rack_test_driver_test.rb deleted file mode 100644 index 0f037760d3..0000000000 --- a/actionpack/test/system_testing/capybara_rack_test_driver_test.rb +++ /dev/null @@ -1,26 +0,0 @@ -require 'abstract_unit' - -class CapybaraRackTestDriverTest < ActiveSupport::TestCase - def test_default_driver_adapter - assert_kind_of SystemTesting::DriverAdapters::CapybaraRackTestDriver, Rails::SystemTestCase.driver - end - - def test_default_settings - assert_equal 'Capybara', Rails::SystemTestCase.driver.useragent - end - - def test_setting_useragent - Rails::SystemTestCase.driver = SystemTesting::DriverAdapters::CapybaraRackTestDriver.new( - useragent: 'x' - ) - assert_equal 'x', Rails::SystemTestCase.driver.useragent - end - - def test_does_not_accept_nonsense_kwargs - assert_raises ArgumentError do - Rails::SystemTestCase.driver = SystemTesting::DriverAdapters::CapybaraRackTestDriver.new( - made_up_arg: 'x' - ) - end - end -end diff --git a/actionpack/test/system_testing/capybara_selenium_driver_test.rb b/actionpack/test/system_testing/capybara_selenium_driver_test.rb deleted file mode 100644 index 6357b35aa6..0000000000 --- a/actionpack/test/system_testing/capybara_selenium_driver_test.rb +++ /dev/null @@ -1,58 +0,0 @@ -require 'abstract_unit' - -class CapybaraSeleniumDriverTest < ActiveSupport::TestCase - def setup - Rails::SystemTestCase.driver = :capybara_selenium_driver - end - - def test_setting_driver_adapter_to_selenium - assert_kind_of SystemTesting::DriverAdapters::CapybaraSeleniumDriver, Rails::SystemTestCase.driver - end - - def test_default_settings - assert_equal :chrome, Rails::SystemTestCase.driver.browser - assert_equal :puma, Rails::SystemTestCase.driver.server - assert_equal 28100, Rails::SystemTestCase.driver.port - assert_equal [1400,1400], Rails::SystemTestCase.driver.screen_size - end - - def test_setting_browser - Rails::SystemTestCase.driver = SystemTesting::DriverAdapters::CapybaraSeleniumDriver.new( - browser: :firefox - ) - - assert_equal :firefox, Rails::SystemTestCase.driver.browser - end - - def test_setting_server - Rails::SystemTestCase.driver = SystemTesting::DriverAdapters::CapybaraSeleniumDriver.new( - server: :webrick - ) - - assert_equal :webrick, Rails::SystemTestCase.driver.server - end - - def test_setting_port - Rails::SystemTestCase.driver = SystemTesting::DriverAdapters::CapybaraSeleniumDriver.new( - port: 3000 - ) - - assert_equal 3000, Rails::SystemTestCase.driver.port - end - - def test_setting_screen_size - Rails::SystemTestCase.driver = SystemTesting::DriverAdapters::CapybaraSeleniumDriver.new( - screen_size: [ 800, 800 ] - ) - - assert_equal [ 800, 800 ], Rails::SystemTestCase.driver.screen_size - end - - def test_does_not_accept_nonsense_kwargs - assert_raises ArgumentError do - Rails::SystemTestCase.driver = SystemTesting::DriverAdapters::CapybaraSeleniumDriver.new( - made_up_arg: 'x' - ) - end - end -end diff --git a/actionpack/test/system_testing/driver_adapter_test.rb b/actionpack/test/system_testing/driver_adapter_test.rb index bfbeaa63a2..220a755bd3 100644 --- a/actionpack/test/system_testing/driver_adapter_test.rb +++ b/actionpack/test/system_testing/driver_adapter_test.rb @@ -7,13 +7,7 @@ class DriverAdapterTest < ActiveSupport::TestCase end assert_nothing_raised do - Rails::SystemTestCase.driver = :capybara_selenium_driver - end - end - - test 'settings can only be used for the appropriate adapter' do - assert_raises(ArgumentError) do - Rails::SystemTestCase.driver = SystemTesting::DriverAdapters::CapybaraRackTestDriver.new(something: 'test') + Rails::SystemTestCase.driver = :rack_test end end end diff --git a/actionpack/test/system_testing/rails_selenium_driver_test.rb b/actionpack/test/system_testing/rails_selenium_driver_test.rb new file mode 100644 index 0000000000..c239444838 --- /dev/null +++ b/actionpack/test/system_testing/rails_selenium_driver_test.rb @@ -0,0 +1,58 @@ +require 'abstract_unit' + +class RailsSeleniumDriverTest < ActiveSupport::TestCase + def setup + Rails::SystemTestCase.driver = :rails_selenium_driver + end + + def test_default_driver_adapter + assert_kind_of SystemTesting::DriverAdapters::RailsSeleniumDriver, Rails::SystemTestCase.driver + end + + def test_default_settings + assert_equal :chrome, Rails::SystemTestCase.driver.browser + assert_equal :puma, Rails::SystemTestCase.driver.server + assert_equal 28100, Rails::SystemTestCase.driver.port + assert_equal [1400,1400], Rails::SystemTestCase.driver.screen_size + end + + def test_setting_browser + Rails::SystemTestCase.driver = SystemTesting::DriverAdapters::RailsSeleniumDriver.new( + browser: :firefox + ) + + assert_equal :firefox, Rails::SystemTestCase.driver.browser + end + + def test_setting_server + Rails::SystemTestCase.driver = SystemTesting::DriverAdapters::RailsSeleniumDriver.new( + server: :webrick + ) + + assert_equal :webrick, Rails::SystemTestCase.driver.server + end + + def test_setting_port + Rails::SystemTestCase.driver = SystemTesting::DriverAdapters::RailsSeleniumDriver.new( + port: 3000 + ) + + assert_equal 3000, Rails::SystemTestCase.driver.port + end + + def test_setting_screen_size + Rails::SystemTestCase.driver = SystemTesting::DriverAdapters::RailsSeleniumDriver.new( + screen_size: [ 800, 800 ] + ) + + assert_equal [ 800, 800 ], Rails::SystemTestCase.driver.screen_size + end + + def test_does_not_accept_nonsense_kwargs + assert_raises ArgumentError do + Rails::SystemTestCase.driver = SystemTesting::DriverAdapters::RailsSeleniumDriver.new( + made_up_arg: 'x' + ) + end + end +end -- cgit v1.2.3 From e9127f7aa082986952ffcc8331b675a3a99c3a83 Mon Sep 17 00:00:00 2001 From: eileencodes Date: Sun, 2 Oct 2016 15:22:27 -0400 Subject: Add support for screenshots This change adds support, tests, and documentation for the screenshot helper. If taking screenshots is supported by the driver (for example Rack Test doesn't support screenshots) then a screenshot will be taken if the test fails. --- .../test/system_testing/screenshot_helper_test.rb | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 actionpack/test/system_testing/screenshot_helper_test.rb (limited to 'actionpack/test') diff --git a/actionpack/test/system_testing/screenshot_helper_test.rb b/actionpack/test/system_testing/screenshot_helper_test.rb new file mode 100644 index 0000000000..8bf2d0c8d0 --- /dev/null +++ b/actionpack/test/system_testing/screenshot_helper_test.rb @@ -0,0 +1,20 @@ +require 'abstract_unit' + +class ScreenshotHelperTest < ActiveSupport::TestCase + def test_driver_support_for_screenshots + Rails::SystemTestCase.driver = :rails_selenium_driver + assert Rails::SystemTestCase.driver.supports_screenshots? + + Rails::SystemTestCase.driver = :rack_test + assert_not Rails::SystemTestCase.driver.supports_screenshots? + + Rails::SystemTestCase.driver = :selenium + assert Rails::SystemTestCase.driver.supports_screenshots? + + Rails::SystemTestCase.driver = :webkit + assert Rails::SystemTestCase.driver.supports_screenshots? + + Rails::SystemTestCase.driver = :poltergeist + assert Rails::SystemTestCase.driver.supports_screenshots? + end +end -- cgit v1.2.3 From c83e6d36dd13baa8b8cb48ce1c628788a2456d21 Mon Sep 17 00:00:00 2001 From: eileencodes Date: Sun, 2 Oct 2016 17:37:57 -0400 Subject: Refactor so all drivers use Puma by default Puma is the default webserver of Rails. Because of this it doesn't make sense to run tests in Webkit if the default server is Puma. Here I've refactored the webserver to be it's own standalone module so it can be shared between Rails' selenium default driver and Capybara's defaut drivers. --- .../test/system_testing/capybara_driver_test.rb | 35 ++++++++++++++++++++-- 1 file changed, 32 insertions(+), 3 deletions(-) (limited to 'actionpack/test') diff --git a/actionpack/test/system_testing/capybara_driver_test.rb b/actionpack/test/system_testing/capybara_driver_test.rb index b409fdfab0..a6be63ba7f 100644 --- a/actionpack/test/system_testing/capybara_driver_test.rb +++ b/actionpack/test/system_testing/capybara_driver_test.rb @@ -1,10 +1,39 @@ require 'abstract_unit' class CapybaraDriverTest < ActiveSupport::TestCase - def test_setting_useragent + def setup + Rails::SystemTestCase.driver = :poltergeist + end + + def test_default_driver_adapter + assert_kind_of SystemTesting::DriverAdapters::CapybaraDriver, Rails::SystemTestCase.driver + end + + def test_default_settings + assert_equal :poltergeist, Rails::SystemTestCase.driver.name + assert_equal :puma, Rails::SystemTestCase.driver.server + assert_equal 28100, Rails::SystemTestCase.driver.port + end + + def test_setting_driver + Rails::SystemTestCase.driver = :webkit + + assert_equal :webkit, Rails::SystemTestCase.driver.name + end + + def test_setting_server Rails::SystemTestCase.driver = SystemTesting::DriverAdapters::CapybaraDriver.new( - :rack_test + server: :webrick ) - assert_equal :rack_test, Rails::SystemTestCase.driver.name + + assert_equal :webrick, Rails::SystemTestCase.driver.server + end + + def test_setting_port + Rails::SystemTestCase.driver = SystemTesting::DriverAdapters::CapybaraDriver.new( + port: 3000 + ) + + assert_equal 3000, Rails::SystemTestCase.driver.port end end -- cgit v1.2.3 From a21e18d5080a2c4808330271885f5664a725d3f3 Mon Sep 17 00:00:00 2001 From: eileencodes Date: Tue, 4 Oct 2016 08:48:21 -0400 Subject: Appease Rubocop Rubocop / code climate don't like single quotes and prefer doubles. --- actionpack/test/system_testing/capybara_driver_test.rb | 2 +- actionpack/test/system_testing/driver_adapter_test.rb | 4 ++-- actionpack/test/system_testing/rails_selenium_driver_test.rb | 4 ++-- actionpack/test/system_testing/screenshot_helper_test.rb | 2 +- 4 files changed, 6 insertions(+), 6 deletions(-) (limited to 'actionpack/test') diff --git a/actionpack/test/system_testing/capybara_driver_test.rb b/actionpack/test/system_testing/capybara_driver_test.rb index a6be63ba7f..129fe95f25 100644 --- a/actionpack/test/system_testing/capybara_driver_test.rb +++ b/actionpack/test/system_testing/capybara_driver_test.rb @@ -1,4 +1,4 @@ -require 'abstract_unit' +require "abstract_unit" class CapybaraDriverTest < ActiveSupport::TestCase def setup diff --git a/actionpack/test/system_testing/driver_adapter_test.rb b/actionpack/test/system_testing/driver_adapter_test.rb index 220a755bd3..035d018adf 100644 --- a/actionpack/test/system_testing/driver_adapter_test.rb +++ b/actionpack/test/system_testing/driver_adapter_test.rb @@ -1,7 +1,7 @@ -require 'abstract_unit' +require "abstract_unit" class DriverAdapterTest < ActiveSupport::TestCase - test 'only registered adapters are accepted' do + test "only registered adapters are accepted" do assert_raises(NameError) do Rails::SystemTestCase.driver = :whatever end diff --git a/actionpack/test/system_testing/rails_selenium_driver_test.rb b/actionpack/test/system_testing/rails_selenium_driver_test.rb index c239444838..25fc6ca097 100644 --- a/actionpack/test/system_testing/rails_selenium_driver_test.rb +++ b/actionpack/test/system_testing/rails_selenium_driver_test.rb @@ -1,4 +1,4 @@ -require 'abstract_unit' +require "abstract_unit" class RailsSeleniumDriverTest < ActiveSupport::TestCase def setup @@ -51,7 +51,7 @@ class RailsSeleniumDriverTest < ActiveSupport::TestCase def test_does_not_accept_nonsense_kwargs assert_raises ArgumentError do Rails::SystemTestCase.driver = SystemTesting::DriverAdapters::RailsSeleniumDriver.new( - made_up_arg: 'x' + made_up_arg: "x" ) end end diff --git a/actionpack/test/system_testing/screenshot_helper_test.rb b/actionpack/test/system_testing/screenshot_helper_test.rb index 8bf2d0c8d0..8060c499df 100644 --- a/actionpack/test/system_testing/screenshot_helper_test.rb +++ b/actionpack/test/system_testing/screenshot_helper_test.rb @@ -1,4 +1,4 @@ -require 'abstract_unit' +require "abstract_unit" class ScreenshotHelperTest < ActiveSupport::TestCase def test_driver_support_for_screenshots -- cgit v1.2.3 From 5bf0aa6745db27c45c0778f9f6e9046f9ee9fb94 Mon Sep 17 00:00:00 2001 From: eileencodes Date: Sun, 6 Nov 2016 18:55:15 -0500 Subject: Turn system testing into it's own gem and rename Renames `Rails::SystemTestCase` to `ActionSystemTest` and moves it to a gem under the Rails name. We need to name the class `ActionSystemTestCase` because the gem expects a module but tests themselves expect a class. Adds MIT-LICENSE, CHANGELOG, and README for the future. --- actionpack/test/abstract_unit.rb | 1 - .../test/system_testing/capybara_driver_test.rb | 39 --------------- .../test/system_testing/driver_adapter_test.rb | 13 ----- .../system_testing/rails_selenium_driver_test.rb | 58 ---------------------- .../test/system_testing/screenshot_helper_test.rb | 20 -------- 5 files changed, 131 deletions(-) delete mode 100644 actionpack/test/system_testing/capybara_driver_test.rb delete mode 100644 actionpack/test/system_testing/driver_adapter_test.rb delete mode 100644 actionpack/test/system_testing/rails_selenium_driver_test.rb delete mode 100644 actionpack/test/system_testing/screenshot_helper_test.rb (limited to 'actionpack/test') diff --git a/actionpack/test/abstract_unit.rb b/actionpack/test/abstract_unit.rb index 3b35af6d3a..459b0d6c54 100644 --- a/actionpack/test/abstract_unit.rb +++ b/actionpack/test/abstract_unit.rb @@ -33,7 +33,6 @@ require "action_view/testing/resolvers" require "action_dispatch" require "active_support/dependencies" require "active_model" -require "system_test_case" require "pp" # require 'pp' early to prevent hidden_methods from not picking up the pretty-print methods until too late diff --git a/actionpack/test/system_testing/capybara_driver_test.rb b/actionpack/test/system_testing/capybara_driver_test.rb deleted file mode 100644 index 129fe95f25..0000000000 --- a/actionpack/test/system_testing/capybara_driver_test.rb +++ /dev/null @@ -1,39 +0,0 @@ -require "abstract_unit" - -class CapybaraDriverTest < ActiveSupport::TestCase - def setup - Rails::SystemTestCase.driver = :poltergeist - end - - def test_default_driver_adapter - assert_kind_of SystemTesting::DriverAdapters::CapybaraDriver, Rails::SystemTestCase.driver - end - - def test_default_settings - assert_equal :poltergeist, Rails::SystemTestCase.driver.name - assert_equal :puma, Rails::SystemTestCase.driver.server - assert_equal 28100, Rails::SystemTestCase.driver.port - end - - def test_setting_driver - Rails::SystemTestCase.driver = :webkit - - assert_equal :webkit, Rails::SystemTestCase.driver.name - end - - def test_setting_server - Rails::SystemTestCase.driver = SystemTesting::DriverAdapters::CapybaraDriver.new( - server: :webrick - ) - - assert_equal :webrick, Rails::SystemTestCase.driver.server - end - - def test_setting_port - Rails::SystemTestCase.driver = SystemTesting::DriverAdapters::CapybaraDriver.new( - port: 3000 - ) - - assert_equal 3000, Rails::SystemTestCase.driver.port - end -end diff --git a/actionpack/test/system_testing/driver_adapter_test.rb b/actionpack/test/system_testing/driver_adapter_test.rb deleted file mode 100644 index 035d018adf..0000000000 --- a/actionpack/test/system_testing/driver_adapter_test.rb +++ /dev/null @@ -1,13 +0,0 @@ -require "abstract_unit" - -class DriverAdapterTest < ActiveSupport::TestCase - test "only registered adapters are accepted" do - assert_raises(NameError) do - Rails::SystemTestCase.driver = :whatever - end - - assert_nothing_raised do - Rails::SystemTestCase.driver = :rack_test - end - end -end diff --git a/actionpack/test/system_testing/rails_selenium_driver_test.rb b/actionpack/test/system_testing/rails_selenium_driver_test.rb deleted file mode 100644 index 25fc6ca097..0000000000 --- a/actionpack/test/system_testing/rails_selenium_driver_test.rb +++ /dev/null @@ -1,58 +0,0 @@ -require "abstract_unit" - -class RailsSeleniumDriverTest < ActiveSupport::TestCase - def setup - Rails::SystemTestCase.driver = :rails_selenium_driver - end - - def test_default_driver_adapter - assert_kind_of SystemTesting::DriverAdapters::RailsSeleniumDriver, Rails::SystemTestCase.driver - end - - def test_default_settings - assert_equal :chrome, Rails::SystemTestCase.driver.browser - assert_equal :puma, Rails::SystemTestCase.driver.server - assert_equal 28100, Rails::SystemTestCase.driver.port - assert_equal [1400,1400], Rails::SystemTestCase.driver.screen_size - end - - def test_setting_browser - Rails::SystemTestCase.driver = SystemTesting::DriverAdapters::RailsSeleniumDriver.new( - browser: :firefox - ) - - assert_equal :firefox, Rails::SystemTestCase.driver.browser - end - - def test_setting_server - Rails::SystemTestCase.driver = SystemTesting::DriverAdapters::RailsSeleniumDriver.new( - server: :webrick - ) - - assert_equal :webrick, Rails::SystemTestCase.driver.server - end - - def test_setting_port - Rails::SystemTestCase.driver = SystemTesting::DriverAdapters::RailsSeleniumDriver.new( - port: 3000 - ) - - assert_equal 3000, Rails::SystemTestCase.driver.port - end - - def test_setting_screen_size - Rails::SystemTestCase.driver = SystemTesting::DriverAdapters::RailsSeleniumDriver.new( - screen_size: [ 800, 800 ] - ) - - assert_equal [ 800, 800 ], Rails::SystemTestCase.driver.screen_size - end - - def test_does_not_accept_nonsense_kwargs - assert_raises ArgumentError do - Rails::SystemTestCase.driver = SystemTesting::DriverAdapters::RailsSeleniumDriver.new( - made_up_arg: "x" - ) - end - end -end diff --git a/actionpack/test/system_testing/screenshot_helper_test.rb b/actionpack/test/system_testing/screenshot_helper_test.rb deleted file mode 100644 index 8060c499df..0000000000 --- a/actionpack/test/system_testing/screenshot_helper_test.rb +++ /dev/null @@ -1,20 +0,0 @@ -require "abstract_unit" - -class ScreenshotHelperTest < ActiveSupport::TestCase - def test_driver_support_for_screenshots - Rails::SystemTestCase.driver = :rails_selenium_driver - assert Rails::SystemTestCase.driver.supports_screenshots? - - Rails::SystemTestCase.driver = :rack_test - assert_not Rails::SystemTestCase.driver.supports_screenshots? - - Rails::SystemTestCase.driver = :selenium - assert Rails::SystemTestCase.driver.supports_screenshots? - - Rails::SystemTestCase.driver = :webkit - assert Rails::SystemTestCase.driver.supports_screenshots? - - Rails::SystemTestCase.driver = :poltergeist - assert Rails::SystemTestCase.driver.supports_screenshots? - end -end -- cgit v1.2.3 From 1a0ca84a064b07ecab798793a3d7ebe89bb6367c Mon Sep 17 00:00:00 2001 From: eileencodes Date: Sun, 19 Feb 2017 11:50:42 -0500 Subject: Move and rename system tests * Move system tests back into Action Pack * Rename `ActionSystemTest` to `ActionDispatch::SystemTestCase` * Remove private base module and only make file for public `SystemTestCase` class, name private module `SystemTesting` * Rename `ActionSystemTestCase` to `ApplicationSystemTestCase` * Update corresponding documentation and guides * Delete old `ActionSystemTest` files --- .../test/dispatch/system_testing/browser_test.rb | 10 ++++++++++ .../test/dispatch/system_testing/driver_test.rb | 9 +++++++++ .../system_testing/screenshot_helper_test.rb | 10 ++++++++++ .../test/dispatch/system_testing/server_test.rb | 10 ++++++++++ .../system_testing/system_test_case_test.rb | 21 +++++++++++++++++++++ 5 files changed, 60 insertions(+) create mode 100644 actionpack/test/dispatch/system_testing/browser_test.rb create mode 100644 actionpack/test/dispatch/system_testing/driver_test.rb create mode 100644 actionpack/test/dispatch/system_testing/screenshot_helper_test.rb create mode 100644 actionpack/test/dispatch/system_testing/server_test.rb create mode 100644 actionpack/test/dispatch/system_testing/system_test_case_test.rb (limited to 'actionpack/test') diff --git a/actionpack/test/dispatch/system_testing/browser_test.rb b/actionpack/test/dispatch/system_testing/browser_test.rb new file mode 100644 index 0000000000..b0ad309492 --- /dev/null +++ b/actionpack/test/dispatch/system_testing/browser_test.rb @@ -0,0 +1,10 @@ +require "abstract_unit" +require "action_dispatch/system_testing/browser" + +class BrowserTest < ActiveSupport::TestCase + test "initializing the browser" do + browser = ActionDispatch::SystemTesting::Browser.new(:chrome, [ 1400, 1400 ]) + assert_equal :chrome, browser.instance_variable_get(:@name) + assert_equal [ 1400, 1400 ], browser.instance_variable_get(:@screen_size) + end +end diff --git a/actionpack/test/dispatch/system_testing/driver_test.rb b/actionpack/test/dispatch/system_testing/driver_test.rb new file mode 100644 index 0000000000..f0ebdb38db --- /dev/null +++ b/actionpack/test/dispatch/system_testing/driver_test.rb @@ -0,0 +1,9 @@ +require "abstract_unit" +require "action_dispatch/system_testing/driver" + +class DriverTest < ActiveSupport::TestCase + test "initializing the driver" do + driver = ActionDispatch::SystemTesting::Driver.new(:selenium) + assert_equal :selenium, driver.instance_variable_get(:@name) + end +end diff --git a/actionpack/test/dispatch/system_testing/screenshot_helper_test.rb b/actionpack/test/dispatch/system_testing/screenshot_helper_test.rb new file mode 100644 index 0000000000..3da89f4a10 --- /dev/null +++ b/actionpack/test/dispatch/system_testing/screenshot_helper_test.rb @@ -0,0 +1,10 @@ +require "abstract_unit" +require "action_dispatch/system_testing/test_helpers/screenshot_helper" + +class ScreenshotHelperTest < ActiveSupport::TestCase + test "image path is saved in tmp directory" do + new_test = ActionDispatch::SystemTestCase.new("x") + + assert_equal "tmp/screenshots/failures_x.png", new_test.send(:image_path) + end +end diff --git a/actionpack/test/dispatch/system_testing/server_test.rb b/actionpack/test/dispatch/system_testing/server_test.rb new file mode 100644 index 0000000000..66842f4ea9 --- /dev/null +++ b/actionpack/test/dispatch/system_testing/server_test.rb @@ -0,0 +1,10 @@ +require "abstract_unit" +require "capybara/dsl" +require "action_dispatch/system_testing/server" + +class ServerTest < ActiveSupport::TestCase + test "initializing the server port" do + server = ActionDispatch::SystemTesting::Server.new.run + assert_includes Capybara.servers, :rails_puma + end +end diff --git a/actionpack/test/dispatch/system_testing/system_test_case_test.rb b/actionpack/test/dispatch/system_testing/system_test_case_test.rb new file mode 100644 index 0000000000..a384902a14 --- /dev/null +++ b/actionpack/test/dispatch/system_testing/system_test_case_test.rb @@ -0,0 +1,21 @@ +require "abstract_unit" + +class SystemTestCaseTest < ActiveSupport::TestCase + test "driven_by sets Capybara's default driver to poltergeist" do + ActionDispatch::SystemTestCase.driven_by :poltergeist + + assert_equal :poltergeist, Capybara.default_driver + end + + test "driven_by sets Capybara's drivers respectively" do + ActionDispatch::SystemTestCase.driven_by :selenium, using: :chrome + + assert_includes Capybara.drivers, :selenium + assert_includes Capybara.drivers, :chrome + assert_equal :chrome, Capybara.default_driver + end + + test "selenium? returns false if driver is poltergeist" do + assert_not ActionDispatch::SystemTestCase.selenium?(:poltergeist) + end +end -- cgit v1.2.3 From 983275eb7c01eaeba5798a422514f4d9d6b74786 Mon Sep 17 00:00:00 2001 From: eileencodes Date: Sun, 19 Feb 2017 12:12:06 -0500 Subject: Fix screenshot helper to provide correct file name We only want the file name to include the word `failures` if it failed, not any time the user wants to take a screenshot during a test run. --- .../test/dispatch/system_testing/screenshot_helper_test.rb | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) (limited to 'actionpack/test') diff --git a/actionpack/test/dispatch/system_testing/screenshot_helper_test.rb b/actionpack/test/dispatch/system_testing/screenshot_helper_test.rb index 3da89f4a10..8c14f799b0 100644 --- a/actionpack/test/dispatch/system_testing/screenshot_helper_test.rb +++ b/actionpack/test/dispatch/system_testing/screenshot_helper_test.rb @@ -5,6 +5,14 @@ class ScreenshotHelperTest < ActiveSupport::TestCase test "image path is saved in tmp directory" do new_test = ActionDispatch::SystemTestCase.new("x") - assert_equal "tmp/screenshots/failures_x.png", new_test.send(:image_path) + assert_equal "tmp/screenshots/x.png", new_test.send(:image_path) + end + + test "image path includes failures text if test did not pass" do + new_test = ActionDispatch::SystemTestCase.new("x") + + new_test.stub :passed?, false do + assert_equal "tmp/screenshots/failures_x.png", new_test.send(:image_path) + end end end -- cgit v1.2.3 From 161cf89e134267f9b579f493ca19b12c30d5fd36 Mon Sep 17 00:00:00 2001 From: eileencodes Date: Sun, 19 Feb 2017 17:49:21 -0500 Subject: Fix default host in setup, move teardown to helper file * Override integration test default host Integration tests automatically set the default host to 'http://example.com'. This works fine for integration tests because they are not real browser sessions, but doesn't work fine for system tests because they are real browser sessions. We can override this by setting the `host!` in `before_setup. The `Capybara.always_include_port` will allow the test to look at `127.0.0.1:port capybara picks` and properly redirect the test. Any application can override this by setting the `host!` in their system test helper. Generally though, applications are going to be using localhost. In this commit I also moved the setup and teardown into their own module for tidiness. * Move teardown settings into system test case These configuration options can be put into the system test case file instead of the generated system tests helper file. This is an implementation detail and therefore shouldn't be generated with the template. --- actionpack/test/dispatch/system_testing/server_test.rb | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) (limited to 'actionpack/test') diff --git a/actionpack/test/dispatch/system_testing/server_test.rb b/actionpack/test/dispatch/system_testing/server_test.rb index 66842f4ea9..10412d6367 100644 --- a/actionpack/test/dispatch/system_testing/server_test.rb +++ b/actionpack/test/dispatch/system_testing/server_test.rb @@ -3,8 +3,15 @@ require "capybara/dsl" require "action_dispatch/system_testing/server" class ServerTest < ActiveSupport::TestCase + setup do + ActionDispatch::SystemTesting::Server.new.run + end + test "initializing the server port" do - server = ActionDispatch::SystemTesting::Server.new.run assert_includes Capybara.servers, :rails_puma end + + test "port is always included" do + assert Capybara.always_include_port, "expected Capybara.always_include_port to be true" + end end -- cgit v1.2.3 From ce7d5fb2e6ffa9ec323510aaff51f10b15f1649a Mon Sep 17 00:00:00 2001 From: Andrew White Date: Wed, 20 Jan 2016 15:03:10 +0000 Subject: Add support for defining custom url helpers in routes.rb Allow the definition of custom url helpers that will be available automatically wherever standard url helpers are available. The current solution is to create helper methods in ApplicationHelper or some other helper module and this isn't a great solution since the url helper module can be called directly or included in another class which doesn't include the normal helper modules. Reference #22512. --- .../dispatch/routing/custom_url_helpers_test.rb | 121 +++++++++++++++++++++ 1 file changed, 121 insertions(+) create mode 100644 actionpack/test/dispatch/routing/custom_url_helpers_test.rb (limited to 'actionpack/test') diff --git a/actionpack/test/dispatch/routing/custom_url_helpers_test.rb b/actionpack/test/dispatch/routing/custom_url_helpers_test.rb new file mode 100644 index 0000000000..ec0484a3ba --- /dev/null +++ b/actionpack/test/dispatch/routing/custom_url_helpers_test.rb @@ -0,0 +1,121 @@ +require "abstract_unit" + +class TestCustomUrlHelpers < ActionDispatch::IntegrationTest + class Linkable + attr_reader :id + + def initialize(id) + @id = id + end + + def linkable_type + self.class.name.demodulize.underscore + end + end + + class Category < Linkable; end + class Collection < Linkable; end + class Product < Linkable; end + + Routes = ActionDispatch::Routing::RouteSet.new + Routes.draw do + default_url_options host: "www.example.com" + + root to: "pages#index" + get "/basket", to: "basket#show", as: :basket + + resources :categories, :collections, :products + + namespace :admin do + get "/dashboard", to: "dashboard#index" + end + + url_helper(:website) { "http://www.rubyonrails.org" } + url_helper(:linkable) { |linkable| [:"#{linkable.linkable_type}", { id: linkable.id }] } + url_helper(:params) { |params| params } + url_helper(:symbol) { :basket } + url_helper(:hash) { { controller: "basket", action: "show" } } + url_helper(:array) { [:admin, :dashboard] } + url_helper(:options) { |options| [:products, options] } + url_helper(:defaults, size: 10) { |options| [:products, options] } + end + + APP = build_app Routes + + def app + APP + end + + include Routes.url_helpers + + def setup + @category = Category.new("1") + @collection = Collection.new("2") + @product = Product.new("3") + @path_params = { "controller" => "pages", "action" => "index" } + @unsafe_params = ActionController::Parameters.new(@path_params) + @safe_params = ActionController::Parameters.new(@path_params).permit(:controller, :action) + end + + def test_custom_path_helper + assert_equal "http://www.rubyonrails.org", website_path + assert_equal "http://www.rubyonrails.org", Routes.url_helpers.website_path + + assert_equal "/categories/1", linkable_path(@category) + assert_equal "/categories/1", Routes.url_helpers.linkable_path(@category) + assert_equal "/collections/2", linkable_path(@collection) + assert_equal "/collections/2", Routes.url_helpers.linkable_path(@collection) + assert_equal "/products/3", linkable_path(@product) + assert_equal "/products/3", Routes.url_helpers.linkable_path(@product) + + assert_equal "/", params_path(@safe_params) + assert_equal "/", Routes.url_helpers.params_path(@safe_params) + assert_raises(ArgumentError) { params_path(@unsafe_params) } + assert_raises(ArgumentError) { Routes.url_helpers.params_path(@unsafe_params) } + + assert_equal "/basket", symbol_path + assert_equal "/basket", Routes.url_helpers.symbol_path + assert_equal "/basket", hash_path + assert_equal "/basket", Routes.url_helpers.hash_path + assert_equal "/admin/dashboard", array_path + assert_equal "/admin/dashboard", Routes.url_helpers.array_path + + assert_equal "/products?page=2", options_path(page: 2) + assert_equal "/products?page=2", Routes.url_helpers.options_path(page: 2) + assert_equal "/products?size=10", defaults_path + assert_equal "/products?size=10", Routes.url_helpers.defaults_path + assert_equal "/products?size=20", defaults_path(size: 20) + assert_equal "/products?size=20", Routes.url_helpers.defaults_path(size: 20) + end + + def test_custom_url_helper + assert_equal "http://www.rubyonrails.org", website_url + assert_equal "http://www.rubyonrails.org", Routes.url_helpers.website_url + + assert_equal "http://www.example.com/categories/1", linkable_url(@category) + assert_equal "http://www.example.com/categories/1", Routes.url_helpers.linkable_url(@category) + assert_equal "http://www.example.com/collections/2", linkable_url(@collection) + assert_equal "http://www.example.com/collections/2", Routes.url_helpers.linkable_url(@collection) + assert_equal "http://www.example.com/products/3", linkable_url(@product) + assert_equal "http://www.example.com/products/3", Routes.url_helpers.linkable_url(@product) + + assert_equal "http://www.example.com/", params_url(@safe_params) + assert_equal "http://www.example.com/", Routes.url_helpers.params_url(@safe_params) + assert_raises(ArgumentError) { params_url(@unsafe_params) } + assert_raises(ArgumentError) { Routes.url_helpers.params_url(@unsafe_params) } + + assert_equal "http://www.example.com/basket", symbol_url + assert_equal "http://www.example.com/basket", Routes.url_helpers.symbol_url + assert_equal "http://www.example.com/basket", hash_url + assert_equal "http://www.example.com/basket", Routes.url_helpers.hash_url + assert_equal "/admin/dashboard", array_path + assert_equal "/admin/dashboard", Routes.url_helpers.array_path + + assert_equal "http://www.example.com/products?page=2", options_url(page: 2) + assert_equal "http://www.example.com/products?page=2", Routes.url_helpers.options_url(page: 2) + assert_equal "http://www.example.com/products?size=10", defaults_url + assert_equal "http://www.example.com/products?size=10", Routes.url_helpers.defaults_url + assert_equal "http://www.example.com/products?size=20", defaults_url(size: 20) + assert_equal "http://www.example.com/products?size=20", Routes.url_helpers.defaults_url(size: 20) + end +end -- cgit v1.2.3 From 47a27e8950ad00654e2ba0420cefd87269e08055 Mon Sep 17 00:00:00 2001 From: Andrew White Date: Tue, 23 Feb 2016 09:59:33 +0000 Subject: Rename url_helper to direct --- .../dispatch/routing/custom_url_helpers_test.rb | 24 +++++++++++----------- 1 file changed, 12 insertions(+), 12 deletions(-) (limited to 'actionpack/test') diff --git a/actionpack/test/dispatch/routing/custom_url_helpers_test.rb b/actionpack/test/dispatch/routing/custom_url_helpers_test.rb index ec0484a3ba..a55ee8e9b0 100644 --- a/actionpack/test/dispatch/routing/custom_url_helpers_test.rb +++ b/actionpack/test/dispatch/routing/custom_url_helpers_test.rb @@ -30,14 +30,14 @@ class TestCustomUrlHelpers < ActionDispatch::IntegrationTest get "/dashboard", to: "dashboard#index" end - url_helper(:website) { "http://www.rubyonrails.org" } - url_helper(:linkable) { |linkable| [:"#{linkable.linkable_type}", { id: linkable.id }] } - url_helper(:params) { |params| params } - url_helper(:symbol) { :basket } - url_helper(:hash) { { controller: "basket", action: "show" } } - url_helper(:array) { [:admin, :dashboard] } - url_helper(:options) { |options| [:products, options] } - url_helper(:defaults, size: 10) { |options| [:products, options] } + direct(:website) { "http://www.rubyonrails.org" } + direct(:linkable) { |linkable| [:"#{linkable.linkable_type}", { id: linkable.id }] } + direct(:params) { |params| params } + direct(:symbol) { :basket } + direct(:hash) { { controller: "basket", action: "show" } } + direct(:array) { [:admin, :dashboard] } + direct(:options) { |options| [:products, options] } + direct(:defaults, size: 10) { |options| [:products, options] } end APP = build_app Routes @@ -57,7 +57,7 @@ class TestCustomUrlHelpers < ActionDispatch::IntegrationTest @safe_params = ActionController::Parameters.new(@path_params).permit(:controller, :action) end - def test_custom_path_helper + def test_direct_paths assert_equal "http://www.rubyonrails.org", website_path assert_equal "http://www.rubyonrails.org", Routes.url_helpers.website_path @@ -88,7 +88,7 @@ class TestCustomUrlHelpers < ActionDispatch::IntegrationTest assert_equal "/products?size=20", Routes.url_helpers.defaults_path(size: 20) end - def test_custom_url_helper + def test_direct_urls assert_equal "http://www.rubyonrails.org", website_url assert_equal "http://www.rubyonrails.org", Routes.url_helpers.website_url @@ -108,8 +108,8 @@ class TestCustomUrlHelpers < ActionDispatch::IntegrationTest assert_equal "http://www.example.com/basket", Routes.url_helpers.symbol_url assert_equal "http://www.example.com/basket", hash_url assert_equal "http://www.example.com/basket", Routes.url_helpers.hash_url - assert_equal "/admin/dashboard", array_path - assert_equal "/admin/dashboard", Routes.url_helpers.array_path + assert_equal "http://www.example.com/admin/dashboard", array_url + assert_equal "http://www.example.com/admin/dashboard", Routes.url_helpers.array_url assert_equal "http://www.example.com/products?page=2", options_url(page: 2) assert_equal "http://www.example.com/products?page=2", Routes.url_helpers.options_url(page: 2) -- cgit v1.2.3 From e96da0a7beb483ed5e8a8096ff67b09ecc5ca7a1 Mon Sep 17 00:00:00 2001 From: Andrew White Date: Mon, 20 Feb 2017 13:54:06 +0000 Subject: Only accept symbols and strings for Mapper#direct --- .../test/dispatch/routing/custom_url_helpers_test.rb | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) (limited to 'actionpack/test') diff --git a/actionpack/test/dispatch/routing/custom_url_helpers_test.rb b/actionpack/test/dispatch/routing/custom_url_helpers_test.rb index a55ee8e9b0..bf4b323cf0 100644 --- a/actionpack/test/dispatch/routing/custom_url_helpers_test.rb +++ b/actionpack/test/dispatch/routing/custom_url_helpers_test.rb @@ -31,6 +31,7 @@ class TestCustomUrlHelpers < ActionDispatch::IntegrationTest end direct(:website) { "http://www.rubyonrails.org" } + direct("string") { "http://www.rubyonrails.org" } direct(:linkable) { |linkable| [:"#{linkable.linkable_type}", { id: linkable.id }] } direct(:params) { |params| params } direct(:symbol) { :basket } @@ -61,6 +62,9 @@ class TestCustomUrlHelpers < ActionDispatch::IntegrationTest assert_equal "http://www.rubyonrails.org", website_path assert_equal "http://www.rubyonrails.org", Routes.url_helpers.website_path + assert_equal "http://www.rubyonrails.org", string_path + assert_equal "http://www.rubyonrails.org", Routes.url_helpers.string_path + assert_equal "/categories/1", linkable_path(@category) assert_equal "/categories/1", Routes.url_helpers.linkable_path(@category) assert_equal "/collections/2", linkable_path(@collection) @@ -92,6 +96,9 @@ class TestCustomUrlHelpers < ActionDispatch::IntegrationTest assert_equal "http://www.rubyonrails.org", website_url assert_equal "http://www.rubyonrails.org", Routes.url_helpers.website_url + assert_equal "http://www.rubyonrails.org", string_url + assert_equal "http://www.rubyonrails.org", Routes.url_helpers.string_url + assert_equal "http://www.example.com/categories/1", linkable_url(@category) assert_equal "http://www.example.com/categories/1", Routes.url_helpers.linkable_url(@category) assert_equal "http://www.example.com/collections/2", linkable_url(@collection) @@ -118,4 +125,14 @@ class TestCustomUrlHelpers < ActionDispatch::IntegrationTest assert_equal "http://www.example.com/products?size=20", defaults_url(size: 20) assert_equal "http://www.example.com/products?size=20", Routes.url_helpers.defaults_url(size: 20) end + + def test_raises_argument_error + routes = ActionDispatch::Routing::RouteSet.new + + assert_raises ArgumentError do + routes.draw do + direct(1) { "http://www.rubyonrails.org" } + end + end + end end -- cgit v1.2.3 From d67e2520289745913e7bab9a852c86b99245f738 Mon Sep 17 00:00:00 2001 From: Andrew White Date: Mon, 20 Feb 2017 14:37:00 +0000 Subject: Add test for calling a url helper in Mapper#direct --- actionpack/test/dispatch/routing/custom_url_helpers_test.rb | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'actionpack/test') diff --git a/actionpack/test/dispatch/routing/custom_url_helpers_test.rb b/actionpack/test/dispatch/routing/custom_url_helpers_test.rb index bf4b323cf0..22e8bbf21e 100644 --- a/actionpack/test/dispatch/routing/custom_url_helpers_test.rb +++ b/actionpack/test/dispatch/routing/custom_url_helpers_test.rb @@ -32,6 +32,7 @@ class TestCustomUrlHelpers < ActionDispatch::IntegrationTest direct(:website) { "http://www.rubyonrails.org" } direct("string") { "http://www.rubyonrails.org" } + direct(:helper) { basket_url } direct(:linkable) { |linkable| [:"#{linkable.linkable_type}", { id: linkable.id }] } direct(:params) { |params| params } direct(:symbol) { :basket } @@ -65,6 +66,9 @@ class TestCustomUrlHelpers < ActionDispatch::IntegrationTest assert_equal "http://www.rubyonrails.org", string_path assert_equal "http://www.rubyonrails.org", Routes.url_helpers.string_path + assert_equal "http://www.example.com/basket", helper_url + assert_equal "http://www.example.com/basket", Routes.url_helpers.helper_url + assert_equal "/categories/1", linkable_path(@category) assert_equal "/categories/1", Routes.url_helpers.linkable_path(@category) assert_equal "/collections/2", linkable_path(@collection) @@ -99,6 +103,9 @@ class TestCustomUrlHelpers < ActionDispatch::IntegrationTest assert_equal "http://www.rubyonrails.org", string_url assert_equal "http://www.rubyonrails.org", Routes.url_helpers.string_url + assert_equal "http://www.example.com/basket", helper_url + assert_equal "http://www.example.com/basket", Routes.url_helpers.helper_url + assert_equal "http://www.example.com/categories/1", linkable_url(@category) assert_equal "http://www.example.com/categories/1", Routes.url_helpers.linkable_url(@category) assert_equal "http://www.example.com/collections/2", linkable_url(@collection) -- cgit v1.2.3 From 3bf47b018be912fc7946342315e67b2ac6c33eaf Mon Sep 17 00:00:00 2001 From: Andrew White Date: Mon, 20 Feb 2017 20:22:42 +0000 Subject: Add custom polymorphic mapping Allow the use of `direct` to specify custom mappings for polymorphic_url, e.g: resource :basket direct(class: "Basket") { [:basket] } This will then generate the following: >> link_to "Basket", @basket => Basket More importantly it will generate the correct url when used with `form_for`. Fixes #1769. --- .../dispatch/routing/custom_url_helpers_test.rb | 145 -------------- .../dispatch/routing/direct_url_helpers_test.rb | 215 +++++++++++++++++++++ 2 files changed, 215 insertions(+), 145 deletions(-) delete mode 100644 actionpack/test/dispatch/routing/custom_url_helpers_test.rb create mode 100644 actionpack/test/dispatch/routing/direct_url_helpers_test.rb (limited to 'actionpack/test') diff --git a/actionpack/test/dispatch/routing/custom_url_helpers_test.rb b/actionpack/test/dispatch/routing/custom_url_helpers_test.rb deleted file mode 100644 index 22e8bbf21e..0000000000 --- a/actionpack/test/dispatch/routing/custom_url_helpers_test.rb +++ /dev/null @@ -1,145 +0,0 @@ -require "abstract_unit" - -class TestCustomUrlHelpers < ActionDispatch::IntegrationTest - class Linkable - attr_reader :id - - def initialize(id) - @id = id - end - - def linkable_type - self.class.name.demodulize.underscore - end - end - - class Category < Linkable; end - class Collection < Linkable; end - class Product < Linkable; end - - Routes = ActionDispatch::Routing::RouteSet.new - Routes.draw do - default_url_options host: "www.example.com" - - root to: "pages#index" - get "/basket", to: "basket#show", as: :basket - - resources :categories, :collections, :products - - namespace :admin do - get "/dashboard", to: "dashboard#index" - end - - direct(:website) { "http://www.rubyonrails.org" } - direct("string") { "http://www.rubyonrails.org" } - direct(:helper) { basket_url } - direct(:linkable) { |linkable| [:"#{linkable.linkable_type}", { id: linkable.id }] } - direct(:params) { |params| params } - direct(:symbol) { :basket } - direct(:hash) { { controller: "basket", action: "show" } } - direct(:array) { [:admin, :dashboard] } - direct(:options) { |options| [:products, options] } - direct(:defaults, size: 10) { |options| [:products, options] } - end - - APP = build_app Routes - - def app - APP - end - - include Routes.url_helpers - - def setup - @category = Category.new("1") - @collection = Collection.new("2") - @product = Product.new("3") - @path_params = { "controller" => "pages", "action" => "index" } - @unsafe_params = ActionController::Parameters.new(@path_params) - @safe_params = ActionController::Parameters.new(@path_params).permit(:controller, :action) - end - - def test_direct_paths - assert_equal "http://www.rubyonrails.org", website_path - assert_equal "http://www.rubyonrails.org", Routes.url_helpers.website_path - - assert_equal "http://www.rubyonrails.org", string_path - assert_equal "http://www.rubyonrails.org", Routes.url_helpers.string_path - - assert_equal "http://www.example.com/basket", helper_url - assert_equal "http://www.example.com/basket", Routes.url_helpers.helper_url - - assert_equal "/categories/1", linkable_path(@category) - assert_equal "/categories/1", Routes.url_helpers.linkable_path(@category) - assert_equal "/collections/2", linkable_path(@collection) - assert_equal "/collections/2", Routes.url_helpers.linkable_path(@collection) - assert_equal "/products/3", linkable_path(@product) - assert_equal "/products/3", Routes.url_helpers.linkable_path(@product) - - assert_equal "/", params_path(@safe_params) - assert_equal "/", Routes.url_helpers.params_path(@safe_params) - assert_raises(ArgumentError) { params_path(@unsafe_params) } - assert_raises(ArgumentError) { Routes.url_helpers.params_path(@unsafe_params) } - - assert_equal "/basket", symbol_path - assert_equal "/basket", Routes.url_helpers.symbol_path - assert_equal "/basket", hash_path - assert_equal "/basket", Routes.url_helpers.hash_path - assert_equal "/admin/dashboard", array_path - assert_equal "/admin/dashboard", Routes.url_helpers.array_path - - assert_equal "/products?page=2", options_path(page: 2) - assert_equal "/products?page=2", Routes.url_helpers.options_path(page: 2) - assert_equal "/products?size=10", defaults_path - assert_equal "/products?size=10", Routes.url_helpers.defaults_path - assert_equal "/products?size=20", defaults_path(size: 20) - assert_equal "/products?size=20", Routes.url_helpers.defaults_path(size: 20) - end - - def test_direct_urls - assert_equal "http://www.rubyonrails.org", website_url - assert_equal "http://www.rubyonrails.org", Routes.url_helpers.website_url - - assert_equal "http://www.rubyonrails.org", string_url - assert_equal "http://www.rubyonrails.org", Routes.url_helpers.string_url - - assert_equal "http://www.example.com/basket", helper_url - assert_equal "http://www.example.com/basket", Routes.url_helpers.helper_url - - assert_equal "http://www.example.com/categories/1", linkable_url(@category) - assert_equal "http://www.example.com/categories/1", Routes.url_helpers.linkable_url(@category) - assert_equal "http://www.example.com/collections/2", linkable_url(@collection) - assert_equal "http://www.example.com/collections/2", Routes.url_helpers.linkable_url(@collection) - assert_equal "http://www.example.com/products/3", linkable_url(@product) - assert_equal "http://www.example.com/products/3", Routes.url_helpers.linkable_url(@product) - - assert_equal "http://www.example.com/", params_url(@safe_params) - assert_equal "http://www.example.com/", Routes.url_helpers.params_url(@safe_params) - assert_raises(ArgumentError) { params_url(@unsafe_params) } - assert_raises(ArgumentError) { Routes.url_helpers.params_url(@unsafe_params) } - - assert_equal "http://www.example.com/basket", symbol_url - assert_equal "http://www.example.com/basket", Routes.url_helpers.symbol_url - assert_equal "http://www.example.com/basket", hash_url - assert_equal "http://www.example.com/basket", Routes.url_helpers.hash_url - assert_equal "http://www.example.com/admin/dashboard", array_url - assert_equal "http://www.example.com/admin/dashboard", Routes.url_helpers.array_url - - assert_equal "http://www.example.com/products?page=2", options_url(page: 2) - assert_equal "http://www.example.com/products?page=2", Routes.url_helpers.options_url(page: 2) - assert_equal "http://www.example.com/products?size=10", defaults_url - assert_equal "http://www.example.com/products?size=10", Routes.url_helpers.defaults_url - assert_equal "http://www.example.com/products?size=20", defaults_url(size: 20) - assert_equal "http://www.example.com/products?size=20", Routes.url_helpers.defaults_url(size: 20) - end - - def test_raises_argument_error - routes = ActionDispatch::Routing::RouteSet.new - - assert_raises ArgumentError do - routes.draw do - direct(1) { "http://www.rubyonrails.org" } - end - end - end -end diff --git a/actionpack/test/dispatch/routing/direct_url_helpers_test.rb b/actionpack/test/dispatch/routing/direct_url_helpers_test.rb new file mode 100644 index 0000000000..56bb7f13b3 --- /dev/null +++ b/actionpack/test/dispatch/routing/direct_url_helpers_test.rb @@ -0,0 +1,215 @@ +require "abstract_unit" + +class TestDirectUrlHelpers < ActionDispatch::IntegrationTest + class Linkable + attr_reader :id + + def initialize(id) + @id = id + end + + def linkable_type + self.class.name.demodulize.underscore + end + end + + class Category < Linkable; end + class Collection < Linkable; end + class Product < Linkable; end + + class Model + extend ActiveModel::Naming + include ActiveModel::Conversion + + attr_reader :id + + def initialize(id = nil) + @id = id + end + + def model_name + @_model_name ||= ActiveModel::Name.new(self.class, nil, self.class.name.demodulize) + end + + def persisted? + false + end + end + + class Basket < Model; end + class User < Model; end + class Video < Model; end + + Routes = ActionDispatch::Routing::RouteSet.new + Routes.draw do + default_url_options host: "www.example.com" + + root to: "pages#index" + get "/basket", to: "basket#show", as: :basket + get "/profile", to: "users#profile", as: :profile + get "/media/:id", to: "media#show", as: :media + + resources :categories, :collections, :products + + namespace :admin do + get "/dashboard", to: "dashboard#index" + end + + direct(:website) { "http://www.rubyonrails.org" } + direct("string") { "http://www.rubyonrails.org" } + direct(:helper) { basket_url } + direct(:linkable) { |linkable| [:"#{linkable.linkable_type}", { id: linkable.id }] } + direct(:params) { |params| params } + direct(:symbol) { :basket } + direct(:hash) { { controller: "basket", action: "show" } } + direct(:array) { [:admin, :dashboard] } + direct(:options) { |options| [:products, options] } + direct(:defaults, size: 10) { |options| [:products, options] } + + direct(class: "Basket") { |basket| [:basket] } + direct(class: "User", anchor: "details") { |user, options| [:profile, options] } + direct(class: "Video") { |video| [:media, { id: video.id }] } + end + + APP = build_app Routes + + def app + APP + end + + include Routes.url_helpers + + def setup + @category = Category.new("1") + @collection = Collection.new("2") + @product = Product.new("3") + @basket = Basket.new + @user = User.new + @video = Video.new("4") + @path_params = { "controller" => "pages", "action" => "index" } + @unsafe_params = ActionController::Parameters.new(@path_params) + @safe_params = ActionController::Parameters.new(@path_params).permit(:controller, :action) + end + + def test_direct_paths + assert_equal "http://www.rubyonrails.org", website_path + assert_equal "http://www.rubyonrails.org", Routes.url_helpers.website_path + + assert_equal "http://www.rubyonrails.org", string_path + assert_equal "http://www.rubyonrails.org", Routes.url_helpers.string_path + + assert_equal "http://www.example.com/basket", helper_url + assert_equal "http://www.example.com/basket", Routes.url_helpers.helper_url + + assert_equal "/categories/1", linkable_path(@category) + assert_equal "/categories/1", Routes.url_helpers.linkable_path(@category) + assert_equal "/collections/2", linkable_path(@collection) + assert_equal "/collections/2", Routes.url_helpers.linkable_path(@collection) + assert_equal "/products/3", linkable_path(@product) + assert_equal "/products/3", Routes.url_helpers.linkable_path(@product) + + assert_equal "/", params_path(@safe_params) + assert_equal "/", Routes.url_helpers.params_path(@safe_params) + assert_raises(ArgumentError) { params_path(@unsafe_params) } + assert_raises(ArgumentError) { Routes.url_helpers.params_path(@unsafe_params) } + + assert_equal "/basket", symbol_path + assert_equal "/basket", Routes.url_helpers.symbol_path + assert_equal "/basket", hash_path + assert_equal "/basket", Routes.url_helpers.hash_path + assert_equal "/admin/dashboard", array_path + assert_equal "/admin/dashboard", Routes.url_helpers.array_path + + assert_equal "/products?page=2", options_path(page: 2) + assert_equal "/products?page=2", Routes.url_helpers.options_path(page: 2) + assert_equal "/products?size=10", defaults_path + assert_equal "/products?size=10", Routes.url_helpers.defaults_path + assert_equal "/products?size=20", defaults_path(size: 20) + assert_equal "/products?size=20", Routes.url_helpers.defaults_path(size: 20) + + assert_equal "/basket", polymorphic_path(@basket) + assert_equal "/basket", Routes.url_helpers.polymorphic_path(@basket) + + assert_equal "/profile#details", polymorphic_path(@user) + assert_equal "/profile#details", Routes.url_helpers.polymorphic_path(@user) + + assert_equal "/profile#password", polymorphic_path(@user, anchor: "password") + assert_equal "/profile#password", Routes.url_helpers.polymorphic_path(@user, anchor: "password") + + assert_equal "/media/4", polymorphic_path(@video) + assert_equal "/media/4", Routes.url_helpers.polymorphic_path(@video) + assert_equal "/media/4", ActionDispatch::Routing::PolymorphicRoutes::HelperMethodBuilder.path.handle_model_call(self, @video) + end + + def test_direct_urls + assert_equal "http://www.rubyonrails.org", website_url + assert_equal "http://www.rubyonrails.org", Routes.url_helpers.website_url + + assert_equal "http://www.rubyonrails.org", string_url + assert_equal "http://www.rubyonrails.org", Routes.url_helpers.string_url + + assert_equal "http://www.example.com/basket", helper_url + assert_equal "http://www.example.com/basket", Routes.url_helpers.helper_url + + assert_equal "http://www.example.com/categories/1", linkable_url(@category) + assert_equal "http://www.example.com/categories/1", Routes.url_helpers.linkable_url(@category) + assert_equal "http://www.example.com/collections/2", linkable_url(@collection) + assert_equal "http://www.example.com/collections/2", Routes.url_helpers.linkable_url(@collection) + assert_equal "http://www.example.com/products/3", linkable_url(@product) + assert_equal "http://www.example.com/products/3", Routes.url_helpers.linkable_url(@product) + + assert_equal "http://www.example.com/", params_url(@safe_params) + assert_equal "http://www.example.com/", Routes.url_helpers.params_url(@safe_params) + assert_raises(ArgumentError) { params_url(@unsafe_params) } + assert_raises(ArgumentError) { Routes.url_helpers.params_url(@unsafe_params) } + + assert_equal "http://www.example.com/basket", symbol_url + assert_equal "http://www.example.com/basket", Routes.url_helpers.symbol_url + assert_equal "http://www.example.com/basket", hash_url + assert_equal "http://www.example.com/basket", Routes.url_helpers.hash_url + assert_equal "http://www.example.com/admin/dashboard", array_url + assert_equal "http://www.example.com/admin/dashboard", Routes.url_helpers.array_url + + assert_equal "http://www.example.com/products?page=2", options_url(page: 2) + assert_equal "http://www.example.com/products?page=2", Routes.url_helpers.options_url(page: 2) + assert_equal "http://www.example.com/products?size=10", defaults_url + assert_equal "http://www.example.com/products?size=10", Routes.url_helpers.defaults_url + assert_equal "http://www.example.com/products?size=20", defaults_url(size: 20) + assert_equal "http://www.example.com/products?size=20", Routes.url_helpers.defaults_url(size: 20) + + assert_equal "http://www.example.com/basket", polymorphic_url(@basket) + assert_equal "http://www.example.com/basket", Routes.url_helpers.polymorphic_url(@basket) + assert_equal "http://www.example.com/basket", polymorphic_url(@basket) + assert_equal "http://www.example.com/basket", Routes.url_helpers.polymorphic_url(@basket) + + assert_equal "http://www.example.com/profile#details", polymorphic_url(@user) + assert_equal "http://www.example.com/profile#details", Routes.url_helpers.polymorphic_url(@user) + + assert_equal "http://www.example.com/profile#password", polymorphic_url(@user, anchor: "password") + assert_equal "http://www.example.com/profile#password", Routes.url_helpers.polymorphic_url(@user, anchor: "password") + + assert_equal "http://www.example.com/media/4", polymorphic_url(@video) + assert_equal "http://www.example.com/media/4", Routes.url_helpers.polymorphic_url(@video) + assert_equal "http://www.example.com/media/4", ActionDispatch::Routing::PolymorphicRoutes::HelperMethodBuilder.url.handle_model_call(self, @video) + end + + def test_raises_argument_error + routes = ActionDispatch::Routing::RouteSet.new + + assert_raises ArgumentError do + routes.draw do + direct(1) { "http://www.rubyonrails.org" } + end + end + end + + def test_missing_class_raises_argument_error + routes = ActionDispatch::Routing::RouteSet.new + + assert_raises ArgumentError do + routes.draw do + direct(fragment: "core") { "http://www.rubyonrails.org" } + end + end + end +end -- cgit v1.2.3 From 80dcfd014b27e560f5c4b07ee5ffa98894d8ff63 Mon Sep 17 00:00:00 2001 From: Andrew White Date: Tue, 21 Feb 2017 08:30:36 +0000 Subject: Raise an error if `direct` is inside a scope block --- actionpack/test/dispatch/routing/direct_url_helpers_test.rb | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'actionpack/test') diff --git a/actionpack/test/dispatch/routing/direct_url_helpers_test.rb b/actionpack/test/dispatch/routing/direct_url_helpers_test.rb index 56bb7f13b3..24e46758cc 100644 --- a/actionpack/test/dispatch/routing/direct_url_helpers_test.rb +++ b/actionpack/test/dispatch/routing/direct_url_helpers_test.rb @@ -212,4 +212,16 @@ class TestDirectUrlHelpers < ActionDispatch::IntegrationTest end end end + + def test_defining_inside_a_scope_raises_runtime_error + routes = ActionDispatch::Routing::RouteSet.new + + assert_raises RuntimeError do + routes.draw do + namespace :admin do + direct(:rubyonrails) { "http://www.rubyonrails.org" } + end + end + end + end end -- cgit v1.2.3 From 81a6761af2b20183c78853caa4daea4ccf6b4cb7 Mon Sep 17 00:00:00 2001 From: Andrew White Date: Tue, 21 Feb 2017 08:49:15 +0000 Subject: Support mapping of non-model classes --- .../dispatch/routing/direct_url_helpers_test.rb | 23 ++++++++++++++++++++++ 1 file changed, 23 insertions(+) (limited to 'actionpack/test') diff --git a/actionpack/test/dispatch/routing/direct_url_helpers_test.rb b/actionpack/test/dispatch/routing/direct_url_helpers_test.rb index 24e46758cc..cc500e02c8 100644 --- a/actionpack/test/dispatch/routing/direct_url_helpers_test.rb +++ b/actionpack/test/dispatch/routing/direct_url_helpers_test.rb @@ -40,12 +40,25 @@ class TestDirectUrlHelpers < ActionDispatch::IntegrationTest class User < Model; end class Video < Model; end + class Article + attr_reader :id + + def self.name + "Article" + end + + def initialize(id) + @id = id + end + end + Routes = ActionDispatch::Routing::RouteSet.new Routes.draw do default_url_options host: "www.example.com" root to: "pages#index" get "/basket", to: "basket#show", as: :basket + get "/posts/:id", to: "posts#show", as: :post get "/profile", to: "users#profile", as: :profile get "/media/:id", to: "media#show", as: :media @@ -66,6 +79,7 @@ class TestDirectUrlHelpers < ActionDispatch::IntegrationTest direct(:options) { |options| [:products, options] } direct(:defaults, size: 10) { |options| [:products, options] } + direct(class: "Article") { |article| [:post, { id: article.id }] } direct(class: "Basket") { |basket| [:basket] } direct(class: "User", anchor: "details") { |user, options| [:profile, options] } direct(class: "Video") { |video| [:media, { id: video.id }] } @@ -86,6 +100,7 @@ class TestDirectUrlHelpers < ActionDispatch::IntegrationTest @basket = Basket.new @user = User.new @video = Video.new("4") + @article = Article.new("5") @path_params = { "controller" => "pages", "action" => "index" } @unsafe_params = ActionController::Parameters.new(@path_params) @safe_params = ActionController::Parameters.new(@path_params).permit(:controller, :action) @@ -139,6 +154,10 @@ class TestDirectUrlHelpers < ActionDispatch::IntegrationTest assert_equal "/media/4", polymorphic_path(@video) assert_equal "/media/4", Routes.url_helpers.polymorphic_path(@video) assert_equal "/media/4", ActionDispatch::Routing::PolymorphicRoutes::HelperMethodBuilder.path.handle_model_call(self, @video) + + assert_equal "/posts/5", polymorphic_path(@article) + assert_equal "/posts/5", Routes.url_helpers.polymorphic_path(@article) + assert_equal "/posts/5", ActionDispatch::Routing::PolymorphicRoutes::HelperMethodBuilder.path.handle_model_call(self, @article) end def test_direct_urls @@ -191,6 +210,10 @@ class TestDirectUrlHelpers < ActionDispatch::IntegrationTest assert_equal "http://www.example.com/media/4", polymorphic_url(@video) assert_equal "http://www.example.com/media/4", Routes.url_helpers.polymorphic_url(@video) assert_equal "http://www.example.com/media/4", ActionDispatch::Routing::PolymorphicRoutes::HelperMethodBuilder.url.handle_model_call(self, @video) + + assert_equal "http://www.example.com/posts/5", polymorphic_url(@article) + assert_equal "http://www.example.com/posts/5", Routes.url_helpers.polymorphic_url(@article) + assert_equal "http://www.example.com/posts/5", ActionDispatch::Routing::PolymorphicRoutes::HelperMethodBuilder.url.handle_model_call(self, @article) end def test_raises_argument_error -- cgit v1.2.3 From 050d7b1b921b7ce782fab23b5e418e6c539e78e0 Mon Sep 17 00:00:00 2001 From: Andrew White Date: Tue, 21 Feb 2017 12:49:45 +0000 Subject: Removed `model_name` method to prevent warning --- actionpack/test/dispatch/routing/direct_url_helpers_test.rb | 1 + 1 file changed, 1 insertion(+) (limited to 'actionpack/test') diff --git a/actionpack/test/dispatch/routing/direct_url_helpers_test.rb b/actionpack/test/dispatch/routing/direct_url_helpers_test.rb index cc500e02c8..9bdda60742 100644 --- a/actionpack/test/dispatch/routing/direct_url_helpers_test.rb +++ b/actionpack/test/dispatch/routing/direct_url_helpers_test.rb @@ -27,6 +27,7 @@ class TestDirectUrlHelpers < ActionDispatch::IntegrationTest @id = id end + remove_method :model_name def model_name @_model_name ||= ActiveModel::Name.new(self.class, nil, self.class.name.demodulize) end -- cgit v1.2.3 From d7c1e62c2cd2969b991bc4a1150b02b27f6d6e3f Mon Sep 17 00:00:00 2001 From: Andrew White Date: Tue, 21 Feb 2017 15:29:10 +0000 Subject: Split direct method into two Use a separate method called `resolve` for the custom polymorphic mapping to clarify the API. --- .../dispatch/routing/custom_url_helpers_test.rb | 291 +++++++++++++++++++++ .../dispatch/routing/direct_url_helpers_test.rb | 251 ------------------ 2 files changed, 291 insertions(+), 251 deletions(-) create mode 100644 actionpack/test/dispatch/routing/custom_url_helpers_test.rb delete mode 100644 actionpack/test/dispatch/routing/direct_url_helpers_test.rb (limited to 'actionpack/test') diff --git a/actionpack/test/dispatch/routing/custom_url_helpers_test.rb b/actionpack/test/dispatch/routing/custom_url_helpers_test.rb new file mode 100644 index 0000000000..6d230a2557 --- /dev/null +++ b/actionpack/test/dispatch/routing/custom_url_helpers_test.rb @@ -0,0 +1,291 @@ +require "abstract_unit" + +class TestCustomUrlHelpers < ActionDispatch::IntegrationTest + class Linkable + attr_reader :id + + def initialize(id) + @id = id + end + + def linkable_type + self.class.name.demodulize.underscore + end + end + + class Category < Linkable; end + class Collection < Linkable; end + class Product < Linkable; end + + class Model + extend ActiveModel::Naming + include ActiveModel::Conversion + + attr_reader :id + + def initialize(id = nil) + @id = id + end + + remove_method :model_name + def model_name + @_model_name ||= ActiveModel::Name.new(self.class, nil, self.class.name.demodulize) + end + + def persisted? + false + end + end + + class Basket < Model; end + class User < Model; end + class Video < Model; end + + class Article + attr_reader :id + + def self.name + "Article" + end + + def initialize(id) + @id = id + end + end + + class Page + attr_reader :id + + def self.name + super.demodulize + end + + def initialize(id) + @id = id + end + end + + class CategoryPage < Page; end + class ProductPage < Page; end + + Routes = ActionDispatch::Routing::RouteSet.new + Routes.draw do + default_url_options host: "www.example.com" + + root to: "pages#index" + get "/basket", to: "basket#show", as: :basket + get "/posts/:id", to: "posts#show", as: :post + get "/profile", to: "users#profile", as: :profile + get "/media/:id", to: "media#show", as: :media + get "/pages/:id", to: "pages#show", as: :page + + resources :categories, :collections, :products + + namespace :admin do + get "/dashboard", to: "dashboard#index" + end + + direct(:website) { "http://www.rubyonrails.org" } + direct("string") { "http://www.rubyonrails.org" } + direct(:helper) { basket_url } + direct(:linkable) { |linkable| [:"#{linkable.linkable_type}", { id: linkable.id }] } + direct(:params) { |params| params } + direct(:symbol) { :basket } + direct(:hash) { { controller: "basket", action: "show" } } + direct(:array) { [:admin, :dashboard] } + direct(:options) { |options| [:products, options] } + direct(:defaults, size: 10) { |options| [:products, options] } + + resolve("Article") { |article| [:post, { id: article.id }] } + resolve("Basket") { |basket| [:basket] } + resolve("User", anchor: "details") { |user, options| [:profile, options] } + resolve("Video") { |video| [:media, { id: video.id }] } + resolve(%w[Page CategoryPage ProductPage]) { |page| [:page, { id: page.id }] } + end + + APP = build_app Routes + + def app + APP + end + + include Routes.url_helpers + + def setup + @category = Category.new("1") + @collection = Collection.new("2") + @product = Product.new("3") + @basket = Basket.new + @user = User.new + @video = Video.new("4") + @article = Article.new("5") + @page = Page.new("6") + @category_page = CategoryPage.new("7") + @product_page = ProductPage.new("8") + @path_params = { "controller" => "pages", "action" => "index" } + @unsafe_params = ActionController::Parameters.new(@path_params) + @safe_params = ActionController::Parameters.new(@path_params).permit(:controller, :action) + end + + def test_direct_paths + assert_equal "http://www.rubyonrails.org", website_path + assert_equal "http://www.rubyonrails.org", Routes.url_helpers.website_path + + assert_equal "http://www.rubyonrails.org", string_path + assert_equal "http://www.rubyonrails.org", Routes.url_helpers.string_path + + assert_equal "http://www.example.com/basket", helper_url + assert_equal "http://www.example.com/basket", Routes.url_helpers.helper_url + + assert_equal "/categories/1", linkable_path(@category) + assert_equal "/categories/1", Routes.url_helpers.linkable_path(@category) + assert_equal "/collections/2", linkable_path(@collection) + assert_equal "/collections/2", Routes.url_helpers.linkable_path(@collection) + assert_equal "/products/3", linkable_path(@product) + assert_equal "/products/3", Routes.url_helpers.linkable_path(@product) + + assert_equal "/", params_path(@safe_params) + assert_equal "/", Routes.url_helpers.params_path(@safe_params) + assert_raises(ArgumentError) { params_path(@unsafe_params) } + assert_raises(ArgumentError) { Routes.url_helpers.params_path(@unsafe_params) } + + assert_equal "/basket", symbol_path + assert_equal "/basket", Routes.url_helpers.symbol_path + assert_equal "/basket", hash_path + assert_equal "/basket", Routes.url_helpers.hash_path + assert_equal "/admin/dashboard", array_path + assert_equal "/admin/dashboard", Routes.url_helpers.array_path + + assert_equal "/products?page=2", options_path(page: 2) + assert_equal "/products?page=2", Routes.url_helpers.options_path(page: 2) + assert_equal "/products?size=10", defaults_path + assert_equal "/products?size=10", Routes.url_helpers.defaults_path + assert_equal "/products?size=20", defaults_path(size: 20) + assert_equal "/products?size=20", Routes.url_helpers.defaults_path(size: 20) + end + + def test_direct_urls + assert_equal "http://www.rubyonrails.org", website_url + assert_equal "http://www.rubyonrails.org", Routes.url_helpers.website_url + + assert_equal "http://www.rubyonrails.org", string_url + assert_equal "http://www.rubyonrails.org", Routes.url_helpers.string_url + + assert_equal "http://www.example.com/basket", helper_url + assert_equal "http://www.example.com/basket", Routes.url_helpers.helper_url + + assert_equal "http://www.example.com/categories/1", linkable_url(@category) + assert_equal "http://www.example.com/categories/1", Routes.url_helpers.linkable_url(@category) + assert_equal "http://www.example.com/collections/2", linkable_url(@collection) + assert_equal "http://www.example.com/collections/2", Routes.url_helpers.linkable_url(@collection) + assert_equal "http://www.example.com/products/3", linkable_url(@product) + assert_equal "http://www.example.com/products/3", Routes.url_helpers.linkable_url(@product) + + assert_equal "http://www.example.com/", params_url(@safe_params) + assert_equal "http://www.example.com/", Routes.url_helpers.params_url(@safe_params) + assert_raises(ArgumentError) { params_url(@unsafe_params) } + assert_raises(ArgumentError) { Routes.url_helpers.params_url(@unsafe_params) } + + assert_equal "http://www.example.com/basket", symbol_url + assert_equal "http://www.example.com/basket", Routes.url_helpers.symbol_url + assert_equal "http://www.example.com/basket", hash_url + assert_equal "http://www.example.com/basket", Routes.url_helpers.hash_url + assert_equal "http://www.example.com/admin/dashboard", array_url + assert_equal "http://www.example.com/admin/dashboard", Routes.url_helpers.array_url + + assert_equal "http://www.example.com/products?page=2", options_url(page: 2) + assert_equal "http://www.example.com/products?page=2", Routes.url_helpers.options_url(page: 2) + assert_equal "http://www.example.com/products?size=10", defaults_url + assert_equal "http://www.example.com/products?size=10", Routes.url_helpers.defaults_url + assert_equal "http://www.example.com/products?size=20", defaults_url(size: 20) + assert_equal "http://www.example.com/products?size=20", Routes.url_helpers.defaults_url(size: 20) + end + + def test_resolve_paths + assert_equal "/basket", polymorphic_path(@basket) + assert_equal "/basket", Routes.url_helpers.polymorphic_path(@basket) + + assert_equal "/profile#details", polymorphic_path(@user) + assert_equal "/profile#details", Routes.url_helpers.polymorphic_path(@user) + + assert_equal "/profile#password", polymorphic_path(@user, anchor: "password") + assert_equal "/profile#password", Routes.url_helpers.polymorphic_path(@user, anchor: "password") + + assert_equal "/media/4", polymorphic_path(@video) + assert_equal "/media/4", Routes.url_helpers.polymorphic_path(@video) + assert_equal "/media/4", ActionDispatch::Routing::PolymorphicRoutes::HelperMethodBuilder.path.handle_model_call(self, @video) + + assert_equal "/posts/5", polymorphic_path(@article) + assert_equal "/posts/5", Routes.url_helpers.polymorphic_path(@article) + assert_equal "/posts/5", ActionDispatch::Routing::PolymorphicRoutes::HelperMethodBuilder.path.handle_model_call(self, @article) + + assert_equal "/pages/6", polymorphic_path(@page) + assert_equal "/pages/6", Routes.url_helpers.polymorphic_path(@page) + assert_equal "/pages/6", ActionDispatch::Routing::PolymorphicRoutes::HelperMethodBuilder.path.handle_model_call(self, @page) + + assert_equal "/pages/7", polymorphic_path(@category_page) + assert_equal "/pages/7", Routes.url_helpers.polymorphic_path(@category_page) + assert_equal "/pages/7", ActionDispatch::Routing::PolymorphicRoutes::HelperMethodBuilder.path.handle_model_call(self, @category_page) + + assert_equal "/pages/8", polymorphic_path(@product_page) + assert_equal "/pages/8", Routes.url_helpers.polymorphic_path(@product_page) + assert_equal "/pages/8", ActionDispatch::Routing::PolymorphicRoutes::HelperMethodBuilder.path.handle_model_call(self, @product_page) + end + + def test_resolve_urls + assert_equal "http://www.example.com/basket", polymorphic_url(@basket) + assert_equal "http://www.example.com/basket", Routes.url_helpers.polymorphic_url(@basket) + assert_equal "http://www.example.com/basket", polymorphic_url(@basket) + assert_equal "http://www.example.com/basket", Routes.url_helpers.polymorphic_url(@basket) + + assert_equal "http://www.example.com/profile#details", polymorphic_url(@user) + assert_equal "http://www.example.com/profile#details", Routes.url_helpers.polymorphic_url(@user) + + assert_equal "http://www.example.com/profile#password", polymorphic_url(@user, anchor: "password") + assert_equal "http://www.example.com/profile#password", Routes.url_helpers.polymorphic_url(@user, anchor: "password") + + assert_equal "http://www.example.com/media/4", polymorphic_url(@video) + assert_equal "http://www.example.com/media/4", Routes.url_helpers.polymorphic_url(@video) + assert_equal "http://www.example.com/media/4", ActionDispatch::Routing::PolymorphicRoutes::HelperMethodBuilder.url.handle_model_call(self, @video) + + assert_equal "http://www.example.com/posts/5", polymorphic_url(@article) + assert_equal "http://www.example.com/posts/5", Routes.url_helpers.polymorphic_url(@article) + assert_equal "http://www.example.com/posts/5", ActionDispatch::Routing::PolymorphicRoutes::HelperMethodBuilder.url.handle_model_call(self, @article) + + assert_equal "http://www.example.com/pages/6", polymorphic_url(@page) + assert_equal "http://www.example.com/pages/6", Routes.url_helpers.polymorphic_url(@page) + assert_equal "http://www.example.com/pages/6", ActionDispatch::Routing::PolymorphicRoutes::HelperMethodBuilder.url.handle_model_call(self, @page) + + assert_equal "http://www.example.com/pages/7", polymorphic_url(@category_page) + assert_equal "http://www.example.com/pages/7", Routes.url_helpers.polymorphic_url(@category_page) + assert_equal "http://www.example.com/pages/7", ActionDispatch::Routing::PolymorphicRoutes::HelperMethodBuilder.url.handle_model_call(self, @category_page) + + assert_equal "http://www.example.com/pages/8", polymorphic_url(@product_page) + assert_equal "http://www.example.com/pages/8", Routes.url_helpers.polymorphic_url(@product_page) + assert_equal "http://www.example.com/pages/8", ActionDispatch::Routing::PolymorphicRoutes::HelperMethodBuilder.url.handle_model_call(self, @product_page) + end + + def test_defining_direct_inside_a_scope_raises_runtime_error + routes = ActionDispatch::Routing::RouteSet.new + + assert_raises RuntimeError do + routes.draw do + namespace :admin do + direct(:rubyonrails) { "http://www.rubyonrails.org" } + end + end + end + end + + def test_defining_resolve_inside_a_scope_raises_runtime_error + routes = ActionDispatch::Routing::RouteSet.new + + assert_raises RuntimeError do + routes.draw do + namespace :admin do + resolve("User") { "/profile" } + end + end + end + end +end diff --git a/actionpack/test/dispatch/routing/direct_url_helpers_test.rb b/actionpack/test/dispatch/routing/direct_url_helpers_test.rb deleted file mode 100644 index 9bdda60742..0000000000 --- a/actionpack/test/dispatch/routing/direct_url_helpers_test.rb +++ /dev/null @@ -1,251 +0,0 @@ -require "abstract_unit" - -class TestDirectUrlHelpers < ActionDispatch::IntegrationTest - class Linkable - attr_reader :id - - def initialize(id) - @id = id - end - - def linkable_type - self.class.name.demodulize.underscore - end - end - - class Category < Linkable; end - class Collection < Linkable; end - class Product < Linkable; end - - class Model - extend ActiveModel::Naming - include ActiveModel::Conversion - - attr_reader :id - - def initialize(id = nil) - @id = id - end - - remove_method :model_name - def model_name - @_model_name ||= ActiveModel::Name.new(self.class, nil, self.class.name.demodulize) - end - - def persisted? - false - end - end - - class Basket < Model; end - class User < Model; end - class Video < Model; end - - class Article - attr_reader :id - - def self.name - "Article" - end - - def initialize(id) - @id = id - end - end - - Routes = ActionDispatch::Routing::RouteSet.new - Routes.draw do - default_url_options host: "www.example.com" - - root to: "pages#index" - get "/basket", to: "basket#show", as: :basket - get "/posts/:id", to: "posts#show", as: :post - get "/profile", to: "users#profile", as: :profile - get "/media/:id", to: "media#show", as: :media - - resources :categories, :collections, :products - - namespace :admin do - get "/dashboard", to: "dashboard#index" - end - - direct(:website) { "http://www.rubyonrails.org" } - direct("string") { "http://www.rubyonrails.org" } - direct(:helper) { basket_url } - direct(:linkable) { |linkable| [:"#{linkable.linkable_type}", { id: linkable.id }] } - direct(:params) { |params| params } - direct(:symbol) { :basket } - direct(:hash) { { controller: "basket", action: "show" } } - direct(:array) { [:admin, :dashboard] } - direct(:options) { |options| [:products, options] } - direct(:defaults, size: 10) { |options| [:products, options] } - - direct(class: "Article") { |article| [:post, { id: article.id }] } - direct(class: "Basket") { |basket| [:basket] } - direct(class: "User", anchor: "details") { |user, options| [:profile, options] } - direct(class: "Video") { |video| [:media, { id: video.id }] } - end - - APP = build_app Routes - - def app - APP - end - - include Routes.url_helpers - - def setup - @category = Category.new("1") - @collection = Collection.new("2") - @product = Product.new("3") - @basket = Basket.new - @user = User.new - @video = Video.new("4") - @article = Article.new("5") - @path_params = { "controller" => "pages", "action" => "index" } - @unsafe_params = ActionController::Parameters.new(@path_params) - @safe_params = ActionController::Parameters.new(@path_params).permit(:controller, :action) - end - - def test_direct_paths - assert_equal "http://www.rubyonrails.org", website_path - assert_equal "http://www.rubyonrails.org", Routes.url_helpers.website_path - - assert_equal "http://www.rubyonrails.org", string_path - assert_equal "http://www.rubyonrails.org", Routes.url_helpers.string_path - - assert_equal "http://www.example.com/basket", helper_url - assert_equal "http://www.example.com/basket", Routes.url_helpers.helper_url - - assert_equal "/categories/1", linkable_path(@category) - assert_equal "/categories/1", Routes.url_helpers.linkable_path(@category) - assert_equal "/collections/2", linkable_path(@collection) - assert_equal "/collections/2", Routes.url_helpers.linkable_path(@collection) - assert_equal "/products/3", linkable_path(@product) - assert_equal "/products/3", Routes.url_helpers.linkable_path(@product) - - assert_equal "/", params_path(@safe_params) - assert_equal "/", Routes.url_helpers.params_path(@safe_params) - assert_raises(ArgumentError) { params_path(@unsafe_params) } - assert_raises(ArgumentError) { Routes.url_helpers.params_path(@unsafe_params) } - - assert_equal "/basket", symbol_path - assert_equal "/basket", Routes.url_helpers.symbol_path - assert_equal "/basket", hash_path - assert_equal "/basket", Routes.url_helpers.hash_path - assert_equal "/admin/dashboard", array_path - assert_equal "/admin/dashboard", Routes.url_helpers.array_path - - assert_equal "/products?page=2", options_path(page: 2) - assert_equal "/products?page=2", Routes.url_helpers.options_path(page: 2) - assert_equal "/products?size=10", defaults_path - assert_equal "/products?size=10", Routes.url_helpers.defaults_path - assert_equal "/products?size=20", defaults_path(size: 20) - assert_equal "/products?size=20", Routes.url_helpers.defaults_path(size: 20) - - assert_equal "/basket", polymorphic_path(@basket) - assert_equal "/basket", Routes.url_helpers.polymorphic_path(@basket) - - assert_equal "/profile#details", polymorphic_path(@user) - assert_equal "/profile#details", Routes.url_helpers.polymorphic_path(@user) - - assert_equal "/profile#password", polymorphic_path(@user, anchor: "password") - assert_equal "/profile#password", Routes.url_helpers.polymorphic_path(@user, anchor: "password") - - assert_equal "/media/4", polymorphic_path(@video) - assert_equal "/media/4", Routes.url_helpers.polymorphic_path(@video) - assert_equal "/media/4", ActionDispatch::Routing::PolymorphicRoutes::HelperMethodBuilder.path.handle_model_call(self, @video) - - assert_equal "/posts/5", polymorphic_path(@article) - assert_equal "/posts/5", Routes.url_helpers.polymorphic_path(@article) - assert_equal "/posts/5", ActionDispatch::Routing::PolymorphicRoutes::HelperMethodBuilder.path.handle_model_call(self, @article) - end - - def test_direct_urls - assert_equal "http://www.rubyonrails.org", website_url - assert_equal "http://www.rubyonrails.org", Routes.url_helpers.website_url - - assert_equal "http://www.rubyonrails.org", string_url - assert_equal "http://www.rubyonrails.org", Routes.url_helpers.string_url - - assert_equal "http://www.example.com/basket", helper_url - assert_equal "http://www.example.com/basket", Routes.url_helpers.helper_url - - assert_equal "http://www.example.com/categories/1", linkable_url(@category) - assert_equal "http://www.example.com/categories/1", Routes.url_helpers.linkable_url(@category) - assert_equal "http://www.example.com/collections/2", linkable_url(@collection) - assert_equal "http://www.example.com/collections/2", Routes.url_helpers.linkable_url(@collection) - assert_equal "http://www.example.com/products/3", linkable_url(@product) - assert_equal "http://www.example.com/products/3", Routes.url_helpers.linkable_url(@product) - - assert_equal "http://www.example.com/", params_url(@safe_params) - assert_equal "http://www.example.com/", Routes.url_helpers.params_url(@safe_params) - assert_raises(ArgumentError) { params_url(@unsafe_params) } - assert_raises(ArgumentError) { Routes.url_helpers.params_url(@unsafe_params) } - - assert_equal "http://www.example.com/basket", symbol_url - assert_equal "http://www.example.com/basket", Routes.url_helpers.symbol_url - assert_equal "http://www.example.com/basket", hash_url - assert_equal "http://www.example.com/basket", Routes.url_helpers.hash_url - assert_equal "http://www.example.com/admin/dashboard", array_url - assert_equal "http://www.example.com/admin/dashboard", Routes.url_helpers.array_url - - assert_equal "http://www.example.com/products?page=2", options_url(page: 2) - assert_equal "http://www.example.com/products?page=2", Routes.url_helpers.options_url(page: 2) - assert_equal "http://www.example.com/products?size=10", defaults_url - assert_equal "http://www.example.com/products?size=10", Routes.url_helpers.defaults_url - assert_equal "http://www.example.com/products?size=20", defaults_url(size: 20) - assert_equal "http://www.example.com/products?size=20", Routes.url_helpers.defaults_url(size: 20) - - assert_equal "http://www.example.com/basket", polymorphic_url(@basket) - assert_equal "http://www.example.com/basket", Routes.url_helpers.polymorphic_url(@basket) - assert_equal "http://www.example.com/basket", polymorphic_url(@basket) - assert_equal "http://www.example.com/basket", Routes.url_helpers.polymorphic_url(@basket) - - assert_equal "http://www.example.com/profile#details", polymorphic_url(@user) - assert_equal "http://www.example.com/profile#details", Routes.url_helpers.polymorphic_url(@user) - - assert_equal "http://www.example.com/profile#password", polymorphic_url(@user, anchor: "password") - assert_equal "http://www.example.com/profile#password", Routes.url_helpers.polymorphic_url(@user, anchor: "password") - - assert_equal "http://www.example.com/media/4", polymorphic_url(@video) - assert_equal "http://www.example.com/media/4", Routes.url_helpers.polymorphic_url(@video) - assert_equal "http://www.example.com/media/4", ActionDispatch::Routing::PolymorphicRoutes::HelperMethodBuilder.url.handle_model_call(self, @video) - - assert_equal "http://www.example.com/posts/5", polymorphic_url(@article) - assert_equal "http://www.example.com/posts/5", Routes.url_helpers.polymorphic_url(@article) - assert_equal "http://www.example.com/posts/5", ActionDispatch::Routing::PolymorphicRoutes::HelperMethodBuilder.url.handle_model_call(self, @article) - end - - def test_raises_argument_error - routes = ActionDispatch::Routing::RouteSet.new - - assert_raises ArgumentError do - routes.draw do - direct(1) { "http://www.rubyonrails.org" } - end - end - end - - def test_missing_class_raises_argument_error - routes = ActionDispatch::Routing::RouteSet.new - - assert_raises ArgumentError do - routes.draw do - direct(fragment: "core") { "http://www.rubyonrails.org" } - end - end - end - - def test_defining_inside_a_scope_raises_runtime_error - routes = ActionDispatch::Routing::RouteSet.new - - assert_raises RuntimeError do - routes.draw do - namespace :admin do - direct(:rubyonrails) { "http://www.rubyonrails.org" } - end - end - end - end -end -- cgit v1.2.3 From a92bd65e37bb08a08fd0aaf4d12c7c6cb92c2c17 Mon Sep 17 00:00:00 2001 From: Andrew White Date: Wed, 22 Feb 2017 14:05:37 +0000 Subject: Add more missing requires Further missing requires for Timeout exposed due to Bundler 1.14.5 --- actionpack/test/controller/live_stream_test.rb | 1 + 1 file changed, 1 insertion(+) (limited to 'actionpack/test') diff --git a/actionpack/test/controller/live_stream_test.rb b/actionpack/test/controller/live_stream_test.rb index e76628b936..581081dd07 100644 --- a/actionpack/test/controller/live_stream_test.rb +++ b/actionpack/test/controller/live_stream_test.rb @@ -1,4 +1,5 @@ require "abstract_unit" +require "timeout" require "concurrent/atomic/count_down_latch" Thread.abort_on_exception = true -- cgit v1.2.3 From 630e709ea6cb535b45a9cbd89c08c572646f5608 Mon Sep 17 00:00:00 2001 From: Andrew White Date: Wed, 22 Feb 2017 21:22:22 +0000 Subject: Clarify use of params in `direct` Since a `direct` url helper block is evaluated using `instance_exec` then methods that are available in the instance context can be accessed, e.g. the params object in a controller action or view. This wasn't clear from the example so expand on that point and add a test case for this situation. --- .../test/dispatch/routing/custom_url_helpers_test.rb | 14 ++++++++++++++ 1 file changed, 14 insertions(+) (limited to 'actionpack/test') diff --git a/actionpack/test/dispatch/routing/custom_url_helpers_test.rb b/actionpack/test/dispatch/routing/custom_url_helpers_test.rb index 6d230a2557..f85b989892 100644 --- a/actionpack/test/dispatch/routing/custom_url_helpers_test.rb +++ b/actionpack/test/dispatch/routing/custom_url_helpers_test.rb @@ -96,6 +96,10 @@ class TestCustomUrlHelpers < ActionDispatch::IntegrationTest direct(:options) { |options| [:products, options] } direct(:defaults, size: 10) { |options| [:products, options] } + direct(:browse, page: 1, size: 10) do |options| + [:products, options.merge(params.permit(:page, :size).to_h.symbolize_keys)] + end + resolve("Article") { |article| [:post, { id: article.id }] } resolve("Basket") { |basket| [:basket] } resolve("User", anchor: "details") { |user, options| [:profile, options] } @@ -127,6 +131,10 @@ class TestCustomUrlHelpers < ActionDispatch::IntegrationTest @safe_params = ActionController::Parameters.new(@path_params).permit(:controller, :action) end + def params + ActionController::Parameters.new(page: 2, size: 25) + end + def test_direct_paths assert_equal "http://www.rubyonrails.org", website_path assert_equal "http://www.rubyonrails.org", Routes.url_helpers.website_path @@ -162,6 +170,9 @@ class TestCustomUrlHelpers < ActionDispatch::IntegrationTest assert_equal "/products?size=10", Routes.url_helpers.defaults_path assert_equal "/products?size=20", defaults_path(size: 20) assert_equal "/products?size=20", Routes.url_helpers.defaults_path(size: 20) + + assert_equal "/products?page=2&size=25", browse_path + assert_raises(NameError) { Routes.url_helpers.browse_path } end def test_direct_urls @@ -199,6 +210,9 @@ class TestCustomUrlHelpers < ActionDispatch::IntegrationTest assert_equal "http://www.example.com/products?size=10", Routes.url_helpers.defaults_url assert_equal "http://www.example.com/products?size=20", defaults_url(size: 20) assert_equal "http://www.example.com/products?size=20", Routes.url_helpers.defaults_url(size: 20) + + assert_equal "http://www.example.com/products?page=2&size=25", browse_url + assert_raises(NameError) { Routes.url_helpers.browse_url } end def test_resolve_paths -- cgit v1.2.3 From ebaf1e709fb64cf7bbddb8f539863c2f24c3d3d5 Mon Sep 17 00:00:00 2001 From: "yuuji.yaginuma" Date: Thu, 23 Feb 2017 22:30:01 +0900 Subject: Do not take screenshot when test skipped --- .../test/dispatch/system_testing/screenshot_helper_test.rb | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'actionpack/test') diff --git a/actionpack/test/dispatch/system_testing/screenshot_helper_test.rb b/actionpack/test/dispatch/system_testing/screenshot_helper_test.rb index 8c14f799b0..3b4ea96c4f 100644 --- a/actionpack/test/dispatch/system_testing/screenshot_helper_test.rb +++ b/actionpack/test/dispatch/system_testing/screenshot_helper_test.rb @@ -15,4 +15,14 @@ class ScreenshotHelperTest < ActiveSupport::TestCase assert_equal "tmp/screenshots/failures_x.png", new_test.send(:image_path) end end + + test "image path does not include failures text if test skipped" do + new_test = ActionDispatch::SystemTestCase.new("x") + + new_test.stub :passed?, false do + new_test.stub :skipped?, true do + assert_equal "tmp/screenshots/x.png", new_test.send(:image_path) + end + end + end end -- cgit v1.2.3 From 704853b58be60b307261da9b9990a4c87c68860e Mon Sep 17 00:00:00 2001 From: Lucas Mazza Date: Thu, 23 Feb 2017 22:48:12 -0300 Subject: Change `SystemTestCase.driven_by` to use `setup`/`teardown` hooks Previously, `driven_by` would change the Capybara configuration when the test case is loaded, and having multiple test classes with different `driven_by` configs would fail as the last loaded would be effective. --- .../system_testing/system_test_case_test.rb | 26 ++++++++++++---------- 1 file changed, 14 insertions(+), 12 deletions(-) (limited to 'actionpack/test') diff --git a/actionpack/test/dispatch/system_testing/system_test_case_test.rb b/actionpack/test/dispatch/system_testing/system_test_case_test.rb index a384902a14..ff01d6739a 100644 --- a/actionpack/test/dispatch/system_testing/system_test_case_test.rb +++ b/actionpack/test/dispatch/system_testing/system_test_case_test.rb @@ -1,21 +1,23 @@ require "abstract_unit" -class SystemTestCaseTest < ActiveSupport::TestCase - test "driven_by sets Capybara's default driver to poltergeist" do - ActionDispatch::SystemTestCase.driven_by :poltergeist - - assert_equal :poltergeist, Capybara.default_driver +class DrivenByCaseTestTest < ActiveSupport::TestCase + test "selenium? returns false if driver is poltergeist" do + assert_not ActionDispatch::SystemTestCase.selenium?(:poltergeist) end +end - test "driven_by sets Capybara's drivers respectively" do - ActionDispatch::SystemTestCase.driven_by :selenium, using: :chrome +class DrivenByRackTestTest < ActionDispatch::SystemTestCase + driven_by :rack_test - assert_includes Capybara.drivers, :selenium - assert_includes Capybara.drivers, :chrome - assert_equal :chrome, Capybara.default_driver + test "uses rack_test" do + assert_equal :rack_test, Capybara.current_driver end +end - test "selenium? returns false if driver is poltergeist" do - assert_not ActionDispatch::SystemTestCase.selenium?(:poltergeist) +class DrivenBySeleniumWithChromeTest < ActionDispatch::SystemTestCase + driven_by :selenium, using: :chrome + + test "uses selenium" do + assert_equal :chrome, Capybara.current_driver end end -- cgit v1.2.3 From 29c02709cd68a122c5db4f58ec0e901fe3d507cc Mon Sep 17 00:00:00 2001 From: Dylan Thacker-Smith Date: Fri, 24 Feb 2017 17:26:54 -0500 Subject: Add missing gzip footer check in ActiveSupport::Gzip.decompress A gzip file has a checksum and length for the decompressed data in its footer which isn't checked by just calling Zlib::GzipReader#read. Calling Zlib::GzipReader#close must be called after reading to the end of the file causes this check to be done, which is done by Zlib::GzipReader.wrap after its block is called. --- actionpack/test/dispatch/static_test.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'actionpack/test') diff --git a/actionpack/test/dispatch/static_test.rb b/actionpack/test/dispatch/static_test.rb index bd8318f5f6..3082d1072b 100644 --- a/actionpack/test/dispatch/static_test.rb +++ b/actionpack/test/dispatch/static_test.rb @@ -224,7 +224,7 @@ module StaticTests def assert_gzip(file_name, response) expected = File.read("#{FIXTURE_LOAD_PATH}/#{public_path}" + file_name) - actual = Zlib::GzipReader.new(StringIO.new(response.body)).read + actual = ActiveSupport::Gzip.decompress(response.body) assert_equal expected, actual end -- cgit v1.2.3 From 558336ee2afad116077df07b5b963077ac5f5021 Mon Sep 17 00:00:00 2001 From: Andrew White Date: Sat, 25 Feb 2017 16:55:07 +0000 Subject: Commit flash changes when using a redirect route. In ca324a0 the flash middleware was effectively removed by its constructor returning the app it was passed and the `commit_flash` call was moved to the `ActionController::Metal#dispatch` method. This broke any redirect routes that modified the flash because the redirect happens before `dispatch` gets called. To fix it, this commit adds a `commit_flash` call in the `serve` method of `ActionDispatch::Routing::Redirect`. Fixes #27992. --- actionpack/test/dispatch/routing_test.rb | 49 ++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) (limited to 'actionpack/test') diff --git a/actionpack/test/dispatch/routing_test.rb b/actionpack/test/dispatch/routing_test.rb index 53758a4fbc..d563df91df 100644 --- a/actionpack/test/dispatch/routing_test.rb +++ b/actionpack/test/dispatch/routing_test.rb @@ -4913,3 +4913,52 @@ class TestInternalRoutingParams < ActionDispatch::IntegrationTest ) end end + +class FlashRedirectTest < ActionDispatch::IntegrationTest + SessionKey = "_myapp_session" + Generator = ActiveSupport::LegacyKeyGenerator.new("b3c631c314c0bbca50c1b2843150fe33") + + class KeyGeneratorMiddleware + def initialize(app) + @app = app + end + + def call(env) + env["action_dispatch.key_generator"] ||= Generator + @app.call(env) + end + end + + class FooController < ActionController::Base + def bar + render plain: (flash[:foo] || "foo") + end + end + + Routes = ActionDispatch::Routing::RouteSet.new + Routes.draw do + get "/foo", to: redirect { |params, req| req.flash[:foo] = "bar"; "/bar" } + get "/bar", to: "flash_redirect_test/foo#bar" + end + + APP = build_app Routes do |middleware| + middleware.use KeyGeneratorMiddleware + middleware.use ActionDispatch::Session::CookieStore, key: SessionKey + middleware.use ActionDispatch::Flash + middleware.delete ActionDispatch::ShowExceptions + end + + def app + APP + end + + include Routes.url_helpers + + def test_block_redirect_commits_flash + get "/foo", env: { "action_dispatch.key_generator" => Generator } + assert_response :redirect + + follow_redirect! + assert_equal "bar", response.body + end +end -- cgit v1.2.3 From 2ee4058cc5c9327ddc5bfecc05e625320dd166a1 Mon Sep 17 00:00:00 2001 From: "yuuji.yaginuma" Date: Wed, 1 Mar 2017 07:54:28 +0900 Subject: Do not take screenshot if driver does not support screenshot `Capybara::RackTest::Driver` does not support taking screenshots. If call `#save_screenshot` on `Capybara::RackTest::Driver` will raise the error. ```ruby Error: UsersTest#test_visiting_the_index: Capybara::NotSupportedByDriverError: Capybara::Driver::Base#save_screenshot ``` To prevent errors, if driver does not support screenshot, do not call it. --- .../system_testing/screenshot_helper_test.rb | 25 ++++++++++++++++++++++ 1 file changed, 25 insertions(+) (limited to 'actionpack/test') diff --git a/actionpack/test/dispatch/system_testing/screenshot_helper_test.rb b/actionpack/test/dispatch/system_testing/screenshot_helper_test.rb index 3b4ea96c4f..d6b501b3ac 100644 --- a/actionpack/test/dispatch/system_testing/screenshot_helper_test.rb +++ b/actionpack/test/dispatch/system_testing/screenshot_helper_test.rb @@ -1,5 +1,6 @@ require "abstract_unit" require "action_dispatch/system_testing/test_helpers/screenshot_helper" +require "capybara/dsl" class ScreenshotHelperTest < ActiveSupport::TestCase test "image path is saved in tmp directory" do @@ -25,4 +26,28 @@ class ScreenshotHelperTest < ActiveSupport::TestCase end end end + + test "rack_test driver does not support screenshot" do + begin + original_driver = Capybara.current_driver + Capybara.current_driver = :rack_test + + new_test = ActionDispatch::SystemTestCase.new("x") + assert_not new_test.send(:supports_screenshot?) + ensure + Capybara.current_driver = original_driver + end + end + + test "selenium driver supports screenshot" do + begin + original_driver = Capybara.current_driver + Capybara.current_driver = :selenium + + new_test = ActionDispatch::SystemTestCase.new("x") + assert new_test.send(:supports_screenshot?) + ensure + Capybara.current_driver = original_driver + end + end end -- cgit v1.2.3 From 32046deced9ab891c59a5474419b69824b3c8803 Mon Sep 17 00:00:00 2001 From: Giorgos Vrettos Date: Thu, 2 Mar 2017 11:54:58 +0200 Subject: Fix malformed asset_url when rendering template with ActionController::Renderer --- actionpack/test/controller/renderer_test.rb | 14 ++++++++++++++ 1 file changed, 14 insertions(+) (limited to 'actionpack/test') diff --git a/actionpack/test/controller/renderer_test.rb b/actionpack/test/controller/renderer_test.rb index 866600b935..81b32a67b3 100644 --- a/actionpack/test/controller/renderer_test.rb +++ b/actionpack/test/controller/renderer_test.rb @@ -103,6 +103,20 @@ class RendererTest < ActiveSupport::TestCase assert_equal "true", content end + test "return valid asset url with defaults" do + renderer = ApplicationController.renderer + content = renderer.render inline: "<%= asset_url 'asset.jpg' %>" + + assert_equal "http://example.org/asset.jpg", content + end + + test "return valid asset url when https is true" do + renderer = ApplicationController.renderer.new https: true + content = renderer.render inline: "<%= asset_url 'asset.jpg' %>" + + assert_equal "https://example.org/asset.jpg", content + end + private def render @render ||= ApplicationController.renderer.method(:render) -- cgit v1.2.3 From 26dcd7f7eec634eef4c16d235a4e574eefa44364 Mon Sep 17 00:00:00 2001 From: "T.J. Schuck" Date: Tue, 7 Mar 2017 19:30:17 -0500 Subject: Tests for delegated public methods on AC::Parameters --- .../test/controller/parameters/accessors_test.rb | 63 ++++++++++++++++++++++ 1 file changed, 63 insertions(+) (limited to 'actionpack/test') diff --git a/actionpack/test/controller/parameters/accessors_test.rb b/actionpack/test/controller/parameters/accessors_test.rb index 2893eb7b91..f17e93a431 100644 --- a/actionpack/test/controller/parameters/accessors_test.rb +++ b/actionpack/test/controller/parameters/accessors_test.rb @@ -53,6 +53,15 @@ class ParametersAccessorsTest < ActiveSupport::TestCase @params.each_pair { |key, value| assert_not(value.permitted?) if key == "person" } end + test "empty? returns true when params contains no key/value pairs" do + params = ActionController::Parameters.new + assert params.empty? + end + + test "empty? returns false when any params are present" do + refute @params.empty? + end + test "except retains permitted status" do @params.permit! assert @params.except(:person).permitted? @@ -75,6 +84,45 @@ class ParametersAccessorsTest < ActiveSupport::TestCase assert_not @params[:person].fetch(:name).permitted? end + test "has_key? returns true if the given key is present in the params" do + assert @params.has_key?(:person) + end + + test "has_key? returns false if the given key is not present in the params" do + refute @params.has_key?(:address) + end + + test "has_value? returns true if the given value is present in the params" do + params = ActionController::Parameters.new(city: "Chicago", state: "Illinois") + assert params.has_value?("Chicago") + end + + test "has_value? returns false if the given value is not present in the params" do + params = ActionController::Parameters.new(city: "Chicago", state: "Illinois") + refute @params.has_value?("New York") + end + + test "include? returns true if the given key is present in the params" do + assert @params.include?(:person) + end + + test "include? returns false if the given key is not present in the params" do + refute @params.include?(:address) + end + + test "key? returns true if the given key is present in the params" do + assert @params.key?(:person) + end + + test "key? returns false if the given key is not present in the params" do + refute @params.key?(:address) + end + + test "keys returns an array of the keys of the params" do + assert_equal ["person"], @params.keys + assert_equal ["age", "name", "addresses"], @params[:person].keys + end + test "reject retains permitted status" do assert_not @params.reject { |k| k == "person" }.permitted? end @@ -120,6 +168,21 @@ class ParametersAccessorsTest < ActiveSupport::TestCase assert_not @params.transform_values { |v| v }.permitted? end + test "value? returns true if the given value is present in the params" do + params = ActionController::Parameters.new(city: "Chicago", state: "Illinois") + assert params.value?("Chicago") + end + + test "value? returns false if the given value is not present in the params" do + params = ActionController::Parameters.new(city: "Chicago", state: "Illinois") + refute params.value?("New York") + end + + test "values returns an array of the values of the params" do + params = ActionController::Parameters.new(city: "Chicago", state: "Illinois") + assert_equal ["Chicago", "Illinois"], params.values + end + test "values_at retains permitted status" do @params.permit! assert @params.values_at(:person).first.permitted? -- cgit v1.2.3 From 4dbebe487df54e8684183f3b3154639a77d8deaa Mon Sep 17 00:00:00 2001 From: eileencodes Date: Sun, 5 Mar 2017 09:38:27 -0500 Subject: Refactor system test driver/browser Since using a browser is only for selenium it doesn't really make sense to have a separate class for handling it there. This brings a lot of the if/else out of the main SystemTestCase class and into the Driver class so we can abstract away all that extra work. --- actionpack/test/dispatch/system_testing/browser_test.rb | 10 ---------- actionpack/test/dispatch/system_testing/driver_test.rb | 11 +++++++++++ .../test/dispatch/system_testing/system_test_case_test.rb | 6 ------ 3 files changed, 11 insertions(+), 16 deletions(-) delete mode 100644 actionpack/test/dispatch/system_testing/browser_test.rb (limited to 'actionpack/test') diff --git a/actionpack/test/dispatch/system_testing/browser_test.rb b/actionpack/test/dispatch/system_testing/browser_test.rb deleted file mode 100644 index b0ad309492..0000000000 --- a/actionpack/test/dispatch/system_testing/browser_test.rb +++ /dev/null @@ -1,10 +0,0 @@ -require "abstract_unit" -require "action_dispatch/system_testing/browser" - -class BrowserTest < ActiveSupport::TestCase - test "initializing the browser" do - browser = ActionDispatch::SystemTesting::Browser.new(:chrome, [ 1400, 1400 ]) - assert_equal :chrome, browser.instance_variable_get(:@name) - assert_equal [ 1400, 1400 ], browser.instance_variable_get(:@screen_size) - end -end diff --git a/actionpack/test/dispatch/system_testing/driver_test.rb b/actionpack/test/dispatch/system_testing/driver_test.rb index f0ebdb38db..96d598a7a3 100644 --- a/actionpack/test/dispatch/system_testing/driver_test.rb +++ b/actionpack/test/dispatch/system_testing/driver_test.rb @@ -6,4 +6,15 @@ class DriverTest < ActiveSupport::TestCase driver = ActionDispatch::SystemTesting::Driver.new(:selenium) assert_equal :selenium, driver.instance_variable_get(:@name) end + + test "initializing the driver with a browser" do + driver = ActionDispatch::SystemTesting::Driver.new(:selenium, using: :chrome, screen_size: [ 1400, 1400 ]) + assert_equal :selenium, driver.instance_variable_get(:@name) + assert_equal :chrome, driver.instance_variable_get(:@using) + assert_equal [ 1400, 1400 ], driver.instance_variable_get(:@screen_size) + end + + test "selenium? returns false if driver is poltergeist" do + assert_not ActionDispatch::SystemTesting::Driver.new(:poltergeist).send(:selenium?) + end end diff --git a/actionpack/test/dispatch/system_testing/system_test_case_test.rb b/actionpack/test/dispatch/system_testing/system_test_case_test.rb index ff01d6739a..c9003e3841 100644 --- a/actionpack/test/dispatch/system_testing/system_test_case_test.rb +++ b/actionpack/test/dispatch/system_testing/system_test_case_test.rb @@ -1,11 +1,5 @@ require "abstract_unit" -class DrivenByCaseTestTest < ActiveSupport::TestCase - test "selenium? returns false if driver is poltergeist" do - assert_not ActionDispatch::SystemTestCase.selenium?(:poltergeist) - end -end - class DrivenByRackTestTest < ActionDispatch::SystemTestCase driven_by :rack_test -- cgit v1.2.3 From 7c9af60e5c26941dc9ec6a263f5fc5fe10050cba Mon Sep 17 00:00:00 2001 From: eileencodes Date: Mon, 6 Mar 2017 12:33:34 -0500 Subject: Call system test driver per-instance rather than globally Previously the system test subclasses would call `driven_by` when the app booted and not again when the test was initialized which resulted in the driver from whichever class was called last to be used in tests. In rails/rails#28144 the `driven_by` method was changed to run `use` on setup and `reset` on teardown. While this was a viable fix this really pointed to the problem that system test `driven_by` was a global setting, rather than a per-class setting. To alieviate this problem calling the driver should be done on an instance level, rather than on the global level. I added an `initialize` method to `SystemTestCase` which will call `use` on the superclass driver. Running the server has been moved to `start_application` so that it only needs to be called once on boot and no options from `driven_by` were being passed to it. This required a largish rewrite of the tests. Each test needs to utilize the subclass so that it can properly test the drivers. `ActionDispatch::SystemTestCase` shouldn't be called directly anymore. --- actionpack/test/abstract_unit.rb | 8 ++++++ .../test/dispatch/system_testing/driver_test.rb | 6 ++--- .../system_testing/screenshot_helper_test.rb | 30 +++++++--------------- .../system_testing/system_test_case_test.rb | 10 +++----- 4 files changed, 23 insertions(+), 31 deletions(-) (limited to 'actionpack/test') diff --git a/actionpack/test/abstract_unit.rb b/actionpack/test/abstract_unit.rb index 459b0d6c54..4185ce1a1f 100644 --- a/actionpack/test/abstract_unit.rb +++ b/actionpack/test/abstract_unit.rb @@ -439,3 +439,11 @@ class ActiveSupport::TestCase skip message if defined?(JRUBY_VERSION) end end + +class DrivenByRackTest < ActionDispatch::SystemTestCase + driven_by :rack_test +end + +class DrivenBySeleniumWithChrome < ActionDispatch::SystemTestCase + driven_by :selenium, using: :chrome +end diff --git a/actionpack/test/dispatch/system_testing/driver_test.rb b/actionpack/test/dispatch/system_testing/driver_test.rb index 96d598a7a3..8f8777b19f 100644 --- a/actionpack/test/dispatch/system_testing/driver_test.rb +++ b/actionpack/test/dispatch/system_testing/driver_test.rb @@ -8,10 +8,10 @@ class DriverTest < ActiveSupport::TestCase end test "initializing the driver with a browser" do - driver = ActionDispatch::SystemTesting::Driver.new(:selenium, using: :chrome, screen_size: [ 1400, 1400 ]) + driver = ActionDispatch::SystemTesting::Driver.new(:selenium, using: :chrome, screen_size: [1400, 1400]) assert_equal :selenium, driver.instance_variable_get(:@name) - assert_equal :chrome, driver.instance_variable_get(:@using) - assert_equal [ 1400, 1400 ], driver.instance_variable_get(:@screen_size) + assert_equal :chrome, driver.instance_variable_get(:@browser) + assert_equal [1400, 1400], driver.instance_variable_get(:@screen_size) end test "selenium? returns false if driver is poltergeist" do diff --git a/actionpack/test/dispatch/system_testing/screenshot_helper_test.rb b/actionpack/test/dispatch/system_testing/screenshot_helper_test.rb index d6b501b3ac..a83818fd80 100644 --- a/actionpack/test/dispatch/system_testing/screenshot_helper_test.rb +++ b/actionpack/test/dispatch/system_testing/screenshot_helper_test.rb @@ -4,13 +4,13 @@ require "capybara/dsl" class ScreenshotHelperTest < ActiveSupport::TestCase test "image path is saved in tmp directory" do - new_test = ActionDispatch::SystemTestCase.new("x") + new_test = DrivenBySeleniumWithChrome.new("x") assert_equal "tmp/screenshots/x.png", new_test.send(:image_path) end test "image path includes failures text if test did not pass" do - new_test = ActionDispatch::SystemTestCase.new("x") + new_test = DrivenBySeleniumWithChrome.new("x") new_test.stub :passed?, false do assert_equal "tmp/screenshots/failures_x.png", new_test.send(:image_path) @@ -18,7 +18,7 @@ class ScreenshotHelperTest < ActiveSupport::TestCase end test "image path does not include failures text if test skipped" do - new_test = ActionDispatch::SystemTestCase.new("x") + new_test = DrivenBySeleniumWithChrome.new("x") new_test.stub :passed?, false do new_test.stub :skipped?, true do @@ -26,28 +26,16 @@ class ScreenshotHelperTest < ActiveSupport::TestCase end end end +end +class RackTestScreenshotsTest < DrivenByRackTest test "rack_test driver does not support screenshot" do - begin - original_driver = Capybara.current_driver - Capybara.current_driver = :rack_test - - new_test = ActionDispatch::SystemTestCase.new("x") - assert_not new_test.send(:supports_screenshot?) - ensure - Capybara.current_driver = original_driver - end + assert_not self.send(:supports_screenshot?) end +end +class SeleniumScreenshotsTest < DrivenBySeleniumWithChrome test "selenium driver supports screenshot" do - begin - original_driver = Capybara.current_driver - Capybara.current_driver = :selenium - - new_test = ActionDispatch::SystemTestCase.new("x") - assert new_test.send(:supports_screenshot?) - ensure - Capybara.current_driver = original_driver - end + assert self.send(:supports_screenshot?) end end diff --git a/actionpack/test/dispatch/system_testing/system_test_case_test.rb b/actionpack/test/dispatch/system_testing/system_test_case_test.rb index c9003e3841..1a9421c098 100644 --- a/actionpack/test/dispatch/system_testing/system_test_case_test.rb +++ b/actionpack/test/dispatch/system_testing/system_test_case_test.rb @@ -1,17 +1,13 @@ require "abstract_unit" -class DrivenByRackTestTest < ActionDispatch::SystemTestCase - driven_by :rack_test - +class SetDriverToRackTestTest < DrivenByRackTest test "uses rack_test" do assert_equal :rack_test, Capybara.current_driver end end -class DrivenBySeleniumWithChromeTest < ActionDispatch::SystemTestCase - driven_by :selenium, using: :chrome - +class SetDriverToSeleniumTest < DrivenBySeleniumWithChrome test "uses selenium" do - assert_equal :chrome, Capybara.current_driver + assert_equal :selenium, Capybara.current_driver end end -- cgit v1.2.3 From 4d7986283653ff4fbd5a82dd273ed9795a0c6a65 Mon Sep 17 00:00:00 2001 From: Edouard CHIN Date: Thu, 9 Mar 2017 01:42:16 -0500 Subject: Added `reverse_merge`/`reverse_merge!` to AC::Parameters: - This PR adds the `reverse_merge` and `reverse_merge!` method to `ActionController::Parameters` - Fixes #28353 --- .../parameters/parameters_permit_test.rb | 29 ++++++++++++++++++++++ 1 file changed, 29 insertions(+) (limited to 'actionpack/test') diff --git a/actionpack/test/controller/parameters/parameters_permit_test.rb b/actionpack/test/controller/parameters/parameters_permit_test.rb index 8920914af1..b51c55b1f1 100644 --- a/actionpack/test/controller/parameters/parameters_permit_test.rb +++ b/actionpack/test/controller/parameters/parameters_permit_test.rb @@ -302,6 +302,35 @@ class ParametersPermitTest < ActiveSupport::TestCase assert_equal "32", @params[:person][:age] end + test "#reverse_merge with parameters" do + default_params = ActionController::Parameters.new(id: "1234", person: {}).permit! + merged_params = @params.reverse_merge(default_params) + + assert_equal "1234", merged_params[:id] + refute_predicate merged_params[:person], :empty? + end + + test "not permitted is sticky beyond reverse_merge!" do + merged_params = @params.reverse_merge(a: "b") + + refute_predicate @params, :permitted? + end + + test "permitted is sticky beyond reverse_merge!" do + @params.permit! + merged_params = @params.reverse_merge(a: "b") + + assert_predicate @params, :permitted? + end + + test "#reverse_merge! with parameters" do + default_params = ActionController::Parameters.new(id: "1234", person: {}).permit! + @params.reverse_merge!(default_params) + + assert_equal "1234", @params[:id] + refute_predicate @params[:person], :empty? + end + test "modifying the parameters" do @params[:person][:hometown] = "Chicago" @params[:person][:family] = { brother: "Jonas" } -- cgit v1.2.3 From 6568da325d7445d1dd85b941ac54a6f41ec5ebe5 Mon Sep 17 00:00:00 2001 From: "yuuji.yaginuma" Date: Sat, 11 Mar 2017 14:03:20 +0900 Subject: Use correct value in `reverse_merge` test --- .../test/controller/parameters/parameters_permit_test.rb | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) (limited to 'actionpack/test') diff --git a/actionpack/test/controller/parameters/parameters_permit_test.rb b/actionpack/test/controller/parameters/parameters_permit_test.rb index b51c55b1f1..9f3025587e 100644 --- a/actionpack/test/controller/parameters/parameters_permit_test.rb +++ b/actionpack/test/controller/parameters/parameters_permit_test.rb @@ -310,17 +310,13 @@ class ParametersPermitTest < ActiveSupport::TestCase refute_predicate merged_params[:person], :empty? end - test "not permitted is sticky beyond reverse_merge!" do - merged_params = @params.reverse_merge(a: "b") - - refute_predicate @params, :permitted? + test "not permitted is sticky beyond reverse_merge" do + refute_predicate @params.reverse_merge(a: "b"), :permitted? end - test "permitted is sticky beyond reverse_merge!" do + test "permitted is sticky beyond reverse_merge" do @params.permit! - merged_params = @params.reverse_merge(a: "b") - - assert_predicate @params, :permitted? + assert_predicate @params.reverse_merge(a: "b"), :permitted? end test "#reverse_merge! with parameters" do -- cgit v1.2.3 From c5f8fe9379dc144a54ceb5663aa449d35d1bf405 Mon Sep 17 00:00:00 2001 From: alpaca-tc Date: Wed, 8 Mar 2017 18:54:52 +0900 Subject: Fixes ActionController::Rendering#with_defaults `env` is undefined. --- actionpack/test/controller/renderer_test.rb | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'actionpack/test') diff --git a/actionpack/test/controller/renderer_test.rb b/actionpack/test/controller/renderer_test.rb index 81b32a67b3..052c974d68 100644 --- a/actionpack/test/controller/renderer_test.rb +++ b/actionpack/test/controller/renderer_test.rb @@ -19,6 +19,16 @@ class RendererTest < ActiveSupport::TestCase assert_equal controller, renderer.controller end + test "creating with new defaults" do + renderer = ApplicationController.renderer + + new_defaults = { https: true } + new_renderer = renderer.with_defaults(new_defaults).new + content = new_renderer.render(inline: "<%= request.ssl? %>") + + assert_equal "true", content + end + test "rendering with a class renderer" do renderer = ApplicationController.renderer content = renderer.render template: "ruby_template" -- cgit v1.2.3 From ec99107a2982236c726699cbbefc8839de278b93 Mon Sep 17 00:00:00 2001 From: Fumiaki MATSUSHIMA Date: Wed, 8 Mar 2017 21:17:44 +0900 Subject: Pass options to `driven_by` Capybara drivers can handle some options such like `url`. ### before ``` # test/test_helper.rb Capybara.register_driver :remote_chrome do |app| Capybara::Selenium::Driver.new(app, browser: :chrome, url: "http://example.com/wd/hub") end # test/application_system_test_case.rb class ApplicationSystemTestCase < ActionDispatch::SystemTestCase driven_by :remote_chrome end ``` ### after ``` # test/application_system_test_case.rb class ApplicationSystemTestCase < ActionDispatch::SystemTestCase driven_by :selenium, using: :chrome, screen_size: [1400, 1400], options: {url: "http://chrome:4444/wd/hub"} end ``` --- actionpack/test/dispatch/system_testing/driver_test.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'actionpack/test') diff --git a/actionpack/test/dispatch/system_testing/driver_test.rb b/actionpack/test/dispatch/system_testing/driver_test.rb index 8f8777b19f..814e1d707b 100644 --- a/actionpack/test/dispatch/system_testing/driver_test.rb +++ b/actionpack/test/dispatch/system_testing/driver_test.rb @@ -8,10 +8,11 @@ class DriverTest < ActiveSupport::TestCase end test "initializing the driver with a browser" do - driver = ActionDispatch::SystemTesting::Driver.new(:selenium, using: :chrome, screen_size: [1400, 1400]) + driver = ActionDispatch::SystemTesting::Driver.new(:selenium, using: :chrome, screen_size: [1400, 1400], options: { url: "http://example.com/wd/hub" }) assert_equal :selenium, driver.instance_variable_get(:@name) assert_equal :chrome, driver.instance_variable_get(:@browser) assert_equal [1400, 1400], driver.instance_variable_get(:@screen_size) + assert_equal ({ url: "http://example.com/wd/hub" }), driver.instance_variable_get(:@options) end test "selenium? returns false if driver is poltergeist" do -- cgit v1.2.3 From 886085d6431e4f42191028d05519789f67d32bf8 Mon Sep 17 00:00:00 2001 From: Andrew White Date: Wed, 15 Mar 2017 16:47:21 +0000 Subject: Remove unnecessary params munging In 9b654d4 some params munging was added to ensure that they were set whenever `recognize_path` would call either a proc or callable constraint. Since we no longer mutate the environment hash within the method it's now unnecessary and actually causes params to leak between route matches before checking constraints. Fixes #28398. --- actionpack/test/dispatch/routing_test.rb | 61 ++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) (limited to 'actionpack/test') diff --git a/actionpack/test/dispatch/routing_test.rb b/actionpack/test/dispatch/routing_test.rb index d563df91df..64818e6ca1 100644 --- a/actionpack/test/dispatch/routing_test.rb +++ b/actionpack/test/dispatch/routing_test.rb @@ -4962,3 +4962,64 @@ class FlashRedirectTest < ActionDispatch::IntegrationTest assert_equal "bar", response.body end end + +class TestRecognizePath < ActionDispatch::IntegrationTest + class PageConstraint + attr_reader :key, :pattern + + def initialize(key, pattern) + @key = key + @pattern = pattern + end + + def matches?(request) + request.path_parameters[key] =~ pattern + end + end + + stub_controllers do |routes| + Routes = routes + routes.draw do + get "/hash/:foo", to: "pages#show", constraints: { foo: /foo/ } + get "/hash/:bar", to: "pages#show", constraints: { bar: /bar/ } + + get "/proc/:foo", to: "pages#show", constraints: proc { |r| r.path_parameters[:foo] =~ /foo/ } + get "/proc/:bar", to: "pages#show", constraints: proc { |r| r.path_parameters[:bar] =~ /bar/ } + + get "/class/:foo", to: "pages#show", constraints: PageConstraint.new(:foo, /foo/) + get "/class/:bar", to: "pages#show", constraints: PageConstraint.new(:bar, /bar/) + end + end + + APP = build_app Routes + def app + APP + end + + def test_hash_constraints_dont_leak_between_routes + expected_params = { controller: "pages", action: "show", bar: "bar" } + actual_params = recognize_path("/hash/bar") + + assert_equal expected_params, actual_params + end + + def test_proc_constraints_dont_leak_between_routes + expected_params = { controller: "pages", action: "show", bar: "bar" } + actual_params = recognize_path("/proc/bar") + + assert_equal expected_params, actual_params + end + + def test_class_constraints_dont_leak_between_routes + expected_params = { controller: "pages", action: "show", bar: "bar" } + actual_params = recognize_path("/class/bar") + + assert_equal expected_params, actual_params + end + + private + + def recognize_path(*args) + Routes.recognize_path(*args) + end +end -- cgit v1.2.3 From 35afd2c53b4a49a6b4495b167eef233428123b4a Mon Sep 17 00:00:00 2001 From: Andrew White Date: Fri, 17 Mar 2017 17:07:09 +0000 Subject: Add support for calling nested direct routes (#28462) Not all requirements can be expressed in terms of polymorphic url options so add a `route_for` method that allows calling another direct route (or regular named route) which a set of arguments, e.g: resources :buckets direct :recordable do |recording| route_for(:bucket, recording.bucket) end direct :threadable do |threadable| route_for(:recordable, threadable.parent) end This maintains the context of the original caller, e.g. threadable_path(threadable) # => /buckets/1 threadable_url(threadable) # => http://example.com/buckets/1 --- .../dispatch/routing/custom_url_helpers_test.rb | 36 +++++++++++++++++----- 1 file changed, 28 insertions(+), 8 deletions(-) (limited to 'actionpack/test') diff --git a/actionpack/test/dispatch/routing/custom_url_helpers_test.rb b/actionpack/test/dispatch/routing/custom_url_helpers_test.rb index f85b989892..cb5ca5888b 100644 --- a/actionpack/test/dispatch/routing/custom_url_helpers_test.rb +++ b/actionpack/test/dispatch/routing/custom_url_helpers_test.rb @@ -4,18 +4,23 @@ class TestCustomUrlHelpers < ActionDispatch::IntegrationTest class Linkable attr_reader :id + def self.name + super.demodulize + end + def initialize(id) @id = id end def linkable_type - self.class.name.demodulize.underscore + self.class.name.underscore end end class Category < Linkable; end class Collection < Linkable; end class Product < Linkable; end + class Manufacturer < Linkable; end class Model extend ActiveModel::Naming @@ -79,7 +84,7 @@ class TestCustomUrlHelpers < ActionDispatch::IntegrationTest get "/media/:id", to: "media#show", as: :media get "/pages/:id", to: "pages#show", as: :page - resources :categories, :collections, :products + resources :categories, :collections, :products, :manufacturers namespace :admin do get "/dashboard", to: "dashboard#index" @@ -89,6 +94,7 @@ class TestCustomUrlHelpers < ActionDispatch::IntegrationTest direct("string") { "http://www.rubyonrails.org" } direct(:helper) { basket_url } direct(:linkable) { |linkable| [:"#{linkable.linkable_type}", { id: linkable.id }] } + direct(:nested) { |linkable| route_for(:linkable, linkable) } direct(:params) { |params| params } direct(:symbol) { :basket } direct(:hash) { { controller: "basket", action: "show" } } @@ -102,6 +108,7 @@ class TestCustomUrlHelpers < ActionDispatch::IntegrationTest resolve("Article") { |article| [:post, { id: article.id }] } resolve("Basket") { |basket| [:basket] } + resolve("Manufacturer") { |manufacturer| route_for(:linkable, manufacturer) } resolve("User", anchor: "details") { |user, options| [:profile, options] } resolve("Video") { |video| [:media, { id: video.id }] } resolve(%w[Page CategoryPage ProductPage]) { |page| [:page, { id: page.id }] } @@ -119,6 +126,7 @@ class TestCustomUrlHelpers < ActionDispatch::IntegrationTest @category = Category.new("1") @collection = Collection.new("2") @product = Product.new("3") + @manufacturer = Manufacturer.new("apple") @basket = Basket.new @user = User.new @video = Video.new("4") @@ -136,14 +144,14 @@ class TestCustomUrlHelpers < ActionDispatch::IntegrationTest end def test_direct_paths - assert_equal "http://www.rubyonrails.org", website_path - assert_equal "http://www.rubyonrails.org", Routes.url_helpers.website_path + assert_equal "/", website_path + assert_equal "/", Routes.url_helpers.website_path - assert_equal "http://www.rubyonrails.org", string_path - assert_equal "http://www.rubyonrails.org", Routes.url_helpers.string_path + assert_equal "/", string_path + assert_equal "/", Routes.url_helpers.string_path - assert_equal "http://www.example.com/basket", helper_url - assert_equal "http://www.example.com/basket", Routes.url_helpers.helper_url + assert_equal "/basket", helper_path + assert_equal "/basket", Routes.url_helpers.helper_path assert_equal "/categories/1", linkable_path(@category) assert_equal "/categories/1", Routes.url_helpers.linkable_path(@category) @@ -152,6 +160,9 @@ class TestCustomUrlHelpers < ActionDispatch::IntegrationTest assert_equal "/products/3", linkable_path(@product) assert_equal "/products/3", Routes.url_helpers.linkable_path(@product) + assert_equal "/categories/1", nested_path(@category) + assert_equal "/categories/1", Routes.url_helpers.nested_path(@category) + assert_equal "/", params_path(@safe_params) assert_equal "/", Routes.url_helpers.params_path(@safe_params) assert_raises(ArgumentError) { params_path(@unsafe_params) } @@ -192,6 +203,9 @@ class TestCustomUrlHelpers < ActionDispatch::IntegrationTest assert_equal "http://www.example.com/products/3", linkable_url(@product) assert_equal "http://www.example.com/products/3", Routes.url_helpers.linkable_url(@product) + assert_equal "http://www.example.com/categories/1", nested_url(@category) + assert_equal "http://www.example.com/categories/1", Routes.url_helpers.nested_url(@category) + assert_equal "http://www.example.com/", params_url(@safe_params) assert_equal "http://www.example.com/", Routes.url_helpers.params_url(@safe_params) assert_raises(ArgumentError) { params_url(@unsafe_params) } @@ -244,6 +258,9 @@ class TestCustomUrlHelpers < ActionDispatch::IntegrationTest assert_equal "/pages/8", polymorphic_path(@product_page) assert_equal "/pages/8", Routes.url_helpers.polymorphic_path(@product_page) assert_equal "/pages/8", ActionDispatch::Routing::PolymorphicRoutes::HelperMethodBuilder.path.handle_model_call(self, @product_page) + + assert_equal "/manufacturers/apple", polymorphic_path(@manufacturer) + assert_equal "/manufacturers/apple", Routes.url_helpers.polymorphic_path(@manufacturer) end def test_resolve_urls @@ -277,6 +294,9 @@ class TestCustomUrlHelpers < ActionDispatch::IntegrationTest assert_equal "http://www.example.com/pages/8", polymorphic_url(@product_page) assert_equal "http://www.example.com/pages/8", Routes.url_helpers.polymorphic_url(@product_page) assert_equal "http://www.example.com/pages/8", ActionDispatch::Routing::PolymorphicRoutes::HelperMethodBuilder.url.handle_model_call(self, @product_page) + + assert_equal "http://www.example.com/manufacturers/apple", polymorphic_url(@manufacturer) + assert_equal "http://www.example.com/manufacturers/apple", Routes.url_helpers.polymorphic_url(@manufacturer) end def test_defining_direct_inside_a_scope_raises_runtime_error -- cgit v1.2.3 From 843345cae59b73e50af685ac1303a419a06b4c91 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafael=20Mendon=C3=A7a=20Fran=C3=A7a?= Date: Tue, 21 Mar 2017 13:37:53 -0400 Subject: Revert "Merge pull request #27775 from domcleal/27774-format-reset" This reverts commit c6f9f8c28a720ad4ec7cf3613dddfa451d5968e2, reversing changes made to c309073c7476f50dfb1e796d058580f176101c36. Reason: This is fixing the behavior in the wrong place. Now the request path after the request is nil and there is no way to assert that. Also the test that was added in that PR also fails in 4.2 where the reporter says it was passing. The reason the bahavior changed between Rails 4.2 and Rails 5 is that the format in the path is now respected. The correct way to fix the problem is not doign two requests in the same controller test and use integrations tests. This change caused a regression between Rails 5.0.1 and 5.0.2. --- actionpack/test/controller/test_case_test.rb | 19 +++++-------------- 1 file changed, 5 insertions(+), 14 deletions(-) (limited to 'actionpack/test') diff --git a/actionpack/test/controller/test_case_test.rb b/actionpack/test/controller/test_case_test.rb index 891ce0e905..cd0c790b08 100644 --- a/actionpack/test/controller/test_case_test.rb +++ b/actionpack/test/controller/test_case_test.rb @@ -679,6 +679,11 @@ XML assert_equal "baz", @request.filtered_parameters[:foo] end + def test_path_is_kept_after_the_request + get :test_params, params: { id: "foo" } + assert_equal "foo", @request.path + end + def test_path_params_reset_between_request get :test_params, params: { id: "foo" } assert_equal "foo", @request.path_parameters[:id] @@ -728,20 +733,6 @@ XML assert_equal "text/html", @response.body end - def test_request_path_info_and_format_reset - get :test_format, format: "json" - assert_equal "application/json", @response.body - - get :test_uri, format: "json" - assert_equal "/test_case_test/test/test_uri.json", @response.body - - get :test_format - assert_equal "text/html", @response.body - - get :test_uri - assert_equal "/test_case_test/test/test_uri", @response.body - end - def test_request_format_kwarg_overrides_params get :test_format, format: "json", params: { format: "html" } assert_equal "application/json", @response.body -- cgit v1.2.3 From ea5aa2525b4fbc7d8323a7e4c3ee98db2ee5acb4 Mon Sep 17 00:00:00 2001 From: Sid Ngeth Date: Fri, 17 Feb 2017 17:03:09 -0500 Subject: Wrap store accessors in parameters Modified params wrapper to account for model's stored_attributes --- actionpack/test/controller/params_wrapper_test.rb | 13 +++++++++++++ 1 file changed, 13 insertions(+) (limited to 'actionpack/test') diff --git a/actionpack/test/controller/params_wrapper_test.rb b/actionpack/test/controller/params_wrapper_test.rb index faa57c4559..1eb92abae4 100644 --- a/actionpack/test/controller/params_wrapper_test.rb +++ b/actionpack/test/controller/params_wrapper_test.rb @@ -35,6 +35,10 @@ class ParamsWrapperTest < ActionController::TestCase end class Person + def self.stores_attributes + { settings: [:color, :size] } + end + def self.attribute_names [] end @@ -62,6 +66,15 @@ class ParamsWrapperTest < ActionController::TestCase end end + def test_store_accessors_wrapped + with_default_wrapper_options do + @request.env["CONTENT_TYPE"] = "application/json" + post :parse, params: { "username" => "sikachu", "color" => "blue", "size" => "large" } + assert_parameters("username" => "sikachu", "color" => "blue", "size" => "large", + "user" => { "username" => "sikachu", "color" => "blue", "size" => "large" }) + end + end + def test_specify_wrapper_name with_default_wrapper_options do UsersController.wrap_parameters :person -- cgit v1.2.3 From 681277e53bf084195eab437b3006043bbd40ef4d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafael=20Mendon=C3=A7a=20Fran=C3=A7a?= Date: Tue, 21 Mar 2017 16:50:35 -0400 Subject: Fix test that was asserting the wrong thing --- actionpack/test/controller/parameters/accessors_test.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'actionpack/test') diff --git a/actionpack/test/controller/parameters/accessors_test.rb b/actionpack/test/controller/parameters/accessors_test.rb index f17e93a431..7725c25e22 100644 --- a/actionpack/test/controller/parameters/accessors_test.rb +++ b/actionpack/test/controller/parameters/accessors_test.rb @@ -99,7 +99,7 @@ class ParametersAccessorsTest < ActiveSupport::TestCase test "has_value? returns false if the given value is not present in the params" do params = ActionController::Parameters.new(city: "Chicago", state: "Illinois") - refute @params.has_value?("New York") + refute params.has_value?("New York") end test "include? returns true if the given key is present in the params" do -- cgit v1.2.3 From b7bd4e2848145fe6b342d510b9e250ab7799563c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafael=20Mendon=C3=A7a=20Fran=C3=A7a?= Date: Tue, 21 Mar 2017 16:51:56 -0400 Subject: Fix test assertion --- actionpack/test/controller/test_case_test.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'actionpack/test') diff --git a/actionpack/test/controller/test_case_test.rb b/actionpack/test/controller/test_case_test.rb index cd0c790b08..3a4307b64b 100644 --- a/actionpack/test/controller/test_case_test.rb +++ b/actionpack/test/controller/test_case_test.rb @@ -681,7 +681,7 @@ XML def test_path_is_kept_after_the_request get :test_params, params: { id: "foo" } - assert_equal "foo", @request.path + assert_equal "/test_case_test/test/test_params/foo", @request.path end def test_path_params_reset_between_request -- cgit v1.2.3 From edcbb2e5b7505e072ae981a4eb5b23dc5d537e42 Mon Sep 17 00:00:00 2001 From: "yuuji.yaginuma" Date: Wed, 22 Mar 2017 08:17:26 +0900 Subject: Fix store accessors in parameters test * The method name must be `stored_attributes`, not `stores_attributes`. * `attribute_names` must return a non-empty value. Because `stored_attributes` is not checked if `attribute_names` is empty. Follow up to #28056 --- actionpack/test/controller/params_wrapper_test.rb | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) (limited to 'actionpack/test') diff --git a/actionpack/test/controller/params_wrapper_test.rb b/actionpack/test/controller/params_wrapper_test.rb index 1eb92abae4..2a41d57b26 100644 --- a/actionpack/test/controller/params_wrapper_test.rb +++ b/actionpack/test/controller/params_wrapper_test.rb @@ -32,13 +32,13 @@ class ParamsWrapperTest < ActionController::TestCase def self.attribute_names [] end - end - class Person - def self.stores_attributes + def self.stored_attributes { settings: [:color, :size] } end + end + class Person def self.attribute_names [] end @@ -67,11 +67,13 @@ class ParamsWrapperTest < ActionController::TestCase end def test_store_accessors_wrapped - with_default_wrapper_options do - @request.env["CONTENT_TYPE"] = "application/json" - post :parse, params: { "username" => "sikachu", "color" => "blue", "size" => "large" } - assert_parameters("username" => "sikachu", "color" => "blue", "size" => "large", - "user" => { "username" => "sikachu", "color" => "blue", "size" => "large" }) + assert_called(User, :attribute_names, times: 2, returns: ["username"]) do + with_default_wrapper_options do + @request.env["CONTENT_TYPE"] = "application/json" + post :parse, params: { "username" => "sikachu", "color" => "blue", "size" => "large" } + assert_parameters("username" => "sikachu", "color" => "blue", "size" => "large", + "user" => { "username" => "sikachu", "color" => "blue", "size" => "large" }) + end end end -- cgit v1.2.3 From 84bfb81a62c142e63569863535f0204669bd006c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafael=20Mendon=C3=A7a=20Fran=C3=A7a?= Date: Mon, 27 Mar 2017 19:51:21 -0400 Subject: Make sure that ActionController::Api can include helpers Closes #28554 --- .../test/controller/api/with_helpers_test.rb | 26 ++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 actionpack/test/controller/api/with_helpers_test.rb (limited to 'actionpack/test') diff --git a/actionpack/test/controller/api/with_helpers_test.rb b/actionpack/test/controller/api/with_helpers_test.rb new file mode 100644 index 0000000000..9882a25ae1 --- /dev/null +++ b/actionpack/test/controller/api/with_helpers_test.rb @@ -0,0 +1,26 @@ +require "abstract_unit" + +module ApiWithHelper + def my_helper + "helper" + end +end + +class WithHelpersController < ActionController::API + include ActionController::Helpers + helper ApiWithHelper + + def with_helpers + render plain: self.class.helpers.my_helper + end +end + +class WithHelpersTest < ActionController::TestCase + tests WithHelpersController + + def test_with_helpers + get :with_helpers + + assert_equal "helper", response.body + end +end -- cgit v1.2.3 From 24e0fa7c4ab81f6c2fb2b3af90ee217620f30d17 Mon Sep 17 00:00:00 2001 From: Fumiaki MATSUSHIMA Date: Mon, 27 Mar 2017 21:13:44 +0900 Subject: Make `driven_by` overridable Sometimes we want to use rack_test partially instead of selenium for test speed: ```ruby class ApplicationSystemTestCase < ActionDispatch::SystemTestCase driven_by :selenium, using: :chrome, screen_size: [1400, 1400], options: {url: "http://chrome:4444/wd/hub"} end class WithJavaScriptTest < ApplicationSystemTestCase end class WithoutJavaScriptTest < ApplicationSystemTestCase driven_by :rack_test end ``` In the abobe case, `WithoutJavaScriptTest` uses selenium because `SystemTestCase` calls superclass' driver on `#initialize` (`self.class.superclass.driver.use`). Using `class_attribute` can handle inherited `driven_by`. --- actionpack/test/dispatch/system_testing/system_test_case_test.rb | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'actionpack/test') diff --git a/actionpack/test/dispatch/system_testing/system_test_case_test.rb b/actionpack/test/dispatch/system_testing/system_test_case_test.rb index 1a9421c098..33d98f924f 100644 --- a/actionpack/test/dispatch/system_testing/system_test_case_test.rb +++ b/actionpack/test/dispatch/system_testing/system_test_case_test.rb @@ -6,6 +6,14 @@ class SetDriverToRackTestTest < DrivenByRackTest end end +class OverrideSeleniumSubclassToRackTestTest < DrivenBySeleniumWithChrome + driven_by :rack_test + + test "uses rack_test" do + assert_equal :rack_test, Capybara.current_driver + end +end + class SetDriverToSeleniumTest < DrivenBySeleniumWithChrome test "uses selenium" do assert_equal :selenium, Capybara.current_driver -- cgit v1.2.3 From 862cc0bfbe6f125aff4e76c2d71764a14bcf4891 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafael=20Mendon=C3=A7a=20Fran=C3=A7a?= Date: Wed, 29 Mar 2017 12:40:57 -0400 Subject: Add test to make sure subclasses also get helpers --- actionpack/test/controller/api/with_helpers_test.rb | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) (limited to 'actionpack/test') diff --git a/actionpack/test/controller/api/with_helpers_test.rb b/actionpack/test/controller/api/with_helpers_test.rb index 9882a25ae1..06db949153 100644 --- a/actionpack/test/controller/api/with_helpers_test.rb +++ b/actionpack/test/controller/api/with_helpers_test.rb @@ -15,6 +15,12 @@ class WithHelpersController < ActionController::API end end +class SubclassWithHelpersController < WithHelpersController + def with_helpers + render plain: self.class.helpers.my_helper + end +end + class WithHelpersTest < ActionController::TestCase tests WithHelpersController @@ -24,3 +30,13 @@ class WithHelpersTest < ActionController::TestCase assert_equal "helper", response.body end end + +class SubclassWithHelpersTest < ActionController::TestCase + tests WithHelpersController + + def test_with_helpers + get :with_helpers + + assert_equal "helper", response.body + end +end -- cgit v1.2.3 From 0117810cdab34d168b0579a6736c5d58c5ab84c7 Mon Sep 17 00:00:00 2001 From: Matt Casper Date: Wed, 29 Mar 2017 09:56:27 -0500 Subject: Add aliases for reverse_merge to with_defaults In the context of controller parameters, reverse_merge is commonly used to provide defaults for user input. Having an alias to reverse_merge called with_defaults feels more idiomatic for Rails. --- .../test/controller/parameters/parameters_permit_test.rb | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) (limited to 'actionpack/test') diff --git a/actionpack/test/controller/parameters/parameters_permit_test.rb b/actionpack/test/controller/parameters/parameters_permit_test.rb index 9f3025587e..3e067314d6 100644 --- a/actionpack/test/controller/parameters/parameters_permit_test.rb +++ b/actionpack/test/controller/parameters/parameters_permit_test.rb @@ -310,6 +310,14 @@ class ParametersPermitTest < ActiveSupport::TestCase refute_predicate merged_params[:person], :empty? end + test "#with_defaults is an alias of reverse_merge" do + default_params = ActionController::Parameters.new(id: "1234", person: {}).permit! + merged_params = @params.with_defaults(default_params) + + assert_equal "1234", merged_params[:id] + refute_predicate merged_params[:person], :empty? + end + test "not permitted is sticky beyond reverse_merge" do refute_predicate @params.reverse_merge(a: "b"), :permitted? end @@ -327,6 +335,14 @@ class ParametersPermitTest < ActiveSupport::TestCase refute_predicate @params[:person], :empty? end + test "#with_defaults! is an alias of reverse_merge!" do + default_params = ActionController::Parameters.new(id: "1234", person: {}).permit! + @params.with_defaults!(default_params) + + assert_equal "1234", @params[:id] + refute_predicate @params[:person], :empty? + end + test "modifying the parameters" do @params[:person][:hometown] = "Chicago" @params[:person][:family] = { brother: "Jonas" } -- cgit v1.2.3 From 9d695743dee0eb57cedd1f0e7a9dc1a16ef0b530 Mon Sep 17 00:00:00 2001 From: Jon Moss Date: Wed, 29 Mar 2017 17:36:54 -0400 Subject: Do not include default response headers for AC::Metal In Rails 4.2, `ActionController::Metal` controllers did not include the default headers from `ActionDispatch::Response`. However, through e16afe6, and a general shift towards having `ActionController::Metal` objects contain `ActionDispatch::Response` objects (instead of just returning an array of status, headers, and body), this behavior was lost. This PR helps to restore the original behavior by having `ActionController::Metal` controllers generate Response objects without the default headers, while `ActionController::Base` now overrides the factory method to make sure its version does have the default headers. --- actionpack/test/controller/base_test.rb | 27 +++++++++++++++++++++++++++ actionpack/test/controller/metal_test.rb | 30 ++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+) create mode 100644 actionpack/test/controller/metal_test.rb (limited to 'actionpack/test') diff --git a/actionpack/test/controller/base_test.rb b/actionpack/test/controller/base_test.rb index 42a5157010..4e969fac07 100644 --- a/actionpack/test/controller/base_test.rb +++ b/actionpack/test/controller/base_test.rb @@ -11,6 +11,12 @@ end class EmptyController < ActionController::Base end +class SimpleController < ActionController::Base + def hello + self.response_body = "hello" + end +end + class NonEmptyController < ActionController::Base def public_action head :ok @@ -118,6 +124,27 @@ class ControllerInstanceTests < ActiveSupport::TestCase controller = klass.new assert_equal "examples", controller.controller_path end + + def test_response_has_default_headers + original_default_headers = ActionDispatch::Response.default_headers + + ActionDispatch::Response.default_headers = { + "X-Frame-Options" => "DENY", + "X-Content-Type-Options" => "nosniff", + "X-XSS-Protection" => "1;" + } + + response_headers = SimpleController.action("hello").call( + "REQUEST_METHOD" => "GET", + "rack.input" => -> {} + )[1] + + assert response_headers.key?("X-Frame-Options") + assert response_headers.key?("X-Content-Type-Options") + assert response_headers.key?("X-XSS-Protection") + ensure + ActionDispatch::Response.default_headers = original_default_headers + end end class PerformActionTest < ActionController::TestCase diff --git a/actionpack/test/controller/metal_test.rb b/actionpack/test/controller/metal_test.rb new file mode 100644 index 0000000000..e16452ed6f --- /dev/null +++ b/actionpack/test/controller/metal_test.rb @@ -0,0 +1,30 @@ +require "abstract_unit" + +class MetalControllerInstanceTests < ActiveSupport::TestCase + class SimpleController < ActionController::Metal + def hello + self.response_body = "hello" + end + end + + def test_response_has_default_headers + original_default_headers = ActionDispatch::Response.default_headers + + ActionDispatch::Response.default_headers = { + "X-Frame-Options" => "DENY", + "X-Content-Type-Options" => "nosniff", + "X-XSS-Protection" => "1;" + } + + response_headers = SimpleController.action("hello").call( + "REQUEST_METHOD" => "GET", + "rack.input" => -> {} + )[1] + + refute response_headers.key?("X-Frame-Options") + refute response_headers.key?("X-Content-Type-Options") + refute response_headers.key?("X-XSS-Protection") + ensure + ActionDispatch::Response.default_headers = original_default_headers + end +end -- cgit v1.2.3