comparison 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
comparison
equal deleted inserted replaced
1297:0a574315af3e 1298:4f746d8966dd
1 # Redmine - project management software 1 # Redmine - project management software
2 # Copyright (C) 2006-2012 Jean-Philippe Lang 2 # Copyright (C) 2006-2013 Jean-Philippe Lang
3 # 3 #
4 # This program is free software; you can redistribute it and/or 4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License 5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2 6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version. 7 # of the License, or (at your option) any later version.
24 require File.expand_path(File.dirname(__FILE__) + '/object_helpers') 24 require File.expand_path(File.dirname(__FILE__) + '/object_helpers')
25 include ObjectHelpers 25 include ObjectHelpers
26 26
27 class ActiveSupport::TestCase 27 class ActiveSupport::TestCase
28 include ActionDispatch::TestProcess 28 include ActionDispatch::TestProcess
29 29
30 # Transactional fixtures accelerate your tests by wrapping each test method
31 # in a transaction that's rolled back on completion. This ensures that the
32 # test database remains unchanged so your fixtures don't have to be reloaded
33 # between every test method. Fewer database queries means faster tests.
34 #
35 # Read Mike Clark's excellent walkthrough at
36 # http://clarkware.com/cgi/blosxom/2005/10/24#Rails10FastTesting
37 #
38 # Every Active Record database supports transactions except MyISAM tables
39 # in MySQL. Turn off transactional fixtures in this case; however, if you
40 # don't care one way or the other, switching from MyISAM to InnoDB tables
41 # is recommended.
42 self.use_transactional_fixtures = true 30 self.use_transactional_fixtures = true
43
44 # Instantiated fixtures are slow, but give you @david where otherwise you
45 # would need people(:david). If you don't want to migrate your existing
46 # test cases which use the @david style and don't mind the speed hit (each
47 # instantiated fixtures translates to a database query per test method),
48 # then set this back to true.
49 self.use_instantiated_fixtures = false 31 self.use_instantiated_fixtures = false
50
51 # Add more helper methods to be used by all tests here...
52 32
53 def log_user(login, password) 33 def log_user(login, password)
54 User.anonymous 34 User.anonymous
55 get "/login" 35 get "/login"
56 assert_equal nil, session[:user_id] 36 assert_equal nil, session[:user_id]
105 def set_fixtures_attachments_directory 85 def set_fixtures_attachments_directory
106 Attachment.storage_path = "#{Rails.root}/test/fixtures/files" 86 Attachment.storage_path = "#{Rails.root}/test/fixtures/files"
107 end 87 end
108 88
109 def with_settings(options, &block) 89 def with_settings(options, &block)
110 saved_settings = options.keys.inject({}) {|h, k| h[k] = Setting[k].is_a?(Symbol) ? Setting[k] : Setting[k].dup; h} 90 saved_settings = options.keys.inject({}) do |h, k|
91 h[k] = case Setting[k]
92 when Symbol, false, true, nil
93 Setting[k]
94 else
95 Setting[k].dup
96 end
97 h
98 end
111 options.each {|k, v| Setting[k] = v} 99 options.each {|k, v| Setting[k] = v}
112 yield 100 yield
113 ensure 101 ensure
114 saved_settings.each {|k, v| Setting[k] = v} if saved_settings 102 saved_settings.each {|k, v| Setting[k] = v} if saved_settings
115 end 103 end
207 end 195 end
208 196
209 def mail_body(mail) 197 def mail_body(mail)
210 mail.parts.first.body.encoded 198 mail.parts.first.body.encoded
211 end 199 end
212 200 end
213 # Shoulda macros 201
214 def self.should_render_404 202 module Redmine
215 should_respond_with :not_found 203 module ApiTest
216 should_render_template 'common/error' 204 # Base class for API tests
217 end 205 class Base < ActionDispatch::IntegrationTest
218 206 # Test that a request allows the three types of API authentication
219 def self.should_have_before_filter(expected_method, options = {}) 207 #
220 should_have_filter('before', expected_method, options) 208 # * HTTP Basic with username and password
221 end 209 # * HTTP Basic with an api key for the username
222 210 # * Key based with the key=X parameter
223 def self.should_have_after_filter(expected_method, options = {}) 211 #
224 should_have_filter('after', expected_method, options) 212 # @param [Symbol] http_method the HTTP method for request (:get, :post, :put, :delete)
225 end 213 # @param [String] url the request url
226 214 # @param [optional, Hash] parameters additional request parameters
227 def self.should_have_filter(filter_type, expected_method, options) 215 # @param [optional, Hash] options additional options
228 description = "have #{filter_type}_filter :#{expected_method}" 216 # @option options [Symbol] :success_code Successful response code (:success)
229 description << " with #{options.inspect}" unless options.empty? 217 # @option options [Symbol] :failure_code Failure response code (:unauthorized)
230 218 def self.should_allow_api_authentication(http_method, url, parameters={}, options={})
231 should description do 219 should_allow_http_basic_auth_with_username_and_password(http_method, url, parameters, options)
232 klass = "action_controller/filters/#{filter_type}_filter".classify.constantize 220 should_allow_http_basic_auth_with_key(http_method, url, parameters, options)
233 expected = klass.new(:filter, expected_method.to_sym, options) 221 should_allow_key_based_auth(http_method, url, parameters, options)
234 assert_equal 1, @controller.class.filter_chain.select { |filter| 222 end
235 filter.method == expected.method && filter.kind == expected.kind && 223
236 filter.options == expected.options && filter.class == expected.class 224 # Test that a request allows the username and password for HTTP BASIC
237 }.size 225 #
226 # @param [Symbol] http_method the HTTP method for request (:get, :post, :put, :delete)
227 # @param [String] url the request url
228 # @param [optional, Hash] parameters additional request parameters
229 # @param [optional, Hash] options additional options
230 # @option options [Symbol] :success_code Successful response code (:success)
231 # @option options [Symbol] :failure_code Failure response code (:unauthorized)
232 def self.should_allow_http_basic_auth_with_username_and_password(http_method, url, parameters={}, options={})
233 success_code = options[:success_code] || :success
234 failure_code = options[:failure_code] || :unauthorized
235
236 context "should allow http basic auth using a username and password for #{http_method} #{url}" do
237 context "with a valid HTTP authentication" do
238 setup do
239 @user = User.generate! do |user|
240 user.admin = true
241 user.password = 'my_password'
242 end
243 send(http_method, url, parameters, credentials(@user.login, 'my_password'))
244 end
245
246 should_respond_with success_code
247 should_respond_with_content_type_based_on_url(url)
248 should "login as the user" do
249 assert_equal @user, User.current
250 end
251 end
252
253 context "with an invalid HTTP authentication" do
254 setup do
255 @user = User.generate!
256 send(http_method, url, parameters, credentials(@user.login, 'wrong_password'))
257 end
258
259 should_respond_with failure_code
260 should_respond_with_content_type_based_on_url(url)
261 should "not login as the user" do
262 assert_equal User.anonymous, User.current
263 end
264 end
265
266 context "without credentials" do
267 setup do
268 send(http_method, url, parameters)
269 end
270
271 should_respond_with failure_code
272 should_respond_with_content_type_based_on_url(url)
273 should "include_www_authenticate_header" do
274 assert @controller.response.headers.has_key?('WWW-Authenticate')
275 end
276 end
277 end
278 end
279
280 # Test that a request allows the API key with HTTP BASIC
281 #
282 # @param [Symbol] http_method the HTTP method for request (:get, :post, :put, :delete)
283 # @param [String] url the request url
284 # @param [optional, Hash] parameters additional request parameters
285 # @param [optional, Hash] options additional options
286 # @option options [Symbol] :success_code Successful response code (:success)
287 # @option options [Symbol] :failure_code Failure response code (:unauthorized)
288 def self.should_allow_http_basic_auth_with_key(http_method, url, parameters={}, options={})
289 success_code = options[:success_code] || :success
290 failure_code = options[:failure_code] || :unauthorized
291
292 context "should allow http basic auth with a key for #{http_method} #{url}" do
293 context "with a valid HTTP authentication using the API token" do
294 setup do
295 @user = User.generate! do |user|
296 user.admin = true
297 end
298 @token = Token.create!(:user => @user, :action => 'api')
299 send(http_method, url, parameters, credentials(@token.value, 'X'))
300 end
301 should_respond_with success_code
302 should_respond_with_content_type_based_on_url(url)
303 should_be_a_valid_response_string_based_on_url(url)
304 should "login as the user" do
305 assert_equal @user, User.current
306 end
307 end
308
309 context "with an invalid HTTP authentication" do
310 setup do
311 @user = User.generate!
312 @token = Token.create!(:user => @user, :action => 'feeds')
313 send(http_method, url, parameters, credentials(@token.value, 'X'))
314 end
315 should_respond_with failure_code
316 should_respond_with_content_type_based_on_url(url)
317 should "not login as the user" do
318 assert_equal User.anonymous, User.current
319 end
320 end
321 end
322 end
323
324 # Test that a request allows full key authentication
325 #
326 # @param [Symbol] http_method the HTTP method for request (:get, :post, :put, :delete)
327 # @param [String] url the request url, without the key=ZXY parameter
328 # @param [optional, Hash] parameters additional request parameters
329 # @param [optional, Hash] options additional options
330 # @option options [Symbol] :success_code Successful response code (:success)
331 # @option options [Symbol] :failure_code Failure response code (:unauthorized)
332 def self.should_allow_key_based_auth(http_method, url, parameters={}, options={})
333 success_code = options[:success_code] || :success
334 failure_code = options[:failure_code] || :unauthorized
335
336 context "should allow key based auth using key=X for #{http_method} #{url}" do
337 context "with a valid api token" do
338 setup do
339 @user = User.generate! do |user|
340 user.admin = true
341 end
342 @token = Token.create!(:user => @user, :action => 'api')
343 # Simple url parse to add on ?key= or &key=
344 request_url = if url.match(/\?/)
345 url + "&key=#{@token.value}"
346 else
347 url + "?key=#{@token.value}"
348 end
349 send(http_method, request_url, parameters)
350 end
351 should_respond_with success_code
352 should_respond_with_content_type_based_on_url(url)
353 should_be_a_valid_response_string_based_on_url(url)
354 should "login as the user" do
355 assert_equal @user, User.current
356 end
357 end
358
359 context "with an invalid api token" do
360 setup do
361 @user = User.generate! do |user|
362 user.admin = true
363 end
364 @token = Token.create!(:user => @user, :action => 'feeds')
365 # Simple url parse to add on ?key= or &key=
366 request_url = if url.match(/\?/)
367 url + "&key=#{@token.value}"
368 else
369 url + "?key=#{@token.value}"
370 end
371 send(http_method, request_url, parameters)
372 end
373 should_respond_with failure_code
374 should_respond_with_content_type_based_on_url(url)
375 should "not login as the user" do
376 assert_equal User.anonymous, User.current
377 end
378 end
379 end
380
381 context "should allow key based auth using X-Redmine-API-Key header for #{http_method} #{url}" do
382 setup do
383 @user = User.generate! do |user|
384 user.admin = true
385 end
386 @token = Token.create!(:user => @user, :action => 'api')
387 send(http_method, url, parameters, {'X-Redmine-API-Key' => @token.value.to_s})
388 end
389 should_respond_with success_code
390 should_respond_with_content_type_based_on_url(url)
391 should_be_a_valid_response_string_based_on_url(url)
392 should "login as the user" do
393 assert_equal @user, User.current
394 end
395 end
396 end
397
398 # Uses should_respond_with_content_type based on what's in the url:
399 #
400 # '/project/issues.xml' => should_respond_with_content_type :xml
401 # '/project/issues.json' => should_respond_with_content_type :json
402 #
403 # @param [String] url Request
404 def self.should_respond_with_content_type_based_on_url(url)
405 case
406 when url.match(/xml/i)
407 should "respond with XML" do
408 assert_equal 'application/xml', @response.content_type
409 end
410 when url.match(/json/i)
411 should "respond with JSON" do
412 assert_equal 'application/json', @response.content_type
413 end
414 else
415 raise "Unknown content type for should_respond_with_content_type_based_on_url: #{url}"
416 end
417 end
418
419 # Uses the url to assert which format the response should be in
420 #
421 # '/project/issues.xml' => should_be_a_valid_xml_string
422 # '/project/issues.json' => should_be_a_valid_json_string
423 #
424 # @param [String] url Request
425 def self.should_be_a_valid_response_string_based_on_url(url)
426 case
427 when url.match(/xml/i)
428 should_be_a_valid_xml_string
429 when url.match(/json/i)
430 should_be_a_valid_json_string
431 else
432 raise "Unknown content type for should_be_a_valid_response_based_on_url: #{url}"
433 end
434 end
435
436 # Checks that the response is a valid JSON string
437 def self.should_be_a_valid_json_string
438 should "be a valid JSON string (or empty)" do
439 assert(response.body.blank? || ActiveSupport::JSON.decode(response.body))
440 end
441 end
442
443 # Checks that the response is a valid XML string
444 def self.should_be_a_valid_xml_string
445 should "be a valid XML string" do
446 assert REXML::Document.new(response.body)
447 end
448 end
449
450 def self.should_respond_with(status)
451 should "respond with #{status}" do
452 assert_response status
453 end
454 end
238 end 455 end
239 end 456 end
240
241 # Test that a request allows the three types of API authentication
242 #
243 # * HTTP Basic with username and password
244 # * HTTP Basic with an api key for the username
245 # * Key based with the key=X parameter
246 #
247 # @param [Symbol] http_method the HTTP method for request (:get, :post, :put, :delete)
248 # @param [String] url the request url
249 # @param [optional, Hash] parameters additional request parameters
250 # @param [optional, Hash] options additional options
251 # @option options [Symbol] :success_code Successful response code (:success)
252 # @option options [Symbol] :failure_code Failure response code (:unauthorized)
253 def self.should_allow_api_authentication(http_method, url, parameters={}, options={})
254 should_allow_http_basic_auth_with_username_and_password(http_method, url, parameters, options)
255 should_allow_http_basic_auth_with_key(http_method, url, parameters, options)
256 should_allow_key_based_auth(http_method, url, parameters, options)
257 end
258
259 # Test that a request allows the username and password for HTTP BASIC
260 #
261 # @param [Symbol] http_method the HTTP method for request (:get, :post, :put, :delete)
262 # @param [String] url the request url
263 # @param [optional, Hash] parameters additional request parameters
264 # @param [optional, Hash] options additional options
265 # @option options [Symbol] :success_code Successful response code (:success)
266 # @option options [Symbol] :failure_code Failure response code (:unauthorized)
267 def self.should_allow_http_basic_auth_with_username_and_password(http_method, url, parameters={}, options={})
268 success_code = options[:success_code] || :success
269 failure_code = options[:failure_code] || :unauthorized
270
271 context "should allow http basic auth using a username and password for #{http_method} #{url}" do
272 context "with a valid HTTP authentication" do
273 setup do
274 @user = User.generate! do |user|
275 user.admin = true
276 user.password = 'my_password'
277 end
278 send(http_method, url, parameters, credentials(@user.login, 'my_password'))
279 end
280
281 should_respond_with success_code
282 should_respond_with_content_type_based_on_url(url)
283 should "login as the user" do
284 assert_equal @user, User.current
285 end
286 end
287
288 context "with an invalid HTTP authentication" do
289 setup do
290 @user = User.generate!
291 send(http_method, url, parameters, credentials(@user.login, 'wrong_password'))
292 end
293
294 should_respond_with failure_code
295 should_respond_with_content_type_based_on_url(url)
296 should "not login as the user" do
297 assert_equal User.anonymous, User.current
298 end
299 end
300
301 context "without credentials" do
302 setup do
303 send(http_method, url, parameters)
304 end
305
306 should_respond_with failure_code
307 should_respond_with_content_type_based_on_url(url)
308 should "include_www_authenticate_header" do
309 assert @controller.response.headers.has_key?('WWW-Authenticate')
310 end
311 end
312 end
313 end
314
315 # Test that a request allows the API key with HTTP BASIC
316 #
317 # @param [Symbol] http_method the HTTP method for request (:get, :post, :put, :delete)
318 # @param [String] url the request url
319 # @param [optional, Hash] parameters additional request parameters
320 # @param [optional, Hash] options additional options
321 # @option options [Symbol] :success_code Successful response code (:success)
322 # @option options [Symbol] :failure_code Failure response code (:unauthorized)
323 def self.should_allow_http_basic_auth_with_key(http_method, url, parameters={}, options={})
324 success_code = options[:success_code] || :success
325 failure_code = options[:failure_code] || :unauthorized
326
327 context "should allow http basic auth with a key for #{http_method} #{url}" do
328 context "with a valid HTTP authentication using the API token" do
329 setup do
330 @user = User.generate! do |user|
331 user.admin = true
332 end
333 @token = Token.create!(:user => @user, :action => 'api')
334 send(http_method, url, parameters, credentials(@token.value, 'X'))
335 end
336 should_respond_with success_code
337 should_respond_with_content_type_based_on_url(url)
338 should_be_a_valid_response_string_based_on_url(url)
339 should "login as the user" do
340 assert_equal @user, User.current
341 end
342 end
343
344 context "with an invalid HTTP authentication" do
345 setup do
346 @user = User.generate!
347 @token = Token.create!(:user => @user, :action => 'feeds')
348 send(http_method, url, parameters, credentials(@token.value, 'X'))
349 end
350 should_respond_with failure_code
351 should_respond_with_content_type_based_on_url(url)
352 should "not login as the user" do
353 assert_equal User.anonymous, User.current
354 end
355 end
356 end
357 end
358
359 # Test that a request allows full key authentication
360 #
361 # @param [Symbol] http_method the HTTP method for request (:get, :post, :put, :delete)
362 # @param [String] url the request url, without the key=ZXY parameter
363 # @param [optional, Hash] parameters additional request parameters
364 # @param [optional, Hash] options additional options
365 # @option options [Symbol] :success_code Successful response code (:success)
366 # @option options [Symbol] :failure_code Failure response code (:unauthorized)
367 def self.should_allow_key_based_auth(http_method, url, parameters={}, options={})
368 success_code = options[:success_code] || :success
369 failure_code = options[:failure_code] || :unauthorized
370
371 context "should allow key based auth using key=X for #{http_method} #{url}" do
372 context "with a valid api token" do
373 setup do
374 @user = User.generate! do |user|
375 user.admin = true
376 end
377 @token = Token.create!(:user => @user, :action => 'api')
378 # Simple url parse to add on ?key= or &key=
379 request_url = if url.match(/\?/)
380 url + "&key=#{@token.value}"
381 else
382 url + "?key=#{@token.value}"
383 end
384 send(http_method, request_url, parameters)
385 end
386 should_respond_with success_code
387 should_respond_with_content_type_based_on_url(url)
388 should_be_a_valid_response_string_based_on_url(url)
389 should "login as the user" do
390 assert_equal @user, User.current
391 end
392 end
393
394 context "with an invalid api token" do
395 setup do
396 @user = User.generate! do |user|
397 user.admin = true
398 end
399 @token = Token.create!(:user => @user, :action => 'feeds')
400 # Simple url parse to add on ?key= or &key=
401 request_url = if url.match(/\?/)
402 url + "&key=#{@token.value}"
403 else
404 url + "?key=#{@token.value}"
405 end
406 send(http_method, request_url, parameters)
407 end
408 should_respond_with failure_code
409 should_respond_with_content_type_based_on_url(url)
410 should "not login as the user" do
411 assert_equal User.anonymous, User.current
412 end
413 end
414 end
415
416 context "should allow key based auth using X-Redmine-API-Key header for #{http_method} #{url}" do
417 setup do
418 @user = User.generate! do |user|
419 user.admin = true
420 end
421 @token = Token.create!(:user => @user, :action => 'api')
422 send(http_method, url, parameters, {'X-Redmine-API-Key' => @token.value.to_s})
423 end
424 should_respond_with success_code
425 should_respond_with_content_type_based_on_url(url)
426 should_be_a_valid_response_string_based_on_url(url)
427 should "login as the user" do
428 assert_equal @user, User.current
429 end
430 end
431 end
432
433 # Uses should_respond_with_content_type based on what's in the url:
434 #
435 # '/project/issues.xml' => should_respond_with_content_type :xml
436 # '/project/issues.json' => should_respond_with_content_type :json
437 #
438 # @param [String] url Request
439 def self.should_respond_with_content_type_based_on_url(url)
440 case
441 when url.match(/xml/i)
442 should "respond with XML" do
443 assert_equal 'application/xml', @response.content_type
444 end
445 when url.match(/json/i)
446 should "respond with JSON" do
447 assert_equal 'application/json', @response.content_type
448 end
449 else
450 raise "Unknown content type for should_respond_with_content_type_based_on_url: #{url}"
451 end
452 end
453
454 # Uses the url to assert which format the response should be in
455 #
456 # '/project/issues.xml' => should_be_a_valid_xml_string
457 # '/project/issues.json' => should_be_a_valid_json_string
458 #
459 # @param [String] url Request
460 def self.should_be_a_valid_response_string_based_on_url(url)
461 case
462 when url.match(/xml/i)
463 should_be_a_valid_xml_string
464 when url.match(/json/i)
465 should_be_a_valid_json_string
466 else
467 raise "Unknown content type for should_be_a_valid_response_based_on_url: #{url}"
468 end
469 end
470
471 # Checks that the response is a valid JSON string
472 def self.should_be_a_valid_json_string
473 should "be a valid JSON string (or empty)" do
474 assert(response.body.blank? || ActiveSupport::JSON.decode(response.body))
475 end
476 end
477
478 # Checks that the response is a valid XML string
479 def self.should_be_a_valid_xml_string
480 should "be a valid XML string" do
481 assert REXML::Document.new(response.body)
482 end
483 end
484
485 def self.should_respond_with(status)
486 should "respond with #{status}" do
487 assert_response status
488 end
489 end
490 end 457 end
491 458
492 # Simple module to "namespace" all of the API tests 459 # URL helpers do not work with config.threadsafe!
493 module ApiTest 460 # https://github.com/rspec/rspec-rails/issues/476#issuecomment-4705454
461 ActionView::TestCase::TestController.instance_eval do
462 helper Rails.application.routes.url_helpers
494 end 463 end
464 ActionView::TestCase::TestController.class_eval do
465 def _routes
466 Rails.application.routes
467 end
468 end