annotate test/test_helper.rb @ 894:4cea72d9bcb4 feature_55

Close obsolete branch feature_55
author Chris Cannam
date Fri, 04 Feb 2011 15:54:00 +0000
parents 94944d00e43c
children af80e5618e9b 8661b858af72
rev   line source
Chris@0 1 # redMine - project management software
Chris@0 2 # Copyright (C) 2006 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@0 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@0 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@0 22 require File.join(RAILS_ROOT,'test', 'mocks', 'open_id_authentication_mock.rb')
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@0 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@0 60
Chris@0 61 def uploaded_test_file(name, mime)
Chris@0 62 ActionController::TestUploadedFile.new(ActiveSupport::TestCase.fixture_path + "/files/#{name}", mime)
Chris@0 63 end
Chris@0 64
Chris@0 65 # Mock out a file
Chris@14 66 def self.mock_file
Chris@0 67 file = 'a_file.png'
Chris@0 68 file.stubs(:size).returns(32)
Chris@0 69 file.stubs(:original_filename).returns('a_file.png')
Chris@0 70 file.stubs(:content_type).returns('image/png')
Chris@0 71 file.stubs(:read).returns(false)
Chris@0 72 file
Chris@0 73 end
Chris@14 74
Chris@14 75 def mock_file
Chris@14 76 self.class.mock_file
Chris@14 77 end
Chris@14 78
Chris@0 79 # Use a temporary directory for attachment related tests
Chris@0 80 def set_tmp_attachments_directory
Chris@0 81 Dir.mkdir "#{RAILS_ROOT}/tmp/test" unless File.directory?("#{RAILS_ROOT}/tmp/test")
Chris@0 82 Dir.mkdir "#{RAILS_ROOT}/tmp/test/attachments" unless File.directory?("#{RAILS_ROOT}/tmp/test/attachments")
Chris@0 83 Attachment.storage_path = "#{RAILS_ROOT}/tmp/test/attachments"
Chris@0 84 end
Chris@0 85
Chris@0 86 def with_settings(options, &block)
Chris@0 87 saved_settings = options.keys.inject({}) {|h, k| h[k] = Setting[k].dup; h}
Chris@0 88 options.each {|k, v| Setting[k] = v}
Chris@0 89 yield
Chris@0 90 saved_settings.each {|k, v| Setting[k] = v}
Chris@0 91 end
Chris@0 92
Chris@14 93 def change_user_password(login, new_password)
Chris@14 94 user = User.first(:conditions => {:login => login})
Chris@14 95 user.password, user.password_confirmation = new_password, new_password
Chris@14 96 user.save!
Chris@14 97 end
Chris@14 98
Chris@0 99 def self.ldap_configured?
Chris@0 100 @test_ldap = Net::LDAP.new(:host => '127.0.0.1', :port => 389)
Chris@0 101 return @test_ldap.bind
Chris@0 102 rescue Exception => e
Chris@0 103 # LDAP is not listening
Chris@0 104 return nil
Chris@0 105 end
Chris@0 106
Chris@0 107 # Returns the path to the test +vendor+ repository
Chris@0 108 def self.repository_path(vendor)
Chris@0 109 File.join(RAILS_ROOT.gsub(%r{config\/\.\.}, ''), "/tmp/test/#{vendor.downcase}_repository")
Chris@0 110 end
Chris@0 111
Chris@0 112 # Returns true if the +vendor+ test repository is configured
Chris@0 113 def self.repository_configured?(vendor)
Chris@0 114 File.directory?(repository_path(vendor))
Chris@0 115 end
chris@37 116
chris@37 117 def assert_error_tag(options={})
chris@37 118 assert_tag({:tag => 'p', :attributes => { :id => 'errorExplanation' }}.merge(options))
chris@37 119 end
Chris@0 120
Chris@0 121 # Shoulda macros
Chris@0 122 def self.should_render_404
Chris@0 123 should_respond_with :not_found
chris@37 124 should_render_template 'common/error'
Chris@0 125 end
Chris@0 126
Chris@0 127 def self.should_have_before_filter(expected_method, options = {})
Chris@0 128 should_have_filter('before', expected_method, options)
Chris@0 129 end
Chris@0 130
Chris@0 131 def self.should_have_after_filter(expected_method, options = {})
Chris@0 132 should_have_filter('after', expected_method, options)
Chris@0 133 end
Chris@0 134
Chris@0 135 def self.should_have_filter(filter_type, expected_method, options)
Chris@0 136 description = "have #{filter_type}_filter :#{expected_method}"
Chris@0 137 description << " with #{options.inspect}" unless options.empty?
Chris@0 138
Chris@0 139 should description do
Chris@0 140 klass = "action_controller/filters/#{filter_type}_filter".classify.constantize
Chris@0 141 expected = klass.new(:filter, expected_method.to_sym, options)
Chris@0 142 assert_equal 1, @controller.class.filter_chain.select { |filter|
Chris@0 143 filter.method == expected.method && filter.kind == expected.kind &&
Chris@0 144 filter.options == expected.options && filter.class == expected.class
Chris@0 145 }.size
Chris@0 146 end
Chris@0 147 end
Chris@0 148
Chris@0 149 def self.should_show_the_old_and_new_values_for(prop_key, model, &block)
Chris@0 150 context "" do
Chris@0 151 setup do
Chris@0 152 if block_given?
Chris@0 153 instance_eval &block
Chris@0 154 else
Chris@0 155 @old_value = model.generate!
Chris@0 156 @new_value = model.generate!
Chris@0 157 end
Chris@0 158 end
Chris@0 159
Chris@0 160 should "use the new value's name" do
Chris@0 161 @detail = JournalDetail.generate!(:property => 'attr',
Chris@0 162 :old_value => @old_value.id,
Chris@0 163 :value => @new_value.id,
Chris@0 164 :prop_key => prop_key)
Chris@0 165
Chris@0 166 assert_match @new_value.name, show_detail(@detail, true)
Chris@0 167 end
Chris@0 168
Chris@0 169 should "use the old value's name" do
Chris@0 170 @detail = JournalDetail.generate!(:property => 'attr',
Chris@0 171 :old_value => @old_value.id,
Chris@0 172 :value => @new_value.id,
Chris@0 173 :prop_key => prop_key)
Chris@0 174
Chris@0 175 assert_match @old_value.name, show_detail(@detail, true)
Chris@0 176 end
Chris@0 177 end
Chris@0 178 end
Chris@14 179
Chris@14 180 def self.should_create_a_new_user(&block)
Chris@14 181 should "create a new user" do
Chris@14 182 user = instance_eval &block
Chris@14 183 assert user
Chris@14 184 assert_kind_of User, user
Chris@14 185 assert !user.new_record?
Chris@14 186 end
Chris@14 187 end
chris@37 188
chris@37 189 # Test that a request allows the three types of API authentication
chris@37 190 #
chris@37 191 # * HTTP Basic with username and password
chris@37 192 # * HTTP Basic with an api key for the username
chris@37 193 # * Key based with the key=X parameter
chris@37 194 #
chris@37 195 # @param [Symbol] http_method the HTTP method for request (:get, :post, :put, :delete)
chris@37 196 # @param [String] url the request url
chris@37 197 # @param [optional, Hash] parameters additional request parameters
chris@37 198 # @param [optional, Hash] options additional options
chris@37 199 # @option options [Symbol] :success_code Successful response code (:success)
chris@37 200 # @option options [Symbol] :failure_code Failure response code (:unauthorized)
chris@37 201 def self.should_allow_api_authentication(http_method, url, parameters={}, options={})
chris@37 202 should_allow_http_basic_auth_with_username_and_password(http_method, url, parameters, options)
chris@37 203 should_allow_http_basic_auth_with_key(http_method, url, parameters, options)
chris@37 204 should_allow_key_based_auth(http_method, url, parameters, options)
chris@37 205 end
chris@37 206
chris@37 207 # Test that a request allows the username and password for HTTP BASIC
chris@37 208 #
chris@37 209 # @param [Symbol] http_method the HTTP method for request (:get, :post, :put, :delete)
chris@37 210 # @param [String] url the request url
chris@37 211 # @param [optional, Hash] parameters additional request parameters
chris@37 212 # @param [optional, Hash] options additional options
chris@37 213 # @option options [Symbol] :success_code Successful response code (:success)
chris@37 214 # @option options [Symbol] :failure_code Failure response code (:unauthorized)
chris@37 215 def self.should_allow_http_basic_auth_with_username_and_password(http_method, url, parameters={}, options={})
chris@37 216 success_code = options[:success_code] || :success
chris@37 217 failure_code = options[:failure_code] || :unauthorized
chris@37 218
chris@37 219 context "should allow http basic auth using a username and password for #{http_method} #{url}" do
chris@37 220 context "with a valid HTTP authentication" do
chris@37 221 setup do
chris@37 222 @user = User.generate_with_protected!(:password => 'my_password', :password_confirmation => 'my_password', :admin => true) # Admin so they can access the project
chris@37 223 @authorization = ActionController::HttpAuthentication::Basic.encode_credentials(@user.login, 'my_password')
chris@37 224 send(http_method, url, parameters, {:authorization => @authorization})
chris@37 225 end
chris@37 226
chris@37 227 should_respond_with success_code
chris@37 228 should_respond_with_content_type_based_on_url(url)
chris@37 229 should "login as the user" do
chris@37 230 assert_equal @user, User.current
chris@37 231 end
chris@37 232 end
chris@37 233
chris@37 234 context "with an invalid HTTP authentication" do
chris@37 235 setup do
chris@37 236 @user = User.generate_with_protected!
chris@37 237 @authorization = ActionController::HttpAuthentication::Basic.encode_credentials(@user.login, 'wrong_password')
chris@37 238 send(http_method, url, parameters, {:authorization => @authorization})
chris@37 239 end
chris@37 240
chris@37 241 should_respond_with failure_code
chris@37 242 should_respond_with_content_type_based_on_url(url)
chris@37 243 should "not login as the user" do
chris@37 244 assert_equal User.anonymous, User.current
chris@37 245 end
chris@37 246 end
chris@37 247
chris@37 248 context "without credentials" do
chris@37 249 setup do
chris@37 250 send(http_method, url, parameters, {:authorization => ''})
chris@37 251 end
chris@37 252
chris@37 253 should_respond_with failure_code
chris@37 254 should_respond_with_content_type_based_on_url(url)
chris@37 255 should "include_www_authenticate_header" do
chris@37 256 assert @controller.response.headers.has_key?('WWW-Authenticate')
chris@37 257 end
chris@37 258 end
chris@37 259 end
chris@37 260
chris@37 261 end
chris@37 262
chris@37 263 # Test that a request allows the API key with HTTP BASIC
chris@37 264 #
chris@37 265 # @param [Symbol] http_method the HTTP method for request (:get, :post, :put, :delete)
chris@37 266 # @param [String] url the request url
chris@37 267 # @param [optional, Hash] parameters additional request parameters
chris@37 268 # @param [optional, Hash] options additional options
chris@37 269 # @option options [Symbol] :success_code Successful response code (:success)
chris@37 270 # @option options [Symbol] :failure_code Failure response code (:unauthorized)
chris@37 271 def self.should_allow_http_basic_auth_with_key(http_method, url, parameters={}, options={})
chris@37 272 success_code = options[:success_code] || :success
chris@37 273 failure_code = options[:failure_code] || :unauthorized
chris@37 274
chris@37 275 context "should allow http basic auth with a key for #{http_method} #{url}" do
chris@37 276 context "with a valid HTTP authentication using the API token" do
chris@37 277 setup do
chris@37 278 @user = User.generate_with_protected!(:admin => true)
chris@37 279 @token = Token.generate!(:user => @user, :action => 'api')
chris@37 280 @authorization = ActionController::HttpAuthentication::Basic.encode_credentials(@token.value, 'X')
chris@37 281 send(http_method, url, parameters, {:authorization => @authorization})
chris@37 282 end
chris@37 283
chris@37 284 should_respond_with success_code
chris@37 285 should_respond_with_content_type_based_on_url(url)
chris@37 286 should_be_a_valid_response_string_based_on_url(url)
chris@37 287 should "login as the user" do
chris@37 288 assert_equal @user, User.current
chris@37 289 end
chris@37 290 end
chris@37 291
chris@37 292 context "with an invalid HTTP authentication" do
chris@37 293 setup do
chris@37 294 @user = User.generate_with_protected!
chris@37 295 @token = Token.generate!(:user => @user, :action => 'feeds')
chris@37 296 @authorization = ActionController::HttpAuthentication::Basic.encode_credentials(@token.value, 'X')
chris@37 297 send(http_method, url, parameters, {:authorization => @authorization})
chris@37 298 end
chris@37 299
chris@37 300 should_respond_with failure_code
chris@37 301 should_respond_with_content_type_based_on_url(url)
chris@37 302 should "not login as the user" do
chris@37 303 assert_equal User.anonymous, User.current
chris@37 304 end
chris@37 305 end
chris@37 306 end
chris@37 307 end
chris@37 308
chris@37 309 # Test that a request allows full key authentication
chris@37 310 #
chris@37 311 # @param [Symbol] http_method the HTTP method for request (:get, :post, :put, :delete)
chris@37 312 # @param [String] url the request url, without the key=ZXY parameter
chris@37 313 # @param [optional, Hash] parameters additional request parameters
chris@37 314 # @param [optional, Hash] options additional options
chris@37 315 # @option options [Symbol] :success_code Successful response code (:success)
chris@37 316 # @option options [Symbol] :failure_code Failure response code (:unauthorized)
chris@37 317 def self.should_allow_key_based_auth(http_method, url, parameters={}, options={})
chris@37 318 success_code = options[:success_code] || :success
chris@37 319 failure_code = options[:failure_code] || :unauthorized
chris@37 320
chris@37 321 context "should allow key based auth using key=X for #{http_method} #{url}" do
chris@37 322 context "with a valid api token" do
chris@37 323 setup do
chris@37 324 @user = User.generate_with_protected!(:admin => true)
chris@37 325 @token = Token.generate!(:user => @user, :action => 'api')
chris@37 326 # Simple url parse to add on ?key= or &key=
chris@37 327 request_url = if url.match(/\?/)
chris@37 328 url + "&key=#{@token.value}"
chris@37 329 else
chris@37 330 url + "?key=#{@token.value}"
chris@37 331 end
chris@37 332 send(http_method, request_url, parameters)
chris@37 333 end
chris@37 334
chris@37 335 should_respond_with success_code
chris@37 336 should_respond_with_content_type_based_on_url(url)
chris@37 337 should_be_a_valid_response_string_based_on_url(url)
chris@37 338 should "login as the user" do
chris@37 339 assert_equal @user, User.current
chris@37 340 end
chris@37 341 end
chris@37 342
chris@37 343 context "with an invalid api token" do
chris@37 344 setup do
chris@37 345 @user = User.generate_with_protected!
chris@37 346 @token = Token.generate!(:user => @user, :action => 'feeds')
chris@37 347 # Simple url parse to add on ?key= or &key=
chris@37 348 request_url = if url.match(/\?/)
chris@37 349 url + "&key=#{@token.value}"
chris@37 350 else
chris@37 351 url + "?key=#{@token.value}"
chris@37 352 end
chris@37 353 send(http_method, request_url, parameters)
chris@37 354 end
chris@37 355
chris@37 356 should_respond_with failure_code
chris@37 357 should_respond_with_content_type_based_on_url(url)
chris@37 358 should "not login as the user" do
chris@37 359 assert_equal User.anonymous, User.current
chris@37 360 end
chris@37 361 end
chris@37 362 end
chris@37 363
chris@37 364 end
chris@37 365
chris@37 366 # Uses should_respond_with_content_type based on what's in the url:
chris@37 367 #
chris@37 368 # '/project/issues.xml' => should_respond_with_content_type :xml
chris@37 369 # '/project/issues.json' => should_respond_with_content_type :json
chris@37 370 #
chris@37 371 # @param [String] url Request
chris@37 372 def self.should_respond_with_content_type_based_on_url(url)
chris@37 373 case
chris@37 374 when url.match(/xml/i)
chris@37 375 should_respond_with_content_type :xml
chris@37 376 when url.match(/json/i)
chris@37 377 should_respond_with_content_type :json
chris@37 378 else
chris@37 379 raise "Unknown content type for should_respond_with_content_type_based_on_url: #{url}"
chris@37 380 end
chris@37 381
chris@37 382 end
chris@37 383
chris@37 384 # Uses the url to assert which format the response should be in
chris@37 385 #
chris@37 386 # '/project/issues.xml' => should_be_a_valid_xml_string
chris@37 387 # '/project/issues.json' => should_be_a_valid_json_string
chris@37 388 #
chris@37 389 # @param [String] url Request
chris@37 390 def self.should_be_a_valid_response_string_based_on_url(url)
chris@37 391 case
chris@37 392 when url.match(/xml/i)
chris@37 393 should_be_a_valid_xml_string
chris@37 394 when url.match(/json/i)
chris@37 395 should_be_a_valid_json_string
chris@37 396 else
chris@37 397 raise "Unknown content type for should_be_a_valid_response_based_on_url: #{url}"
chris@37 398 end
chris@37 399
chris@37 400 end
chris@37 401
chris@37 402 # Checks that the response is a valid JSON string
chris@37 403 def self.should_be_a_valid_json_string
chris@37 404 should "be a valid JSON string (or empty)" do
chris@37 405 assert (response.body.blank? || ActiveSupport::JSON.decode(response.body))
chris@37 406 end
chris@37 407 end
chris@37 408
chris@37 409 # Checks that the response is a valid XML string
chris@37 410 def self.should_be_a_valid_xml_string
chris@37 411 should "be a valid XML string" do
chris@37 412 assert REXML::Document.new(response.body)
chris@37 413 end
chris@37 414 end
chris@37 415
Chris@0 416 end
chris@37 417
chris@37 418 # Simple module to "namespace" all of the API tests
chris@37 419 module ApiTest
chris@37 420 end