annotate test/test_helper.rb @ 669:202986dd17e4 live bibliography_plugin_alpha

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