configuration_test.rb 14.8 KB
Newer Older
1 2
require "isolation/abstract_unit"

3 4 5 6 7 8 9 10 11 12 13 14
class ::MyMailInterceptor
  def self.delivering_email(email); email; end
end

class ::MyOtherMailInterceptor < ::MyMailInterceptor; end

class ::MyMailObserver
  def self.delivered_email(email); email; end
end

class ::MyOtherMailObserver < ::MyMailObserver; end

15
module ApplicationTests
16
  class ConfigurationTest < Test::Unit::TestCase
17 18
    include ActiveSupport::Testing::Isolation

19 20 21 22 23 24 25 26
    def new_app
      File.expand_path("#{app_path}/../new_app")
    end

    def copy_app
      FileUtils.cp_r(app_path, new_app)
    end

27 28 29 30
    def app
      @app ||= Rails.application
    end

31 32 33
    def setup
      build_app
      boot_rails
C
Carl Lerche 已提交
34
      FileUtils.rm_rf("#{app_path}/config/environments")
35
    end
36 37 38

    def teardown
      teardown_app
39 40 41
      FileUtils.rm_rf(new_app) if File.directory?(new_app)
    end

42 43 44 45 46 47
    test "Rails.groups returns available groups" do
      require "rails"

      Rails.env = "development"
      assert_equal [:default, "development"], Rails.groups
      assert_equal [:default, "development", :assets], Rails.groups(:assets => [:development])
J
José Valim 已提交
48
      assert_equal [:default, "development", :another, :assets], Rails.groups(:another, :assets => %w(development))
49 50 51 52 53 54 55 56

      Rails.env = "test"
      assert_equal [:default, "test"], Rails.groups(:assets => [:development])

      ENV["RAILS_GROUPS"] = "javascripts,stylesheets"
      assert_equal [:default, "test", "javascripts", "stylesheets"], Rails.groups
    end

57
    test "Rails.application is nil until app is initialized" do
58
      require 'rails'
59
      assert_nil Rails.application
60
      require "#{app_path}/config/environment"
61
      assert_equal AppTemplate::Application.instance, Rails.application
62 63
    end

64
    test "Rails.application responds to all instance methods" do
65
      require "#{app_path}/config/environment"
66 67
      assert_respond_to Rails.application, :routes_reloader
      assert_equal Rails.application.routes_reloader, AppTemplate::Application.routes_reloader
68 69 70 71 72
    end

    test "Rails::Application responds to paths" do
      require "#{app_path}/config/environment"
      assert_respond_to AppTemplate::Application, :paths
73
      assert_equal AppTemplate::Application.paths["app/views"].expanded, ["#{app_path}/app/views"]
74 75
    end

76
    test "the application root is set correctly" do
77
      require "#{app_path}/config/environment"
78
      assert_equal Pathname.new(app_path), Rails.application.root
79 80
    end

81 82 83 84 85
    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

86
    test "the application root can be set" do
87
      copy_app
88
      add_to_config <<-RUBY
89
        config.root = '#{new_app}'
90
      RUBY
J
José Valim 已提交
91

92
      use_frameworks []
J
José Valim 已提交
93

94
      require "#{app_path}/config/environment"
95
      assert_equal Pathname.new(new_app), Rails.application.root
96 97 98 99 100
    end

    test "the application root is Dir.pwd if there is no config.ru" do
      File.delete("#{app_path}/config.ru")

101 102 103
      use_frameworks []

      Dir.chdir("#{app_path}") do
104
        require "#{app_path}/config/environment"
105
        assert_equal Pathname.new("#{app_path}"), Rails.application.root
106
      end
107
    end
108

109 110 111 112
    test "Rails.root should be a Pathname" do
      add_to_config <<-RUBY
        config.root = "#{app_path}"
      RUBY
113
      require "#{app_path}/config/environment"
114
      assert_instance_of Pathname, Rails.root
115
    end
116 117 118 119 120 121 122

    test "marking the application as threadsafe sets the correct config variables" do
      add_to_config <<-RUBY
        config.threadsafe!
      RUBY

      require "#{app_path}/config/application"
123
      assert AppTemplate::Application.config.allow_concurrency
124 125
    end

126 127 128 129 130
    test "asset_path defaults to nil for application" do
      require "#{app_path}/config/environment"
      assert_equal nil, AppTemplate::Application.config.asset_path
    end

131 132 133 134 135 136
    test "the application can be marked as threadsafe when there are no frameworks" do
      FileUtils.rm_rf("#{app_path}/config/environments")
      add_to_config <<-RUBY
        config.threadsafe!
      RUBY

137 138
      use_frameworks []

139 140 141 142
      assert_nothing_raised do
        require "#{app_path}/config/application"
      end
    end
