annotate .svn/pristine/b5/b5842903c628d7f7ca625476f554fde27898338a.svn-base @ 1327:287f201c2802 redmine-2.2-integration

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