annotate test/test_helper.rb @ 1298:4f746d8966dd redmine_2.3_integration

Merge from redmine-2.3 branch to create new branch redmine-2.3-integration
author Chris Cannam
date Fri, 14 Jun 2013 09:28:30 +0100
parents bb32da3bea34 622f24f53b42
children
rev   line source
Chris@441 1 # Redmine - project management software
Chris@1295 2 # Copyright (C) 2006-2013 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@1295 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@1295 90 saved_settings = options.keys.inject({}) do |h, k|
Chris@1295 91 h[k] = case Setting[k]
Chris@1295 92 when Symbol, false, true, nil
Chris@1295 93 Setting[k]
Chris@1295 94 else
Chris@1295 95 Setting[k].dup
Chris@1295 96 end
Chris@1295 97 h
Chris@1295 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@14 114 def change_user_password(login, new_password)
Chris@14 115 user = User.first(:conditions => {:login => login})
Chris@14 116 user.password, user.password_confirmation = new_password, new_password
Chris@14 117 user.save!
Chris@14 118 end
Chris@14 119
Chris@0 120 def self.ldap_configured?
Chris@0 121 @test_ldap = Net::LDAP.new(:host => '127.0.0.1', :port => 389)
Chris@0 122 return @test_ldap.bind
Chris@0 123 rescue Exception => e
Chris@0 124 # LDAP is not listening
Chris@0 125 return nil
Chris@0 126 end
Chris@909 127
Chris@1115 128 def self.convert_installed?
Chris@1115 129 Redmine::Thumbnail.convert_available?
Chris@1115 130 end
Chris@1115 131
Chris@0 132 # Returns the path to the test +vendor+ repository
Chris@0 133 def self.repository_path(vendor)
Chris@909 134 Rails.root.join("tmp/test/#{vendor.downcase}_repository").to_s
Chris@0 135 end
Chris@909 136
Chris@441 137 # Returns the url of the subversion test repository
Chris@441 138 def self.subversion_repository_url
Chris@441 139 path = repository_path('subversion')
Chris@441 140 path = '/' + path unless path.starts_with?('/')
Chris@441 141 "file://#{path}"
Chris@441 142 end
Chris@909 143
Chris@0 144 # Returns true if the +vendor+ test repository is configured
Chris@0 145 def self.repository_configured?(vendor)
Chris@0 146 File.directory?(repository_path(vendor))
Chris@0 147 end
Chris@909 148
Chris@1115 149 def repository_path_hash(arr)
Chris@1115 150 hs = {}
Chris@1115 151 hs[:path] = arr.join("/")
Chris@1115 152 hs[:param] = arr.join("/")
Chris@1115 153 hs
Chris@1115 154 end
Chris@1115 155
Chris@1115 156 def assert_save(object)
Chris@1115 157 saved = object.save
Chris@1115 158 message = "#{object.class} could not be saved"
Chris@1115 159 errors = object.errors.full_messages.map {|m| "- #{m}"}
Chris@1115 160 message << ":\n#{errors.join("\n")}" if errors.any?
Chris@1115 161 assert_equal true, saved, message
Chris@1115 162 end
Chris@1115 163
chris@37 164 def assert_error_tag(options={})
Chris@119 165 assert_tag({:attributes => { :id => 'errorExplanation' }}.merge(options))
chris@37 166 end
Chris@0 167
Chris@1115 168 def assert_include(expected, s, message=nil)
Chris@1115 169 assert s.include?(expected), (message || "\"#{expected}\" not found in \"#{s}\"")
Chris@1115 170 end
Chris@1115 171
Chris@1115 172 def assert_not_include(expected, s)
Chris@1115 173 assert !s.include?(expected), "\"#{expected}\" found in \"#{s}\""
Chris@1115 174 end
Chris@1115 175
Chris@1115 176 def assert_select_in(text, *args, &block)
Chris@1115 177 d = HTML::Document.new(CGI::unescapeHTML(String.new(text))).root
Chris@1115 178 assert_select(d, *args, &block)
Chris@1115 179 end
Chris@1115 180
Chris@1115 181 def assert_mail_body_match(expected, mail)
Chris@1115 182 if expected.is_a?(String)
Chris@1115 183 assert_include expected, mail_body(mail)
Chris@1115 184 else
Chris@1115 185 assert_match expected, mail_body(mail)
Chris@1115 186 end
Chris@1115 187 end
Chris@1115 188
Chris@1115 189 def assert_mail_body_no_match(expected, mail)
Chris@1115 190 if expected.is_a?(String)
Chris@1115 191 assert_not_include expected, mail_body(mail)
Chris@1115 192 else
Chris@1115 193 assert_no_match expected, mail_body(mail)
Chris@1115 194 end
Chris@1115 195 end
Chris@1115 196
Chris@1115 197 def mail_body(mail)
Chris@1115 198 mail.parts.first.body.encoded
Chris@909 199 end
Chris@1295 200 end
Chris@909 201
Chris@1295 202 module Redmine
Chris@1295 203 module ApiTest
Chris@1295 204 # Base class for API tests
Chris@1295 205 class Base < ActionDispatch::IntegrationTest
Chris@1295 206 # Test that a request allows the three types of API authentication
Chris@1295 207 #
Chris@1295 208 # * HTTP Basic with username and password
Chris@1295 209 # * HTTP Basic with an api key for the username
Chris@1295 210 # * Key based with the key=X parameter
Chris@1295 211 #
Chris@1295 212 # @param [Symbol] http_method the HTTP method for request (:get, :post, :put, :delete)
Chris@1295 213 # @param [String] url the request url
Chris@1295 214 # @param [optional, Hash] parameters additional request parameters
Chris@1295 215 # @param [optional, Hash] options additional options
Chris@1295 216 # @option options [Symbol] :success_code Successful response code (:success)
Chris@1295 217 # @option options [Symbol] :failure_code Failure response code (:unauthorized)
Chris@1295 218 def self.should_allow_api_authentication(http_method, url, parameters={}, options={})
Chris@1295 219 should_allow_http_basic_auth_with_username_and_password(http_method, url, parameters, options)
Chris@1295 220 should_allow_http_basic_auth_with_key(http_method, url, parameters, options)
Chris@1295 221 should_allow_key_based_auth(http_method, url, parameters, options)
Chris@1295 222 end
Chris@1295 223
Chris@1295 224 # Test that a request allows the username and password for HTTP BASIC
Chris@1295 225 #
Chris@1295 226 # @param [Symbol] http_method the HTTP method for request (:get, :post, :put, :delete)
Chris@1295 227 # @param [String] url the request url
Chris@1295 228 # @param [optional, Hash] parameters additional request parameters
Chris@1295 229 # @param [optional, Hash] options additional options
Chris@1295 230 # @option options [Symbol] :success_code Successful response code (:success)
Chris@1295 231 # @option options [Symbol] :failure_code Failure response code (:unauthorized)
Chris@1295 232 def self.should_allow_http_basic_auth_with_username_and_password(http_method, url, parameters={}, options={})
Chris@1295 233 success_code = options[:success_code] || :success
Chris@1295 234 failure_code = options[:failure_code] || :unauthorized
Chris@1295 235
Chris@1295 236 context "should allow http basic auth using a username and password for #{http_method} #{url}" do
Chris@1295 237 context "with a valid HTTP authentication" do
Chris@1295 238 setup do
Chris@1295 239 @user = User.generate! do |user|
Chris@1295 240 user.admin = true
Chris@1295 241 user.password = 'my_password'
Chris@1295 242 end
Chris@1295 243 send(http_method, url, parameters, credentials(@user.login, 'my_password'))
Chris@1295 244 end
Chris@1295 245
Chris@1295 246 should_respond_with success_code
Chris@1295 247 should_respond_with_content_type_based_on_url(url)
Chris@1295 248 should "login as the user" do
Chris@1295 249 assert_equal @user, User.current
Chris@1295 250 end
Chris@1115 251 end
Chris@1295 252
Chris@1295 253 context "with an invalid HTTP authentication" do
Chris@1295 254 setup do
Chris@1295 255 @user = User.generate!
Chris@1295 256 send(http_method, url, parameters, credentials(@user.login, 'wrong_password'))
Chris@1295 257 end
Chris@1295 258
Chris@1295 259 should_respond_with failure_code
Chris@1295 260 should_respond_with_content_type_based_on_url(url)
Chris@1295 261 should "not login as the user" do
Chris@1295 262 assert_equal User.anonymous, User.current
Chris@1295 263 end
Chris@1295 264 end
Chris@1295 265
Chris@1295 266 context "without credentials" do
Chris@1295 267 setup do
Chris@1295 268 send(http_method, url, parameters)
Chris@1295 269 end
Chris@1295 270
Chris@1295 271 should_respond_with failure_code
Chris@1295 272 should_respond_with_content_type_based_on_url(url)
Chris@1295 273 should "include_www_authenticate_header" do
Chris@1295 274 assert @controller.response.headers.has_key?('WWW-Authenticate')
Chris@1295 275 end
Chris@1295 276 end
chris@37 277 end
chris@37 278 end
Chris@1295 279
Chris@1295 280 # Test that a request allows the API key with HTTP BASIC
Chris@1295 281 #
Chris@1295 282 # @param [Symbol] http_method the HTTP method for request (:get, :post, :put, :delete)
Chris@1295 283 # @param [String] url the request url
Chris@1295 284 # @param [optional, Hash] parameters additional request parameters
Chris@1295 285 # @param [optional, Hash] options additional options
Chris@1295 286 # @option options [Symbol] :success_code Successful response code (:success)
Chris@1295 287 # @option options [Symbol] :failure_code Failure response code (:unauthorized)
Chris@1295 288 def self.should_allow_http_basic_auth_with_key(http_method, url, parameters={}, options={})
Chris@1295 289 success_code = options[:success_code] || :success
Chris@1295 290 failure_code = options[:failure_code] || :unauthorized
Chris@1295 291
Chris@1295 292 context "should allow http basic auth with a key for #{http_method} #{url}" do
Chris@1295 293 context "with a valid HTTP authentication using the API token" do
Chris@1295 294 setup do
Chris@1295 295 @user = User.generate! do |user|
Chris@1295 296 user.admin = true
Chris@1295 297 end
Chris@1295 298 @token = Token.create!(:user => @user, :action => 'api')
Chris@1295 299 send(http_method, url, parameters, credentials(@token.value, 'X'))
Chris@1295 300 end
Chris@1295 301 should_respond_with success_code
Chris@1295 302 should_respond_with_content_type_based_on_url(url)
Chris@1295 303 should_be_a_valid_response_string_based_on_url(url)
Chris@1295 304 should "login as the user" do
Chris@1295 305 assert_equal @user, User.current
Chris@1295 306 end
Chris@1295 307 end
Chris@1295 308
Chris@1295 309 context "with an invalid HTTP authentication" do
Chris@1295 310 setup do
Chris@1295 311 @user = User.generate!
Chris@1295 312 @token = Token.create!(:user => @user, :action => 'feeds')
Chris@1295 313 send(http_method, url, parameters, credentials(@token.value, 'X'))
Chris@1295 314 end
Chris@1295 315 should_respond_with failure_code
Chris@1295 316 should_respond_with_content_type_based_on_url(url)
Chris@1295 317 should "not login as the user" do
Chris@1295 318 assert_equal User.anonymous, User.current
Chris@1295 319 end
Chris@1295 320 end
chris@37 321 end
chris@37 322 end
Chris@1295 323
Chris@1295 324 # Test that a request allows full key authentication
Chris@1295 325 #
Chris@1295 326 # @param [Symbol] http_method the HTTP method for request (:get, :post, :put, :delete)
Chris@1295 327 # @param [String] url the request url, without the key=ZXY parameter
Chris@1295 328 # @param [optional, Hash] parameters additional request parameters
Chris@1295 329 # @param [optional, Hash] options additional options
Chris@1295 330 # @option options [Symbol] :success_code Successful response code (:success)
Chris@1295 331 # @option options [Symbol] :failure_code Failure response code (:unauthorized)
Chris@1295 332 def self.should_allow_key_based_auth(http_method, url, parameters={}, options={})
Chris@1295 333 success_code = options[:success_code] || :success
Chris@1295 334 failure_code = options[:failure_code] || :unauthorized
Chris@1295 335
Chris@1295 336 context "should allow key based auth using key=X for #{http_method} #{url}" do
Chris@1295 337 context "with a valid api token" do
Chris@1295 338 setup do
Chris@1295 339 @user = User.generate! do |user|
Chris@1295 340 user.admin = true
Chris@1295 341 end
Chris@1295 342 @token = Token.create!(:user => @user, :action => 'api')
Chris@1295 343 # Simple url parse to add on ?key= or &key=
Chris@1295 344 request_url = if url.match(/\?/)
Chris@1295 345 url + "&key=#{@token.value}"
Chris@1295 346 else
Chris@1295 347 url + "?key=#{@token.value}"
Chris@1295 348 end
Chris@1295 349 send(http_method, request_url, parameters)
Chris@1295 350 end
Chris@1295 351 should_respond_with success_code
Chris@1295 352 should_respond_with_content_type_based_on_url(url)
Chris@1295 353 should_be_a_valid_response_string_based_on_url(url)
Chris@1295 354 should "login as the user" do
Chris@1295 355 assert_equal @user, User.current
Chris@1295 356 end
Chris@1295 357 end
Chris@1295 358
Chris@1295 359 context "with an invalid api token" do
Chris@1295 360 setup do
Chris@1295 361 @user = User.generate! do |user|
Chris@1295 362 user.admin = true
Chris@1295 363 end
Chris@1295 364 @token = Token.create!(:user => @user, :action => 'feeds')
Chris@1295 365 # Simple url parse to add on ?key= or &key=
Chris@1295 366 request_url = if url.match(/\?/)
Chris@1295 367 url + "&key=#{@token.value}"
Chris@1295 368 else
Chris@1295 369 url + "?key=#{@token.value}"
Chris@1295 370 end
Chris@1295 371 send(http_method, request_url, parameters)
Chris@1295 372 end
Chris@1295 373 should_respond_with failure_code
Chris@1295 374 should_respond_with_content_type_based_on_url(url)
Chris@1295 375 should "not login as the user" do
Chris@1295 376 assert_equal User.anonymous, User.current
Chris@1295 377 end
Chris@1295 378 end
chris@37 379 end
Chris@1295 380
Chris@1295 381 context "should allow key based auth using X-Redmine-API-Key header for #{http_method} #{url}" do
Chris@1295 382 setup do
Chris@1295 383 @user = User.generate! do |user|
Chris@1295 384 user.admin = true
Chris@1295 385 end
Chris@1295 386 @token = Token.create!(:user => @user, :action => 'api')
Chris@1295 387 send(http_method, url, parameters, {'X-Redmine-API-Key' => @token.value.to_s})
Chris@1295 388 end
Chris@1295 389 should_respond_with success_code
Chris@1295 390 should_respond_with_content_type_based_on_url(url)
Chris@1295 391 should_be_a_valid_response_string_based_on_url(url)
Chris@1295 392 should "login as the user" do
Chris@1295 393 assert_equal @user, User.current
Chris@1295 394 end
Chris@1295 395 end
Chris@1295 396 end
Chris@1295 397
Chris@1295 398 # Uses should_respond_with_content_type based on what's in the url:
Chris@1295 399 #
Chris@1295 400 # '/project/issues.xml' => should_respond_with_content_type :xml
Chris@1295 401 # '/project/issues.json' => should_respond_with_content_type :json
Chris@1295 402 #
Chris@1295 403 # @param [String] url Request
Chris@1295 404 def self.should_respond_with_content_type_based_on_url(url)
Chris@1295 405 case
Chris@1295 406 when url.match(/xml/i)
Chris@1295 407 should "respond with XML" do
Chris@1295 408 assert_equal 'application/xml', @response.content_type
Chris@1295 409 end
Chris@1295 410 when url.match(/json/i)
Chris@1295 411 should "respond with JSON" do
Chris@1295 412 assert_equal 'application/json', @response.content_type
Chris@1295 413 end
Chris@1295 414 else
Chris@1295 415 raise "Unknown content type for should_respond_with_content_type_based_on_url: #{url}"
Chris@1295 416 end
Chris@1295 417 end
Chris@1295 418
Chris@1295 419 # Uses the url to assert which format the response should be in
Chris@1295 420 #
Chris@1295 421 # '/project/issues.xml' => should_be_a_valid_xml_string
Chris@1295 422 # '/project/issues.json' => should_be_a_valid_json_string
Chris@1295 423 #
Chris@1295 424 # @param [String] url Request
Chris@1295 425 def self.should_be_a_valid_response_string_based_on_url(url)
Chris@1295 426 case
Chris@1295 427 when url.match(/xml/i)
Chris@1295 428 should_be_a_valid_xml_string
Chris@1295 429 when url.match(/json/i)
Chris@1295 430 should_be_a_valid_json_string
Chris@1295 431 else
Chris@1295 432 raise "Unknown content type for should_be_a_valid_response_based_on_url: #{url}"
Chris@1295 433 end
Chris@1295 434 end
Chris@1295 435
Chris@1295 436 # Checks that the response is a valid JSON string
Chris@1295 437 def self.should_be_a_valid_json_string
Chris@1295 438 should "be a valid JSON string (or empty)" do
Chris@1295 439 assert(response.body.blank? || ActiveSupport::JSON.decode(response.body))
Chris@1295 440 end
Chris@1295 441 end
Chris@1295 442
Chris@1295 443 # Checks that the response is a valid XML string
Chris@1295 444 def self.should_be_a_valid_xml_string
Chris@1295 445 should "be a valid XML string" do
Chris@1295 446 assert REXML::Document.new(response.body)
Chris@1295 447 end
Chris@1295 448 end
Chris@1295 449
Chris@1295 450 def self.should_respond_with(status)
Chris@1295 451 should "respond with #{status}" do
Chris@1295 452 assert_response status
chris@37 453 end
chris@37 454 end
chris@37 455 end
chris@37 456 end
Chris@1295 457 end
chris@37 458
Chris@1295 459 # URL helpers do not work with config.threadsafe!
Chris@1295 460 # https://github.com/rspec/rspec-rails/issues/476#issuecomment-4705454
Chris@1295 461 ActionView::TestCase::TestController.instance_eval do
Chris@1295 462 helper Rails.application.routes.url_helpers
Chris@1295 463 end
Chris@1295 464 ActionView::TestCase::TestController.class_eval do
Chris@1295 465 def _routes
Chris@1295 466 Rails.application.routes
Chris@1115 467 end
Chris@0 468 end