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

module ApplicationTests
4
  class ConfigurationTest < Test::Unit::TestCase
5 6
    include ActiveSupport::Testing::Isolation

7 8 9 10 11 12 13 14
    def new_app
      File.expand_path("#{app_path}/../new_app")
    end

    def copy_app
      FileUtils.cp_r(app_path, new_app)
    end

15 16 17 18
    def app
      @app ||= Rails.application
    end

19 20 21
    def setup
      build_app
      boot_rails
C
Carl Lerche 已提交
22
      FileUtils.rm_rf("#{app_path}/config/environments")
23 24
    end

25 26 27 28
    def teardown
      FileUtils.rm_rf(new_app) if File.directory?(new_app)
    end

29
    test "Rails.application is nil until app is initialized" do
30
      require 'rails'
31
      assert_nil Rails.application
32
      require "#{app_path}/config/environment"
33
      assert_equal AppTemplate::Application.instance, Rails.application
34 35
    end

36
    test "Rails.application responds to all instance methods" do
37
      require "#{app_path}/config/environment"
38 39
      assert_respond_to Rails.application, :routes_reloader
      assert_equal Rails.application.routes_reloader, AppTemplate::Application.routes_reloader
40 41 42 43 44 45
    end

    test "Rails::Application responds to paths" do
      require "#{app_path}/config/environment"
      assert_respond_to AppTemplate::Application, :paths
      assert_equal AppTemplate::Application.paths.app.views.to_a, ["#{app_path}/app/views"]
46 47
    end

48
    test "the application root is set correctly" do
49
      require "#{app_path}/config/environment"
50
      assert_equal Pathname.new(app_path), Rails.application.root
51 52
    end

53 54 55 56 57
    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

58
    test "the application root can be set" do
59
      copy_app
60
      add_to_config <<-RUBY
61
        config.root = '#{new_app}'
62
      RUBY
J
José Valim 已提交
63

64
      use_frameworks []
J
José Valim 已提交
65

66
      require "#{app_path}/config/environment"
67
      assert_equal Pathname.new(new_app), Rails.application.root
68 69 70 71 72
    end

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

73 74 75
      use_frameworks []

      Dir.chdir("#{app_path}") do
76
        require "#{app_path}/config/environment"
77
        assert_equal Pathname.new("#{app_path}"), Rails.application.root
78
      end
79
    end
80

81 82 83 84
    test "Rails.root should be a Pathname" do
      add_to_config <<-RUBY
        config.root = "#{app_path}"
      RUBY
85
      require "#{app_path}/config/environment"
86
      assert_instance_of Pathname, Rails.root
87
    end
88 89 90 91 92 93 94

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

      require "#{app_path}/config/application"
95
      assert AppTemplate::Application.config.allow_concurrency
96 97 98 99 100 101 102 103
    end

    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

104 105
      use_frameworks []

106 107 108 109
      assert_nothing_raised do
        require "#{app_path}/config/application"
      end
    end
110

111
    test "frameworks are not preloaded by default" do
112 113 114 115 116 117 118 119 120 121 122 123 124 125
      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
126

127 128
    test "filter_parameters should be able to set via config.filter_parameters" do
      add_to_config <<-RUBY
129
        config.filter_parameters += [ :foo, 'bar', lambda { |key, value|
130
          value = value.reverse if key =~ /baz/
131
        }]
132
      RUBY
133

134 135 136 137
      assert_nothing_raised do
        require "#{app_path}/config/application"
      end
    end
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156

    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
157

158 159 160 161 162 163 164 165 166 167 168 169 170 171
    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

172 173 174 175 176 177
    test "config.encoding sets the default encoding" do
      add_to_config <<-RUBY
        config.encoding = "utf-8"
      RUBY

      require "#{app_path}/config/application"
178
      assert_utf8
179 180
    end

181 182 183 184 185 186 187 188 189
    test "config.paths.public sets Rails.public_path" do
      add_to_config <<-RUBY
        config.paths.public = "somewhere"
      RUBY

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

190 191
    test "config.secret_token is sent in env" do
      make_basic_app do |app|
192
        app.config.secret_token = 'b3c631c314c0bbca50c1b2843150fe33'
193
        app.config.session_store :disabled
194 195 196 197 198 199 200 201 202 203
      end

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

      get "/"
204
      assert_equal 'b3c631c314c0bbca50c1b2843150fe33', last_response.body
205 206
    end

C
Carlhuda 已提交
207
    test "protect from forgery is the default in a new app" do
208
      make_basic_app
C
Carlhuda 已提交
209

210 211
      class ::OmgController < ActionController::Base
        protect_from_forgery
C
Carlhuda 已提交
212

213 214
        def index
          render :inline => "<%= csrf_meta_tag %>"
C
Carlhuda 已提交
215 216 217 218 219 220
        end
      end

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

    test "config.action_controller.perform_caching = true" do
223 224 225
      make_basic_app do |app|
        app.config.action_controller.perform_caching = true
      end
226 227

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

230 231
        caches_action :index
        def index
S
Santiago Pastorino 已提交
232 233
          @@count += 1
          render :text => @@count
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
        end
      end

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

    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 已提交
249 250
        @@count = 0

251 252
        caches_action :index
        def index
S
Santiago Pastorino 已提交
253 254
          @@count += 1
          render :text => @@count
255 256 257 258 259 260 261 262
        end
      end

      get "/"
      res = last_response.body
      get "/"
      assert_not_equal res, last_response.body
    end
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277

    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
278
  end
279
end