annotate test/test_helper.rb @ 1171:b4558bc5837f bug_505

Close obsolete branch bug_505
author Chris Cannam
date Fri, 03 Aug 2012 19:40:23 +0100
parents 5e80956cc792
children bb32da3bea34
rev   line source
Chris@441 1 # Redmine - project management software
Chris@441 2 # Copyright (C) 2006-2011 Jean-Philippe Lang
Chris@0 3 #
Chris@0 4 # This program is free software; you can redistribute it and/or
Chris@0 5 # modify it under the terms of the GNU General Public License
Chris@0 6 # as published by the Free Software Foundation; either version 2
Chris@0 7 # of the License, or (at your option) any later version.
Chris@909 8 #
Chris@0 9 # This program is distributed in the hope that it will be useful,
Chris@0 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@0 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@0 12 # GNU General Public License for more details.
Chris@909 13 #
Chris@0 14 # You should have received a copy of the GNU General Public License
Chris@0 15 # along with this program; if not, write to the Free Software
Chris@0 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Chris@0 17
Chris@0 18 ENV["RAILS_ENV"] = "test"
Chris@0 19 require File.expand_path(File.dirname(__FILE__) + "/../config/environment")
Chris@0 20 require 'test_help'
Chris@0 21 require File.expand_path(File.dirname(__FILE__) + '/helper_testcase')
Chris@909 22 require Rails.root.join('test', 'mocks', 'open_id_authentication_mock.rb').to_s
Chris@0 23
Chris@0 24 require File.expand_path(File.dirname(__FILE__) + '/object_daddy_helpers')
Chris@0 25 include ObjectDaddyHelpers
Chris@0 26
Chris@0 27 class ActiveSupport::TestCase
Chris@0 28 # Transactional fixtures accelerate your tests by wrapping each test method
Chris@0 29 # in a transaction that's rolled back on completion. This ensures that the
Chris@0 30 # test database remains unchanged so your fixtures don't have to be reloaded
Chris@0 31 # between every test method. Fewer database queries means faster tests.
Chris@0 32 #
Chris@0 33 # Read Mike Clark's excellent walkthrough at
Chris@0 34 # http://clarkware.com/cgi/blosxom/2005/10/24#Rails10FastTesting
Chris@0 35 #
Chris@0 36 # Every Active Record database supports transactions except MyISAM tables
Chris@0 37 # in MySQL. Turn off transactional fixtures in this case; however, if you
Chris@0 38 # don't care one way or the other, switching from MyISAM to InnoDB tables
Chris@0 39 # is recommended.
Chris@0 40 self.use_transactional_fixtures = true
Chris@0 41
Chris@0 42 # Instantiated fixtures are slow, but give you @david where otherwise you
Chris@0 43 # would need people(:david). If you don't want to migrate your existing
Chris@0 44 # test cases which use the @david style and don't mind the speed hit (each
Chris@0 45 # instantiated fixtures translates to a database query per test method),
Chris@0 46 # then set this back to true.
Chris@0 47 self.use_instantiated_fixtures = false
Chris@0 48
Chris@0 49 # Add more helper methods to be used by all tests here...
Chris@909 50
Chris@0 51 def log_user(login, password)
Chris@0 52 User.anonymous
Chris@0 53 get "/login"
Chris@0 54 assert_equal nil, session[:user_id]
Chris@0 55 assert_response :success
Chris@0 56 assert_template "account/login"
Chris@0 57 post "/login", :username => login, :password => password
Chris@0 58 assert_equal login, User.find(session[:user_id]).login
Chris@0 59 end
Chris@909 60
Chris@0 61 def uploaded_test_file(name, mime)
Chris@909 62 ActionController::TestUploadedFile.new(
Chris@909 63 ActiveSupport::TestCase.fixture_path + "/files/#{name}", mime, true)
Chris@0 64 end
Chris@0 65
Chris@0 66 # Mock out a file
Chris@14 67 def self.mock_file
Chris@0 68 file = 'a_file.png'
Chris@0 69 file.stubs(:size).returns(32)
Chris@0 70 file.stubs(:original_filename).returns('a_file.png')
Chris@0 71 file.stubs(:content_type).returns('image/png')
Chris@0 72 file.stubs(:read).returns(false)
Chris@0 73 file
Chris@0 74 end
Chris@14 75
Chris@14 76 def mock_file
Chris@14 77 self.class.mock_file
Chris@14 78 end
Chris@14 79
Chris@909 80 def mock_file_with_options(options={})
Chris@909 81 file = ''
Chris@909 82 file.stubs(:size).returns(32)
Chris@909 83 original_filename = options[:original_filename] || nil
Chris@909 84 file.stubs(:original_filename).returns(original_filename)
Chris@909 85 content_type = options[:content_type] || nil
Chris@909 86 file.stubs(:content_type).returns(content_type)
Chris@909 87 file.stubs(:read).returns(false)
Chris@909 88 file
Chris@909 89 end
Chris@909 90
Chris@0 91 # Use a temporary directory for attachment related tests
Chris@0 92 def set_tmp_attachments_directory
Chris@909 93 Dir.mkdir "#{Rails.root}/tmp/test" unless File.directory?("#{Rails.root}/tmp/test")
Chris@909 94 unless File.directory?("#{Rails.root}/tmp/test/attachments")
Chris@909 95 Dir.mkdir "#{Rails.root}/tmp/test/attachments"
Chris@909 96 end
Chris@909 97 Attachment.storage_path = "#{Rails.root}/tmp/test/attachments"
Chris@0 98 end
Chris@909 99
Chris@0 100 def with_settings(options, &block)
Chris@909 101 saved_settings = options.keys.inject({}) {|h, k| h[k] = Setting[k].is_a?(Symbol) ? Setting[k] : Setting[k].dup; h}
Chris@0 102 options.each {|k, v| Setting[k] = v}
Chris@0 103 yield
Chris@441 104 ensure
Chris@909 105 saved_settings.each {|k, v| Setting[k] = v} if saved_settings
Chris@0 106 end
Chris@0 107
Chris@14 108 def change_user_password(login, new_password)
Chris@14 109 user = User.first(:conditions => {:login => login})
Chris@14 110 user.password, user.password_confirmation = new_password, new_password
Chris@14 111 user.save!
Chris@14 112 end
Chris@14 113
Chris@0 114 def self.ldap_configured?
Chris@0 115 @test_ldap = Net::LDAP.new(:host => '127.0.0.1', :port => 389)
Chris@0 116 return @test_ldap.bind
Chris@0 117 rescue Exception => e
Chris@0 118 # LDAP is not listening
Chris@0 119 return nil
Chris@0 120 end
Chris@909 121
Chris@0 122 # Returns the path to the test +vendor+ repository
Chris@0 123 def self.repository_path(vendor)
Chris@909 124 Rails.root.join("tmp/test/#{vendor.downcase}_repository").to_s
Chris@0 125 end
Chris@909 126
Chris@441 127 # Returns the url of the subversion test repository
Chris@441 128 def self.subversion_repository_url
Chris@441 129 path = repository_path('subversion')
Chris@441 130 path = '/' + path unless path.starts_with?('/')
Chris@441 131 "file://#{path}"
Chris@441 132 end
Chris@909 133
Chris@0 134 # Returns true if the +vendor+ test repository is configured
Chris@0 135 def self.repository_configured?(vendor)
Chris@0 136 File.directory?(repository_path(vendor))
Chris@0 137 end
Chris@909 138
chris@37 139 def assert_error_tag(options={})
Chris@119 140 assert_tag({:attributes => { :id => 'errorExplanation' }}.merge(options))
chris@37 141 end
Chris@0 142
Chris@909 143 def assert_include(expected, s)
Chris@909 144 assert s.include?(expected), "\"#{expected}\" not found in \"#{s}\""
Chris@909 145 end
Chris@909 146
Chris@0 147 # Shoulda macros
Chris@0 148 def self.should_render_404
Chris@0 149 should_respond_with :not_found
chris@37 150 should_render_template 'common/error'
Chris@0 151 end
Chris@0 152
Chris@0 153 def self.should_have_before_filter(expected_method, options = {})
Chris@0 154 should_have_filter('before', expected_method, options)
Chris@0 155 end
Chris@0 156
Chris@0 157 def self.should_have_after_filter(expected_method, options = {})
Chris@0 158 should_have_filter('after', expected_method, options)
Chris@0 159 end
Chris@0 160
Chris@0 161 def self.should_have_filter(filter_type, expected_method, options)
Chris@0 162 description = "have #{filter_type}_filter :#{expected_method}"
Chris@0 163 description << " with #{options.inspect}" unless options.empty?
Chris@0 164
Chris@0 165 should description do
Chris@0 166 klass = "action_controller/filters/#{filter_type}_filter".classify.constantize
Chris@0 167 expected = klass.new(:filter, expected_method.to_sym, options)
Chris@0 168 assert_equal 1, @controller.class.filter_chain.select { |filter|
Chris@0 169 filter.method == expected.method && filter.kind == expected.kind &&
Chris@0 170 filter.options == expected.options && filter.class == expected.class
Chris@0 171 }.size
Chris@0 172 end
Chris@0 173 end
Chris@0 174
Chris@0 175 def self.should_show_the_old_and_new_values_for(prop_key, model, &block)
Chris@0 176 context "" do
Chris@0 177 setup do
Chris@0 178 if block_given?
Chris@0 179 instance_eval &block
Chris@0 180 else
Chris@0 181 @old_value = model.generate!
Chris@0 182 @new_value = model.generate!
Chris@0 183 end
Chris@0 184 end
Chris@0 185
Chris@0 186 should "use the new value's name" do
Chris@0 187 @detail = JournalDetail.generate!(:property => 'attr',
Chris@0 188 :old_value => @old_value.id,
Chris@0 189 :value => @new_value.id,
Chris@0 190 :prop_key => prop_key)
Chris@909 191
Chris@0 192 assert_match @new_value.name, show_detail(@detail, true)
Chris@0 193 end
Chris@0 194
Chris@0 195 should "use the old value's name" do
Chris@0 196 @detail = JournalDetail.generate!(:property => 'attr',
Chris@0 197 :old_value => @old_value.id,
Chris@0 198 :value => @new_value.id,
Chris@0 199 :prop_key => prop_key)
Chris@909 200
Chris@0 201 assert_match @old_value.name, show_detail(@detail, true)
Chris@0 202 end
Chris@0 203 end
Chris@0 204 end
Chris@14 205
Chris@14 206 def self.should_create_a_new_user(&block)
Chris@14 207 should "create a new user" do
Chris@14 208 user = instance_eval &block
Chris@14 209 assert user
Chris@14 210 assert_kind_of User, user
Chris@14 211 assert !user.new_record?
Chris@14 212 end
Chris@14 213 end
chris@37 214
chris@37 215 # Test that a request allows the three types of API authentication
chris@37 216 #
chris@37 217 # * HTTP Basic with username and password
chris@37 218 # * HTTP Basic with an api key for the username
chris@37 219 # * Key based with the key=X parameter
chris@37 220 #
chris@37 221 # @param [Symbol] http_method the HTTP method for request (:get, :post, :put, :delete)
chris@37 222 # @param [String] url the request url
chris@37 223 # @param [optional, Hash] parameters additional request parameters
chris@37 224 # @param [optional, Hash] options additional options
chris@37 225 # @option options [Symbol] :success_code Successful response code (:success)
chris@37 226 # @option options [Symbol] :failure_code Failure response code (:unauthorized)
chris@37 227 def self.should_allow_api_authentication(http_method, url, parameters={}, options={})
chris@37 228 should_allow_http_basic_auth_with_username_and_password(http_method, url, parameters, options)
chris@37 229 should_allow_http_basic_auth_with_key(http_method, url, parameters, options)
chris@37 230 should_allow_key_based_auth(http_method, url, parameters, options)
chris@37 231 end
chris@37 232
chris@37 233 # Test that a request allows the username and password for HTTP BASIC
chris@37 234 #
chris@37 235 # @param [Symbol] http_method the HTTP method for request (:get, :post, :put, :delete)
chris@37 236 # @param [String] url the request url
chris@37 237 # @param [optional, Hash] parameters additional request parameters
chris@37 238 # @param [optional, Hash] options additional options
chris@37 239 # @option options [Symbol] :success_code Successful response code (:success)
chris@37 240 # @option options [Symbol] :failure_code Failure response code (:unauthorized)
chris@37 241 def self.should_allow_http_basic_auth_with_username_and_password(http_method, url, parameters={}, options={})
chris@37 242 success_code = options[:success_code] || :success
chris@37 243 failure_code = options[:failure_code] || :unauthorized
Chris@909 244
chris@37 245 context "should allow http basic auth using a username and password for #{http_method} #{url}" do
chris@37 246 context "with a valid HTTP authentication" do
chris@37 247 setup do
chris@37 248 @user = User.generate_with_protected!(:password => 'my_password', :password_confirmation => 'my_password', :admin => true) # Admin so they can access the project
chris@37 249 @authorization = ActionController::HttpAuthentication::Basic.encode_credentials(@user.login, 'my_password')
chris@37 250 send(http_method, url, parameters, {:authorization => @authorization})
chris@37 251 end
Chris@909 252
chris@37 253 should_respond_with success_code
chris@37 254 should_respond_with_content_type_based_on_url(url)
chris@37 255 should "login as the user" do
chris@37 256 assert_equal @user, User.current
chris@37 257 end
chris@37 258 end
chris@37 259
chris@37 260 context "with an invalid HTTP authentication" do
chris@37 261 setup do
chris@37 262 @user = User.generate_with_protected!
chris@37 263 @authorization = ActionController::HttpAuthentication::Basic.encode_credentials(@user.login, 'wrong_password')
chris@37 264 send(http_method, url, parameters, {:authorization => @authorization})
chris@37 265 end
Chris@909 266
chris@37 267 should_respond_with failure_code
chris@37 268 should_respond_with_content_type_based_on_url(url)
chris@37 269 should "not login as the user" do
chris@37 270 assert_equal User.anonymous, User.current
chris@37 271 end
chris@37 272 end
Chris@909 273
chris@37 274 context "without credentials" do
chris@37 275 setup do
chris@37 276 send(http_method, url, parameters, {:authorization => ''})
chris@37 277 end
chris@37 278
chris@37 279 should_respond_with failure_code
chris@37 280 should_respond_with_content_type_based_on_url(url)
chris@37 281 should "include_www_authenticate_header" do
chris@37 282 assert @controller.response.headers.has_key?('WWW-Authenticate')
chris@37 283 end
chris@37 284 end
chris@37 285 end
chris@37 286
chris@37 287 end
chris@37 288
chris@37 289 # Test that a request allows the API key with HTTP BASIC
chris@37 290 #
chris@37 291 # @param [Symbol] http_method the HTTP method for request (:get, :post, :put, :delete)
chris@37 292 # @param [String] url the request url
chris@37 293 # @param [optional, Hash] parameters additional request parameters
chris@37 294 # @param [optional, Hash] options additional options
chris@37 295 # @option options [Symbol] :success_code Successful response code (:success)
chris@37 296 # @option options [Symbol] :failure_code Failure response code (:unauthorized)
chris@37 297 def self.should_allow_http_basic_auth_with_key(http_method, url, parameters={}, options={})
chris@37 298 success_code = options[:success_code] || :success
chris@37 299 failure_code = options[:failure_code] || :unauthorized
chris@37 300
chris@37 301 context "should allow http basic auth with a key for #{http_method} #{url}" do
chris@37 302 context "with a valid HTTP authentication using the API token" do
chris@37 303 setup do
chris@37 304 @user = User.generate_with_protected!(:admin => true)
chris@37 305 @token = Token.generate!(:user => @user, :action => 'api')
chris@37 306 @authorization = ActionController::HttpAuthentication::Basic.encode_credentials(@token.value, 'X')
chris@37 307 send(http_method, url, parameters, {:authorization => @authorization})
chris@37 308 end
Chris@909 309
chris@37 310 should_respond_with success_code
chris@37 311 should_respond_with_content_type_based_on_url(url)
chris@37 312 should_be_a_valid_response_string_based_on_url(url)
chris@37 313 should "login as the user" do
chris@37 314 assert_equal @user, User.current
chris@37 315 end
chris@37 316 end
chris@37 317
chris@37 318 context "with an invalid HTTP authentication" do
chris@37 319 setup do
chris@37 320 @user = User.generate_with_protected!
chris@37 321 @token = Token.generate!(:user => @user, :action => 'feeds')
chris@37 322 @authorization = ActionController::HttpAuthentication::Basic.encode_credentials(@token.value, 'X')
chris@37 323 send(http_method, url, parameters, {:authorization => @authorization})
chris@37 324 end
chris@37 325
chris@37 326 should_respond_with failure_code
chris@37 327 should_respond_with_content_type_based_on_url(url)
chris@37 328 should "not login as the user" do
chris@37 329 assert_equal User.anonymous, User.current
chris@37 330 end
chris@37 331 end
chris@37 332 end
chris@37 333 end
Chris@909 334
chris@37 335 # Test that a request allows full key authentication
chris@37 336 #
chris@37 337 # @param [Symbol] http_method the HTTP method for request (:get, :post, :put, :delete)
chris@37 338 # @param [String] url the request url, without the key=ZXY parameter
chris@37 339 # @param [optional, Hash] parameters additional request parameters
chris@37 340 # @param [optional, Hash] options additional options
chris@37 341 # @option options [Symbol] :success_code Successful response code (:success)
chris@37 342 # @option options [Symbol] :failure_code Failure response code (:unauthorized)
chris@37 343 def self.should_allow_key_based_auth(http_method, url, parameters={}, options={})
chris@37 344 success_code = options[:success_code] || :success
chris@37 345 failure_code = options[:failure_code] || :unauthorized
chris@37 346
chris@37 347 context "should allow key based auth using key=X for #{http_method} #{url}" do
chris@37 348 context "with a valid api token" do
chris@37 349 setup do
chris@37 350 @user = User.generate_with_protected!(:admin => true)
chris@37 351 @token = Token.generate!(:user => @user, :action => 'api')
chris@37 352 # Simple url parse to add on ?key= or &key=
chris@37 353 request_url = if url.match(/\?/)
chris@37 354 url + "&key=#{@token.value}"
chris@37 355 else
chris@37 356 url + "?key=#{@token.value}"
chris@37 357 end
chris@37 358 send(http_method, request_url, parameters)
chris@37 359 end
Chris@909 360
chris@37 361 should_respond_with success_code
chris@37 362 should_respond_with_content_type_based_on_url(url)
chris@37 363 should_be_a_valid_response_string_based_on_url(url)
chris@37 364 should "login as the user" do
chris@37 365 assert_equal @user, User.current
chris@37 366 end
chris@37 367 end
chris@37 368
chris@37 369 context "with an invalid api token" do
chris@37 370 setup do
chris@37 371 @user = User.generate_with_protected!
chris@37 372 @token = Token.generate!(:user => @user, :action => 'feeds')
chris@37 373 # Simple url parse to add on ?key= or &key=
chris@37 374 request_url = if url.match(/\?/)
chris@37 375 url + "&key=#{@token.value}"
chris@37 376 else
chris@37 377 url + "?key=#{@token.value}"
chris@37 378 end
chris@37 379 send(http_method, request_url, parameters)
chris@37 380 end
Chris@909 381
chris@37 382 should_respond_with failure_code
chris@37 383 should_respond_with_content_type_based_on_url(url)
chris@37 384 should "not login as the user" do
chris@37 385 assert_equal User.anonymous, User.current
chris@37 386 end
chris@37 387 end
chris@37 388 end
Chris@909 389
Chris@119 390 context "should allow key based auth using X-Redmine-API-Key header for #{http_method} #{url}" do
Chris@119 391 setup do
Chris@119 392 @user = User.generate_with_protected!(:admin => true)
Chris@119 393 @token = Token.generate!(:user => @user, :action => 'api')
Chris@119 394 send(http_method, url, parameters, {'X-Redmine-API-Key' => @token.value.to_s})
Chris@119 395 end
Chris@909 396
Chris@119 397 should_respond_with success_code
Chris@119 398 should_respond_with_content_type_based_on_url(url)
Chris@119 399 should_be_a_valid_response_string_based_on_url(url)
Chris@119 400 should "login as the user" do
Chris@119 401 assert_equal @user, User.current
Chris@119 402 end
Chris@119 403 end
chris@37 404 end
chris@37 405
chris@37 406 # Uses should_respond_with_content_type based on what's in the url:
chris@37 407 #
chris@37 408 # '/project/issues.xml' => should_respond_with_content_type :xml
chris@37 409 # '/project/issues.json' => should_respond_with_content_type :json
chris@37 410 #
chris@37 411 # @param [String] url Request
chris@37 412 def self.should_respond_with_content_type_based_on_url(url)
chris@37 413 case
chris@37 414 when url.match(/xml/i)
chris@37 415 should_respond_with_content_type :xml
chris@37 416 when url.match(/json/i)
chris@37 417 should_respond_with_content_type :json
chris@37 418 else
chris@37 419 raise "Unknown content type for should_respond_with_content_type_based_on_url: #{url}"
chris@37 420 end
Chris@909 421
chris@37 422 end
chris@37 423
chris@37 424 # Uses the url to assert which format the response should be in
chris@37 425 #
chris@37 426 # '/project/issues.xml' => should_be_a_valid_xml_string
chris@37 427 # '/project/issues.json' => should_be_a_valid_json_string
chris@37 428 #
chris@37 429 # @param [String] url Request
chris@37 430 def self.should_be_a_valid_response_string_based_on_url(url)
chris@37 431 case
chris@37 432 when url.match(/xml/i)
chris@37 433 should_be_a_valid_xml_string
chris@37 434 when url.match(/json/i)
chris@37 435 should_be_a_valid_json_string
chris@37 436 else
chris@37 437 raise "Unknown content type for should_be_a_valid_response_based_on_url: #{url}"
chris@37 438 end
Chris@909 439
chris@37 440 end
Chris@909 441
chris@37 442 # Checks that the response is a valid JSON string
chris@37 443 def self.should_be_a_valid_json_string
chris@37 444 should "be a valid JSON string (or empty)" do
Chris@441 445 assert(response.body.blank? || ActiveSupport::JSON.decode(response.body))
chris@37 446 end
chris@37 447 end
chris@37 448
chris@37 449 # Checks that the response is a valid XML string
chris@37 450 def self.should_be_a_valid_xml_string
chris@37 451 should "be a valid XML string" do
chris@37 452 assert REXML::Document.new(response.body)
chris@37 453 end
chris@37 454 end
Chris@909 455
Chris@0 456 end
chris@37 457
chris@37 458 # Simple module to "namespace" all of the API tests
chris@37 459 module ApiTest
chris@37 460 end