PageRenderTime 54ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 0ms

/railties/test/application/configuration_test.rb

https://github.com/ghar/rails
Ruby | 529 lines | 412 code | 117 blank | 0 comment | 5 complexity | 731cca59d81818fb565e9fcdfcaa99d3 MD5 | raw file
  1. require "isolation/abstract_unit"
  2. class ::MyMailInterceptor
  3. def self.delivering_email(email); email; end
  4. end
  5. class ::MyOtherMailInterceptor < ::MyMailInterceptor; end
  6. class ::MyMailObserver
  7. def self.delivered_email(email); email; end
  8. end
  9. class ::MyOtherMailObserver < ::MyMailObserver; end
  10. module ApplicationTests
  11. class ConfigurationTest < Test::Unit::TestCase
  12. include ActiveSupport::Testing::Isolation
  13. def new_app
  14. File.expand_path("#{app_path}/../new_app")
  15. end
  16. def copy_app
  17. FileUtils.cp_r(app_path, new_app)
  18. end
  19. def app
  20. @app ||= Rails.application
  21. end
  22. def setup
  23. build_app
  24. boot_rails
  25. FileUtils.rm_rf("#{app_path}/config/environments")
  26. end
  27. def teardown
  28. teardown_app
  29. FileUtils.rm_rf(new_app) if File.directory?(new_app)
  30. end
  31. test "Rails.groups returns available groups" do
  32. require "rails"
  33. Rails.env = "development"
  34. assert_equal [:default, "development"], Rails.groups
  35. assert_equal [:default, "development", :assets], Rails.groups(:assets => [:development])
  36. assert_equal [:default, "development", :another, :assets], Rails.groups(:another, :assets => %w(development))
  37. Rails.env = "test"
  38. assert_equal [:default, "test"], Rails.groups(:assets => [:development])
  39. ENV["RAILS_GROUPS"] = "javascripts,stylesheets"
  40. assert_equal [:default, "test", "javascripts", "stylesheets"], Rails.groups
  41. end
  42. test "Rails.application is nil until app is initialized" do
  43. require 'rails'
  44. assert_nil Rails.application
  45. require "#{app_path}/config/environment"
  46. assert_equal AppTemplate::Application.instance, Rails.application
  47. end
  48. test "Rails.application responds to all instance methods" do
  49. require "#{app_path}/config/environment"
  50. assert_respond_to Rails.application, :routes_reloader
  51. assert_equal Rails.application.routes_reloader, AppTemplate::Application.routes_reloader
  52. end
  53. test "Rails::Application responds to paths" do
  54. require "#{app_path}/config/environment"
  55. assert_respond_to AppTemplate::Application, :paths
  56. assert_equal AppTemplate::Application.paths["app/views"].expanded, ["#{app_path}/app/views"]
  57. end
  58. test "the application root is set correctly" do
  59. require "#{app_path}/config/environment"
  60. assert_equal Pathname.new(app_path), Rails.application.root
  61. end
  62. test "the application root can be seen from the application singleton" do
  63. require "#{app_path}/config/environment"
  64. assert_equal Pathname.new(app_path), AppTemplate::Application.root
  65. end
  66. test "the application root can be set" do
  67. copy_app
  68. add_to_config <<-RUBY
  69. config.root = '#{new_app}'
  70. RUBY
  71. use_frameworks []
  72. require "#{app_path}/config/environment"
  73. assert_equal Pathname.new(new_app), Rails.application.root
  74. end
  75. test "the application root is Dir.pwd if there is no config.ru" do
  76. File.delete("#{app_path}/config.ru")
  77. use_frameworks []
  78. Dir.chdir("#{app_path}") do
  79. require "#{app_path}/config/environment"
  80. assert_equal Pathname.new("#{app_path}"), Rails.application.root
  81. end
  82. end
  83. test "Rails.root should be a Pathname" do
  84. add_to_config <<-RUBY
  85. config.root = "#{app_path}"
  86. RUBY
  87. require "#{app_path}/config/environment"
  88. assert_instance_of Pathname, Rails.root
  89. end
  90. test "marking the application as threadsafe sets the correct config variables" do
  91. add_to_config <<-RUBY
  92. config.threadsafe!
  93. RUBY
  94. require "#{app_path}/config/application"
  95. assert AppTemplate::Application.config.allow_concurrency
  96. end
  97. test "asset_path defaults to nil for application" do
  98. require "#{app_path}/config/environment"
  99. assert_equal nil, AppTemplate::Application.config.asset_path
  100. end
  101. test "the application can be marked as threadsafe when there are no frameworks" do
  102. FileUtils.rm_rf("#{app_path}/config/environments")
  103. add_to_config <<-RUBY
  104. config.threadsafe!
  105. RUBY
  106. use_frameworks []
  107. assert_nothing_raised do
  108. require "#{app_path}/config/application"
  109. end
  110. end
  111. test "frameworks are not preloaded by default" do
  112. require "#{app_path}/config/environment"
  113. assert ActionController.autoload?(:RecordIdentifier)
  114. end
  115. test "frameworks are preloaded with config.preload_frameworks is set" do
  116. add_to_config <<-RUBY
  117. config.preload_frameworks = true
  118. RUBY
  119. require "#{app_path}/config/environment"
  120. assert !ActionController.autoload?(:RecordIdentifier)
  121. end
  122. test "filter_parameters should be able to set via config.filter_parameters" do
  123. add_to_config <<-RUBY
  124. config.filter_parameters += [ :foo, 'bar', lambda { |key, value|
  125. value = value.reverse if key =~ /baz/
  126. }]
  127. RUBY
  128. assert_nothing_raised do
  129. require "#{app_path}/config/application"
  130. end
  131. end
  132. test "config.to_prepare is forwarded to ActionDispatch" do
  133. $prepared = false
  134. add_to_config <<-RUBY
  135. config.to_prepare do
  136. $prepared = true
  137. end
  138. RUBY
  139. assert !$prepared
  140. require "#{app_path}/config/environment"
  141. require 'rack/test'
  142. extend Rack::Test::Methods
  143. get "/"
  144. assert $prepared
  145. end
  146. def assert_utf8
  147. if RUBY_VERSION < '1.9'
  148. assert_equal "UTF8", $KCODE
  149. else
  150. assert_equal Encoding::UTF_8, Encoding.default_external
  151. assert_equal Encoding::UTF_8, Encoding.default_internal
  152. end
  153. end
  154. test "skipping config.encoding still results in 'utf-8' as the default" do
  155. require "#{app_path}/config/application"
  156. assert_utf8
  157. end
  158. test "config.encoding sets the default encoding" do
  159. add_to_config <<-RUBY
  160. config.encoding = "utf-8"
  161. RUBY
  162. require "#{app_path}/config/application"
  163. assert_utf8
  164. end
  165. test "config.paths.public sets Rails.public_path" do
  166. add_to_config <<-RUBY
  167. config.paths["public"] = "somewhere"
  168. RUBY
  169. require "#{app_path}/config/application"
  170. assert_equal File.join(app_path, "somewhere"), Rails.public_path
  171. end
  172. test "config.secret_token is sent in env" do
  173. make_basic_app do |app|
  174. app.config.secret_token = 'b3c631c314c0bbca50c1b2843150fe33'
  175. app.config.session_store :disabled
  176. end
  177. class ::OmgController < ActionController::Base
  178. def index
  179. cookies.signed[:some_key] = "some_value"
  180. render :text => env["action_dispatch.secret_token"]
  181. end
  182. end
  183. get "/"
  184. assert_equal 'b3c631c314c0bbca50c1b2843150fe33', last_response.body
  185. end
  186. test "protect from forgery is the default in a new app" do
  187. make_basic_app
  188. class ::OmgController < ActionController::Base
  189. def index
  190. render :inline => "<%= csrf_meta_tags %>"
  191. end
  192. end
  193. get "/"
  194. assert last_response.body =~ /csrf\-param/
  195. end
  196. test "request forgery token param can be changed" do
  197. make_basic_app do
  198. app.config.action_controller.request_forgery_protection_token = '_xsrf_token_here'
  199. end
  200. class ::OmgController < ActionController::Base
  201. def index
  202. render :inline => "<%= csrf_meta_tags %>"
  203. end
  204. end
  205. get "/"
  206. assert last_response.body =~ /_xsrf_token_here/
  207. end
  208. test "config.action_controller.perform_caching = true" do
  209. make_basic_app do |app|
  210. app.config.action_controller.perform_caching = true
  211. end
  212. class ::OmgController < ActionController::Base
  213. @@count = 0
  214. caches_action :index
  215. def index
  216. @@count += 1
  217. render :text => @@count
  218. end
  219. end
  220. get "/"
  221. res = last_response.body
  222. get "/"
  223. assert_equal res, last_response.body # value should be unchanged
  224. end
  225. test "sets all Active Record models to whitelist all attributes by default" do
  226. add_to_config <<-RUBY
  227. config.active_record.whitelist_attributes = true
  228. RUBY
  229. require "#{app_path}/config/environment"
  230. assert_equal ActiveModel::MassAssignmentSecurity::WhiteList,
  231. ActiveRecord::Base.active_authorizers[:default].class
  232. assert_equal [""], ActiveRecord::Base.active_authorizers[:default].to_a
  233. end
  234. test "registers interceptors with ActionMailer" do
  235. add_to_config <<-RUBY
  236. config.action_mailer.interceptors = MyMailInterceptor
  237. RUBY
  238. require "#{app_path}/config/environment"
  239. require "mail"
  240. ActionMailer::Base
  241. assert_equal [::MyMailInterceptor], ::Mail.send(:class_variable_get, "@@delivery_interceptors")
  242. end
  243. test "registers multiple interceptors with ActionMailer" do
  244. add_to_config <<-RUBY
  245. config.action_mailer.interceptors = [MyMailInterceptor, "MyOtherMailInterceptor"]
  246. RUBY
  247. require "#{app_path}/config/environment"
  248. require "mail"
  249. ActionMailer::Base
  250. assert_equal [::MyMailInterceptor, ::MyOtherMailInterceptor], ::Mail.send(:class_variable_get, "@@delivery_interceptors")
  251. end
  252. test "registers observers with ActionMailer" do
  253. add_to_config <<-RUBY
  254. config.action_mailer.observers = MyMailObserver
  255. RUBY
  256. require "#{app_path}/config/environment"
  257. require "mail"
  258. ActionMailer::Base
  259. assert_equal [::MyMailObserver], ::Mail.send(:class_variable_get, "@@delivery_notification_observers")
  260. end
  261. test "registers multiple observers with ActionMailer" do
  262. add_to_config <<-RUBY
  263. config.action_mailer.observers = [MyMailObserver, "MyOtherMailObserver"]
  264. RUBY
  265. require "#{app_path}/config/environment"
  266. require "mail"
  267. ActionMailer::Base
  268. assert_equal [::MyMailObserver, ::MyOtherMailObserver], ::Mail.send(:class_variable_get, "@@delivery_notification_observers")
  269. end
  270. test "valid timezone is setup correctly" do
  271. add_to_config <<-RUBY
  272. config.root = "#{app_path}"
  273. config.time_zone = "Wellington"
  274. RUBY
  275. require "#{app_path}/config/environment"
  276. assert_equal "Wellington", Rails.application.config.time_zone
  277. end
  278. test "raises when an invalid timezone is defined in the config" do
  279. add_to_config <<-RUBY
  280. config.root = "#{app_path}"
  281. config.time_zone = "That big hill over yonder hill"
  282. RUBY
  283. assert_raise(ArgumentError) do
  284. require "#{app_path}/config/environment"
  285. end
  286. end
  287. test "config.action_controller.perform_caching = false" do
  288. make_basic_app do |app|
  289. app.config.action_controller.perform_caching = false
  290. end
  291. class ::OmgController < ActionController::Base
  292. @@count = 0
  293. caches_action :index
  294. def index
  295. @@count += 1
  296. render :text => @@count
  297. end
  298. end
  299. get "/"
  300. res = last_response.body
  301. get "/"
  302. assert_not_equal res, last_response.body
  303. end
  304. test "config.asset_path is not passed through env" do
  305. make_basic_app do |app|
  306. app.config.asset_path = "/omg%s"
  307. end
  308. class ::OmgController < ActionController::Base
  309. def index
  310. render :inline => "<%= image_path('foo.jpg') %>"
  311. end
  312. end
  313. get "/"
  314. assert_equal "/omg/images/foo.jpg", last_response.body
  315. end
  316. test "config.action_view.cache_template_loading with cache_classes default" do
  317. add_to_config "config.cache_classes = true"
  318. require "#{app_path}/config/environment"
  319. require 'action_view/base'
  320. assert ActionView::Resolver.caching?
  321. end
  322. test "config.action_view.cache_template_loading without cache_classes default" do
  323. add_to_config "config.cache_classes = false"
  324. require "#{app_path}/config/environment"
  325. require 'action_view/base'
  326. assert !ActionView::Resolver.caching?
  327. end
  328. test "config.action_view.cache_template_loading = false" do
  329. add_to_config <<-RUBY
  330. config.cache_classes = true
  331. config.action_view.cache_template_loading = false
  332. RUBY
  333. require "#{app_path}/config/environment"
  334. require 'action_view/base'
  335. assert !ActionView::Resolver.caching?
  336. end
  337. test "config.action_view.cache_template_loading = true" do
  338. add_to_config <<-RUBY
  339. config.cache_classes = false
  340. config.action_view.cache_template_loading = true
  341. RUBY
  342. require "#{app_path}/config/environment"
  343. require 'action_view/base'
  344. assert ActionView::Resolver.caching?
  345. end
  346. test "config.action_dispatch.show_exceptions is sent in env" do
  347. make_basic_app do |app|
  348. app.config.action_dispatch.show_exceptions = true
  349. end
  350. class ::OmgController < ActionController::Base
  351. def index
  352. render :text => env["action_dispatch.show_exceptions"]
  353. end
  354. end
  355. get "/"
  356. assert_equal 'true', last_response.body
  357. end
  358. test "config.action_controller.wrap_parameters is set in ActionController::Base" do
  359. app_file 'config/initializers/wrap_parameters.rb', <<-RUBY
  360. ActionController::Base.wrap_parameters :format => [:json]
  361. RUBY
  362. app_file 'app/models/post.rb', <<-RUBY
  363. class Post
  364. def self.attribute_names
  365. %w(title)
  366. end
  367. end
  368. RUBY
  369. app_file 'app/controllers/posts_controller.rb', <<-RUBY
  370. class PostsController < ApplicationController
  371. def index
  372. render :text => params[:post].inspect
  373. end
  374. end
  375. RUBY
  376. add_to_config <<-RUBY
  377. routes.append do
  378. resources :posts
  379. end
  380. RUBY
  381. require "#{app_path}/config/environment"
  382. require "rack/test"
  383. extend Rack::Test::Methods
  384. post "/posts.json", '{ "title": "foo", "name": "bar" }', "CONTENT_TYPE" => "application/json"
  385. assert_equal '{"title"=>"foo"}', last_response.body
  386. end
  387. test "config.action_dispatch.ignore_accept_header" do
  388. make_basic_app do |app|
  389. app.config.action_dispatch.ignore_accept_header = true
  390. end
  391. class ::OmgController < ActionController::Base
  392. def index
  393. respond_to do |format|
  394. format.html { render :text => "HTML" }
  395. format.xml { render :text => "XML" }
  396. end
  397. end
  398. end
  399. get "/", {}, "HTTP_ACCEPT" => "application/xml"
  400. assert_equal 'HTML', last_response.body
  401. get "/", { :format => :xml }, "HTTP_ACCEPT" => "application/xml"
  402. assert_equal 'XML', last_response.body
  403. end
  404. test "Rails.application#env_config exists and include some existing parameters" do
  405. make_basic_app
  406. assert_respond_to app, :env_config
  407. assert_equal app.env_config['action_dispatch.parameter_filter'], app.config.filter_parameters
  408. assert_equal app.env_config['action_dispatch.secret_token'], app.config.secret_token
  409. assert_equal app.env_config['action_dispatch.show_exceptions'], app.config.action_dispatch.show_exceptions
  410. end
  411. end
  412. end