提交 7ee60d4c 编写于 作者: K Kasper Timm Hansen

Merge pull request #21368 from rodzyn/remove_mocha

Get rid of mocha tests in actionpack - part 2
......@@ -41,6 +41,8 @@ class << self
def env
@_env ||= ActiveSupport::StringInquirer.new(ENV["RAILS_ENV"] || ENV["RACK_ENV"] || "test")
end
def root; end;
end
end
......@@ -407,7 +409,7 @@ def jruby_skip(message = '')
end
require 'mocha/setup' # FIXME: stop using mocha
require 'minitest/mock'
require 'active_support/testing/method_call_assertions'
class ForkingExecutor
class Server
......@@ -479,3 +481,7 @@ def translate_exceptions(result)
# Use N processes (N defaults to 4)
Minitest.parallel_executor = ForkingExecutor.new(PROCESS_COUNT)
end
class ActiveSupport::TestCase
include ActiveSupport::Testing::MethodCallAssertions
end
......@@ -131,9 +131,7 @@ def form_authenticity_param
# common test methods
module RequestForgeryProtectionTests
def setup
@token = "cf50faa3fe97702ca1ae"
@controller.stubs(:valid_authenticity_token?).with{ |_, t| t == @token }.returns(true)
@controller.stubs(:valid_authenticity_token?).with{ |_, t| t != @token }.returns(false)
@token = Base64.strict_encode64('railstestrailstestrailstestrails')
@old_request_forgery_protection_token = ActionController::Base.request_forgery_protection_token
ActionController::Base.request_forgery_protection_token = :custom_authenticity_token
end
......@@ -255,37 +253,53 @@ def test_should_not_allow_xhr_post_without_token
end
def test_should_allow_post_with_token
assert_not_blocked { post :index, params: { custom_authenticity_token: @token } }
session[:_csrf_token] = @token
@controller.stub :form_authenticity_token, @token do
assert_not_blocked { post :index, params: { custom_authenticity_token: @token } }
end
end
def test_should_allow_patch_with_token
assert_not_blocked { patch :index, params: { custom_authenticity_token: @token } }
session[:_csrf_token] = @token
@controller.stub :form_authenticity_token, @token do
assert_not_blocked { patch :index, params: { custom_authenticity_token: @token } }
end
end
def test_should_allow_put_with_token
assert_not_blocked { put :index, params: { custom_authenticity_token: @token } }
session[:_csrf_token] = @token
@controller.stub :form_authenticity_token, @token do
assert_not_blocked { put :index, params: { custom_authenticity_token: @token } }
end
end
def test_should_allow_delete_with_token
assert_not_blocked { delete :index, params: { custom_authenticity_token: @token } }
session[:_csrf_token] = @token
@controller.stub :form_authenticity_token, @token do
assert_not_blocked { delete :index, params: { custom_authenticity_token: @token } }
end
end
def test_should_allow_post_with_token_in_header
session[:_csrf_token] = @token
@request.env['HTTP_X_CSRF_TOKEN'] = @token
assert_not_blocked { post :index }
end
def test_should_allow_delete_with_token_in_header
session[:_csrf_token] = @token
@request.env['HTTP_X_CSRF_TOKEN'] = @token
assert_not_blocked { delete :index }
end
def test_should_allow_patch_with_token_in_header
session[:_csrf_token] = @token
@request.env['HTTP_X_CSRF_TOKEN'] = @token
assert_not_blocked { patch :index }
end
def test_should_allow_put_with_token_in_header
session[:_csrf_token] = @token
@request.env['HTTP_X_CSRF_TOKEN'] = @token
assert_not_blocked { put :index }
end
......@@ -339,6 +353,7 @@ def test_should_only_allow_same_origin_js_get_with_xhr_header
# Allow non-GET requests since GET is all a remote <script> tag can muster.
def test_should_allow_non_get_js_without_xhr_header
session[:_csrf_token] = @token
assert_cross_origin_not_blocked { post :same_origin_js, params: { custom_authenticity_token: @token } }
assert_cross_origin_not_blocked { post :same_origin_js, params: { format: 'js', custom_authenticity_token: @token } }
assert_cross_origin_not_blocked do
......@@ -412,7 +427,8 @@ class RequestForgeryProtectionControllerUsingResetSessionTest < ActionController
get :meta
assert_select 'meta[name=?][content=?]', 'csrf-param', 'custom_authenticity_token'
assert_select 'meta[name=?]', 'csrf-token'
assert_match(/cf50faa3fe97702ca1ae&lt;=\?/, @response.body)
regexp = "#{@token}&lt;=\?"
assert_match(/#{regexp}/, @response.body)
end
end
end
......
......@@ -71,14 +71,6 @@ def call(env)
end
end
def setup
app = ActiveSupport::OrderedOptions.new
app.config = ActiveSupport::OrderedOptions.new
app.config.assets = ActiveSupport::OrderedOptions.new
app.config.assets.prefix = '/sprockets'
Rails.stubs(:application).returns(app)
end
RoutesApp = Struct.new(:routes).new(SharedTestRoutes)
ProductionApp = ActionDispatch::DebugExceptions.new(Boomer.new(false), RoutesApp)
DevelopmentApp = ActionDispatch::DebugExceptions.new(Boomer.new(true), RoutesApp)
......@@ -338,36 +330,37 @@ def setup
test 'debug exceptions app shows user code that caused the error in source view' do
@app = DevelopmentApp
Rails.stubs(:root).returns(Pathname.new('.'))
cleaner = ActiveSupport::BacktraceCleaner.new.tap do |bc|
bc.add_silencer { |line| line =~ /method_that_raises/ }
bc.add_silencer { |line| line !~ %r{test/dispatch/debug_exceptions_test.rb} }
end
Rails.stub :root, Pathname.new('.') do
cleaner = ActiveSupport::BacktraceCleaner.new.tap do |bc|
bc.add_silencer { |line| line =~ /method_that_raises/ }
bc.add_silencer { |line| line !~ %r{test/dispatch/debug_exceptions_test.rb} }
end
get '/framework_raises', headers: { 'action_dispatch.backtrace_cleaner' => cleaner }
get '/framework_raises', headers: { 'action_dispatch.backtrace_cleaner' => cleaner }
# Assert correct error
assert_response 500
assert_select 'h2', /error in framework/
# Assert correct error
assert_response 500
assert_select 'h2', /error in framework/
# assert source view line is the call to method_that_raises
assert_select 'div.source:not(.hidden)' do
assert_select 'pre .line.active', /method_that_raises/
end
# assert source view line is the call to method_that_raises
assert_select 'div.source:not(.hidden)' do
assert_select 'pre .line.active', /method_that_raises/
end
# assert first source view (hidden) that throws the error
assert_select 'div.source:first' do
assert_select 'pre .line.active', /raise StandardError\.new/
end
# assert first source view (hidden) that throws the error
assert_select 'div.source:first' do
assert_select 'pre .line.active', /raise StandardError\.new/
end
# assert application trace refers to line that calls method_that_raises is first
assert_select '#Application-Trace' do
assert_select 'pre code a:first', %r{test/dispatch/debug_exceptions_test\.rb:\d+:in `call}
end
# assert application trace refers to line that calls method_that_raises is first
assert_select '#Application-Trace' do
assert_select 'pre code a:first', %r{test/dispatch/debug_exceptions_test\.rb:\d+:in `call}
end
# assert framework trace that that threw the error is first
assert_select '#Framework-Trace' do
assert_select 'pre code a:first', /method_that_raises/
# assert framework trace that that threw the error is first
assert_select '#Framework-Trace' do
assert_select 'pre code a:first', /method_that_raises/
end
end
end
end
......@@ -12,12 +12,6 @@ module Routing
class RoutesInspectorTest < ActiveSupport::TestCase
def setup
@set = ActionDispatch::Routing::RouteSet.new
app = ActiveSupport::OrderedOptions.new
app.config = ActiveSupport::OrderedOptions.new
app.config.assets = ActiveSupport::OrderedOptions.new
app.config.assets.prefix = '/sprockets'
Rails.stubs(:application).returns(app)
Rails.stubs(:env).returns("development")
end
def draw(options = {}, &block)
......@@ -316,9 +310,6 @@ def test_regression_route_with_controller_regexp
def test_inspect_routes_shows_resources_route_when_assets_disabled
@set = ActionDispatch::Routing::RouteSet.new
app = ActiveSupport::OrderedOptions.new
Rails.stubs(:application).returns(app)
output = draw do
get '/cart', to: 'cart#show'
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册