| 
									
										
										
										
											2011-03-20 18:25:38 +00:00
										 |  |  | # Redmine - project management software | 
					
						
							| 
									
										
										
										
											2013-01-12 09:29:31 +00:00
										 |  |  | # Copyright (C) 2006-2013  Jean-Philippe Lang | 
					
						
							| 
									
										
										
										
											2006-07-09 16:30:01 +00:00
										 |  |  | # | 
					
						
							|  |  |  | # This program is free software; you can redistribute it and/or | 
					
						
							|  |  |  | # modify it under the terms of the GNU General Public License | 
					
						
							|  |  |  | # as published by the Free Software Foundation; either version 2 | 
					
						
							|  |  |  | # of the License, or (at your option) any later version. | 
					
						
							| 
									
										
										
										
											2011-06-23 11:19:05 +00:00
										 |  |  | # | 
					
						
							| 
									
										
										
										
											2006-07-09 16:30:01 +00:00
										 |  |  | # This program is distributed in the hope that it will be useful, | 
					
						
							|  |  |  | # but WITHOUT ANY WARRANTY; without even the implied warranty of | 
					
						
							|  |  |  | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
					
						
							|  |  |  | # GNU General Public License for more details. | 
					
						
							| 
									
										
										
										
											2011-06-23 11:19:05 +00:00
										 |  |  | # | 
					
						
							| 
									
										
										
										
											2006-07-09 16:30:01 +00:00
										 |  |  | # You should have received a copy of the GNU General Public License | 
					
						
							|  |  |  | # along with this program; if not, write to the Free Software | 
					
						
							|  |  |  | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-04-25 17:17:49 +00:00
										 |  |  | #require 'shoulda' | 
					
						
							| 
									
										
										
										
											2010-07-09 13:46:20 +00:00
										 |  |  | ENV["RAILS_ENV"] = "test" | 
					
						
							| 
									
										
										
										
											2006-06-28 18:11:03 +00:00
										 |  |  | require File.expand_path(File.dirname(__FILE__) + "/../config/environment") | 
					
						
							| 
									
										
										
										
											2012-04-25 17:17:49 +00:00
										 |  |  | require 'rails/test_help' | 
					
						
							| 
									
										
										
										
											2011-11-13 11:47:08 +00:00
										 |  |  | require Rails.root.join('test', 'mocks', 'open_id_authentication_mock.rb').to_s | 
					
						
							| 
									
										
										
										
											2006-06-28 18:11:03 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-04-22 11:58:05 +00:00
										 |  |  | require File.expand_path(File.dirname(__FILE__) + '/object_helpers') | 
					
						
							|  |  |  | include ObjectHelpers | 
					
						
							| 
									
										
										
										
											2009-10-20 00:36:51 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-09-13 17:14:35 +00:00
										 |  |  | class ActiveSupport::TestCase | 
					
						
							| 
									
										
										
										
											2012-04-25 17:17:49 +00:00
										 |  |  |   include ActionDispatch::TestProcess | 
					
						
							|  |  |  |    | 
					
						
							| 
									
										
										
										
											2006-06-28 18:11:03 +00:00
										 |  |  |   # Transactional fixtures accelerate your tests by wrapping each test method | 
					
						
							|  |  |  |   # in a transaction that's rolled back on completion.  This ensures that the | 
					
						
							|  |  |  |   # test database remains unchanged so your fixtures don't have to be reloaded | 
					
						
							|  |  |  |   # between every test method.  Fewer database queries means faster tests. | 
					
						
							|  |  |  |   # | 
					
						
							|  |  |  |   # Read Mike Clark's excellent walkthrough at | 
					
						
							|  |  |  |   #   http://clarkware.com/cgi/blosxom/2005/10/24#Rails10FastTesting | 
					
						
							|  |  |  |   # | 
					
						
							|  |  |  |   # Every Active Record database supports transactions except MyISAM tables | 
					
						
							|  |  |  |   # in MySQL.  Turn off transactional fixtures in this case; however, if you | 
					
						
							|  |  |  |   # don't care one way or the other, switching from MyISAM to InnoDB tables | 
					
						
							|  |  |  |   # is recommended. | 
					
						
							|  |  |  |   self.use_transactional_fixtures = true | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   # Instantiated fixtures are slow, but give you @david where otherwise you | 
					
						
							|  |  |  |   # would need people(:david).  If you don't want to migrate your existing | 
					
						
							|  |  |  |   # test cases which use the @david style and don't mind the speed hit (each | 
					
						
							|  |  |  |   # instantiated fixtures translates to a database query per test method), | 
					
						
							|  |  |  |   # then set this back to true. | 
					
						
							|  |  |  |   self.use_instantiated_fixtures  = false | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   # Add more helper methods to be used by all tests here... | 
					
						
							| 
									
										
										
										
											2011-09-01 00:50:14 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-07-09 16:30:01 +00:00
										 |  |  |   def log_user(login, password) | 
					
						
							| 
									
										
										
										
											2009-12-06 10:28:20 +00:00
										 |  |  |     User.anonymous | 
					
						
							| 
									
										
										
										
											2009-02-21 11:04:50 +00:00
										 |  |  |     get "/login" | 
					
						
							| 
									
										
										
										
											2006-07-29 09:32:58 +00:00
										 |  |  |     assert_equal nil, session[:user_id] | 
					
						
							| 
									
										
										
										
											2006-07-09 16:30:01 +00:00
										 |  |  |     assert_response :success | 
					
						
							|  |  |  |     assert_template "account/login" | 
					
						
							| 
									
										
										
										
											2009-02-21 11:04:50 +00:00
										 |  |  |     post "/login", :username => login, :password => password | 
					
						
							| 
									
										
										
										
											2006-07-29 09:32:58 +00:00
										 |  |  |     assert_equal login, User.find(session[:user_id]).login | 
					
						
							| 
									
										
										
										
											2006-07-09 16:30:01 +00:00
										 |  |  |   end | 
					
						
							| 
									
										
										
										
											2011-09-01 00:50:14 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-10 14:56:29 +00:00
										 |  |  |   def uploaded_test_file(name, mime) | 
					
						
							| 
									
										
										
										
											2012-04-25 17:17:49 +00:00
										 |  |  |     fixture_file_upload("files/#{name}", mime, true) | 
					
						
							| 
									
										
										
										
											2008-01-06 17:06:14 +00:00
										 |  |  |   end | 
					
						
							| 
									
										
										
										
											2010-06-19 03:54:23 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-01-02 19:44:37 +00:00
										 |  |  |   def credentials(user, password=nil) | 
					
						
							| 
									
										
										
										
											2012-04-25 17:17:49 +00:00
										 |  |  |     {'HTTP_AUTHORIZATION' => ActionController::HttpAuthentication::Basic.encode_credentials(user, password || user)} | 
					
						
							| 
									
										
										
										
											2012-01-02 19:44:37 +00:00
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-06-19 03:54:23 +00:00
										 |  |  |   # Mock out a file | 
					
						
							| 
									
										
										
										
											2010-08-19 22:24:42 +00:00
										 |  |  |   def self.mock_file | 
					
						
							| 
									
										
										
										
											2010-06-19 03:54:23 +00:00
										 |  |  |     file = 'a_file.png' | 
					
						
							|  |  |  |     file.stubs(:size).returns(32) | 
					
						
							|  |  |  |     file.stubs(:original_filename).returns('a_file.png') | 
					
						
							|  |  |  |     file.stubs(:content_type).returns('image/png') | 
					
						
							|  |  |  |     file.stubs(:read).returns(false) | 
					
						
							|  |  |  |     file | 
					
						
							|  |  |  |   end | 
					
						
							| 
									
										
										
										
											2010-08-19 22:24:42 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |   def mock_file | 
					
						
							|  |  |  |     self.class.mock_file | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-11-23 05:30:53 +00:00
										 |  |  |   def mock_file_with_options(options={}) | 
					
						
							|  |  |  |     file = '' | 
					
						
							|  |  |  |     file.stubs(:size).returns(32) | 
					
						
							|  |  |  |     original_filename = options[:original_filename] || nil | 
					
						
							|  |  |  |     file.stubs(:original_filename).returns(original_filename) | 
					
						
							|  |  |  |     content_type = options[:content_type] || nil | 
					
						
							|  |  |  |     file.stubs(:content_type).returns(content_type) | 
					
						
							|  |  |  |     file.stubs(:read).returns(false) | 
					
						
							|  |  |  |     file | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-06-09 18:40:59 +00:00
										 |  |  |   # Use a temporary directory for attachment related tests | 
					
						
							|  |  |  |   def set_tmp_attachments_directory | 
					
						
							| 
									
										
										
										
											2011-06-16 04:02:31 +00:00
										 |  |  |     Dir.mkdir "#{Rails.root}/tmp/test" unless File.directory?("#{Rails.root}/tmp/test") | 
					
						
							|  |  |  |     unless File.directory?("#{Rails.root}/tmp/test/attachments") | 
					
						
							|  |  |  |       Dir.mkdir "#{Rails.root}/tmp/test/attachments" | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |     Attachment.storage_path = "#{Rails.root}/tmp/test/attachments" | 
					
						
							| 
									
										
										
										
											2008-06-09 18:40:59 +00:00
										 |  |  |   end | 
					
						
							| 
									
										
										
										
											2011-06-16 04:02:31 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-12-16 17:52:47 +00:00
										 |  |  |   def set_fixtures_attachments_directory | 
					
						
							|  |  |  |     Attachment.storage_path = "#{Rails.root}/test/fixtures/files" | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-02-21 16:04:51 +00:00
										 |  |  |   def with_settings(options, &block) | 
					
						
							| 
									
										
										
										
											2011-11-26 17:37:20 +00:00
										 |  |  |     saved_settings = options.keys.inject({}) {|h, k| h[k] = Setting[k].is_a?(Symbol) ? Setting[k] : Setting[k].dup; h} | 
					
						
							| 
									
										
										
										
											2009-02-21 16:04:51 +00:00
										 |  |  |     options.each {|k, v| Setting[k] = v} | 
					
						
							|  |  |  |     yield | 
					
						
							| 
									
										
										
										
											2011-03-27 15:41:05 +00:00
										 |  |  |   ensure | 
					
						
							| 
									
										
										
										
											2011-11-26 17:37:20 +00:00
										 |  |  |     saved_settings.each {|k, v| Setting[k] = v} if saved_settings | 
					
						
							| 
									
										
										
										
											2009-02-21 16:04:51 +00:00
										 |  |  |   end | 
					
						
							| 
									
										
										
										
											2009-12-30 19:42:19 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-10-18 17:08:42 +00:00
										 |  |  |   # Yields the block with user as the current user | 
					
						
							|  |  |  |   def with_current_user(user, &block) | 
					
						
							|  |  |  |     saved_user = User.current | 
					
						
							|  |  |  |     User.current = user | 
					
						
							|  |  |  |     yield | 
					
						
							|  |  |  |   ensure | 
					
						
							|  |  |  |     User.current = saved_user | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-08-19 01:28:33 +00:00
										 |  |  |   def change_user_password(login, new_password) | 
					
						
							|  |  |  |     user = User.first(:conditions => {:login => login}) | 
					
						
							|  |  |  |     user.password, user.password_confirmation = new_password, new_password | 
					
						
							|  |  |  |     user.save! | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-02-16 16:40:50 +00:00
										 |  |  |   def self.ldap_configured? | 
					
						
							|  |  |  |     @test_ldap = Net::LDAP.new(:host => '127.0.0.1', :port => 389) | 
					
						
							|  |  |  |     return @test_ldap.bind | 
					
						
							| 
									
										
										
										
											2010-02-17 18:08:15 +00:00
										 |  |  |   rescue Exception => e | 
					
						
							|  |  |  |     # LDAP is not listening | 
					
						
							|  |  |  |     return nil | 
					
						
							| 
									
										
										
										
											2010-02-16 16:40:50 +00:00
										 |  |  |   end | 
					
						
							| 
									
										
										
										
											2011-06-20 12:33:58 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-07-07 13:48:07 +00:00
										 |  |  |   def self.convert_installed? | 
					
						
							| 
									
										
										
										
											2012-07-16 17:37:24 +00:00
										 |  |  |     Redmine::Thumbnail.convert_available? | 
					
						
							| 
									
										
										
										
											2012-07-07 13:48:07 +00:00
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-05-01 08:32:21 +00:00
										 |  |  |   # Returns the path to the test +vendor+ repository | 
					
						
							|  |  |  |   def self.repository_path(vendor) | 
					
						
							| 
									
										
										
										
											2011-06-20 12:33:58 +00:00
										 |  |  |     Rails.root.join("tmp/test/#{vendor.downcase}_repository").to_s | 
					
						
							| 
									
										
										
										
											2010-05-01 08:32:21 +00:00
										 |  |  |   end | 
					
						
							| 
									
										
										
										
											2011-06-20 12:33:58 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-03-20 23:10:11 +00:00
										 |  |  |   # Returns the url of the subversion test repository | 
					
						
							| 
									
										
										
										
											2011-03-20 18:25:38 +00:00
										 |  |  |   def self.subversion_repository_url | 
					
						
							|  |  |  |     path = repository_path('subversion') | 
					
						
							|  |  |  |     path = '/' + path unless path.starts_with?('/') | 
					
						
							|  |  |  |     "file://#{path}" | 
					
						
							|  |  |  |   end | 
					
						
							| 
									
										
										
										
											2011-06-20 12:33:58 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-05-01 08:32:21 +00:00
										 |  |  |   # Returns true if the +vendor+ test repository is configured | 
					
						
							|  |  |  |   def self.repository_configured?(vendor) | 
					
						
							|  |  |  |     File.directory?(repository_path(vendor)) | 
					
						
							|  |  |  |   end | 
					
						
							| 
									
										
										
										
											2011-06-20 12:33:58 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-02-23 06:11:59 +00:00
										 |  |  |   def repository_path_hash(arr) | 
					
						
							|  |  |  |     hs = {} | 
					
						
							|  |  |  |     hs[:path]  = arr.join("/") | 
					
						
							| 
									
										
										
										
											2012-04-25 17:17:49 +00:00
										 |  |  |     hs[:param] = arr.join("/") | 
					
						
							| 
									
										
										
										
											2012-02-23 06:11:59 +00:00
										 |  |  |     hs | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-10-10 17:38:17 +00:00
										 |  |  |   def assert_save(object) | 
					
						
							|  |  |  |     saved = object.save | 
					
						
							|  |  |  |     message = "#{object.class} could not be saved" | 
					
						
							|  |  |  |     errors = object.errors.full_messages.map {|m| "- #{m}"} | 
					
						
							|  |  |  |     message << ":\n#{errors.join("\n")}" if errors.any? | 
					
						
							|  |  |  |     assert_equal true, saved, message | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-23 11:07:04 +00:00
										 |  |  |   def assert_error_tag(options={}) | 
					
						
							| 
									
										
										
										
											2010-11-27 10:34:44 +00:00
										 |  |  |     assert_tag({:attributes => { :id => 'errorExplanation' }}.merge(options)) | 
					
						
							| 
									
										
										
										
											2010-10-23 11:07:04 +00:00
										 |  |  |   end | 
					
						
							| 
									
										
										
										
											2011-09-01 00:50:14 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-07-21 06:35:49 +00:00
										 |  |  |   def assert_include(expected, s, message=nil) | 
					
						
							|  |  |  |     assert s.include?(expected), (message || "\"#{expected}\" not found in \"#{s}\"") | 
					
						
							| 
									
										
										
										
											2011-07-10 08:00:25 +00:00
										 |  |  |   end | 
					
						
							| 
									
										
										
										
											2010-02-16 16:40:50 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-03-04 13:28:18 +00:00
										 |  |  |   def assert_not_include(expected, s) | 
					
						
							|  |  |  |     assert !s.include?(expected), "\"#{expected}\" found in \"#{s}\"" | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-10-20 14:49:32 +00:00
										 |  |  |   def assert_select_in(text, *args, &block) | 
					
						
							|  |  |  |     d = HTML::Document.new(CGI::unescapeHTML(String.new(text))).root | 
					
						
							|  |  |  |     assert_select(d, *args, &block) | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-03-04 13:28:18 +00:00
										 |  |  |   def assert_mail_body_match(expected, mail) | 
					
						
							|  |  |  |     if expected.is_a?(String) | 
					
						
							| 
									
										
										
										
											2012-03-04 14:01:54 +00:00
										 |  |  |       assert_include expected, mail_body(mail) | 
					
						
							| 
									
										
										
										
											2012-03-04 13:28:18 +00:00
										 |  |  |     else | 
					
						
							| 
									
										
										
										
											2012-03-04 14:01:54 +00:00
										 |  |  |       assert_match expected, mail_body(mail) | 
					
						
							| 
									
										
										
										
											2012-03-04 13:28:18 +00:00
										 |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   def assert_mail_body_no_match(expected, mail) | 
					
						
							|  |  |  |     if expected.is_a?(String) | 
					
						
							| 
									
										
										
										
											2012-03-04 14:01:54 +00:00
										 |  |  |       assert_not_include expected, mail_body(mail) | 
					
						
							| 
									
										
										
										
											2012-03-04 13:28:18 +00:00
										 |  |  |     else | 
					
						
							| 
									
										
										
										
											2012-03-04 14:01:54 +00:00
										 |  |  |       assert_no_match expected, mail_body(mail) | 
					
						
							| 
									
										
										
										
											2012-03-04 13:28:18 +00:00
										 |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-03-04 14:01:54 +00:00
										 |  |  |   def mail_body(mail) | 
					
						
							| 
									
										
										
										
											2012-04-25 17:17:49 +00:00
										 |  |  |     mail.parts.first.body.encoded | 
					
						
							| 
									
										
										
										
											2012-03-04 14:01:54 +00:00
										 |  |  |   end | 
					
						
							| 
									
										
										
										
											2013-01-22 18:26:04 +00:00
										 |  |  | end | 
					
						
							| 
									
										
										
										
											2010-03-08 16:47:47 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-01-22 18:26:04 +00:00
										 |  |  | module Redmine | 
					
						
							|  |  |  |   module ApiTest | 
					
						
							|  |  |  |     # Base class for API tests | 
					
						
							|  |  |  |     class Base < ActionDispatch::IntegrationTest | 
					
						
							|  |  |  |       # Test that a request allows the three types of API authentication | 
					
						
							|  |  |  |       # | 
					
						
							|  |  |  |       # * HTTP Basic with username and password | 
					
						
							|  |  |  |       # * HTTP Basic with an api key for the username | 
					
						
							|  |  |  |       # * Key based with the key=X parameter | 
					
						
							|  |  |  |       # | 
					
						
							|  |  |  |       # @param [Symbol] http_method the HTTP method for request (:get, :post, :put, :delete) | 
					
						
							|  |  |  |       # @param [String] url the request url | 
					
						
							|  |  |  |       # @param [optional, Hash] parameters additional request parameters | 
					
						
							|  |  |  |       # @param [optional, Hash] options additional options | 
					
						
							|  |  |  |       # @option options [Symbol] :success_code Successful response code (:success) | 
					
						
							|  |  |  |       # @option options [Symbol] :failure_code Failure response code (:unauthorized) | 
					
						
							|  |  |  |       def self.should_allow_api_authentication(http_method, url, parameters={}, options={}) | 
					
						
							|  |  |  |         should_allow_http_basic_auth_with_username_and_password(http_method, url, parameters, options) | 
					
						
							|  |  |  |         should_allow_http_basic_auth_with_key(http_method, url, parameters, options) | 
					
						
							|  |  |  |         should_allow_key_based_auth(http_method, url, parameters, options) | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  |      | 
					
						
							|  |  |  |       # Test that a request allows the username and password for HTTP BASIC | 
					
						
							|  |  |  |       # | 
					
						
							|  |  |  |       # @param [Symbol] http_method the HTTP method for request (:get, :post, :put, :delete) | 
					
						
							|  |  |  |       # @param [String] url the request url | 
					
						
							|  |  |  |       # @param [optional, Hash] parameters additional request parameters | 
					
						
							|  |  |  |       # @param [optional, Hash] options additional options | 
					
						
							|  |  |  |       # @option options [Symbol] :success_code Successful response code (:success) | 
					
						
							|  |  |  |       # @option options [Symbol] :failure_code Failure response code (:unauthorized) | 
					
						
							|  |  |  |       def self.should_allow_http_basic_auth_with_username_and_password(http_method, url, parameters={}, options={}) | 
					
						
							|  |  |  |         success_code = options[:success_code] || :success | 
					
						
							|  |  |  |         failure_code = options[:failure_code] || :unauthorized | 
					
						
							|  |  |  |      | 
					
						
							|  |  |  |         context "should allow http basic auth using a username and password for #{http_method} #{url}" do | 
					
						
							|  |  |  |           context "with a valid HTTP authentication" do | 
					
						
							|  |  |  |             setup do | 
					
						
							|  |  |  |               @user = User.generate! do |user| | 
					
						
							|  |  |  |                 user.admin = true | 
					
						
							|  |  |  |                 user.password = 'my_password' | 
					
						
							|  |  |  |               end | 
					
						
							|  |  |  |               send(http_method, url, parameters, credentials(@user.login, 'my_password')) | 
					
						
							|  |  |  |             end | 
					
						
							|  |  |  |      | 
					
						
							|  |  |  |             should_respond_with success_code | 
					
						
							|  |  |  |             should_respond_with_content_type_based_on_url(url) | 
					
						
							|  |  |  |             should "login as the user" do | 
					
						
							|  |  |  |               assert_equal @user, User.current | 
					
						
							|  |  |  |             end | 
					
						
							|  |  |  |           end | 
					
						
							|  |  |  |      | 
					
						
							|  |  |  |           context "with an invalid HTTP authentication" do | 
					
						
							|  |  |  |             setup do | 
					
						
							|  |  |  |               @user = User.generate! | 
					
						
							|  |  |  |               send(http_method, url, parameters, credentials(@user.login, 'wrong_password')) | 
					
						
							|  |  |  |             end | 
					
						
							|  |  |  |      | 
					
						
							|  |  |  |             should_respond_with failure_code | 
					
						
							|  |  |  |             should_respond_with_content_type_based_on_url(url) | 
					
						
							|  |  |  |             should "not login as the user" do | 
					
						
							|  |  |  |               assert_equal User.anonymous, User.current | 
					
						
							|  |  |  |             end | 
					
						
							|  |  |  |           end | 
					
						
							|  |  |  |      | 
					
						
							|  |  |  |           context "without credentials" do | 
					
						
							|  |  |  |             setup do | 
					
						
							|  |  |  |               send(http_method, url, parameters) | 
					
						
							|  |  |  |             end | 
					
						
							|  |  |  |      | 
					
						
							|  |  |  |             should_respond_with failure_code | 
					
						
							|  |  |  |             should_respond_with_content_type_based_on_url(url) | 
					
						
							|  |  |  |             should "include_www_authenticate_header" do | 
					
						
							|  |  |  |               assert @controller.response.headers.has_key?('WWW-Authenticate') | 
					
						
							|  |  |  |             end | 
					
						
							| 
									
										
										
										
											2012-04-22 11:56:47 +00:00
										 |  |  |           end | 
					
						
							| 
									
										
										
										
											2010-11-02 15:52:06 +00:00
										 |  |  |         end | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2013-01-22 18:26:04 +00:00
										 |  |  |      | 
					
						
							|  |  |  |       # Test that a request allows the API key with HTTP BASIC | 
					
						
							|  |  |  |       # | 
					
						
							|  |  |  |       # @param [Symbol] http_method the HTTP method for request (:get, :post, :put, :delete) | 
					
						
							|  |  |  |       # @param [String] url the request url | 
					
						
							|  |  |  |       # @param [optional, Hash] parameters additional request parameters | 
					
						
							|  |  |  |       # @param [optional, Hash] options additional options | 
					
						
							|  |  |  |       # @option options [Symbol] :success_code Successful response code (:success) | 
					
						
							|  |  |  |       # @option options [Symbol] :failure_code Failure response code (:unauthorized) | 
					
						
							|  |  |  |       def self.should_allow_http_basic_auth_with_key(http_method, url, parameters={}, options={}) | 
					
						
							|  |  |  |         success_code = options[:success_code] || :success | 
					
						
							|  |  |  |         failure_code = options[:failure_code] || :unauthorized | 
					
						
							|  |  |  |      | 
					
						
							|  |  |  |         context "should allow http basic auth with a key for #{http_method} #{url}" do | 
					
						
							|  |  |  |           context "with a valid HTTP authentication using the API token" do | 
					
						
							|  |  |  |             setup do | 
					
						
							|  |  |  |               @user = User.generate! do |user| | 
					
						
							|  |  |  |                 user.admin = true | 
					
						
							|  |  |  |               end | 
					
						
							|  |  |  |               @token = Token.create!(:user => @user, :action => 'api') | 
					
						
							|  |  |  |               send(http_method, url, parameters, credentials(@token.value, 'X')) | 
					
						
							|  |  |  |             end | 
					
						
							|  |  |  |             should_respond_with success_code | 
					
						
							|  |  |  |             should_respond_with_content_type_based_on_url(url) | 
					
						
							|  |  |  |             should_be_a_valid_response_string_based_on_url(url) | 
					
						
							|  |  |  |             should "login as the user" do | 
					
						
							|  |  |  |               assert_equal @user, User.current | 
					
						
							|  |  |  |             end | 
					
						
							|  |  |  |           end | 
					
						
							|  |  |  |      | 
					
						
							|  |  |  |           context "with an invalid HTTP authentication" do | 
					
						
							|  |  |  |             setup do | 
					
						
							|  |  |  |               @user = User.generate! | 
					
						
							|  |  |  |               @token = Token.create!(:user => @user, :action => 'feeds') | 
					
						
							|  |  |  |               send(http_method, url, parameters, credentials(@token.value, 'X')) | 
					
						
							|  |  |  |             end | 
					
						
							|  |  |  |             should_respond_with failure_code | 
					
						
							|  |  |  |             should_respond_with_content_type_based_on_url(url) | 
					
						
							|  |  |  |             should "not login as the user" do | 
					
						
							|  |  |  |               assert_equal User.anonymous, User.current | 
					
						
							|  |  |  |             end | 
					
						
							|  |  |  |           end | 
					
						
							| 
									
										
										
										
											2010-11-02 15:52:06 +00:00
										 |  |  |         end | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2013-01-22 18:26:04 +00:00
										 |  |  |      | 
					
						
							|  |  |  |       # Test that a request allows full key authentication | 
					
						
							|  |  |  |       # | 
					
						
							|  |  |  |       # @param [Symbol] http_method the HTTP method for request (:get, :post, :put, :delete) | 
					
						
							|  |  |  |       # @param [String] url the request url, without the key=ZXY parameter | 
					
						
							|  |  |  |       # @param [optional, Hash] parameters additional request parameters | 
					
						
							|  |  |  |       # @param [optional, Hash] options additional options | 
					
						
							|  |  |  |       # @option options [Symbol] :success_code Successful response code (:success) | 
					
						
							|  |  |  |       # @option options [Symbol] :failure_code Failure response code (:unauthorized) | 
					
						
							|  |  |  |       def self.should_allow_key_based_auth(http_method, url, parameters={}, options={}) | 
					
						
							|  |  |  |         success_code = options[:success_code] || :success | 
					
						
							|  |  |  |         failure_code = options[:failure_code] || :unauthorized | 
					
						
							|  |  |  |      | 
					
						
							|  |  |  |         context "should allow key based auth using key=X for #{http_method} #{url}" do | 
					
						
							|  |  |  |           context "with a valid api token" do | 
					
						
							|  |  |  |             setup do | 
					
						
							|  |  |  |               @user = User.generate! do |user| | 
					
						
							|  |  |  |                 user.admin = true | 
					
						
							|  |  |  |               end | 
					
						
							|  |  |  |               @token = Token.create!(:user => @user, :action => 'api') | 
					
						
							|  |  |  |               # Simple url parse to add on ?key= or &key= | 
					
						
							|  |  |  |               request_url = if url.match(/\?/) | 
					
						
							|  |  |  |                               url + "&key=#{@token.value}" | 
					
						
							|  |  |  |                             else | 
					
						
							|  |  |  |                               url + "?key=#{@token.value}" | 
					
						
							|  |  |  |                             end | 
					
						
							|  |  |  |               send(http_method, request_url, parameters) | 
					
						
							|  |  |  |             end | 
					
						
							|  |  |  |             should_respond_with success_code | 
					
						
							|  |  |  |             should_respond_with_content_type_based_on_url(url) | 
					
						
							|  |  |  |             should_be_a_valid_response_string_based_on_url(url) | 
					
						
							|  |  |  |             should "login as the user" do | 
					
						
							|  |  |  |               assert_equal @user, User.current | 
					
						
							|  |  |  |             end | 
					
						
							|  |  |  |           end | 
					
						
							|  |  |  |      | 
					
						
							|  |  |  |           context "with an invalid api token" do | 
					
						
							|  |  |  |             setup do | 
					
						
							|  |  |  |               @user = User.generate! do |user| | 
					
						
							|  |  |  |                 user.admin = true | 
					
						
							|  |  |  |               end | 
					
						
							|  |  |  |               @token = Token.create!(:user => @user, :action => 'feeds') | 
					
						
							|  |  |  |               # Simple url parse to add on ?key= or &key= | 
					
						
							|  |  |  |               request_url = if url.match(/\?/) | 
					
						
							|  |  |  |                               url + "&key=#{@token.value}" | 
					
						
							|  |  |  |                             else | 
					
						
							|  |  |  |                               url + "?key=#{@token.value}" | 
					
						
							|  |  |  |                             end | 
					
						
							|  |  |  |               send(http_method, request_url, parameters) | 
					
						
							|  |  |  |             end | 
					
						
							|  |  |  |             should_respond_with failure_code | 
					
						
							|  |  |  |             should_respond_with_content_type_based_on_url(url) | 
					
						
							|  |  |  |             should "not login as the user" do | 
					
						
							|  |  |  |               assert_equal User.anonymous, User.current | 
					
						
							|  |  |  |             end | 
					
						
							|  |  |  |           end | 
					
						
							| 
									
										
										
										
											2010-11-02 15:52:06 +00:00
										 |  |  |         end | 
					
						
							| 
									
										
										
										
											2013-01-22 18:26:04 +00:00
										 |  |  |      | 
					
						
							|  |  |  |         context "should allow key based auth using X-Redmine-API-Key header for #{http_method} #{url}" do | 
					
						
							|  |  |  |           setup do | 
					
						
							|  |  |  |             @user = User.generate! do |user| | 
					
						
							|  |  |  |               user.admin = true | 
					
						
							|  |  |  |             end | 
					
						
							|  |  |  |             @token = Token.create!(:user => @user, :action => 'api') | 
					
						
							|  |  |  |             send(http_method, url, parameters, {'X-Redmine-API-Key' => @token.value.to_s}) | 
					
						
							|  |  |  |           end | 
					
						
							|  |  |  |           should_respond_with success_code | 
					
						
							|  |  |  |           should_respond_with_content_type_based_on_url(url) | 
					
						
							|  |  |  |           should_be_a_valid_response_string_based_on_url(url) | 
					
						
							|  |  |  |           should "login as the user" do | 
					
						
							|  |  |  |             assert_equal @user, User.current | 
					
						
							|  |  |  |           end | 
					
						
							| 
									
										
										
										
											2010-11-02 15:52:06 +00:00
										 |  |  |         end | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2013-01-22 18:26:04 +00:00
										 |  |  |      | 
					
						
							|  |  |  |       # Uses should_respond_with_content_type based on what's in the url: | 
					
						
							|  |  |  |       # | 
					
						
							|  |  |  |       # '/project/issues.xml' => should_respond_with_content_type :xml | 
					
						
							|  |  |  |       # '/project/issues.json' => should_respond_with_content_type :json | 
					
						
							|  |  |  |       # | 
					
						
							|  |  |  |       # @param [String] url Request | 
					
						
							|  |  |  |       def self.should_respond_with_content_type_based_on_url(url) | 
					
						
							|  |  |  |         case | 
					
						
							|  |  |  |         when url.match(/xml/i) | 
					
						
							|  |  |  |           should "respond with XML" do | 
					
						
							|  |  |  |             assert_equal 'application/xml', @response.content_type | 
					
						
							| 
									
										
										
										
											2012-04-22 11:56:47 +00:00
										 |  |  |           end | 
					
						
							| 
									
										
										
										
											2013-01-22 18:26:04 +00:00
										 |  |  |         when url.match(/json/i) | 
					
						
							|  |  |  |           should "respond with JSON" do | 
					
						
							|  |  |  |             assert_equal 'application/json', @response.content_type | 
					
						
							|  |  |  |           end | 
					
						
							|  |  |  |         else | 
					
						
							|  |  |  |           raise "Unknown content type for should_respond_with_content_type_based_on_url: #{url}" | 
					
						
							| 
									
										
										
										
											2010-11-03 16:48:23 +00:00
										 |  |  |         end | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2013-01-22 18:26:04 +00:00
										 |  |  |      | 
					
						
							|  |  |  |       # Uses the url to assert which format the response should be in | 
					
						
							|  |  |  |       # | 
					
						
							|  |  |  |       # '/project/issues.xml' => should_be_a_valid_xml_string | 
					
						
							|  |  |  |       # '/project/issues.json' => should_be_a_valid_json_string | 
					
						
							|  |  |  |       # | 
					
						
							|  |  |  |       # @param [String] url Request | 
					
						
							|  |  |  |       def self.should_be_a_valid_response_string_based_on_url(url) | 
					
						
							|  |  |  |         case | 
					
						
							|  |  |  |         when url.match(/xml/i) | 
					
						
							|  |  |  |           should_be_a_valid_xml_string | 
					
						
							|  |  |  |         when url.match(/json/i) | 
					
						
							|  |  |  |           should_be_a_valid_json_string | 
					
						
							|  |  |  |         else | 
					
						
							|  |  |  |           raise "Unknown content type for should_be_a_valid_response_based_on_url: #{url}" | 
					
						
							| 
									
										
										
										
											2010-11-03 16:48:23 +00:00
										 |  |  |         end | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2013-01-22 18:26:04 +00:00
										 |  |  |      | 
					
						
							|  |  |  |       # Checks that the response is a valid JSON string | 
					
						
							|  |  |  |       def self.should_be_a_valid_json_string | 
					
						
							|  |  |  |         should "be a valid JSON string (or empty)" do | 
					
						
							|  |  |  |           assert(response.body.blank? || ActiveSupport::JSON.decode(response.body)) | 
					
						
							| 
									
										
										
										
											2010-11-01 15:45:03 +00:00
										 |  |  |         end | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2013-01-22 18:26:04 +00:00
										 |  |  |      | 
					
						
							|  |  |  |       # Checks that the response is a valid XML string | 
					
						
							|  |  |  |       def self.should_be_a_valid_xml_string | 
					
						
							|  |  |  |         should "be a valid XML string" do | 
					
						
							|  |  |  |           assert REXML::Document.new(response.body) | 
					
						
							| 
									
										
										
										
											2010-11-01 15:45:03 +00:00
										 |  |  |         end | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2013-01-22 18:26:04 +00:00
										 |  |  |      | 
					
						
							|  |  |  |       def self.should_respond_with(status) | 
					
						
							|  |  |  |         should "respond with #{status}" do | 
					
						
							|  |  |  |           assert_response status | 
					
						
							| 
									
										
										
										
											2012-04-22 11:56:47 +00:00
										 |  |  |         end | 
					
						
							| 
									
										
										
										
											2010-12-23 14:49:14 +00:00
										 |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							| 
									
										
										
										
											2010-11-01 15:45:03 +00:00
										 |  |  |   end | 
					
						
							| 
									
										
										
										
											2010-11-01 15:26:05 +00:00
										 |  |  | end | 
					
						
							| 
									
										
										
										
											2012-12-02 18:07:35 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | # URL helpers do not work with config.threadsafe! | 
					
						
							|  |  |  | # https://github.com/rspec/rspec-rails/issues/476#issuecomment-4705454 | 
					
						
							|  |  |  | ActionView::TestCase::TestController.instance_eval do | 
					
						
							|  |  |  |   helper Rails.application.routes.url_helpers | 
					
						
							|  |  |  | end | 
					
						
							|  |  |  | ActionView::TestCase::TestController.class_eval do | 
					
						
							|  |  |  |   def _routes | 
					
						
							|  |  |  |     Rails.application.routes | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | end |