143

144
    test "frameworks are not preloaded by default" do
145 146 147 148 149 150 151 152 153 154 155 156 157 158
      require "#{app_path}/config/environment"

      assert ActionController.autoload?(:RecordIdentifier)
    end

    test "frameworks are preloaded with config.preload_frameworks is set" do
      add_to_config <<-RUBY
        config.preload_frameworks = true
      RUBY

      require "#{app_path}/config/environment"

      assert !ActionController.autoload?(:RecordIdentifier)
    end
159

160 161
    test "filter_parameters should be able to set via config.filter_parameters" do
      add_to_config <<-RUBY
162
        config.filter_parameters += [ :foo, 'bar', lambda { |key, value|
163
          value = value.reverse if key =~ /baz/
164
        }]
165
      RUBY
166

167 168 169 170
      assert_nothing_raised do
        require "#{app_path}/config/application"
      end
    end
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189

    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"
      require 'rack/test'
      extend Rack::Test::Methods

      get "/"
      assert $prepared
    end
190

191 192 193 194 195 196 197 198 199 200 201 202 203 204
    def assert_utf8
      if RUBY_VERSION < '1.9'
        assert_equal "UTF8", $KCODE
      else
        assert_equal Encoding::UTF_8, Encoding.default_external
        assert_equal Encoding::UTF_8, Encoding.default_internal
      end
    end

    test "skipping config.encoding still results in 'utf-8' as the default" do
      require "#{app_path}/config/application"
      assert_utf8
    end

205 206 207 208 209 210
    test "config.encoding sets the default encoding" do
      add_to_config <<-RUBY
        config.encoding = "utf-8"
      RUBY

      require "#{app_path}/config/application"
211
      assert_utf8
212 213
    end

214 215
    test "config.paths.public sets Rails.public_path" do
      add_to_config <<-RUBY
216
        config.paths["public"] = "somewhere"
217 218 219 220 221 222
      RUBY

      require "#{app_path}/config/application"
      assert_equal File.join(app_path, "somewhere"), Rails.public_path
    end

223 224
    test "config.secret_token is sent in env" do
      make_basic_app do |app|
225
        app.config.secret_token = 'b3c631c314c0bbca50c1b2843150fe33'
226
        app.config.session_store :disabled
227 228 229 230 231 232 233 234 235 236
      end

      class ::OmgController < ActionController::Base
        def index
          cookies.signed[:some_key] = "some_value"
          render :text => env["action_dispatch.secret_token"]
        end
      end

      get "/"
237
      assert_equal 'b3c631c314c0bbca50c1b2843150fe33', last_response.body
238 239
    end

C
Carlhuda 已提交
240
    test "protect from forgery is the default in a new app" do
241
      make_basic_app
C
Carlhuda 已提交
242

243 244
      class ::OmgController < ActionController::Base
        def index
245
          render :inline => "<%= csrf_meta_tags %>"
C
Carlhuda 已提交
246 247 248 249 250 251
        end
      end

      get "/"
      assert last_response.body =~ /csrf\-param/
    end
252

253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
    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
          render :inline => "<%= csrf_meta_tags %>"
        end
      end

      get "/"
      assert last_response.body =~ /_xsrf_token_here/
    end

268
    test "config.action_controller.perform_caching = true" do
269 270 271
      make_basic_app do |app|
        app.config.action_controller.perform_caching = true
      end
272 273

      class ::OmgController < ActionController::Base
S
Santiago Pastorino 已提交
274 275
        @@count = 0

276 277
        caches_action :index
        def index
S
Santiago Pastorino 已提交
278 279
          @@count += 1
          render :text => @@count
280 281 282 283 284 285 286 287 288
        end
      end

      get "/"
      res = last_response.body
      get "/"
      assert_equal res, last_response.body # value should be unchanged
    end

289 290 291 292 293 294 295 296 297 298 299 300
    test "sets all Active Record models to whitelist all attributes by default" do
      add_to_config <<-RUBY
        config.active_record.whitelist_attributes = true
      RUBY

      require "#{app_path}/config/environment"

      assert_equal ActiveModel::MassAssignmentSecurity::WhiteList,
                   ActiveRecord::Base.active_authorizers[:default].class
      assert_equal [""], ActiveRecord::Base.active_authorizers[:default].to_a
    end

301 302 303 304 305 306 307 308
    test "registers interceptors with ActionMailer" do
      add_to_config <<-RUBY
        config.action_mailer.interceptors = MyMailInterceptor
      RUBY

      require "#{app_path}/config/environment"
      require "mail"

309
      _ = ActionMailer::Base
310 311 312 313 314 315 316 317 318 319 320 321

      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"

322
      _ = ActionMailer::Base
