diff options
Diffstat (limited to 'actionpack/test/controller')
21 files changed, 682 insertions, 124 deletions
diff --git a/actionpack/test/controller/action_pack_assertions_test.rb b/actionpack/test/controller/action_pack_assertions_test.rb index 9b0d4d0f4c..4ab5d92a2b 100644 --- a/actionpack/test/controller/action_pack_assertions_test.rb +++ b/actionpack/test/controller/action_pack_assertions_test.rb @@ -1,5 +1,5 @@ require 'abstract_unit' -require 'action_controller/vendor/html-scanner' +require 'action_view/vendor/html-scanner' require 'controller/fake_controllers' class ActionPackAssertionsController < ActionController::Base diff --git a/actionpack/test/controller/assert_select_test.rb b/actionpack/test/controller/assert_select_test.rb index 3d667f0a2f..38598a520c 100644 --- a/actionpack/test/controller/assert_select_test.rb +++ b/actionpack/test/controller/assert_select_test.rb @@ -10,6 +10,16 @@ require 'controller/fake_controllers' require 'action_mailer' ActionMailer::Base.view_paths = FIXTURE_LOAD_PATH +class SynchronousQueue < Queue + def push(job) + job.run + end + alias << push + alias enq push +end + +ActionMailer::Base.queue = SynchronousQueue.new + class AssertSelectTest < ActionController::TestCase Assertion = ActiveSupport::TestCase::Assertion diff --git a/actionpack/test/controller/base_test.rb b/actionpack/test/controller/base_test.rb index b9513ccff4..9b42e7631f 100644 --- a/actionpack/test/controller/base_test.rb +++ b/actionpack/test/controller/base_test.rb @@ -1,5 +1,6 @@ require 'abstract_unit' require 'active_support/logger' +require 'controller/fake_models' require 'pp' # require 'pp' early to prevent hidden_methods from not picking up the pretty-print methods until too late # Provide some controller to run the tests on. @@ -63,6 +64,10 @@ end class RecordIdentifierController < ActionController::Base end +class RecordIdentifierWithoutDeprecationController < ActionController::Base + include ActionView::RecordIdentifier +end + class ControllerClassTests < ActiveSupport::TestCase def test_controller_path @@ -81,6 +86,42 @@ class ControllerClassTests < ActiveSupport::TestCase assert_respond_to RecordIdentifierController.new, :dom_id assert_respond_to RecordIdentifierController.new, :dom_class end + + def test_record_identifier_is_deprecated + record = Comment.new + record.save + + dom_id = nil + assert_deprecated 'dom_id method will no longer' do + dom_id = RecordIdentifierController.new.dom_id(record) + end + + assert_equal 'comment_1', dom_id + + dom_class = nil + assert_deprecated 'dom_class method will no longer' do + dom_class = RecordIdentifierController.new.dom_class(record) + end + assert_equal 'comment', dom_class + end + + def test_no_deprecation_when_action_view_record_identifier_is_included + record = Comment.new + record.save + + dom_id = nil + assert_not_deprecated do + dom_id = RecordIdentifierWithoutDeprecationController.new.dom_id(record) + end + + assert_equal 'comment_1', dom_id + + dom_class = nil + assert_not_deprecated do + dom_class = RecordIdentifierWithoutDeprecationController.new.dom_class(record) + end + assert_equal 'comment', dom_class + end end class ControllerInstanceTests < ActiveSupport::TestCase diff --git a/actionpack/test/controller/caching_test.rb b/actionpack/test/controller/caching_test.rb index 0efba5b77f..620479cb0c 100644 --- a/actionpack/test/controller/caching_test.rb +++ b/actionpack/test/controller/caching_test.rb @@ -5,6 +5,43 @@ require 'active_record_unit' CACHE_DIR = 'test_cache' # Don't change '/../temp/' cavalierly or you might hose something you don't want hosed FILE_STORE_PATH = File.join(File.dirname(__FILE__), '/../temp/', CACHE_DIR) + +class CachingMetalController < ActionController::Metal + abstract! + + include ActionController::Caching + + self.page_cache_directory = FILE_STORE_PATH + self.cache_store = :file_store, FILE_STORE_PATH +end + +class PageCachingMetalTestController < CachingMetalController + caches_page :ok + + def ok + self.response_body = 'ok' + end +end + +class PageCachingMetalTest < ActionController::TestCase + tests PageCachingMetalTestController + + def setup + FileUtils.rm_rf(File.dirname(FILE_STORE_PATH)) + FileUtils.mkdir_p(FILE_STORE_PATH) + end + + def teardown + FileUtils.rm_rf(File.dirname(FILE_STORE_PATH)) + end + + def test_should_cache_get_with_ok_status + get :ok + assert_response :ok + assert File.exist?("#{FILE_STORE_PATH}/page_caching_metal_test/ok.html"), 'get with ok status should have been cached' + end +end + ActionController::Base.page_cache_directory = FILE_STORE_PATH class CachingController < ActionController::Base @@ -854,14 +891,17 @@ Ciao CACHED assert_equal expected_body, @response.body - assert_equal "This bit's fragment cached", @store.read('views/test.host/functional_caching/fragment_cached') + assert_equal "This bit's fragment cached", + @store.read("views/test.host/functional_caching/fragment_cached/#{template_digest("functional_caching/fragment_cached", "html")}") end def test_fragment_caching_in_partials get :html_fragment_cached_with_partial assert_response :success assert_match(/Old fragment caching in a partial/, @response.body) - assert_match("Old fragment caching in a partial", @store.read('views/test.host/functional_caching/html_fragment_cached_with_partial')) + + assert_match("Old fragment caching in a partial", + @store.read("views/test.host/functional_caching/html_fragment_cached_with_partial/#{template_digest("functional_caching/_partial", "html")}")) end def test_render_inline_before_fragment_caching @@ -869,7 +909,8 @@ CACHED assert_response :success assert_match(/Some inline content/, @response.body) assert_match(/Some cached content/, @response.body) - assert_match("Some cached content", @store.read('views/test.host/functional_caching/inline_fragment_cached')) + assert_match("Some cached content", + @store.read("views/test.host/functional_caching/inline_fragment_cached/#{template_digest("functional_caching/inline_fragment_cached", "html")}")) end def test_html_formatted_fragment_caching @@ -879,7 +920,8 @@ CACHED assert_equal expected_body, @response.body - assert_equal "<p>ERB</p>", @store.read('views/test.host/functional_caching/formatted_fragment_cached') + assert_equal "<p>ERB</p>", + @store.read("views/test.host/functional_caching/formatted_fragment_cached/#{template_digest("functional_caching/formatted_fragment_cached", "html")}") end def test_xml_formatted_fragment_caching @@ -889,8 +931,14 @@ CACHED assert_equal expected_body, @response.body - assert_equal " <p>Builder</p>\n", @store.read('views/test.host/functional_caching/formatted_fragment_cached') + assert_equal " <p>Builder</p>\n", + @store.read("views/test.host/functional_caching/formatted_fragment_cached/#{template_digest("functional_caching/formatted_fragment_cached", "xml")}") end + + private + def template_digest(name, format) + ActionView::Digestor.digest(name, format, @controller.lookup_context) + end end class CacheHelperOutputBufferTest < ActionController::TestCase @@ -938,5 +986,5 @@ class CacheHelperOutputBufferTest < ActionController::TestCase cache_helper.send :fragment_for, 'Test fragment name', 'Test fragment', &Proc.new{ nil } end end - end + diff --git a/actionpack/test/controller/filters_test.rb b/actionpack/test/controller/filters_test.rb index afc00a3c9d..d203601771 100644 --- a/actionpack/test/controller/filters_test.rb +++ b/actionpack/test/controller/filters_test.rb @@ -193,7 +193,7 @@ class FilterTest < ActionController::TestCase end class ConditionalClassFilter - def self.filter(controller) controller.instance_variable_set(:"@ran_class_filter", true) end + def self.before(controller) controller.instance_variable_set(:"@ran_class_filter", true) end end class OnlyConditionClassController < ConditionalFilterController @@ -309,7 +309,7 @@ class FilterTest < ActionController::TestCase end class AuditFilter - def self.filter(controller) + def self.before(controller) controller.instance_variable_set(:"@was_audited", true) end end @@ -449,7 +449,7 @@ class FilterTest < ActionController::TestCase class ErrorToRescue < Exception; end class RescuingAroundFilterWithBlock - def filter(controller) + def around(controller) begin yield rescue ErrorToRescue => ex @@ -894,7 +894,7 @@ end class ControllerWithFilterClass < PostsController class YieldingFilter < DefaultFilter - def self.filter(controller) + def self.around(controller) yield raise After end @@ -905,7 +905,7 @@ end class ControllerWithFilterInstance < PostsController class YieldingFilter < DefaultFilter - def filter(controller) + def around(controller) yield raise After end @@ -916,13 +916,13 @@ end class ControllerWithFilterMethod < PostsController class YieldingFilter < DefaultFilter - def filter(controller) + def around(controller) yield raise After end end - around_filter YieldingFilter.new.method(:filter), :only => :raises_after + around_filter YieldingFilter.new.method(:around), :only => :raises_after end class ControllerWithProcFilter < PostsController diff --git a/actionpack/test/controller/integration_test.rb b/actionpack/test/controller/integration_test.rb index f18bf33969..cf561d913a 100644 --- a/actionpack/test/controller/integration_test.rb +++ b/actionpack/test/controller/integration_test.rb @@ -1,6 +1,6 @@ require 'abstract_unit' require 'controller/fake_controllers' -require 'action_controller/vendor/html-scanner' +require 'action_view/vendor/html-scanner' class SessionTest < ActiveSupport::TestCase StubApp = lambda { |env| diff --git a/actionpack/test/controller/log_subscriber_test.rb b/actionpack/test/controller/log_subscriber_test.rb index 700fd788fa..a72b6dde1a 100644 --- a/actionpack/test/controller/log_subscriber_test.rb +++ b/actionpack/test/controller/log_subscriber_test.rb @@ -54,6 +54,10 @@ module Another def with_rescued_exception raise SpecialException end + + def with_action_not_found + raise AbstractController::ActionNotFound + end end end @@ -225,6 +229,17 @@ class ACLogSubscriberTest < ActionController::TestCase assert_match(/Completed 406/, logs.last) end + def test_process_action_with_with_action_not_found_logs_404 + begin + get :with_action_not_found + wait + rescue AbstractController::ActionNotFound + end + + assert_equal 2, logs.size + assert_match(/Completed 404/, logs.last) + end + def logs @logs ||= @logger.logged(:info) end diff --git a/actionpack/test/controller/new_base/render_streaming_test.rb b/actionpack/test/controller/new_base/render_streaming_test.rb index bfca8c5c24..f4bdd3e1d4 100644 --- a/actionpack/test/controller/new_base/render_streaming_test.rb +++ b/actionpack/test/controller/new_base/render_streaming_test.rb @@ -85,7 +85,7 @@ module RenderStreaming test "rendering with template exception logs the exception" do io = StringIO.new - _old, ActionController::Base.logger = ActionController::Base.logger, ActiveSupport::Logger.new(io) + _old, ActionView::Base.logger = ActionView::Base.logger, ActiveSupport::Logger.new(io) begin get "/render_streaming/basic/template_exception" diff --git a/actionpack/test/controller/parameters/nested_parameters_test.rb b/actionpack/test/controller/parameters/nested_parameters_test.rb new file mode 100644 index 0000000000..41f5b6e127 --- /dev/null +++ b/actionpack/test/controller/parameters/nested_parameters_test.rb @@ -0,0 +1,113 @@ +require 'abstract_unit' +require 'action_controller/metal/strong_parameters' + +class NestedParametersTest < ActiveSupport::TestCase + test "permitted nested parameters" do + params = ActionController::Parameters.new({ + book: { + title: "Romeo and Juliet", + authors: [{ + name: "William Shakespeare", + born: "1564-04-26" + }, { + name: "Christopher Marlowe" + }], + details: { + pages: 200, + genre: "Tragedy" + } + }, + magazine: "Mjallo!" + }) + + permitted = params.permit book: [ :title, { authors: [ :name ] }, { details: :pages } ] + + assert permitted.permitted? + assert_equal "Romeo and Juliet", permitted[:book][:title] + assert_equal "William Shakespeare", permitted[:book][:authors][0][:name] + assert_equal "Christopher Marlowe", permitted[:book][:authors][1][:name] + assert_equal 200, permitted[:book][:details][:pages] + assert_nil permitted[:book][:details][:genre] + assert_nil permitted[:book][:authors][0][:born] + assert_nil permitted[:magazine] + end + + test "nested arrays with strings" do + params = ActionController::Parameters.new({ + :book => { + :genres => ["Tragedy"] + } + }) + + permitted = params.permit :book => :genres + assert_equal ["Tragedy"], permitted[:book][:genres] + end + + test "permit may specify symbols or strings" do + params = ActionController::Parameters.new({ + :book => { + :title => "Romeo and Juliet", + :author => "William Shakespeare" + }, + :magazine => "Shakespeare Today" + }) + + permitted = params.permit({:book => ["title", :author]}, "magazine") + assert_equal "Romeo and Juliet", permitted[:book][:title] + assert_equal "William Shakespeare", permitted[:book][:author] + assert_equal "Shakespeare Today", permitted[:magazine] + end + + test "nested array with strings that should be hashes" do + params = ActionController::Parameters.new({ + book: { + genres: ["Tragedy"] + } + }) + + permitted = params.permit book: { genres: :type } + assert_empty permitted[:book][:genres] + end + + test "nested array with strings that should be hashes and additional values" do + params = ActionController::Parameters.new({ + book: { + title: "Romeo and Juliet", + genres: ["Tragedy"] + } + }) + + permitted = params.permit book: [ :title, { genres: :type } ] + assert_equal "Romeo and Juliet", permitted[:book][:title] + assert_empty permitted[:book][:genres] + end + + test "nested string that should be a hash" do + params = ActionController::Parameters.new({ + book: { + genre: "Tragedy" + } + }) + + permitted = params.permit book: { genre: :type } + assert_nil permitted[:book][:genre] + end + + test "fields_for-style nested params" do + params = ActionController::Parameters.new({ + book: { + authors_attributes: { + :'0' => { name: 'William Shakespeare', age_of_death: '52' }, + :'-1' => { name: 'Unattributed Assistant' } + } + } + }) + permitted = params.permit book: { authors_attributes: [ :name ] } + + assert_not_nil permitted[:book][:authors_attributes]['0'] + assert_not_nil permitted[:book][:authors_attributes]['-1'] + assert_nil permitted[:book][:authors_attributes]['0'][:age_of_death] + assert_equal 'William Shakespeare', permitted[:book][:authors_attributes]['0'][:name] + assert_equal 'Unattributed Assistant', permitted[:book][:authors_attributes]['-1'][:name] + end +end diff --git a/actionpack/test/controller/parameters/parameters_permit_test.rb b/actionpack/test/controller/parameters/parameters_permit_test.rb new file mode 100644 index 0000000000..7fe8e6051b --- /dev/null +++ b/actionpack/test/controller/parameters/parameters_permit_test.rb @@ -0,0 +1,73 @@ +require 'abstract_unit' +require 'action_controller/metal/strong_parameters' + +class ParametersPermitTest < ActiveSupport::TestCase + setup do + @params = ActionController::Parameters.new({ person: { + age: "32", name: { first: "David", last: "Heinemeier Hansson" } + }}) + end + + test "fetch raises ParameterMissing exception" do + e = assert_raises(ActionController::ParameterMissing) do + @params.fetch :foo + end + assert_equal :foo, e.param + end + + test "fetch doesnt raise ParameterMissing exception if there is a default" do + assert_equal "monkey", @params.fetch(:foo, "monkey") + assert_equal "monkey", @params.fetch(:foo) { "monkey" } + end + + test "permitted is sticky on accessors" do + assert !@params.slice(:person).permitted? + assert !@params[:person][:name].permitted? + + @params.each { |key, value| assert(value.permitted?) if key == :person } + + assert !@params.fetch(:person).permitted? + + assert !@params.values_at(:person).first.permitted? + end + + test "permitted is sticky on mutators" do + assert !@params.delete_if { |k| k == :person }.permitted? + assert !@params.keep_if { |k,v| k == :person }.permitted? + end + + test "permitted is sticky beyond merges" do + assert !@params.merge(a: "b").permitted? + end + + test "modifying the parameters" do + @params[:person][:hometown] = "Chicago" + @params[:person][:family] = { brother: "Jonas" } + + assert_equal "Chicago", @params[:person][:hometown] + assert_equal "Jonas", @params[:person][:family][:brother] + end + + test "permitting parameters that are not there should not include the keys" do + assert !@params.permit(:person, :funky).has_key?(:funky) + end + + test "permit state is kept on a dup" do + @params.permit! + assert_equal @params.permitted?, @params.dup.permitted? + end + + test "permitted takes a default value when Parameters.permit_all_parameters is set" do + begin + ActionController::Parameters.permit_all_parameters = true + params = ActionController::Parameters.new({ person: { + age: "32", name: { first: "David", last: "Heinemeier Hansson" } + }}) + + assert params.slice(:person).permitted? + assert params[:person][:name].permitted? + ensure + ActionController::Parameters.permit_all_parameters = false + end + end +end diff --git a/actionpack/test/controller/parameters/parameters_require_test.rb b/actionpack/test/controller/parameters/parameters_require_test.rb new file mode 100644 index 0000000000..bdaba8d2d8 --- /dev/null +++ b/actionpack/test/controller/parameters/parameters_require_test.rb @@ -0,0 +1,10 @@ +require 'abstract_unit' +require 'action_controller/metal/strong_parameters' + +class ParametersRequireTest < ActiveSupport::TestCase + test "required parameters must be present not merely not nil" do + assert_raises(ActionController::ParameterMissing) do + ActionController::Parameters.new(person: {}).require(:person) + end + end +end diff --git a/actionpack/test/controller/params_wrapper_test.rb b/actionpack/test/controller/params_wrapper_test.rb index 5b05f77045..209f021cf7 100644 --- a/actionpack/test/controller/params_wrapper_test.rb +++ b/actionpack/test/controller/params_wrapper_test.rb @@ -155,7 +155,6 @@ class ParamsWrapperTest < ActionController::TestCase end def test_derived_wrapped_keys_from_matching_model - User.expects(:respond_to?).with(:accessible_attributes).returns(false) User.expects(:respond_to?).with(:attribute_names).returns(true) User.expects(:attribute_names).twice.returns(["username"]) @@ -168,7 +167,6 @@ class ParamsWrapperTest < ActionController::TestCase def test_derived_wrapped_keys_from_specified_model with_default_wrapper_options do - Person.expects(:respond_to?).with(:accessible_attributes).returns(false) Person.expects(:respond_to?).with(:attribute_names).returns(true) Person.expects(:attribute_names).twice.returns(["username"]) @@ -179,46 +177,8 @@ class ParamsWrapperTest < ActionController::TestCase assert_parameters({ 'username' => 'sikachu', 'title' => 'Developer', 'person' => { 'username' => 'sikachu' }}) end end - - def test_accessible_wrapped_keys_from_matching_model - User.expects(:respond_to?).with(:accessible_attributes).returns(true) - User.expects(:accessible_attributes).with(:default).twice.returns(["username"]) - - with_default_wrapper_options do - @request.env['CONTENT_TYPE'] = 'application/json' - post :parse, { 'username' => 'sikachu', 'title' => 'Developer' } - assert_parameters({ 'username' => 'sikachu', 'title' => 'Developer', 'user' => { 'username' => 'sikachu' }}) - end - end - - def test_accessible_wrapped_keys_from_specified_model - with_default_wrapper_options do - Person.expects(:respond_to?).with(:accessible_attributes).returns(true) - Person.expects(:accessible_attributes).with(:default).twice.returns(["username"]) - - UsersController.wrap_parameters Person - - @request.env['CONTENT_TYPE'] = 'application/json' - post :parse, { 'username' => 'sikachu', 'title' => 'Developer' } - assert_parameters({ 'username' => 'sikachu', 'title' => 'Developer', 'person' => { 'username' => 'sikachu' }}) - end - end - - def test_accessible_wrapped_keys_with_role_from_specified_model - with_default_wrapper_options do - Person.expects(:respond_to?).with(:accessible_attributes).returns(true) - Person.expects(:accessible_attributes).with(:admin).twice.returns(["username"]) - - UsersController.wrap_parameters Person, :as => :admin - - @request.env['CONTENT_TYPE'] = 'application/json' - post :parse, { 'username' => 'sikachu', 'title' => 'Developer' } - assert_parameters({ 'username' => 'sikachu', 'title' => 'Developer', 'person' => { 'username' => 'sikachu' }}) - end - end def test_not_wrapping_abstract_model - User.expects(:respond_to?).with(:accessible_attributes).returns(false) User.expects(:respond_to?).with(:attribute_names).returns(true) User.expects(:attribute_names).returns([]) diff --git a/actionpack/test/controller/permitted_params_test.rb b/actionpack/test/controller/permitted_params_test.rb new file mode 100644 index 0000000000..f46249d712 --- /dev/null +++ b/actionpack/test/controller/permitted_params_test.rb @@ -0,0 +1,25 @@ +require 'abstract_unit' + +class PeopleController < ActionController::Base + def create + render text: params[:person].permitted? ? "permitted" : "forbidden" + end + + def create_with_permit + render text: params[:person].permit(:name).permitted? ? "permitted" : "forbidden" + end +end + +class ActionControllerPermittedParamsTest < ActionController::TestCase + tests PeopleController + + test "parameters are forbidden" do + post :create, { person: { name: "Mjallo!" } } + assert_equal "forbidden", response.body + end + + test "parameters can be permitted and are then not forbidden" do + post :create_with_permit, { person: { name: "Mjallo!" } } + assert_equal "permitted", response.body + end +end diff --git a/actionpack/test/controller/record_identifier_test.rb b/actionpack/test/controller/record_identifier_test.rb deleted file mode 100644 index eb38b81e85..0000000000 --- a/actionpack/test/controller/record_identifier_test.rb +++ /dev/null @@ -1,40 +0,0 @@ -require 'abstract_unit' -require 'controller/fake_models' - -class RecordIdentifierTest < ActiveSupport::TestCase - include ActionController::RecordIdentifier - - def setup - @klass = Comment - @record = @klass.new - @singular = 'comment' - @plural = 'comments' - @uncountable = Sheep - end - - def test_dom_id_with_new_record - assert_equal "new_#{@singular}", dom_id(@record) - end - - def test_dom_id_with_new_record_and_prefix - assert_equal "custom_prefix_#{@singular}", dom_id(@record, :custom_prefix) - end - - def test_dom_id_with_saved_record - @record.save - assert_equal "#{@singular}_1", dom_id(@record) - end - - def test_dom_id_with_prefix - @record.save - assert_equal "edit_#{@singular}_1", dom_id(@record, :edit) - end - - def test_dom_class - assert_equal @singular, dom_class(@record) - end - - def test_dom_class_with_prefix - assert_equal "custom_prefix_#{@singular}", dom_class(@record, :custom_prefix) - end -end diff --git a/actionpack/test/controller/render_test.rb b/actionpack/test/controller/render_test.rb index 3f047fc9b5..fd8f87e377 100644 --- a/actionpack/test/controller/render_test.rb +++ b/actionpack/test/controller/render_test.rb @@ -22,6 +22,18 @@ module Quiz end end +class TestControllerWithExtraEtags < ActionController::Base + etag { nil } + etag { 'ab' } + etag { :cde } + etag { [:f] } + etag { nil } + + def fresh + render text: "stale" if stale?(etag: '123') + end +end + class TestController < ActionController::Base protect_from_forgery @@ -1626,6 +1638,26 @@ class LastModifiedRenderTest < ActionController::TestCase end end +class EtagRenderTest < ActionController::TestCase + tests TestControllerWithExtraEtags + + def setup + super + @request.host = "www.nextangle.com" + end + + def test_multiple_etags + @request.if_none_match = %("#{Digest::MD5.hexdigest(ActiveSupport::Cache.expand_cache_key([ "123", 'ab', :cde, [:f] ]))}") + get :fresh + assert_response :not_modified + + @request.if_none_match = %("nomatch") + get :fresh + assert_response :success + end +end + + class MetalRenderTest < ActionController::TestCase tests MetalTestController diff --git a/actionpack/test/controller/request_forgery_protection_test.rb b/actionpack/test/controller/request_forgery_protection_test.rb index 0289f4070b..1f637eb791 100644 --- a/actionpack/test/controller/request_forgery_protection_test.rb +++ b/actionpack/test/controller/request_forgery_protection_test.rb @@ -56,22 +56,18 @@ module RequestForgeryProtectionActions end # sample controllers -class RequestForgeryProtectionController < ActionController::Base +class RequestForgeryProtectionControllerUsingResetSession < ActionController::Base include RequestForgeryProtectionActions - protect_from_forgery :only => %w(index meta) + protect_from_forgery :only => %w(index meta), :with => :reset_session end class RequestForgeryProtectionControllerUsingException < ActionController::Base include RequestForgeryProtectionActions - protect_from_forgery :only => %w(index meta) - - def handle_unverified_request - raise(ActionController::InvalidAuthenticityToken) - end + protect_from_forgery :only => %w(index meta), :with => :exception end -class FreeCookieController < RequestForgeryProtectionController +class FreeCookieController < RequestForgeryProtectionControllerUsingResetSession self.allow_forgery_protection = false def index @@ -83,7 +79,7 @@ class FreeCookieController < RequestForgeryProtectionController end end -class CustomAuthenticityParamController < RequestForgeryProtectionController +class CustomAuthenticityParamController < RequestForgeryProtectionControllerUsingResetSession def form_authenticity_param 'foobar' end @@ -268,7 +264,7 @@ end # OK let's get our test on -class RequestForgeryProtectionControllerTest < ActionController::TestCase +class RequestForgeryProtectionControllerUsingResetSessionTest < ActionController::TestCase include RequestForgeryProtectionTests setup do diff --git a/actionpack/test/controller/required_params_test.rb b/actionpack/test/controller/required_params_test.rb new file mode 100644 index 0000000000..661bcb3945 --- /dev/null +++ b/actionpack/test/controller/required_params_test.rb @@ -0,0 +1,30 @@ +require 'abstract_unit' + +class BooksController < ActionController::Base + def create + params.require(:book).require(:name) + head :ok + end +end + +class ActionControllerRequiredParamsTest < ActionController::TestCase + tests BooksController + + test "missing required parameters will raise exception" do + post :create, { magazine: { name: "Mjallo!" } } + assert_response :bad_request + + post :create, { book: { title: "Mjallo!" } } + assert_response :bad_request + end + + test "required parameters that are present will not raise" do + post :create, { book: { name: "Mjallo!" } } + assert_response :ok + end + + test "missing parameters will be mentioned in the return" do + post :create, { magazine: { name: "Mjallo!" } } + assert_equal "Required parameter missing: book", response.body + end +end diff --git a/actionpack/test/controller/routing_test.rb b/actionpack/test/controller/routing_test.rb index 6cc1370105..f0430e516f 100644 --- a/actionpack/test/controller/routing_test.rb +++ b/actionpack/test/controller/routing_test.rb @@ -197,7 +197,7 @@ class LegacyRouteSetTests < ActiveSupport::TestCase end def test_regexp_precidence - @rs.draw do + rs.draw do get '/whois/:domain', :constraints => { :domain => /\w+\.[\w\.]+/ }, :to => lambda { |env| [200, {}, %w{regexp}] } @@ -216,7 +216,7 @@ class LegacyRouteSetTests < ActiveSupport::TestCase end } - @rs.draw do + rs.draw do get '/', :constraints => subdomain.new, :to => lambda { |env| [200, {}, %w{default}] } get '/', :constraints => { :subdomain => 'clients' }, @@ -228,7 +228,7 @@ class LegacyRouteSetTests < ActiveSupport::TestCase end def test_lambda_constraints - @rs.draw do + rs.draw do get '/', :constraints => lambda { |req| req.subdomain.present? and req.subdomain != "clients" }, :to => lambda { |env| [200, {}, %w{default}] } @@ -266,22 +266,22 @@ class LegacyRouteSetTests < ActiveSupport::TestCase def test_draw_with_block_arity_one_raises assert_raise(RuntimeError) do - @rs.draw { |map| map.match '/:controller(/:action(/:id))' } + rs.draw { |map| map.match '/:controller(/:action(/:id))' } end end def test_specific_controller_action_failure - @rs.draw do + rs.draw do mount lambda {} => "/foo" end - assert_raises(ActionController::RoutingError) do - url_for(@rs, :controller => "omg", :action => "lol") + assert_raises(ActionController::UrlGenerationError) do + url_for(rs, :controller => "omg", :action => "lol") end end def test_default_setup - @rs.draw { get '/:controller(/:action(/:id))' } + rs.draw { get '/:controller(/:action(/:id))' } assert_equal({:controller => "content", :action => 'index'}, rs.recognize_path("/content")) assert_equal({:controller => "content", :action => 'list'}, rs.recognize_path("/content/list")) assert_equal({:controller => "content", :action => 'show', :id => '10'}, rs.recognize_path("/content/show/10")) @@ -298,8 +298,8 @@ class LegacyRouteSetTests < ActiveSupport::TestCase end def test_ignores_leading_slash - @rs.clear! - @rs.draw { get '/:controller(/:action(/:id))'} + rs.clear! + rs.draw { get '/:controller(/:action(/:id))'} test_default_setup end @@ -470,7 +470,7 @@ class LegacyRouteSetTests < ActiveSupport::TestCase end def test_changing_controller - @rs.draw { get ':controller/:action/:id' } + rs.draw { get ':controller/:action/:id' } assert_equal '/admin/stuff/show/10', url_for(rs, {:controller => 'stuff', :action => 'show', :id => 10}, @@ -514,7 +514,7 @@ class LegacyRouteSetTests < ActiveSupport::TestCase rs.draw do get 'post/:id' => 'post#show', :constraints => { :id => /\d+/ }, :as => 'post' end - assert_raise(ActionController::RoutingError) do + assert_raise(ActionController::UrlGenerationError) do url_for(rs, { :controller => 'post', :action => 'show', :bad_param => "foo", :use_route => "post" }) end end @@ -583,7 +583,7 @@ class LegacyRouteSetTests < ActiveSupport::TestCase end def test_action_expiry - @rs.draw { get ':controller(/:action(/:id))' } + rs.draw { get ':controller(/:action(/:id))' } assert_equal '/content', url_for(rs, { :controller => 'content' }, { :controller => 'content', :action => 'show' }) end @@ -594,7 +594,7 @@ class LegacyRouteSetTests < ActiveSupport::TestCase assert_equal '/post/10', url_for(rs, { :controller => 'post', :action => 'show', :id => 10 }) - assert_raise ActionController::RoutingError do + assert_raise(ActionController::UrlGenerationError) do url_for(rs, { :controller => 'post', :action => 'show' }) end end @@ -760,7 +760,7 @@ class LegacyRouteSetTests < ActiveSupport::TestCase get 'foos/:id' => 'foos#show', :as => 'foo_with_requirement', :constraints => { :id => /\d+/ } end - assert_raise(ActionController::RoutingError) do + assert_raise(ActionController::UrlGenerationError) do setup_for_named_route.send(:foo_with_requirement_url, "I am Against the constraints") end end @@ -1051,7 +1051,7 @@ class RouteSetTest < ActiveSupport::TestCase set.draw do get "/people" => "missing#index" end - + assert_raise(ActionController::RoutingError) { set.recognize_path("/people", :method => :get) } @@ -1459,7 +1459,7 @@ class RouteSetTest < ActiveSupport::TestCase url = url_for(set, { :controller => 'pages', :action => 'show', :name => 'david' }) assert_equal "/page/david", url - assert_raise ActionController::RoutingError do + assert_raise(ActionController::UrlGenerationError) do url_for(set, { :controller => 'pages', :action => 'show', :name => 'davidjamis' }) end url = url_for(set, { :controller => 'pages', :action => 'show', :name => 'JAMIS' }) diff --git a/actionpack/test/controller/selector_test.rb b/actionpack/test/controller/selector_test.rb index 5e302da212..1e80c8601c 100644 --- a/actionpack/test/controller/selector_test.rb +++ b/actionpack/test/controller/selector_test.rb @@ -5,7 +5,7 @@ require 'abstract_unit' require 'controller/fake_controllers' -require 'action_controller/vendor/html-scanner' +require 'action_view/vendor/html-scanner' class SelectorTest < ActiveSupport::TestCase # diff --git a/actionpack/test/controller/spec_style_test.rb b/actionpack/test/controller/spec_style_test.rb new file mode 100644 index 0000000000..e118c584ca --- /dev/null +++ b/actionpack/test/controller/spec_style_test.rb @@ -0,0 +1,208 @@ +require "abstract_unit" + +class ApplicationController < ActionController::Base; end +class ModelsController < ApplicationController; end +module Admin + class WidgetsController < ApplicationController; end +end + +# ApplicationController +describe ApplicationController do + describe "nested" do + describe "even deeper" do + it "exists" do + assert_kind_of ApplicationController, @controller + end + end + end +end + +describe ApplicationController, :index do + describe "nested" do + describe "even deeper" do + it "exists" do + assert_kind_of ApplicationController, @controller + end + end + end +end + +describe ApplicationController, "unauthenticated user" do + describe "nested" do + describe "even deeper" do + it "exists" do + assert_kind_of ApplicationController, @controller + end + end + end +end + +describe "ApplicationControllerTest" do + describe "nested" do + describe "even deeper" do + it "exists" do + assert_kind_of ApplicationController, @controller + end + end + end +end + +describe "ApplicationControllerTest", :index do + describe "nested" do + describe "even deeper" do + it "exists" do + assert_kind_of ApplicationController, @controller + end + end + end +end + +describe "ApplicationControllerTest", "unauthenticated user" do + describe "nested" do + describe "even deeper" do + it "exists" do + assert_kind_of ApplicationController, @controller + end + end + end +end + +# ModelsController +describe ModelsController do + describe "nested" do + describe "even deeper" do + it "exists" do + assert_kind_of ModelsController, @controller + end + end + end +end + +describe ModelsController, :index do + describe "nested" do + describe "even deeper" do + it "exists" do + assert_kind_of ModelsController, @controller + end + end + end +end + +describe ModelsController, "unauthenticated user" do + describe "nested" do + describe "even deeper" do + it "exists" do + assert_kind_of ModelsController, @controller + end + end + end +end + +describe "ModelsControllerTest" do + describe "nested" do + describe "even deeper" do + it "exists" do + assert_kind_of ModelsController, @controller + end + end + end +end + +describe "ModelsControllerTest", :index do + describe "nested" do + describe "even deeper" do + it "exists" do + assert_kind_of ModelsController, @controller + end + end + end +end + +describe "ModelsControllerTest", "unauthenticated user" do + describe "nested" do + describe "even deeper" do + it "exists" do + assert_kind_of ModelsController, @controller + end + end + end +end + +# Nested Admin::WidgetsControllerTest +module Admin + class WidgetsControllerTest < ActionController::TestCase + test "exists" do + assert_kind_of Admin::WidgetsController, @controller + end + end + + describe WidgetsController do + describe "index" do + it "respond successful" do + assert_kind_of Admin::WidgetsController, @controller + end + end + end + + describe WidgetsController, "unauthenticated users" do + describe "index" do + it "respond successful" do + assert_kind_of Admin::WidgetsController, @controller + end + end + end +end + +class Admin::WidgetsControllerTest < ActionController::TestCase + test "exists here too" do + assert_kind_of Admin::WidgetsController, @controller + end +end + +describe Admin::WidgetsController do + describe "index" do + it "respond successful" do + assert_kind_of Admin::WidgetsController, @controller + end + end +end + +describe Admin::WidgetsController, "unauthenticated users" do + describe "index" do + it "respond successful" do + assert_kind_of Admin::WidgetsController, @controller + end + end +end + +describe "Admin::WidgetsController" do + describe "index" do + it "respond successful" do + assert_kind_of Admin::WidgetsController, @controller + end + end +end + +describe "Admin::WidgetsControllerTest" do + describe "index" do + it "respond successful" do + assert_kind_of Admin::WidgetsController, @controller + end + end +end + +describe "Admin::WidgetsController", "unauthenticated users" do + describe "index" do + it "respond successful" do + assert_kind_of Admin::WidgetsController, @controller + end + end +end + +describe "Admin::WidgetsControllerTest", "unauthenticated users" do + describe "index" do + it "respond successful" do + assert_kind_of Admin::WidgetsController, @controller + end + end +end diff --git a/actionpack/test/controller/spec_type_test.rb b/actionpack/test/controller/spec_type_test.rb new file mode 100644 index 0000000000..13be8a3405 --- /dev/null +++ b/actionpack/test/controller/spec_type_test.rb @@ -0,0 +1,37 @@ +require "abstract_unit" + +class ApplicationController < ActionController::Base; end +class ModelsController < ApplicationController; end + +class ActionControllerSpecTypeTest < ActiveSupport::TestCase + def assert_controller actual + assert_equal ActionController::TestCase, actual + end + + def refute_controller actual + refute_equal ActionController::TestCase, actual + end + + def test_spec_type_resolves_for_class_constants + assert_controller MiniTest::Spec.spec_type(ApplicationController) + assert_controller MiniTest::Spec.spec_type(ModelsController) + end + + def test_spec_type_resolves_for_matching_strings + assert_controller MiniTest::Spec.spec_type("WidgetController") + assert_controller MiniTest::Spec.spec_type("WidgetControllerTest") + assert_controller MiniTest::Spec.spec_type("Widget Controller Test") + # And is not case sensitive + assert_controller MiniTest::Spec.spec_type("widgetcontroller") + assert_controller MiniTest::Spec.spec_type("widgetcontrollertest") + assert_controller MiniTest::Spec.spec_type("widget controller test") + end + + def test_spec_type_wont_match_non_space_characters + refute_controller MiniTest::Spec.spec_type("Widget Controller\tTest") + refute_controller MiniTest::Spec.spec_type("Widget Controller\rTest") + refute_controller MiniTest::Spec.spec_type("Widget Controller\nTest") + refute_controller MiniTest::Spec.spec_type("Widget Controller\fTest") + refute_controller MiniTest::Spec.spec_type("Widget ControllerXTest") + end +end |