2009-10-08 21:12:28 -04:00
|
|
|
require "isolation/abstract_unit"
|
2011-12-21 17:03:43 -05:00
|
|
|
require 'rack/test'
|
2012-12-20 17:41:52 -05:00
|
|
|
require 'env_helpers'
|
2009-10-08 21:12:28 -04:00
|
|
|
|
2011-04-02 04:51:47 -04:00
|
|
|
class ::MyMailInterceptor
|
|
|
|
def self.delivering_email(email); email; end
|
|
|
|
end
|
|
|
|
|
|
|
|
class ::MyOtherMailInterceptor < ::MyMailInterceptor; end
|
|
|
|
|
2014-06-15 08:13:34 -04:00
|
|
|
class ::MyPreviewMailInterceptor
|
|
|
|
def self.previewing_email(email); email; end
|
|
|
|
end
|
|
|
|
|
|
|
|
class ::MyOtherPreviewMailInterceptor < ::MyPreviewMailInterceptor; end
|
|
|
|
|
2011-04-02 04:51:47 -04:00
|
|
|
class ::MyMailObserver
|
|
|
|
def self.delivered_email(email); email; end
|
|
|
|
end
|
|
|
|
|
|
|
|
class ::MyOtherMailObserver < ::MyMailObserver; end
|
|
|
|
|
2009-10-08 21:12:28 -04:00
|
|
|
module ApplicationTests
|
2012-01-05 20:30:17 -05:00
|
|
|
class ConfigurationTest < ActiveSupport::TestCase
|
2009-10-08 21:12:28 -04:00
|
|
|
include ActiveSupport::Testing::Isolation
|
2011-12-21 17:03:43 -05:00
|
|
|
include Rack::Test::Methods
|
2012-12-20 17:41:52 -05:00
|
|
|
include EnvHelpers
|
2009-10-08 21:12:28 -04:00
|
|
|
|
2009-12-22 20:03:23 -05:00
|
|
|
def new_app
|
|
|
|
File.expand_path("#{app_path}/../new_app")
|
|
|
|
end
|
|
|
|
|
|
|
|
def copy_app
|
|
|
|
FileUtils.cp_r(app_path, new_app)
|
|
|
|
end
|
|
|
|
|
2010-01-27 11:46:55 -05:00
|
|
|
def app
|
|
|
|
@app ||= Rails.application
|
|
|
|
end
|
|
|
|
|
2009-10-08 21:12:28 -04:00
|
|
|
def setup
|
|
|
|
build_app
|
|
|
|
boot_rails
|
2009-12-31 16:11:54 -05:00
|
|
|
FileUtils.rm_rf("#{app_path}/config/environments")
|
2009-10-08 21:12:28 -04:00
|
|
|
end
|
2011-06-06 08:54:05 -04:00
|
|
|
|
|
|
|
def teardown
|
|
|
|
teardown_app
|
2010-01-27 15:20:32 -05:00
|
|
|
FileUtils.rm_rf(new_app) if File.directory?(new_app)
|
|
|
|
end
|
|
|
|
|
2012-12-20 17:41:52 -05:00
|
|
|
test "Rails.env does not set the RAILS_ENV environment variable which would leak out into rake tasks" do
|
|
|
|
require "rails"
|
|
|
|
|
|
|
|
switch_env "RAILS_ENV", nil do
|
|
|
|
Rails.env = "development"
|
|
|
|
assert_equal "development", Rails.env
|
|
|
|
assert_nil ENV['RAILS_ENV']
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-11-04 17:14:03 -05:00
|
|
|
test "lib dir is on LOAD_PATH during config" do
|
|
|
|
app_file 'lib/my_logger.rb', <<-RUBY
|
|
|
|
require "logger"
|
|
|
|
class MyLogger < ::Logger
|
|
|
|
end
|
|
|
|
RUBY
|
|
|
|
add_to_top_of_config <<-RUBY
|
2014-11-04 17:54:52 -05:00
|
|
|
require 'my_logger'
|
2014-11-04 17:14:03 -05:00
|
|
|
config.logger = MyLogger.new STDOUT
|
|
|
|
RUBY
|
|
|
|
require "#{app_path}/config/environment"
|
2014-11-04 17:54:52 -05:00
|
|
|
assert_equal 'MyLogger', Rails.application.config.logger.class.name
|
2014-11-04 17:14:03 -05:00
|
|
|
end
|
|
|
|
|
2012-06-06 16:47:03 -04:00
|
|
|
test "a renders exception on pending migration" do
|
|
|
|
add_to_config <<-RUBY
|
|
|
|
config.active_record.migration_error = :page_load
|
|
|
|
config.consider_all_requests_local = true
|
|
|
|
config.action_dispatch.show_exceptions = true
|
|
|
|
RUBY
|
|
|
|
|
2014-07-07 21:46:39 -04:00
|
|
|
app_file 'db/migrate/20140708012246_create_user.rb', <<-RUBY
|
|
|
|
class CreateUser < ActiveRecord::Migration
|
|
|
|
def change
|
|
|
|
create_table :users
|
|
|
|
end
|
|
|
|
end
|
|
|
|
RUBY
|
|
|
|
|
2012-06-06 16:47:03 -04:00
|
|
|
require "#{app_path}/config/environment"
|
|
|
|
|
2014-07-07 21:46:39 -04:00
|
|
|
ActiveRecord::Migrator.migrations_paths = ["#{app_path}/db/migrate"]
|
|
|
|
|
|
|
|
begin
|
|
|
|
get "/foo"
|
|
|
|
assert_equal 500, last_response.status
|
|
|
|
assert_match "ActiveRecord::PendingMigrationError", last_response.body
|
|
|
|
ensure
|
|
|
|
ActiveRecord::Migrator.migrations_paths = nil
|
|
|
|
end
|
2012-06-06 16:47:03 -04:00
|
|
|
end
|
|
|
|
|
2011-06-21 07:02:47 -04:00
|
|
|
test "Rails.groups returns available groups" do
|
|
|
|
require "rails"
|
|
|
|
|
|
|
|
Rails.env = "development"
|
|
|
|
assert_equal [:default, "development"], Rails.groups
|
2012-06-23 06:42:00 -04:00
|
|
|
assert_equal [:default, "development", :assets], Rails.groups(assets: [:development])
|
|
|
|
assert_equal [:default, "development", :another, :assets], Rails.groups(:another, assets: %w(development))
|
2011-06-21 07:02:47 -04:00
|
|
|
|
|
|
|
Rails.env = "test"
|
2012-06-23 06:42:00 -04:00
|
|
|
assert_equal [:default, "test"], Rails.groups(assets: [:development])
|
2011-06-21 07:02:47 -04:00
|
|
|
|
|
|
|
ENV["RAILS_GROUPS"] = "javascripts,stylesheets"
|
|
|
|
assert_equal [:default, "test", "javascripts", "stylesheets"], Rails.groups
|
|
|
|
end
|
|
|
|
|
2010-07-19 11:53:14 -04:00
|
|
|
test "Rails.application is nil until app is initialized" do
|
2010-01-26 06:14:48 -05:00
|
|
|
require 'rails'
|
2010-07-19 11:53:14 -04:00
|
|
|
assert_nil Rails.application
|
2010-01-26 06:14:48 -05:00
|
|
|
require "#{app_path}/config/environment"
|
2010-07-19 11:53:14 -04:00
|
|
|
assert_equal AppTemplate::Application.instance, Rails.application
|
2010-01-26 06:14:48 -05:00
|
|
|
end
|
|
|
|
|
2010-07-19 11:53:14 -04:00
|
|
|
test "Rails.application responds to all instance methods" do
|
2010-01-27 15:20:32 -05:00
|
|
|
require "#{app_path}/config/environment"
|
2010-07-19 11:53:14 -04:00
|
|
|
assert_respond_to Rails.application, :routes_reloader
|
|
|
|
assert_equal Rails.application.routes_reloader, AppTemplate::Application.routes_reloader
|
2010-01-30 06:30:15 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
test "Rails::Application responds to paths" do
|
|
|
|
require "#{app_path}/config/environment"
|
|
|
|
assert_respond_to AppTemplate::Application, :paths
|
2014-08-26 11:53:19 -04:00
|
|
|
assert_equal ["#{app_path}/app/views"], AppTemplate::Application.paths["app/views"].expanded
|
2010-01-27 15:20:32 -05:00
|
|
|
end
|
|
|
|
|
2009-10-08 21:12:28 -04:00
|
|
|
test "the application root is set correctly" do
|
2009-10-14 19:13:45 -04:00
|
|
|
require "#{app_path}/config/environment"
|
2009-10-16 15:49:39 -04:00
|
|
|
assert_equal Pathname.new(app_path), Rails.application.root
|
2009-10-14 19:13:45 -04:00
|
|
|
end
|
|
|
|
|
2010-01-03 01:49:40 -05:00
|
|
|
test "the application root can be seen from the application singleton" do
|
|
|
|
require "#{app_path}/config/environment"
|
|
|
|
assert_equal Pathname.new(app_path), AppTemplate::Application.root
|
|
|
|
end
|
|
|
|
|
2009-10-14 19:13:45 -04:00
|
|
|
test "the application root can be set" do
|
2009-12-22 20:03:23 -05:00
|
|
|
copy_app
|
2009-10-14 19:13:45 -04:00
|
|
|
add_to_config <<-RUBY
|
2009-12-22 20:03:23 -05:00
|
|
|
config.root = '#{new_app}'
|
2009-10-14 19:13:45 -04:00
|
|
|
RUBY
|
2010-01-22 10:24:44 -05:00
|
|
|
|
2009-12-22 20:03:23 -05:00
|
|
|
use_frameworks []
|
2010-01-22 10:24:44 -05:00
|
|
|
|
2009-10-14 19:13:45 -04:00
|
|
|
require "#{app_path}/config/environment"
|
2009-12-22 20:03:23 -05:00
|
|
|
assert_equal Pathname.new(new_app), Rails.application.root
|
2009-10-14 19:13:45 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
test "the application root is Dir.pwd if there is no config.ru" do
|
|
|
|
File.delete("#{app_path}/config.ru")
|
|
|
|
|
2009-12-22 20:03:23 -05:00
|
|
|
use_frameworks []
|
|
|
|
|
|
|
|
Dir.chdir("#{app_path}") do
|
2009-10-14 19:13:45 -04:00
|
|
|
require "#{app_path}/config/environment"
|
2009-12-22 20:03:23 -05:00
|
|
|
assert_equal Pathname.new("#{app_path}"), Rails.application.root
|
2009-10-14 19:13:45 -04:00
|
|
|
end
|
2009-10-08 21:12:28 -04:00
|
|
|
end
|
2009-12-02 13:14:02 -05:00
|
|
|
|
2010-01-26 07:57:11 -05:00
|
|
|
test "Rails.root should be a Pathname" do
|
|
|
|
add_to_config <<-RUBY
|
|
|
|
config.root = "#{app_path}"
|
|
|
|
RUBY
|
2009-12-23 22:00:20 -05:00
|
|
|
require "#{app_path}/config/environment"
|
2010-01-26 07:57:11 -05:00
|
|
|
assert_instance_of Pathname, Rails.root
|
2009-12-02 13:14:02 -05:00
|
|
|
end
|
2009-12-16 01:07:12 -05:00
|
|
|
|
2012-10-02 20:44:02 -04:00
|
|
|
test "Rails.public_path should be a Pathname" do
|
|
|
|
add_to_config <<-RUBY
|
|
|
|
config.paths["public"] = "somewhere"
|
|
|
|
RUBY
|
|
|
|
require "#{app_path}/config/environment"
|
|
|
|
assert_instance_of Pathname, Rails.public_path
|
|
|
|
end
|
|
|
|
|
2012-08-01 15:10:55 -04:00
|
|
|
test "initialize an eager loaded, cache classes app" do
|
2009-12-16 01:07:12 -05:00
|
|
|
add_to_config <<-RUBY
|
2012-08-01 15:10:55 -04:00
|
|
|
config.eager_load = true
|
|
|
|
config.cache_classes = true
|
2009-12-16 01:07:12 -05:00
|
|
|
RUBY
|
|
|
|
|
|
|
|
require "#{app_path}/config/application"
|
2013-06-03 23:57:01 -04:00
|
|
|
assert Rails.application.initialize!
|
2009-12-16 01:07:12 -05:00
|
|
|
end
|
|
|
|
|
2012-08-01 15:10:55 -04:00
|
|
|
test "application is always added to eager_load namespaces" do
|
2012-06-15 14:36:21 -04:00
|
|
|
require "#{app_path}/config/application"
|
2014-09-21 17:36:00 -04:00
|
|
|
assert_includes Rails.application.config.eager_load_namespaces, AppTemplate::Application
|
2012-06-15 14:36:21 -04:00
|
|
|
end
|
|
|
|
|
2012-08-01 15:10:55 -04:00
|
|
|
test "the application can be eager loaded even when there are no frameworks" do
|
2009-12-16 01:07:12 -05:00
|
|
|
FileUtils.rm_rf("#{app_path}/config/environments")
|
|
|
|
add_to_config <<-RUBY
|
2012-08-01 15:10:55 -04:00
|
|
|
config.eager_load = true
|
|
|
|
config.cache_classes = true
|
2009-12-16 01:07:12 -05:00
|
|
|
RUBY
|
|
|
|
|
2009-12-31 13:36:24 -05:00
|
|
|
use_frameworks []
|
|
|
|
|
2009-12-16 01:07:12 -05:00
|
|
|
assert_nothing_raised do
|
|
|
|
require "#{app_path}/config/application"
|
|
|
|
end
|
|
|
|
end
|
2009-12-29 22:04:14 -05:00
|
|
|
|
2010-01-20 23:48:27 -05:00
|
|
|
test "filter_parameters should be able to set via config.filter_parameters" do
|
|
|
|
add_to_config <<-RUBY
|
2010-01-21 10:50:11 -05:00
|
|
|
config.filter_parameters += [ :foo, 'bar', lambda { |key, value|
|
2010-01-20 23:48:27 -05:00
|
|
|
value = value.reverse if key =~ /baz/
|
2010-01-21 10:50:11 -05:00
|
|
|
}]
|
2010-01-20 23:48:27 -05:00
|
|
|
RUBY
|
2010-01-26 07:57:11 -05:00
|
|
|
|
2010-01-20 23:48:27 -05:00
|
|
|
assert_nothing_raised do
|
|
|
|
require "#{app_path}/config/application"
|
|
|
|
end
|
|
|
|
end
|
2010-01-27 11:46:55 -05:00
|
|
|
|
2013-01-05 13:04:15 -05:00
|
|
|
test "filter_parameters should be able to set via config.filter_parameters in an initializer" do
|
2013-01-05 13:41:39 -05:00
|
|
|
app_file 'config/initializers/filter_parameters_logging.rb', <<-RUBY
|
|
|
|
Rails.application.config.filter_parameters += [ :password, :foo, 'bar' ]
|
2013-01-05 13:04:15 -05:00
|
|
|
RUBY
|
|
|
|
|
|
|
|
require "#{app_path}/config/environment"
|
|
|
|
|
|
|
|
assert_equal [:password, :foo, 'bar'], Rails.application.env_config['action_dispatch.parameter_filter']
|
|
|
|
end
|
|
|
|
|
2010-01-27 11:46:55 -05:00
|
|
|
test "config.to_prepare is forwarded to ActionDispatch" do
|
|
|
|
$prepared = false
|
|
|
|
|
|
|
|
add_to_config <<-RUBY
|
|
|
|
config.to_prepare do
|
|
|
|
$prepared = true
|
|
|
|
end
|
|
|
|
RUBY
|
|
|
|
|
|
|
|
assert !$prepared
|
|
|
|
|
|
|
|
require "#{app_path}/config/environment"
|
|
|
|
|
|
|
|
get "/"
|
|
|
|
assert $prepared
|
|
|
|
end
|
2010-02-23 20:03:06 -05:00
|
|
|
|
2010-08-15 19:29:27 -04:00
|
|
|
def assert_utf8
|
2011-12-20 11:58:45 -05:00
|
|
|
assert_equal Encoding::UTF_8, Encoding.default_external
|
|
|
|
assert_equal Encoding::UTF_8, Encoding.default_internal
|
2010-08-15 19:29:27 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
test "skipping config.encoding still results in 'utf-8' as the default" do
|
|
|
|
require "#{app_path}/config/application"
|
|
|
|
assert_utf8
|
|
|
|
end
|
|
|
|
|
2010-04-06 20:24:29 -04:00
|
|
|
test "config.encoding sets the default encoding" do
|
|
|
|
add_to_config <<-RUBY
|
|
|
|
config.encoding = "utf-8"
|
|
|
|
RUBY
|
|
|
|
|
|
|
|
require "#{app_path}/config/application"
|
2010-08-15 19:29:27 -04:00
|
|
|
assert_utf8
|
2010-04-06 20:24:29 -04:00
|
|
|
end
|
|
|
|
|
2010-04-08 06:52:37 -04:00
|
|
|
test "config.paths.public sets Rails.public_path" do
|
|
|
|
add_to_config <<-RUBY
|
2010-10-06 11:18:59 -04:00
|
|
|
config.paths["public"] = "somewhere"
|
2010-04-08 06:52:37 -04:00
|
|
|
RUBY
|
|
|
|
|
|
|
|
require "#{app_path}/config/application"
|
2012-10-02 20:44:02 -04:00
|
|
|
assert_equal Pathname.new(app_path).join("somewhere"), Rails.public_path
|
2010-04-08 06:52:37 -04:00
|
|
|
end
|
|
|
|
|
2012-11-02 18:27:51 -04:00
|
|
|
test "Use key_generator when secret_key_base is set" do
|
2012-10-30 23:06:46 -04:00
|
|
|
make_basic_app do |app|
|
2013-12-10 10:04:07 -05:00
|
|
|
app.secrets.secret_key_base = 'b3c631c314c0bbca50c1b2843150fe33'
|
2012-10-30 23:06:46 -04:00
|
|
|
app.config.session_store :disabled
|
|
|
|
end
|
|
|
|
|
|
|
|
class ::OmgController < ActionController::Base
|
|
|
|
def index
|
|
|
|
cookies.signed[:some_key] = "some_value"
|
|
|
|
render text: cookies[:some_key]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
get "/"
|
|
|
|
|
|
|
|
secret = app.key_generator.generate_key('signed cookie')
|
|
|
|
verifier = ActiveSupport::MessageVerifier.new(secret)
|
|
|
|
assert_equal 'some_value', verifier.verify(last_response.body)
|
|
|
|
end
|
|
|
|
|
2013-11-19 19:26:52 -05:00
|
|
|
test "application verifier can be used in the entire application" do
|
|
|
|
make_basic_app do |app|
|
2013-12-10 10:04:07 -05:00
|
|
|
app.secrets.secret_key_base = 'b3c631c314c0bbca50c1b2843150fe33'
|
2013-11-19 19:26:52 -05:00
|
|
|
app.config.session_store :disabled
|
|
|
|
end
|
|
|
|
|
2013-12-19 14:04:07 -05:00
|
|
|
message = app.message_verifier(:sensitive_value).generate("some_value")
|
2013-11-19 19:26:52 -05:00
|
|
|
|
2013-12-19 14:04:07 -05:00
|
|
|
assert_equal 'some_value', Rails.application.message_verifier(:sensitive_value).verify(message)
|
2013-11-19 19:26:52 -05:00
|
|
|
|
2013-12-19 14:04:07 -05:00
|
|
|
secret = app.key_generator.generate_key('sensitive_value')
|
2013-11-19 19:26:52 -05:00
|
|
|
verifier = ActiveSupport::MessageVerifier.new(secret)
|
2013-11-21 21:03:28 -05:00
|
|
|
assert_equal 'some_value', verifier.verify(message)
|
2013-11-19 19:26:52 -05:00
|
|
|
end
|
|
|
|
|
2014-10-27 13:04:37 -04:00
|
|
|
test "application message verifier can be used when the key_generator is ActiveSupport::LegacyKeyGenerator" do
|
|
|
|
app_file 'config/initializers/secret_token.rb', <<-RUBY
|
|
|
|
Rails.application.config.secret_token = "b3c631c314c0bbca50c1b2843150fe33"
|
|
|
|
RUBY
|
|
|
|
app_file 'config/secrets.yml', <<-YAML
|
|
|
|
development:
|
|
|
|
secret_key_base:
|
|
|
|
YAML
|
|
|
|
require "#{app_path}/config/environment"
|
|
|
|
|
|
|
|
|
2014-11-10 18:19:37 -05:00
|
|
|
assert_equal app.env_config['action_dispatch.key_generator'], Rails.application.key_generator
|
2014-10-27 13:04:37 -04:00
|
|
|
assert_equal app.env_config['action_dispatch.key_generator'].class, ActiveSupport::LegacyKeyGenerator
|
|
|
|
message = app.message_verifier(:sensitive_value).generate("some_value")
|
|
|
|
assert_equal 'some_value', Rails.application.message_verifier(:sensitive_value).verify(message)
|
|
|
|
end
|
|
|
|
|
|
|
|
test "warns when secrets.secret_key_base is blank and config.secret_token is set" do
|
|
|
|
app_file 'config/initializers/secret_token.rb', <<-RUBY
|
|
|
|
Rails.application.config.secret_token = "b3c631c314c0bbca50c1b2843150fe33"
|
|
|
|
RUBY
|
|
|
|
app_file 'config/secrets.yml', <<-YAML
|
|
|
|
development:
|
|
|
|
secret_key_base:
|
|
|
|
YAML
|
|
|
|
require "#{app_path}/config/environment"
|
|
|
|
|
|
|
|
assert_deprecated(/You didn't set `secret_key_base`./) do
|
|
|
|
app.env_config
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-11-10 18:18:57 -05:00
|
|
|
test "prefer secrets.secret_token over config.secret_token" do
|
2014-10-27 13:04:37 -04:00
|
|
|
app_file 'config/initializers/secret_token.rb', <<-RUBY
|
|
|
|
Rails.application.config.secret_token = ""
|
|
|
|
RUBY
|
|
|
|
app_file 'config/secrets.yml', <<-YAML
|
|
|
|
development:
|
|
|
|
secret_token: 3b7cd727ee24e8444053437c36cc66c3
|
|
|
|
YAML
|
|
|
|
require "#{app_path}/config/environment"
|
|
|
|
|
|
|
|
assert_equal '3b7cd727ee24e8444053437c36cc66c3', app.secrets.secret_token
|
|
|
|
end
|
|
|
|
|
2013-11-21 20:42:10 -05:00
|
|
|
test "application verifier can build different verifiers" do
|
|
|
|
make_basic_app do |app|
|
2013-12-10 10:04:07 -05:00
|
|
|
app.secrets.secret_key_base = 'b3c631c314c0bbca50c1b2843150fe33'
|
2013-11-21 20:42:10 -05:00
|
|
|
app.config.session_store :disabled
|
|
|
|
end
|
|
|
|
|
2013-12-19 14:04:07 -05:00
|
|
|
default_verifier = app.message_verifier(:sensitive_value)
|
|
|
|
text_verifier = app.message_verifier(:text)
|
2013-12-02 17:16:39 -05:00
|
|
|
|
|
|
|
message = text_verifier.generate('some_value')
|
|
|
|
|
|
|
|
assert_equal 'some_value', text_verifier.verify(message)
|
|
|
|
assert_raises ActiveSupport::MessageVerifier::InvalidSignature do
|
|
|
|
default_verifier.verify(message)
|
|
|
|
end
|
|
|
|
|
2013-12-19 14:04:07 -05:00
|
|
|
assert_equal default_verifier.object_id, app.message_verifier(:sensitive_value).object_id
|
2013-12-02 17:16:39 -05:00
|
|
|
assert_not_equal default_verifier.object_id, text_verifier.object_id
|
2013-11-21 20:42:10 -05:00
|
|
|
end
|
|
|
|
|
2013-12-25 17:34:25 -05:00
|
|
|
test "secrets.secret_key_base is used when config/secrets.yml is present" do
|
2013-12-12 14:58:53 -05:00
|
|
|
app_file 'config/secrets.yml', <<-YAML
|
2013-12-10 10:04:07 -05:00
|
|
|
development:
|
|
|
|
secret_key_base: 3b7cd727ee24e8444053437c36cc66c3
|
|
|
|
YAML
|
|
|
|
|
|
|
|
require "#{app_path}/config/environment"
|
|
|
|
assert_equal '3b7cd727ee24e8444053437c36cc66c3', app.secrets.secret_key_base
|
|
|
|
end
|
|
|
|
|
|
|
|
test "secret_key_base is copied from config to secrets when not set" do
|
2013-12-12 14:58:53 -05:00
|
|
|
remove_file "config/secrets.yml"
|
2013-12-10 10:04:07 -05:00
|
|
|
app_file 'config/initializers/secret_token.rb', <<-RUBY
|
|
|
|
Rails.application.config.secret_key_base = "3b7cd727ee24e8444053437c36cc66c3"
|
|
|
|
RUBY
|
|
|
|
|
|
|
|
require "#{app_path}/config/environment"
|
|
|
|
assert_equal '3b7cd727ee24e8444053437c36cc66c3', app.secrets.secret_key_base
|
|
|
|
end
|
|
|
|
|
2014-10-27 13:04:37 -04:00
|
|
|
test "config.secret_token over-writes a blank secrets.secret_token" do
|
|
|
|
app_file 'config/initializers/secret_token.rb', <<-RUBY
|
2014-11-10 18:18:57 -05:00
|
|
|
Rails.application.config.secret_token = "b3c631c314c0bbca50c1b2843150fe33"
|
2014-10-27 13:04:37 -04:00
|
|
|
RUBY
|
|
|
|
app_file 'config/secrets.yml', <<-YAML
|
|
|
|
development:
|
|
|
|
secret_key_base:
|
|
|
|
secret_token:
|
|
|
|
YAML
|
|
|
|
require "#{app_path}/config/environment"
|
|
|
|
|
|
|
|
assert_equal 'b3c631c314c0bbca50c1b2843150fe33', app.secrets.secret_token
|
|
|
|
assert_equal 'b3c631c314c0bbca50c1b2843150fe33', app.config.secret_token
|
|
|
|
end
|
|
|
|
|
2013-12-25 17:34:25 -05:00
|
|
|
test "custom secrets saved in config/secrets.yml are loaded in app secrets" do
|
2013-12-12 14:58:53 -05:00
|
|
|
app_file 'config/secrets.yml', <<-YAML
|
2013-12-12 11:39:15 -05:00
|
|
|
development:
|
|
|
|
secret_key_base: 3b7cd727ee24e8444053437c36cc66c3
|
|
|
|
aws_access_key_id: myamazonaccesskeyid
|
|
|
|
aws_secret_access_key: myamazonsecretaccesskey
|
|
|
|
YAML
|
|
|
|
|
|
|
|
require "#{app_path}/config/environment"
|
|
|
|
assert_equal 'myamazonaccesskeyid', app.secrets.aws_access_key_id
|
|
|
|
assert_equal 'myamazonsecretaccesskey', app.secrets.aws_secret_access_key
|
|
|
|
end
|
|
|
|
|
2014-02-12 11:17:00 -05:00
|
|
|
test "blank config/secrets.yml does not crash the loading process" do
|
|
|
|
app_file 'config/secrets.yml', <<-YAML
|
|
|
|
YAML
|
|
|
|
require "#{app_path}/config/environment"
|
|
|
|
|
|
|
|
assert_nil app.secrets.not_defined
|
|
|
|
end
|
|
|
|
|
2014-10-27 13:04:37 -04:00
|
|
|
test "config.secret_key_base over-writes a blank secrets.secret_key_base" do
|
|
|
|
app_file 'config/initializers/secret_token.rb', <<-RUBY
|
|
|
|
Rails.application.config.secret_key_base = "iaminallyoursecretkeybase"
|
|
|
|
RUBY
|
|
|
|
app_file 'config/secrets.yml', <<-YAML
|
|
|
|
development:
|
|
|
|
secret_key_base:
|
|
|
|
YAML
|
|
|
|
require "#{app_path}/config/environment"
|
|
|
|
|
|
|
|
assert_equal "iaminallyoursecretkeybase", app.secrets.secret_key_base
|
|
|
|
end
|
|
|
|
|
|
|
|
test "uses ActiveSupport::LegacyKeyGenerator as app.key_generator when secrets.secret_key_base is blank" do
|
|
|
|
app_file 'config/initializers/secret_token.rb', <<-RUBY
|
|
|
|
Rails.application.config.secret_token = "b3c631c314c0bbca50c1b2843150fe33"
|
|
|
|
RUBY
|
|
|
|
app_file 'config/secrets.yml', <<-YAML
|
|
|
|
development:
|
|
|
|
secret_key_base:
|
|
|
|
YAML
|
|
|
|
require "#{app_path}/config/environment"
|
|
|
|
|
|
|
|
assert_equal 'b3c631c314c0bbca50c1b2843150fe33', app.config.secret_token
|
|
|
|
assert_equal nil, app.secrets.secret_key_base
|
|
|
|
assert_equal app.key_generator.class, ActiveSupport::LegacyKeyGenerator
|
|
|
|
end
|
|
|
|
|
|
|
|
test "uses ActiveSupport::LegacyKeyGenerator with config.secret_token as app.key_generator when secrets.secret_key_base is blank" do
|
|
|
|
app_file 'config/initializers/secret_token.rb', <<-RUBY
|
|
|
|
Rails.application.config.secret_token = ""
|
|
|
|
RUBY
|
|
|
|
app_file 'config/secrets.yml', <<-YAML
|
|
|
|
development:
|
|
|
|
secret_key_base:
|
|
|
|
YAML
|
|
|
|
require "#{app_path}/config/environment"
|
|
|
|
|
|
|
|
assert_equal '', app.config.secret_token
|
|
|
|
assert_equal nil, app.secrets.secret_key_base
|
|
|
|
assert_raise ArgumentError, /\AA secret is required/ do
|
|
|
|
app.key_generator
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-03-19 14:09:41 -04:00
|
|
|
test "protect from forgery is the default in a new app" do
|
2010-03-26 19:58:55 -04:00
|
|
|
make_basic_app
|
2010-03-19 14:09:41 -04:00
|
|
|
|
2010-03-26 19:58:55 -04:00
|
|
|
class ::OmgController < ActionController::Base
|
|
|
|
def index
|
2012-10-14 06:03:39 -04:00
|
|
|
render inline: "<%= csrf_meta_tags %>"
|
2010-03-19 14:09:41 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
get "/"
|
|
|
|
assert last_response.body =~ /csrf\-param/
|
|
|
|
end
|
2010-04-13 19:15:18 -04:00
|
|
|
|
2014-12-01 11:23:00 -05:00
|
|
|
test "default form builder specified as a string" do
|
|
|
|
|
|
|
|
app_file 'config/initializers/form_builder.rb', <<-RUBY
|
|
|
|
class CustomFormBuilder < ActionView::Helpers::FormBuilder
|
|
|
|
def text_field(attribute, *args)
|
|
|
|
label(attribute) + super(attribute, *args)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
Rails.configuration.action_view.default_form_builder = "CustomFormBuilder"
|
|
|
|
RUBY
|
|
|
|
|
|
|
|
app_file 'app/models/post.rb', <<-RUBY
|
|
|
|
class Post
|
|
|
|
include ActiveModel::Model
|
|
|
|
attr_accessor :name
|
|
|
|
end
|
|
|
|
RUBY
|
|
|
|
|
|
|
|
|
|
|
|
app_file 'app/controllers/posts_controller.rb', <<-RUBY
|
|
|
|
class PostsController < ApplicationController
|
|
|
|
def index
|
|
|
|
render inline: "<%= begin; form_for(Post.new) {|f| f.text_field(:name)}; rescue => e; e.to_s; end %>"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
RUBY
|
|
|
|
|
|
|
|
add_to_config <<-RUBY
|
|
|
|
routes.prepend do
|
|
|
|
resources :posts
|
|
|
|
end
|
|
|
|
RUBY
|
|
|
|
|
|
|
|
require "#{app_path}/config/environment"
|
|
|
|
|
|
|
|
get "/posts"
|
|
|
|
assert_match(/label/, last_response.body)
|
|
|
|
end
|
|
|
|
|
2011-05-06 17:03:55 -04:00
|
|
|
test "default method for update can be changed" do
|
|
|
|
app_file 'app/models/post.rb', <<-RUBY
|
|
|
|
class Post
|
2014-05-12 20:03:58 -04:00
|
|
|
include ActiveModel::Model
|
2011-05-06 17:03:55 -04:00
|
|
|
def to_key; [1]; end
|
|
|
|
def persisted?; true; end
|
|
|
|
end
|
|
|
|
RUBY
|
|
|
|
|
2014-07-07 21:46:39 -04:00
|
|
|
token = "cf50faa3fe97702ca1ae"
|
|
|
|
|
2011-05-06 17:03:55 -04:00
|
|
|
app_file 'app/controllers/posts_controller.rb', <<-RUBY
|
|
|
|
class PostsController < ApplicationController
|
|
|
|
def show
|
2012-10-14 06:03:39 -04:00
|
|
|
render inline: "<%= begin; form_for(Post.new) {}; rescue => e; e.to_s; end %>"
|
2011-05-06 17:03:55 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def update
|
2012-10-14 06:03:39 -04:00
|
|
|
render text: "update"
|
2011-05-06 17:03:55 -04:00
|
|
|
end
|
2014-07-07 21:46:39 -04:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def form_authenticity_token; token; end # stub the authenticy token
|
2011-05-06 17:03:55 -04:00
|
|
|
end
|
|
|
|
RUBY
|
|
|
|
|
|
|
|
add_to_config <<-RUBY
|
|
|
|
routes.prepend do
|
|
|
|
resources :posts
|
|
|
|
end
|
|
|
|
RUBY
|
|
|
|
|
|
|
|
require "#{app_path}/config/environment"
|
|
|
|
|
2012-10-14 06:03:39 -04:00
|
|
|
params = {authenticity_token: token}
|
2012-03-09 11:33:06 -05:00
|
|
|
|
2011-05-06 17:03:55 -04:00
|
|
|
get "/posts/1"
|
2012-05-30 05:08:56 -04:00
|
|
|
assert_match(/patch/, last_response.body)
|
2011-05-06 17:03:55 -04:00
|
|
|
|
2012-03-09 11:33:06 -05:00
|
|
|
patch "/posts/1", params
|
2012-05-30 05:08:56 -04:00
|
|
|
assert_match(/update/, last_response.body)
|
2011-05-06 17:03:55 -04:00
|
|
|
|
2012-03-09 11:33:06 -05:00
|
|
|
patch "/posts/1", params
|
2012-02-24 23:07:58 -05:00
|
|
|
assert_equal 200, last_response.status
|
|
|
|
|
2012-03-09 11:33:06 -05:00
|
|
|
put "/posts/1", params
|
2012-05-30 05:08:56 -04:00
|
|
|
assert_match(/update/, last_response.body)
|
2012-02-24 23:07:58 -05:00
|
|
|
|
2012-03-09 11:33:06 -05:00
|
|
|
put "/posts/1", params
|
2012-02-24 23:07:58 -05:00
|
|
|
assert_equal 200, last_response.status
|
2011-05-06 17:03:55 -04:00
|
|
|
end
|
|
|
|
|
2011-05-09 19:17:38 -04:00
|
|
|
test "request forgery token param can be changed" do
|
|
|
|
make_basic_app do
|
|
|
|
app.config.action_controller.request_forgery_protection_token = '_xsrf_token_here'
|
|
|
|
end
|
|
|
|
|
|
|
|
class ::OmgController < ActionController::Base
|
|
|
|
def index
|
2012-10-14 06:03:39 -04:00
|
|
|
render inline: "<%= csrf_meta_tags %>"
|
2011-05-09 19:17:38 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
get "/"
|
2014-07-30 18:52:51 -04:00
|
|
|
assert_match "_xsrf_token_here", last_response.body
|
2011-05-09 19:17:38 -04:00
|
|
|
end
|
|
|
|
|
2011-12-23 11:56:49 -05:00
|
|
|
test "sets ActionDispatch.test_app" do
|
|
|
|
make_basic_app
|
|
|
|
assert_equal Rails.application, ActionDispatch.test_app
|
|
|
|
end
|
|
|
|
|
2012-01-17 05:56:50 -05:00
|
|
|
test "sets ActionDispatch::Response.default_charset" do
|
|
|
|
make_basic_app do |app|
|
|
|
|
app.config.action_dispatch.default_charset = "utf-16"
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_equal "utf-16", ActionDispatch::Response.default_charset
|
|
|
|
end
|
|
|
|
|
2011-04-02 04:51:47 -04:00
|
|
|
test "registers interceptors with ActionMailer" do
|
|
|
|
add_to_config <<-RUBY
|
|
|
|
config.action_mailer.interceptors = MyMailInterceptor
|
|
|
|
RUBY
|
|
|
|
|
|
|
|
require "#{app_path}/config/environment"
|
|
|
|
require "mail"
|
|
|
|
|
2011-09-30 04:42:43 -04:00
|
|
|
_ = ActionMailer::Base
|
2011-04-02 04:51:47 -04:00
|
|
|
|
|
|
|
assert_equal [::MyMailInterceptor], ::Mail.send(:class_variable_get, "@@delivery_interceptors")
|
|
|
|
end
|
|
|
|
|
|
|
|
test "registers multiple interceptors with ActionMailer" do
|
|
|
|
add_to_config <<-RUBY
|
|
|
|
config.action_mailer.interceptors = [MyMailInterceptor, "MyOtherMailInterceptor"]
|
|
|
|
RUBY
|
|
|
|
|
|
|
|
require "#{app_path}/config/environment"
|
|
|
|
require "mail"
|
|
|
|
|
2011-09-30 04:42:43 -04:00
|
|
|
_ = ActionMailer::Base
|
2011-04-02 04:51:47 -04:00
|
|
|
|
|
|
|
assert_equal [::MyMailInterceptor, ::MyOtherMailInterceptor], ::Mail.send(:class_variable_get, "@@delivery_interceptors")
|
|
|
|
end
|
|
|
|
|
2014-06-15 08:13:34 -04:00
|
|
|
test "registers preview interceptors with ActionMailer" do
|
|
|
|
add_to_config <<-RUBY
|
|
|
|
config.action_mailer.preview_interceptors = MyPreviewMailInterceptor
|
|
|
|
RUBY
|
|
|
|
|
|
|
|
require "#{app_path}/config/environment"
|
|
|
|
require "mail"
|
|
|
|
|
|
|
|
_ = ActionMailer::Base
|
|
|
|
|
|
|
|
assert_equal [::MyPreviewMailInterceptor], ActionMailer::Base.preview_interceptors
|
|
|
|
end
|
|
|
|
|
|
|
|
test "registers multiple preview interceptors with ActionMailer" do
|
|
|
|
add_to_config <<-RUBY
|
|
|
|
config.action_mailer.preview_interceptors = [MyPreviewMailInterceptor, "MyOtherPreviewMailInterceptor"]
|
|
|
|
RUBY
|
|
|
|
|
|
|
|
require "#{app_path}/config/environment"
|
|
|
|
require "mail"
|
|
|
|
|
|
|
|
_ = ActionMailer::Base
|
|
|
|
|
|
|
|
assert_equal [MyPreviewMailInterceptor, MyOtherPreviewMailInterceptor], ActionMailer::Base.preview_interceptors
|
|
|
|
end
|
|
|
|
|
2011-04-02 04:51:47 -04:00
|
|
|
test "registers observers with ActionMailer" do
|
|
|
|
add_to_config <<-RUBY
|
|
|
|
config.action_mailer.observers = MyMailObserver
|
|
|
|
RUBY
|
|
|
|
|
|
|
|
require "#{app_path}/config/environment"
|
|
|
|
require "mail"
|
|
|
|
|
2011-09-30 04:42:43 -04:00
|
|
|
_ = ActionMailer::Base
|
2011-04-02 04:51:47 -04:00
|
|
|
|
|
|
|
assert_equal [::MyMailObserver], ::Mail.send(:class_variable_get, "@@delivery_notification_observers")
|
|
|
|
end
|
|
|
|
|
|
|
|
test "registers multiple observers with ActionMailer" do
|
|
|
|
add_to_config <<-RUBY
|
|
|
|
config.action_mailer.observers = [MyMailObserver, "MyOtherMailObserver"]
|
|
|
|
RUBY
|
|
|
|
|
|
|
|
require "#{app_path}/config/environment"
|
|
|
|
require "mail"
|
|
|
|
|
2011-09-30 04:42:43 -04:00
|
|
|
_ = ActionMailer::Base
|
2011-04-02 04:51:47 -04:00
|
|
|
|
|
|
|
assert_equal [::MyMailObserver, ::MyOtherMailObserver], ::Mail.send(:class_variable_get, "@@delivery_notification_observers")
|
|
|
|
end
|
|
|
|
|
2011-04-04 18:33:29 -04:00
|
|
|
test "valid timezone is setup correctly" do
|
|
|
|
add_to_config <<-RUBY
|
|
|
|
config.root = "#{app_path}"
|
2014-04-14 17:51:34 -04:00
|
|
|
config.time_zone = "Wellington"
|
2011-04-04 18:33:29 -04:00
|
|
|
RUBY
|
|
|
|
|
|
|
|
require "#{app_path}/config/environment"
|
|
|
|
|
2013-01-22 06:29:20 -05:00
|
|
|
assert_equal "Wellington", Rails.application.config.time_zone
|
2011-04-04 18:33:29 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
test "raises when an invalid timezone is defined in the config" do
|
|
|
|
add_to_config <<-RUBY
|
|
|
|
config.root = "#{app_path}"
|
2014-04-14 17:51:34 -04:00
|
|
|
config.time_zone = "That big hill over yonder hill"
|
2011-04-04 18:33:29 -04:00
|
|
|
RUBY
|
|
|
|
|
|
|
|
assert_raise(ArgumentError) do
|
|
|
|
require "#{app_path}/config/environment"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-09-18 10:18:19 -04:00
|
|
|
test "valid beginning of week is setup correctly" do
|
|
|
|
add_to_config <<-RUBY
|
|
|
|
config.root = "#{app_path}"
|
2014-04-14 17:51:34 -04:00
|
|
|
config.beginning_of_week = :wednesday
|
2012-09-18 10:18:19 -04:00
|
|
|
RUBY
|
|
|
|
|
|
|
|
require "#{app_path}/config/environment"
|
|
|
|
|
|
|
|
assert_equal :wednesday, Rails.application.config.beginning_of_week
|
|
|
|
end
|
|
|
|
|
|
|
|
test "raises when an invalid beginning of week is defined in the config" do
|
|
|
|
add_to_config <<-RUBY
|
|
|
|
config.root = "#{app_path}"
|
2014-04-14 17:51:34 -04:00
|
|
|
config.beginning_of_week = :invalid
|
2012-09-18 10:18:19 -04:00
|
|
|
RUBY
|
|
|
|
|
|
|
|
assert_raise(ArgumentError) do
|
|
|
|
require "#{app_path}/config/environment"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-12-16 15:37:48 -05:00
|
|
|
test "config.action_view.cache_template_loading with cache_classes default" do
|
|
|
|
add_to_config "config.cache_classes = true"
|
|
|
|
require "#{app_path}/config/environment"
|
|
|
|
require 'action_view/base'
|
|
|
|
|
2013-12-05 13:29:59 -05:00
|
|
|
assert_equal true, ActionView::Resolver.caching?
|
2010-12-16 15:37:48 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
test "config.action_view.cache_template_loading without cache_classes default" do
|
|
|
|
add_to_config "config.cache_classes = false"
|
|
|
|
require "#{app_path}/config/environment"
|
|
|
|
require 'action_view/base'
|
|
|
|
|
2013-12-05 13:29:59 -05:00
|
|
|
assert_equal false, ActionView::Resolver.caching?
|
2010-12-16 15:37:48 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
test "config.action_view.cache_template_loading = false" do
|
|
|
|
add_to_config <<-RUBY
|
|
|
|
config.cache_classes = true
|
|
|
|
config.action_view.cache_template_loading = false
|
|
|
|
RUBY
|
|
|
|
require "#{app_path}/config/environment"
|
|
|
|
require 'action_view/base'
|
|
|
|
|
2013-12-05 13:29:59 -05:00
|
|
|
assert_equal false, ActionView::Resolver.caching?
|
2010-12-16 15:37:48 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
test "config.action_view.cache_template_loading = true" do
|
|
|
|
add_to_config <<-RUBY
|
|
|
|
config.cache_classes = false
|
|
|
|
config.action_view.cache_template_loading = true
|
|
|
|
RUBY
|
|
|
|
require "#{app_path}/config/environment"
|
|
|
|
require 'action_view/base'
|
|
|
|
|
2013-12-05 13:29:59 -05:00
|
|
|
assert_equal true, ActionView::Resolver.caching?
|
2010-12-16 15:37:48 -05:00
|
|
|
end
|
2011-02-22 14:25:38 -05:00
|
|
|
|
2013-12-05 13:25:30 -05:00
|
|
|
test "config.action_view.cache_template_loading with cache_classes in an environment" do
|
|
|
|
build_app(initializers: true)
|
|
|
|
add_to_env_config "development", "config.cache_classes = false"
|
|
|
|
|
|
|
|
# These requires are to emulate an engine loading Action View before the application
|
|
|
|
require 'action_view'
|
|
|
|
require 'action_view/railtie'
|
|
|
|
require 'action_view/base'
|
|
|
|
|
|
|
|
require "#{app_path}/config/environment"
|
|
|
|
|
|
|
|
assert_equal false, ActionView::Resolver.caching?
|
|
|
|
end
|
|
|
|
|
2011-02-22 14:25:38 -05:00
|
|
|
test "config.action_dispatch.show_exceptions is sent in env" do
|
|
|
|
make_basic_app do |app|
|
|
|
|
app.config.action_dispatch.show_exceptions = true
|
|
|
|
end
|
|
|
|
|
|
|
|
class ::OmgController < ActionController::Base
|
|
|
|
def index
|
2012-10-14 06:03:39 -04:00
|
|
|
render text: env["action_dispatch.show_exceptions"]
|
2011-02-22 14:25:38 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
get "/"
|
|
|
|
assert_equal 'true', last_response.body
|
|
|
|
end
|
2011-04-28 04:56:11 -04:00
|
|
|
|
|
|
|
test "config.action_controller.wrap_parameters is set in ActionController::Base" do
|
|
|
|
app_file 'config/initializers/wrap_parameters.rb', <<-RUBY
|
2012-10-14 06:03:39 -04:00
|
|
|
ActionController::Base.wrap_parameters format: [:json]
|
2011-04-28 04:56:11 -04:00
|
|
|
RUBY
|
2011-05-06 01:11:06 -04:00
|
|
|
|
|
|
|
app_file 'app/models/post.rb', <<-RUBY
|
|
|
|
class Post
|
2011-05-17 08:32:14 -04:00
|
|
|
def self.attribute_names
|
2011-05-06 01:11:06 -04:00
|
|
|
%w(title)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
RUBY
|
|
|
|
|
2012-03-09 11:33:06 -05:00
|
|
|
app_file 'app/controllers/application_controller.rb', <<-RUBY
|
|
|
|
class ApplicationController < ActionController::Base
|
2012-06-23 06:42:00 -04:00
|
|
|
protect_from_forgery with: :reset_session # as we are testing API here
|
2012-03-09 11:33:06 -05:00
|
|
|
end
|
|
|
|
RUBY
|
|
|
|
|
2011-05-06 01:11:06 -04:00
|
|
|
app_file 'app/controllers/posts_controller.rb', <<-RUBY
|
|
|
|
class PostsController < ApplicationController
|
2011-10-27 02:16:59 -04:00
|
|
|
def create
|
2012-10-14 06:03:39 -04:00
|
|
|
render text: params[:post].inspect
|
2011-05-06 01:11:06 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
RUBY
|
|
|
|
|
|
|
|
add_to_config <<-RUBY
|
2011-12-21 16:54:39 -05:00
|
|
|
routes.prepend do
|
2011-05-06 01:11:06 -04:00
|
|
|
resources :posts
|
|
|
|
end
|
|
|
|
RUBY
|
|
|
|
|
2011-04-28 04:56:11 -04:00
|
|
|
require "#{app_path}/config/environment"
|
|
|
|
|
2011-05-06 01:11:06 -04:00
|
|
|
post "/posts.json", '{ "title": "foo", "name": "bar" }', "CONTENT_TYPE" => "application/json"
|
|
|
|
assert_equal '{"title"=>"foo"}', last_response.body
|
2011-04-28 04:56:11 -04:00
|
|
|
end
|
2011-05-02 17:38:39 -04:00
|
|
|
|
2012-08-30 17:36:59 -04:00
|
|
|
test "config.action_controller.permit_all_parameters = true" do
|
|
|
|
app_file 'app/controllers/posts_controller.rb', <<-RUBY
|
|
|
|
class PostsController < ActionController::Base
|
|
|
|
def create
|
2012-10-14 06:03:39 -04:00
|
|
|
render text: params[:post].permitted? ? "permitted" : "forbidden"
|
2012-08-30 17:36:59 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
RUBY
|
|
|
|
|
|
|
|
add_to_config <<-RUBY
|
|
|
|
routes.prepend do
|
|
|
|
resources :posts
|
|
|
|
end
|
|
|
|
config.action_controller.permit_all_parameters = true
|
|
|
|
RUBY
|
|
|
|
|
|
|
|
require "#{app_path}/config/environment"
|
|
|
|
|
2012-10-14 06:03:39 -04:00
|
|
|
post "/posts", {post: {"title" =>"zomg"}}
|
2012-08-30 17:36:59 -04:00
|
|
|
assert_equal 'permitted', last_response.body
|
|
|
|
end
|
|
|
|
|
2013-01-19 12:32:27 -05:00
|
|
|
test "config.action_controller.action_on_unpermitted_parameters = :raise" do
|
|
|
|
app_file 'app/controllers/posts_controller.rb', <<-RUBY
|
|
|
|
class PostsController < ActionController::Base
|
|
|
|
def create
|
|
|
|
render text: params.require(:post).permit(:name)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
RUBY
|
|
|
|
|
|
|
|
add_to_config <<-RUBY
|
|
|
|
routes.prepend do
|
|
|
|
resources :posts
|
|
|
|
end
|
|
|
|
config.action_controller.action_on_unpermitted_parameters = :raise
|
|
|
|
RUBY
|
|
|
|
|
|
|
|
require "#{app_path}/config/environment"
|
|
|
|
|
|
|
|
assert_equal :raise, ActionController::Parameters.action_on_unpermitted_parameters
|
|
|
|
|
|
|
|
post "/posts", {post: {"title" =>"zomg"}}
|
|
|
|
assert_match "We're sorry, but something went wrong", last_response.body
|
|
|
|
end
|
|
|
|
|
2014-06-27 02:01:30 -04:00
|
|
|
test "config.action_controller.always_permitted_parameters are: controller, action by default" do
|
|
|
|
require "#{app_path}/config/environment"
|
|
|
|
assert_equal %w(controller action), ActionController::Parameters.always_permitted_parameters
|
|
|
|
end
|
|
|
|
|
|
|
|
test "config.action_controller.always_permitted_parameters = ['controller', 'action', 'format']" do
|
|
|
|
add_to_config <<-RUBY
|
|
|
|
config.action_controller.always_permitted_parameters = %w( controller action format )
|
|
|
|
RUBY
|
|
|
|
require "#{app_path}/config/environment"
|
|
|
|
assert_equal %w( controller action format ), ActionController::Parameters.always_permitted_parameters
|
|
|
|
end
|
|
|
|
|
2014-06-27 16:08:40 -04:00
|
|
|
test "config.action_controller.always_permitted_parameters = ['controller','action','format'] does not raise exeception" do
|
|
|
|
app_file 'app/controllers/posts_controller.rb', <<-RUBY
|
|
|
|
class PostsController < ActionController::Base
|
|
|
|
def create
|
|
|
|
render text: params.permit(post: [:title])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
RUBY
|
|
|
|
|
|
|
|
add_to_config <<-RUBY
|
|
|
|
routes.prepend do
|
|
|
|
resources :posts
|
|
|
|
end
|
|
|
|
config.action_controller.always_permitted_parameters = %w( controller action format )
|
|
|
|
config.action_controller.action_on_unpermitted_parameters = :raise
|
|
|
|
RUBY
|
|
|
|
|
|
|
|
require "#{app_path}/config/environment"
|
|
|
|
|
|
|
|
assert_equal :raise, ActionController::Parameters.action_on_unpermitted_parameters
|
|
|
|
|
|
|
|
post "/posts", {post: {"title" =>"zomg"}, format: "json"}
|
|
|
|
assert_equal 200, last_response.status
|
|
|
|
end
|
|
|
|
|
2013-01-19 12:32:27 -05:00
|
|
|
test "config.action_controller.action_on_unpermitted_parameters is :log by default on development" do
|
|
|
|
ENV["RAILS_ENV"] = "development"
|
|
|
|
|
|
|
|
require "#{app_path}/config/environment"
|
|
|
|
|
|
|
|
assert_equal :log, ActionController::Parameters.action_on_unpermitted_parameters
|
|
|
|
end
|
|
|
|
|
2013-03-24 09:35:41 -04:00
|
|
|
test "config.action_controller.action_on_unpermitted_parameters is :log by default on test" do
|
2013-01-19 12:32:27 -05:00
|
|
|
ENV["RAILS_ENV"] = "test"
|
|
|
|
|
|
|
|
require "#{app_path}/config/environment"
|
|
|
|
|
|
|
|
assert_equal :log, ActionController::Parameters.action_on_unpermitted_parameters
|
|
|
|
end
|
|
|
|
|
|
|
|
test "config.action_controller.action_on_unpermitted_parameters is false by default on production" do
|
|
|
|
ENV["RAILS_ENV"] = "production"
|
|
|
|
|
|
|
|
require "#{app_path}/config/environment"
|
|
|
|
|
|
|
|
assert_equal false, ActionController::Parameters.action_on_unpermitted_parameters
|
|
|
|
end
|
|
|
|
|
2011-05-02 17:38:39 -04:00
|
|
|
test "config.action_dispatch.ignore_accept_header" do
|
|
|
|
make_basic_app do |app|
|
|
|
|
app.config.action_dispatch.ignore_accept_header = true
|
|
|
|
end
|
|
|
|
|
|
|
|
class ::OmgController < ActionController::Base
|
|
|
|
def index
|
|
|
|
respond_to do |format|
|
2012-10-14 06:03:39 -04:00
|
|
|
format.html { render text: "HTML" }
|
|
|
|
format.xml { render text: "XML" }
|
2011-05-02 17:38:39 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
get "/", {}, "HTTP_ACCEPT" => "application/xml"
|
|
|
|
assert_equal 'HTML', last_response.body
|
|
|
|
|
2012-10-14 06:03:39 -04:00
|
|
|
get "/", { format: :xml }, "HTTP_ACCEPT" => "application/xml"
|
2011-05-02 17:38:39 -04:00
|
|
|
assert_equal 'XML', last_response.body
|
|
|
|
end
|
2011-07-11 05:05:26 -04:00
|
|
|
|
|
|
|
test "Rails.application#env_config exists and include some existing parameters" do
|
|
|
|
make_basic_app
|
|
|
|
|
|
|
|
assert_respond_to app, :env_config
|
2011-11-28 11:25:37 -05:00
|
|
|
assert_equal app.env_config['action_dispatch.parameter_filter'], app.config.filter_parameters
|
|
|
|
assert_equal app.env_config['action_dispatch.show_exceptions'], app.config.action_dispatch.show_exceptions
|
|
|
|
assert_equal app.env_config['action_dispatch.logger'], Rails.logger
|
|
|
|
assert_equal app.env_config['action_dispatch.backtrace_cleaner'], Rails.backtrace_cleaner
|
2012-09-30 22:34:12 -04:00
|
|
|
assert_equal app.env_config['action_dispatch.key_generator'], Rails.application.key_generator
|
2011-07-11 05:05:26 -04:00
|
|
|
end
|
2012-01-27 09:01:14 -05:00
|
|
|
|
2012-01-27 13:02:33 -05:00
|
|
|
test "config.colorize_logging default is true" do
|
2012-01-27 09:01:14 -05:00
|
|
|
make_basic_app
|
|
|
|
assert app.config.colorize_logging
|
|
|
|
end
|
2012-06-15 13:04:13 -04:00
|
|
|
|
2012-08-24 17:02:27 -04:00
|
|
|
test "config.session_store with :active_record_store with activerecord-session_store gem" do
|
|
|
|
begin
|
|
|
|
make_basic_app do |app|
|
|
|
|
ActionDispatch::Session::ActiveRecordStore = Class.new(ActionDispatch::Session::CookieStore)
|
|
|
|
app.config.session_store :active_record_store
|
|
|
|
end
|
|
|
|
ensure
|
|
|
|
ActionDispatch::Session.send :remove_const, :ActiveRecordStore
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
test "config.session_store with :active_record_store without activerecord-session_store gem" do
|
|
|
|
assert_raise RuntimeError, /activerecord-session_store/ do
|
|
|
|
make_basic_app do |app|
|
|
|
|
app.config.session_store :active_record_store
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2013-07-30 11:25:00 -04:00
|
|
|
|
2014-11-25 06:01:11 -05:00
|
|
|
test "Blank config.log_level is not deprecated for non-production environment" do
|
2014-11-25 05:17:13 -05:00
|
|
|
with_rails_env "development" do
|
2014-11-25 06:01:11 -05:00
|
|
|
assert_not_deprecated do
|
|
|
|
make_basic_app do |app|
|
|
|
|
app.config.log_level = nil
|
|
|
|
end
|
|
|
|
end
|
2014-11-25 05:17:13 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-11-25 06:01:11 -05:00
|
|
|
test "Blank config.log_level is deprecated for the production environment" do
|
2014-11-25 05:17:13 -05:00
|
|
|
with_rails_env "production" do
|
2014-11-25 06:01:11 -05:00
|
|
|
assert_deprecated(/log_level/) do
|
|
|
|
make_basic_app do |app|
|
|
|
|
app.config.log_level = nil
|
|
|
|
end
|
|
|
|
end
|
2014-11-25 05:17:13 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-11-25 06:01:11 -05:00
|
|
|
test "Not blank config.log_level is not deprecated for the production environment" do
|
2014-11-25 05:17:13 -05:00
|
|
|
with_rails_env "production" do
|
2014-11-25 06:01:11 -05:00
|
|
|
assert_not_deprecated do
|
|
|
|
make_basic_app do |app|
|
|
|
|
app.config.log_level = :info
|
|
|
|
end
|
|
|
|
end
|
2014-11-25 05:17:13 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-02-21 09:50:19 -05:00
|
|
|
test "config.log_level with custom logger" do
|
2013-07-30 11:25:00 -04:00
|
|
|
make_basic_app do |app|
|
|
|
|
app.config.logger = Logger.new(STDOUT)
|
|
|
|
app.config.log_level = :info
|
|
|
|
end
|
|
|
|
assert_equal Logger::INFO, Rails.logger.level
|
|
|
|
end
|
2013-08-16 11:22:08 -04:00
|
|
|
|
|
|
|
test "respond_to? accepts include_private" do
|
|
|
|
make_basic_app
|
|
|
|
|
|
|
|
assert_not Rails.configuration.respond_to?(:method_missing)
|
|
|
|
assert Rails.configuration.respond_to?(:method_missing, true)
|
|
|
|
end
|
2014-02-05 02:32:38 -05:00
|
|
|
|
|
|
|
test "config.active_record.dump_schema_after_migration is false on production" do
|
|
|
|
build_app
|
|
|
|
ENV["RAILS_ENV"] = "production"
|
|
|
|
|
|
|
|
require "#{app_path}/config/environment"
|
|
|
|
|
|
|
|
assert_not ActiveRecord::Base.dump_schema_after_migration
|
|
|
|
end
|
|
|
|
|
|
|
|
test "config.active_record.dump_schema_after_migration is true by default on development" do
|
|
|
|
ENV["RAILS_ENV"] = "development"
|
|
|
|
|
|
|
|
require "#{app_path}/config/environment"
|
|
|
|
|
|
|
|
assert ActiveRecord::Base.dump_schema_after_migration
|
|
|
|
end
|
2014-03-16 15:57:21 -04:00
|
|
|
|
|
|
|
test "config.annotations wrapping SourceAnnotationExtractor::Annotation class" do
|
|
|
|
make_basic_app do |app|
|
|
|
|
app.config.annotations.register_extensions("coffee") do |tag|
|
|
|
|
/#\s*(#{tag}):?\s*(.*)$/
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_not_nil SourceAnnotationExtractor::Annotation.extensions[/\.(coffee)$/]
|
|
|
|
end
|
2014-04-14 17:56:59 -04:00
|
|
|
|
|
|
|
test "rake_tasks block works at instance level" do
|
|
|
|
app_file "config/environments/development.rb", <<-RUBY
|
|
|
|
Rails.application.configure do
|
2014-07-30 18:49:20 -04:00
|
|
|
config.ran_block = false
|
|
|
|
|
2014-04-14 17:56:59 -04:00
|
|
|
rake_tasks do
|
2014-07-30 18:49:20 -04:00
|
|
|
config.ran_block = true
|
2014-04-14 17:56:59 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
RUBY
|
|
|
|
|
|
|
|
require "#{app_path}/config/environment"
|
2014-07-30 18:49:20 -04:00
|
|
|
assert_not Rails.configuration.ran_block
|
2014-04-14 17:56:59 -04:00
|
|
|
|
|
|
|
require 'rake'
|
|
|
|
require 'rake/testtask'
|
|
|
|
require 'rdoc/task'
|
|
|
|
|
|
|
|
Rails.application.load_tasks
|
2014-07-30 18:49:20 -04:00
|
|
|
assert Rails.configuration.ran_block
|
2014-04-14 17:56:59 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
test "generators block works at instance level" do
|
|
|
|
app_file "config/environments/development.rb", <<-RUBY
|
|
|
|
Rails.application.configure do
|
2014-07-30 18:49:20 -04:00
|
|
|
config.ran_block = false
|
|
|
|
|
2014-04-14 17:56:59 -04:00
|
|
|
generators do
|
2014-07-30 18:49:20 -04:00
|
|
|
config.ran_block = true
|
2014-04-14 17:56:59 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
RUBY
|
|
|
|
|
|
|
|
require "#{app_path}/config/environment"
|
2014-07-30 18:49:20 -04:00
|
|
|
assert_not Rails.configuration.ran_block
|
2014-04-14 17:56:59 -04:00
|
|
|
|
|
|
|
Rails.application.load_generators
|
2014-07-30 18:49:20 -04:00
|
|
|
assert Rails.configuration.ran_block
|
2014-04-14 17:56:59 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
test "console block works at instance level" do
|
|
|
|
app_file "config/environments/development.rb", <<-RUBY
|
|
|
|
Rails.application.configure do
|
2014-07-30 18:49:20 -04:00
|
|
|
config.ran_block = false
|
|
|
|
|
2014-04-14 17:56:59 -04:00
|
|
|
console do
|
2014-07-30 18:49:20 -04:00
|
|
|
config.ran_block = true
|
2014-04-14 17:56:59 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
RUBY
|
|
|
|
|
|
|
|
require "#{app_path}/config/environment"
|
2014-07-30 18:49:20 -04:00
|
|
|
assert_not Rails.configuration.ran_block
|
2014-04-14 17:56:59 -04:00
|
|
|
|
|
|
|
Rails.application.load_console
|
2014-07-30 18:49:20 -04:00
|
|
|
assert Rails.configuration.ran_block
|
2014-04-14 17:56:59 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
test "runner block works at instance level" do
|
|
|
|
app_file "config/environments/development.rb", <<-RUBY
|
|
|
|
Rails.application.configure do
|
2014-07-30 18:49:20 -04:00
|
|
|
config.ran_block = false
|
|
|
|
|
2014-04-14 17:56:59 -04:00
|
|
|
runner do
|
2014-07-30 18:49:20 -04:00
|
|
|
config.ran_block = true
|
2014-04-14 17:56:59 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
RUBY
|
|
|
|
|
|
|
|
require "#{app_path}/config/environment"
|
2014-07-30 18:49:20 -04:00
|
|
|
assert_not Rails.configuration.ran_block
|
2014-04-14 17:56:59 -04:00
|
|
|
|
|
|
|
Rails.application.load_runner
|
2014-07-30 18:49:20 -04:00
|
|
|
assert Rails.configuration.ran_block
|
2014-04-14 17:56:59 -04:00
|
|
|
end
|
2014-05-07 16:03:23 -04:00
|
|
|
|
|
|
|
test "loading the first existing database configuration available" do
|
|
|
|
app_file 'config/environments/development.rb', <<-RUBY
|
|
|
|
|
|
|
|
Rails.application.configure do
|
|
|
|
config.paths.add 'config/database', with: 'config/nonexistant.yml'
|
|
|
|
config.paths['config/database'] << 'config/database.yml'
|
|
|
|
end
|
|
|
|
RUBY
|
|
|
|
|
|
|
|
require "#{app_path}/config/environment"
|
|
|
|
|
2014-07-30 18:52:51 -04:00
|
|
|
assert_kind_of Hash, Rails.application.config.database_configuration
|
2014-05-07 16:03:23 -04:00
|
|
|
end
|
2014-06-29 10:12:25 -04:00
|
|
|
|
2014-09-10 06:25:01 -04:00
|
|
|
test 'raises with proper error message if no database configuration found' do
|
|
|
|
FileUtils.rm("#{app_path}/config/database.yml")
|
|
|
|
require "#{app_path}/config/environment"
|
|
|
|
err = assert_raises RuntimeError do
|
|
|
|
Rails.application.config.database_configuration
|
|
|
|
end
|
|
|
|
assert_match 'config/database', err.message
|
|
|
|
end
|
|
|
|
|
2014-07-01 12:09:24 -04:00
|
|
|
test 'config.action_mailer.show_previews defaults to true in development' do
|
2014-06-29 10:12:25 -04:00
|
|
|
Rails.env = "development"
|
|
|
|
require "#{app_path}/config/environment"
|
|
|
|
|
2014-07-01 12:09:24 -04:00
|
|
|
assert Rails.application.config.action_mailer.show_previews
|
2014-06-29 10:12:25 -04:00
|
|
|
end
|
|
|
|
|
2014-07-01 12:09:24 -04:00
|
|
|
test 'config.action_mailer.show_previews defaults to false in production' do
|
2014-06-29 10:12:25 -04:00
|
|
|
Rails.env = "production"
|
|
|
|
require "#{app_path}/config/environment"
|
|
|
|
|
2014-07-30 18:52:51 -04:00
|
|
|
assert_equal false, Rails.application.config.action_mailer.show_previews
|
2014-06-29 10:12:25 -04:00
|
|
|
end
|
|
|
|
|
2014-07-01 12:09:24 -04:00
|
|
|
test 'config.action_mailer.show_previews can be set in the configuration file' do
|
2014-06-29 10:12:25 -04:00
|
|
|
Rails.env = "production"
|
|
|
|
add_to_config <<-RUBY
|
2014-07-01 12:09:24 -04:00
|
|
|
config.action_mailer.show_previews = true
|
2014-06-29 10:12:25 -04:00
|
|
|
RUBY
|
|
|
|
require "#{app_path}/config/environment"
|
|
|
|
|
2014-07-30 18:52:51 -04:00
|
|
|
assert_equal true, Rails.application.config.action_mailer.show_previews
|
2014-06-29 10:12:25 -04:00
|
|
|
end
|
2014-07-10 15:40:07 -04:00
|
|
|
|
|
|
|
test "config_for loads custom configuration from yaml files" do
|
|
|
|
app_file 'config/custom.yml', <<-RUBY
|
|
|
|
development:
|
|
|
|
key: 'custom key'
|
|
|
|
RUBY
|
|
|
|
|
|
|
|
add_to_config <<-RUBY
|
|
|
|
config.my_custom_config = config_for('custom')
|
|
|
|
RUBY
|
|
|
|
|
|
|
|
require "#{app_path}/config/environment"
|
|
|
|
|
|
|
|
assert_equal 'custom key', Rails.application.config.my_custom_config['key']
|
|
|
|
end
|
|
|
|
|
|
|
|
test "config_for raises an exception if the file does not exist" do
|
|
|
|
add_to_config <<-RUBY
|
|
|
|
config.my_custom_config = config_for('custom')
|
|
|
|
RUBY
|
|
|
|
|
|
|
|
exception = assert_raises(RuntimeError) do
|
|
|
|
require "#{app_path}/config/environment"
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_equal "Could not load configuration. No such file - #{app_path}/config/custom.yml", exception.message
|
|
|
|
end
|
|
|
|
|
|
|
|
test "config_for without the environment configured returns an empty hash" do
|
|
|
|
app_file 'config/custom.yml', <<-RUBY
|
|
|
|
test:
|
|
|
|
key: 'custom key'
|
|
|
|
RUBY
|
|
|
|
|
|
|
|
add_to_config <<-RUBY
|
|
|
|
config.my_custom_config = config_for('custom')
|
|
|
|
RUBY
|
|
|
|
require "#{app_path}/config/environment"
|
|
|
|
|
|
|
|
assert_equal({}, Rails.application.config.my_custom_config)
|
|
|
|
end
|
|
|
|
|
|
|
|
test "config_for with empty file returns an empty hash" do
|
|
|
|
app_file 'config/custom.yml', <<-RUBY
|
|
|
|
RUBY
|
|
|
|
|
|
|
|
add_to_config <<-RUBY
|
|
|
|
config.my_custom_config = config_for('custom')
|
|
|
|
RUBY
|
|
|
|
require "#{app_path}/config/environment"
|
|
|
|
|
|
|
|
assert_equal({}, Rails.application.config.my_custom_config)
|
|
|
|
end
|
|
|
|
|
|
|
|
test "config_for containing ERB tags should evaluate" do
|
|
|
|
app_file 'config/custom.yml', <<-RUBY
|
|
|
|
development:
|
|
|
|
key: <%= 'custom key' %>
|
|
|
|
RUBY
|
|
|
|
|
|
|
|
add_to_config <<-RUBY
|
|
|
|
config.my_custom_config = config_for('custom')
|
|
|
|
RUBY
|
|
|
|
require "#{app_path}/config/environment"
|
|
|
|
|
|
|
|
assert_equal 'custom key', Rails.application.config.my_custom_config['key']
|
|
|
|
end
|
|
|
|
|
|
|
|
test "config_for with syntax error show a more descritive exception" do
|
|
|
|
app_file 'config/custom.yml', <<-RUBY
|
|
|
|
development:
|
|
|
|
key: foo:
|
|
|
|
RUBY
|
|
|
|
|
|
|
|
add_to_config <<-RUBY
|
|
|
|
config.my_custom_config = config_for('custom')
|
|
|
|
RUBY
|
|
|
|
|
|
|
|
exception = assert_raises(RuntimeError) do
|
|
|
|
require "#{app_path}/config/environment"
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_match 'YAML syntax error occurred while parsing', exception.message
|
|
|
|
end
|
2009-10-08 21:12:28 -04:00
|
|
|
end
|
2009-12-06 20:23:43 -05:00
|
|
|
end
|