annotate test/test_helper.rb @ 1516:b450a9d58aed redmine-2.4

Update to Redmine SVN revision 13356 on 2.4-stable branch
author Chris Cannam
date Tue, 09 Sep 2014 09:28:31 +0100
parents e248c7af89ec
children dffacf8a6908
rev   line source
Chris@441 1 # Redmine - project management software
Chris@1494 2 # Copyright (C) 2006-2014 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@1115 18 #require 'shoulda'
Chris@0 19 ENV["RAILS_ENV"] = "test"
Chris@0 20 require File.expand_path(File.dirname(__FILE__) + "/../config/environment")
Chris@1115 21 require 'rails/test_help'
Chris@909 22 require Rails.root.join('test', 'mocks', 'open_id_authentication_mock.rb').to_s
Chris@0 23
Chris@1115 24 require File.expand_path(File.dirname(__FILE__) + '/object_helpers')
Chris@1115 25 include ObjectHelpers
Chris@0 26
Chris@0 27 class ActiveSupport::TestCase
Chris@1115 28 include ActionDispatch::TestProcess
Chris@1464 29
Chris@0 30 self.use_transactional_fixtures = true
Chris@0 31 self.use_instantiated_fixtures = false
Chris@0 32
Chris@0 33 def log_user(login, password)
Chris@0 34 User.anonymous
Chris@0 35 get "/login"
Chris@0 36 assert_equal nil, session[:user_id]
Chris@0 37 assert_response :success
Chris@0 38 assert_template "account/login"
Chris@0 39 post "/login", :username => login, :password => password
Chris@0 40 assert_equal login, User.find(session[:user_id]).login
Chris@0 41 end
Chris@909 42
Chris@0 43 def uploaded_test_file(name, mime)
Chris@1115 44 fixture_file_upload("files/#{name}", mime, true)
Chris@1115 45 end
Chris@1115 46
Chris@1115 47 def credentials(user, password=nil)
Chris@1115 48 {'HTTP_AUTHORIZATION' => ActionController::HttpAuthentication::Basic.encode_credentials(user, password || user)}
Chris@0 49 end
Chris@0 50
Chris@0 51 # Mock out a file
Chris@14 52 def self.mock_file
Chris@0 53 file = 'a_file.png'
Chris@0 54 file.stubs(:size).returns(32)
Chris@0 55 file.stubs(:original_filename).returns('a_file.png')
Chris@0 56 file.stubs(:content_type).returns('image/png')
Chris@0 57 file.stubs(:read).returns(false)
Chris@0 58 file
Chris@0 59 end
Chris@14 60
Chris@14 61 def mock_file
Chris@14 62 self.class.mock_file
Chris@14 63 end
Chris@14 64
Chris@909 65 def mock_file_with_options(options={})
Chris@909 66 file = ''
Chris@909 67 file.stubs(:size).returns(32)
Chris@909 68 original_filename = options[:original_filename] || nil
Chris@909 69 file.stubs(:original_filename).returns(original_filename)
Chris@909 70 content_type = options[:content_type] || nil
Chris@909 71 file.stubs(:content_type).returns(content_type)
Chris@909 72 file.stubs(:read).returns(false)
Chris@909 73 file
Chris@909 74 end
Chris@909 75
Chris@0 76 # Use a temporary directory for attachment related tests
Chris@0 77 def set_tmp_attachments_directory
Chris@909 78 Dir.mkdir "#{Rails.root}/tmp/test" unless File.directory?("#{Rails.root}/tmp/test")
Chris@909 79 unless File.directory?("#{Rails.root}/tmp/test/attachments")
Chris@909 80 Dir.mkdir "#{Rails.root}/tmp/test/attachments"
Chris@909 81 end
Chris@909 82 Attachment.storage_path = "#{Rails.root}/tmp/test/attachments"
Chris@0 83 end
Chris@909 84
Chris@1115 85 def set_fixtures_attachments_directory
Chris@1115 86 Attachment.storage_path = "#{Rails.root}/test/fixtures/files"
Chris@1115 87 end
Chris@1115 88
Chris@0 89 def with_settings(options, &block)
Chris@1464 90 saved_settings = options.keys.inject({}) do |h, k|
Chris@1464 91 h[k] = case Setting[k]
Chris@1464 92 when Symbol, false, true, nil
Chris@1464 93 Setting[k]
Chris@1464 94 else
Chris@1464 95 Setting[k].dup
Chris@1464 96 end
Chris@1464 97 h
Chris@1464 98 end
Chris@0 99 options.each {|k, v| Setting[k] = v}
Chris@0 100 yield
Chris@441 101 ensure
Chris@909 102 saved_settings.each {|k, v| Setting[k] = v} if saved_settings
Chris@0 103 end
Chris@0 104
Chris@1115 105 # Yields the block with user as the current user
Chris@1115 106 def with_current_user(user, &block)
Chris@1115 107 saved_user = User.current
Chris@1115 108 User.current = user
Chris@1115 109 yield
Chris@1115 110 ensure
Chris@1115 111 User.current = saved_user
Chris@1115 112 end
Chris@1115 113
Chris@1494 114 def with_locale(locale, &block)
Chris@1494 115 saved_localed = ::I18n.locale
Chris@1494 116 ::I18n.locale = locale
Chris@1494 117 yield
Chris@1494 118 ensure
Chris@1494 119 ::I18n.locale = saved_localed
Chris@1494 120 end
Chris@1494 121
Chris@14 122 def change_user_password(login, new_password)
Chris@1464 123 user = User.where(:login => login).first
Chris@14 124 user.password, user.password_confirmation = new_password, new_password
Chris@14 125 user.save!
Chris@14 126 end
Chris@14 127
Chris@0 128 def self.ldap_configured?
Chris@0 129 @test_ldap = Net::LDAP.new(:host => '127.0.0.1', :port => 389)
Chris@0 130 return @test_ldap.bind
Chris@0 131 rescue Exception => e
Chris@0 132 # LDAP is not listening
Chris@0 133 return nil
Chris@0 134 end
Chris@909 135
Chris@1115 136 def self.convert_installed?
Chris@1115 137 Redmine::Thumbnail.convert_available?
Chris@1115 138 end
Chris@1115 139
Chris@0 140 # Returns the path to the test +vendor+ repository
Chris@0 141 def self.repository_path(vendor)
Chris@909 142 Rails.root.join("tmp/test/#{vendor.downcase}_repository").to_s
Chris@0 143 end
Chris@909 144
Chris@441 145 # Returns the url of the subversion test repository
Chris@441 146 def self.subversion_repository_url
Chris@441 147 path = repository_path('subversion')
Chris@441 148 path = '/' + path unless path.starts_with?('/')
Chris@441 149 "file://#{path}"
Chris@441 150 end
Chris@909 151
Chris@0 152 # Returns true if the +vendor+ test repository is configured
Chris@0 153 def self.repository_configured?(vendor)
Chris@0 154 File.directory?(repository_path(vendor))
Chris@0 155 end
Chris@909 156
Chris@1115 157 def repository_path_hash(arr)
Chris@1115 158 hs = {}
Chris@1115 159 hs[:path] = arr.join("/")
Chris@1115 160 hs[:param] = arr.join("/")
Chris@1115 161 hs
Chris@1115 162 end
Chris@1115 163
Chris@1115 164 def assert_save(object)
Chris@1115 165 saved = object.save
Chris@1115 166 message = "#{object.class} could not be saved"
Chris@1115 167 errors = object.errors.full_messages.map {|m| "- #{m}"}
Chris@1115 168 message << ":\n#{errors.join("\n")}" if errors.any?
Chris@1115 169 assert_equal true, saved, message
Chris@1115 170 end
Chris@1115 171
chris@37 172 def assert_error_tag(options={})
Chris@119 173 assert_tag({:attributes => { :id => 'errorExplanation' }}.merge(options))
chris@37 174 end
Chris@0 175
Chris@1115 176 def assert_include(expected, s, message=nil)
Chris@1115 177 assert s.include?(expected), (message || "\"#{expected}\" not found in \"#{s}\"")
Chris@1115 178 end
Chris@1115 179
Chris@1464 180 def assert_not_include(expected, s, message=nil)
Chris@1464 181 assert !s.include?(expected), (message || "\"#{expected}\" found in \"#{s}\"")
Chris@1115 182 end
Chris@1115 183
Chris@1115 184 def assert_select_in(text, *args, &block)
Chris@1115 185 d = HTML::Document.new(CGI::unescapeHTML(String.new(text))).root
Chris@1115 186 assert_select(d, *args, &block)
Chris@1115 187 end
Chris@1115 188
Chris@1464 189 def assert_mail_body_match(expected, mail, message=nil)
Chris@1115 190 if expected.is_a?(String)
Chris@1464 191 assert_include expected, mail_body(mail), message
Chris@1115 192 else
Chris@1464 193 assert_match expected, mail_body(mail), message
Chris@1115 194 end
Chris@1115 195 end
Chris@1115 196
Chris@1464 197 def assert_mail_body_no_match(expected, mail, message=nil)
Chris@1115 198 if expected.is_a?(String)
Chris@1464 199 assert_not_include expected, mail_body(mail), message
Chris@1115 200 else
Chris@1464 201 assert_no_match expected, mail_body(mail), message
Chris@1115 202 end
Chris@1115 203 end
Chris@1115 204
Chris@1115 205 def mail_body(mail)
Chris@1115 206 mail.parts.first.body.encoded
Chris@909 207 end
Chris@1464 208 end
Chris@909 209
Chris@1464 210 module Redmine
Chris@1464 211 module ApiTest
Chris@1464 212 # Base class for API tests
Chris@1464 213 class Base < ActionDispatch::IntegrationTest
Chris@1464 214 # Test that a request allows the three types of API authentication
Chris@1464 215 #
Chris@1464 216 # * HTTP Basic with username and password
Chris@1464 217 # * HTTP Basic with an api key for the username
Chris@1464 218 # * Key based with the key=X parameter
Chris@1464 219 #
Chris@1464 220 # @param [Symbol] http_method the HTTP method for request (:get, :post, :put, :delete)
Chris@1464 221 # @param [String] url the request url
Chris@1464 222 # @param [optional, Hash] parameters additional request parameters
Chris@1464 223 # @param [optional, Hash] options additional options
Chris@1464 224 # @option options [Symbol] :success_code Successful response code (:success)
Chris@1464 225 # @option options [Symbol] :failure_code Failure response code (:unauthorized)
Chris@1464 226 def self.should_allow_api_authentication(http_method, url, parameters={}, options={})
Chris@1464 227 should_allow_http_basic_auth_with_username_and_password(http_method, url, parameters, options)
Chris@1464 228 should_allow_http_basic_auth_with_key(http_method, url, parameters, options)
Chris@1464 229 should_allow_key_based_auth(http_method, url, parameters, options)
Chris@1464 230 end
Chris@1464 231
Chris@1464 232 # Test that a request allows the username and password for HTTP BASIC
Chris@1464 233 #
Chris@1464 234 # @param [Symbol] http_method the HTTP method for request (:get, :post, :put, :delete)
Chris@1464 235 # @param [String] url the request url
Chris@1464 236 # @param [optional, Hash] parameters additional request parameters
Chris@1464 237 # @param [optional, Hash] options additional options
Chris@1464 238 # @option options [Symbol] :success_code Successful response code (:success)
Chris@1464 239 # @option options [Symbol] :failure_code Failure response code (:unauthorized)
Chris@1464 240 def self.should_allow_http_basic_auth_with_username_and_password(http_method, url, parameters={}, options={})
Chris@1464 241 success_code = options[:success_code] || :success
Chris@1464 242 failure_code = options[:failure_code] || :unauthorized
Chris@1464 243
Chris@1464 244 context "should allow http basic auth using a username and password for #{http_method} #{url}" do
Chris@1464 245 context "with a valid HTTP authentication" do
Chris@1464 246 setup do
Chris@1464 247 @user = User.generate! do |user|
Chris@1464 248 user.admin = true
Chris@1464 249 user.password = 'my_password'
Chris@1464 250 end
Chris@1464 251 send(http_method, url, parameters, credentials(@user.login, 'my_password'))
Chris@1464 252 end
Chris@1464 253
Chris@1464 254 should_respond_with success_code
Chris@1464 255 should_respond_with_content_type_based_on_url(url)
Chris@1464 256 should "login as the user" do
Chris@1464 257 assert_equal @user, User.current
Chris@1464 258 end
Chris@1115 259 end
Chris@1464 260
Chris@1464 261 context "with an invalid HTTP authentication" do
Chris@1464 262 setup do
Chris@1464 263 @user = User.generate!
Chris@1464 264 send(http_method, url, parameters, credentials(@user.login, 'wrong_password'))
Chris@1464 265 end
Chris@1464 266
Chris@1464 267 should_respond_with failure_code
Chris@1464 268 should_respond_with_content_type_based_on_url(url)
Chris@1464 269 should "not login as the user" do
Chris@1464 270 assert_equal User.anonymous, User.current
Chris@1464 271 end
Chris@1464 272 end
Chris@1464 273
Chris@1464 274 context "without credentials" do
Chris@1464 275 setup do
Chris@1464 276 send(http_method, url, parameters)
Chris@1464 277 end
Chris@1464 278
Chris@1464 279 should_respond_with failure_code
Chris@1464 280 should_respond_with_content_type_based_on_url(url)
Chris@1464 281 should "include_www_authenticate_header" do
Chris@1464 282 assert @controller.response.headers.has_key?('WWW-Authenticate')
Chris@1464 283 end
Chris@1464 284 end
chris@37 285 end
chris@37 286 end
Chris@1464 287
Chris@1464 288 # Test that a request allows the API key with HTTP BASIC
Chris@1464 289 #
Chris@1464 290 # @param [Symbol] http_method the HTTP method for request (:get, :post, :put, :delete)
Chris@1464 291 # @param [String] url the request url
Chris@1464 292 # @param [optional, Hash] parameters additional request parameters
Chris@1464 293 # @param [optional, Hash] options additional options
Chris@1464 294 # @option options [Symbol] :success_code Successful response code (:success)
Chris@1464 295 # @option options [Symbol] :failure_code Failure response code (:unauthorized)
Chris@1464 296 def self.should_allow_http_basic_auth_with_key(http_method, url, parameters={}, options={})
Chris@1464 297 success_code = options[:success_code] || :success
Chris@1464 298 failure_code = options[:failure_code] || :unauthorized
Chris@1464 299
Chris@1464 300 context "should allow http basic auth with a key for #{http_method} #{url}" do
Chris@1464 301 context "with a valid HTTP authentication using the API token" do
Chris@1464 302 setup do
Chris@1464 303 @user = User.generate! do |user|
Chris@1464 304 user.admin = true
Chris@1464 305 end
Chris@1464 306 @token = Token.create!(:user => @user, :action => 'api')
Chris@1464 307 send(http_method, url, parameters, credentials(@token.value, 'X'))
Chris@1464 308 end
Chris@1464 309 should_respond_with success_code
Chris@1464 310 should_respond_with_content_type_based_on_url(url)
Chris@1464 311 should_be_a_valid_response_string_based_on_url(url)
Chris@1464 312 should "login as the user" do
Chris@1464 313 assert_equal @user, User.current
Chris@1464 314 end
Chris@1464 315 end
Chris@1464 316
Chris@1464 317 context "with an invalid HTTP authentication" do
Chris@1464 318 setup do
Chris@1464 319 @user = User.generate!
Chris@1464 320 @token = Token.create!(:user => @user, :action => 'feeds')
Chris@1464 321 send(http_method, url, parameters, credentials(@token.value, 'X'))
Chris@1464 322 end
Chris@1464 323 should_respond_with failure_code
Chris@1464 324 should_respond_with_content_type_based_on_url(url)
Chris@1464 325 should "not login as the user" do
Chris@1464 326 assert_equal User.anonymous, User.current
Chris@1464 327 end
Chris@1464 328 end
chris@37 329 end
chris@37 330 end
Chris@1464 331
Chris@1464 332 # Test that a request allows full key authentication
Chris@1464 333 #
Chris@1464 334 # @param [Symbol] http_method the HTTP method for request (:get, :post, :put, :delete)
Chris@1464 335 # @param [String] url the request url, without the key=ZXY parameter
Chris@1464 336 # @param [optional, Hash] parameters additional request parameters
Chris@1464 337 # @param [optional, Hash] options additional options
Chris@1464 338 # @option options [Symbol] :success_code Successful response code (:success)
Chris@1464 339 # @option options [Symbol] :failure_code Failure response code (:unauthorized)
Chris@1464 340 def self.should_allow_key_based_auth(http_method, url, parameters={}, options={})
Chris@1464 341 success_code = options[:success_code] || :success
Chris@1464 342 failure_code = options[:failure_code] || :unauthorized
Chris@1464 343
Chris@1464 344 context "should allow key based auth using key=X for #{http_method} #{url}" do
Chris@1464 345 context "with a valid api token" do
Chris@1464 346 setup do
Chris@1464 347 @user = User.generate! do |user|
Chris@1464 348 user.admin = true
Chris@1464 349 end
Chris@1464 350 @token = Token.create!(:user => @user, :action => 'api')
Chris@1464 351 # Simple url parse to add on ?key= or &key=
Chris@1464 352 request_url = if url.match(/\?/)
Chris@1464 353 url + "&key=#{@token.value}"
Chris@1464 354 else
Chris@1464 355 url + "?key=#{@token.value}"
Chris@1464 356 end
Chris@1464 357 send(http_method, request_url, parameters)
Chris@1464 358 end
Chris@1464 359 should_respond_with success_code
Chris@1464 360 should_respond_with_content_type_based_on_url(url)
Chris@1464 361 should_be_a_valid_response_string_based_on_url(url)
Chris@1464 362 should "login as the user" do
Chris@1464 363 assert_equal @user, User.current
Chris@1464 364 end
Chris@1464 365 end
Chris@1464 366
Chris@1464 367 context "with an invalid api token" do
Chris@1464 368 setup do
Chris@1464 369 @user = User.generate! do |user|
Chris@1464 370 user.admin = true
Chris@1464 371 end
Chris@1464 372 @token = Token.create!(:user => @user, :action => 'feeds')
Chris@1464 373 # Simple url parse to add on ?key= or &key=
Chris@1464 374 request_url = if url.match(/\?/)
Chris@1464 375 url + "&key=#{@token.value}"
Chris@1464 376 else
Chris@1464 377 url + "?key=#{@token.value}"
Chris@1464 378 end
Chris@1464 379 send(http_method, request_url, parameters)
Chris@1464 380 end
Chris@1464 381 should_respond_with failure_code
Chris@1464 382 should_respond_with_content_type_based_on_url(url)
Chris@1464 383 should "not login as the user" do
Chris@1464 384 assert_equal User.anonymous, User.current
Chris@1464 385 end
Chris@1464 386 end
chris@37 387 end
Chris@1464 388
Chris@1464 389 context "should allow key based auth using X-Redmine-API-Key header for #{http_method} #{url}" do
Chris@1464 390 setup do
Chris@1464 391 @user = User.generate! do |user|
Chris@1464 392 user.admin = true
Chris@1464 393 end
Chris@1464 394 @token = Token.create!(:user => @user, :action => 'api')
Chris@1464 395 send(http_method, url, parameters, {'X-Redmine-API-Key' => @token.value.to_s})
Chris@1464 396 end
Chris@1464 397 should_respond_with success_code
Chris@1464 398 should_respond_with_content_type_based_on_url(url)
Chris@1464 399 should_be_a_valid_response_string_based_on_url(url)
Chris@1464 400 should "login as the user" do
Chris@1464 401 assert_equal @user, User.current
Chris@1464 402 end
Chris@1464 403 end
Chris@1464 404 end
Chris@1464 405
Chris@1464 406 # Uses should_respond_with_content_type based on what's in the url:
Chris@1464 407 #
Chris@1464 408 # '/project/issues.xml' => should_respond_with_content_type :xml
Chris@1464 409 # '/project/issues.json' => should_respond_with_content_type :json
Chris@1464 410 #
Chris@1464 411 # @param [String] url Request
Chris@1464 412 def self.should_respond_with_content_type_based_on_url(url)
Chris@1464 413 case
Chris@1464 414 when url.match(/xml/i)
Chris@1464 415 should "respond with XML" do
Chris@1464 416 assert_equal 'application/xml', @response.content_type
Chris@1464 417 end
Chris@1464 418 when url.match(/json/i)
Chris@1464 419 should "respond with JSON" do
Chris@1464 420 assert_equal 'application/json', @response.content_type
Chris@1464 421 end
Chris@1464 422 else
Chris@1464 423 raise "Unknown content type for should_respond_with_content_type_based_on_url: #{url}"
Chris@1464 424 end
Chris@1464 425 end
Chris@1464 426
Chris@1464 427 # Uses the url to assert which format the response should be in
Chris@1464 428 #
Chris@1464 429 # '/project/issues.xml' => should_be_a_valid_xml_string
Chris@1464 430 # '/project/issues.json' => should_be_a_valid_json_string
Chris@1464 431 #
Chris@1464 432 # @param [String] url Request
Chris@1464 433 def self.should_be_a_valid_response_string_based_on_url(url)
Chris@1464 434 case
Chris@1464 435 when url.match(/xml/i)
Chris@1464 436 should_be_a_valid_xml_string
Chris@1464 437 when url.match(/json/i)
Chris@1464 438 should_be_a_valid_json_string
Chris@1464 439 else
Chris@1464 440 raise "Unknown content type for should_be_a_valid_response_based_on_url: #{url}"
Chris@1464 441 end
Chris@1464 442 end
Chris@1464 443
Chris@1464 444 # Checks that the response is a valid JSON string
Chris@1464 445 def self.should_be_a_valid_json_string
Chris@1464 446 should "be a valid JSON string (or empty)" do
Chris@1464 447 assert(response.body.blank? || ActiveSupport::JSON.decode(response.body))
Chris@1464 448 end
Chris@1464 449 end
Chris@1464 450
Chris@1464 451 # Checks that the response is a valid XML string
Chris@1464 452 def self.should_be_a_valid_xml_string
Chris@1464 453 should "be a valid XML string" do
Chris@1464 454 assert REXML::Document.new(response.body)
Chris@1464 455 end
Chris@1464 456 end
Chris@1464 457
Chris@1464 458 def self.should_respond_with(status)
Chris@1464 459 should "respond with #{status}" do
Chris@1464 460 assert_response status
chris@37 461 end
chris@37 462 end
chris@37 463 end
chris@37 464 end
Chris@1464 465 end
chris@37 466
Chris@1464 467 # URL helpers do not work with config.threadsafe!
Chris@1464 468 # https://github.com/rspec/rspec-rails/issues/476#issuecomment-4705454
Chris@1464 469 ActionView::TestCase::TestController.instance_eval do
Chris@1464 470 helper Rails.application.routes.url_helpers
Chris@1464 471 end
Chris@1464 472 ActionView::TestCase::TestController.class_eval do
Chris@1464 473 def _routes
Chris@1464 474 Rails.application.routes
Chris@1115 475 end
Chris@0 476 end