323 324 325 326 327 328 329 330 331 332 333 334

      assert_equal [::MyMailInterceptor, ::MyOtherMailInterceptor], ::Mail.send(:class_variable_get, "@@delivery_interceptors")
    end

    test "registers observers with ActionMailer" do
      add_to_config <<-RUBY
        config.action_mailer.observers = MyMailObserver
      RUBY

      require "#{app_path}/config/environment"
      require "mail"

335
      _ = ActionMailer::Base
336 337 338 339 340 341 342 343 344 345 346 347

      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"

348
      _ = ActionMailer::Base
349 350 351 352

      assert_equal [::MyMailObserver, ::MyOtherMailObserver], ::Mail.send(:class_variable_get, "@@delivery_notification_observers")
    end

353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374
    test "valid timezone is setup correctly" do
      add_to_config <<-RUBY
        config.root = "#{app_path}"
          config.time_zone = "Wellington"
      RUBY

      require "#{app_path}/config/environment"

      assert_equal "Wellington", Rails.application.config.time_zone
    end

    test "raises when an invalid timezone is defined in the config" do
      add_to_config <<-RUBY
        config.root = "#{app_path}"
          config.time_zone = "That big hill over yonder hill"
      RUBY

      assert_raise(ArgumentError) do
        require "#{app_path}/config/environment"
      end
    end

375 376 377 378 379 380
    test "config.action_controller.perform_caching = false" do
      make_basic_app do |app|
        app.config.action_controller.perform_caching = false
      end

      class ::OmgController < ActionController::Base
S
Santiago Pastorino 已提交
381 382
        @@count = 0

383 384
        caches_action :index
        def index
S
Santiago Pastorino 已提交
385 386
          @@count += 1
          render :text => @@count
387 388 389 390 391 392 393 394
        end
      end

      get "/"
      res = last_response.body
      get "/"
      assert_not_equal res, last_response.body
    end
395 396 397 398 399 400 401 402 403 404 405 406 407 408 409

    test "config.asset_path is not passed through env" do
      make_basic_app do |app|
        app.config.asset_path = "/omg%s"
      end

      class ::OmgController < ActionController::Base
        def index
          render :inline => "<%= image_path('foo.jpg') %>"
        end
      end

      get "/"
      assert_equal "/omg/images/foo.jpg", last_response.body
    end
410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447

    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'

      assert ActionView::Resolver.caching?
    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'

      assert !ActionView::Resolver.caching?
    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'

      assert !ActionView::Resolver.caching?
    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'

      assert ActionView::Resolver.caching?
    end
448 449 450 451 452 453 454 455 456 457 458 459 460 461 462

    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
          render :text => env["action_dispatch.show_exceptions"]
        end
      end

      get "/"
      assert_equal 'true', last_response.body
    end
463 464 465 466 467

    test "config.action_controller.wrap_parameters is set in ActionController::Base" do
      app_file 'config/initializers/wrap_parameters.rb', <<-RUBY
        ActionController::Base.wrap_parameters :format => [:json]
      RUBY
468 469 470

      app_file 'app/models/post.rb', <<-RUBY
      class Post
J
José Valim 已提交
471
        def self.attribute_names
472 473 474 475 476 477 478
          %w(title)
        end
      end
      RUBY

      app_file 'app/controllers/posts_controller.rb', <<-RUBY
      class PostsController < ApplicationController
479
        def create
480 481 482 483 484 485 486 487 488 489 490
          render :text => params[:post].inspect
        end
      end
      RUBY

      add_to_config <<-RUBY
        routes.append do
          resources :posts
        end
      RUBY

491
      require "#{app_path}/config/environment"
492 493
      require "rack/test"
      extend Rack::Test::Methods
494

495 496
      post "/posts.json", '{ "title": "foo", "name": "bar" }', "CONTENT_TYPE" => "application/json"
      assert_equal '{"title"=>"foo"}', last_response.body
497
    end
498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518

    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|
            format.html { render :text => "HTML" }
            format.xml { render :text => "XML" }
          end
        end
      end

      get "/", {}, "HTTP_ACCEPT" => "application/xml"
      assert_equal 'HTML', last_response.body

      get "/", { :format => :xml }, "HTTP_ACCEPT" => "application/xml"
      assert_equal 'XML', last_response.body
    end
519 520 521 522 523 524 525 526 527

    test "Rails.application#env_config exists and include some existing parameters" do
      make_basic_app

      assert_respond_to app, :env_config
      assert_equal      app.env_config['action_dispatch.parameter_filter'], app.config.filter_parameters
      assert_equal      app.env_config['action_dispatch.secret_token'],     app.config.secret_token
      assert_equal      app.env_config['action_dispatch.show_exceptions'],  app.config.action_dispatch.show_exceptions
    end
528
  end
529
end