| 
									
										
										
										
											2011-03-20 18:25:38 +00:00
										 |  |  | # Redmine - project management software | 
					
						
							| 
									
										
										
										
											2016-03-13 10:30:10 +00:00
										 |  |  | # Copyright (C) 2006-2016  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. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-02 19:59:38 +00:00
										 |  |  | if ENV["COVERAGE"] | 
					
						
							|  |  |  |   require 'simplecov' | 
					
						
							|  |  |  |   require File.expand_path(File.dirname(__FILE__) + "/coverage/html_formatter") | 
					
						
							|  |  |  |   SimpleCov.formatter = Redmine::Coverage::HtmlFormatter | 
					
						
							|  |  |  |   SimpleCov.start 'rails' | 
					
						
							|  |  |  | end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-22 18:10:26 +00:00
										 |  |  | $redmine_test_ldap_server = ENV['REDMINE_TEST_LDAP_SERVER'] || '127.0.0.1' | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-10-28 18:29:44 +00:00
										 |  |  | require 'net/ldap' | 
					
						
							| 
									
										
										
										
											2015-01-17 17:02:55 +00:00
										 |  |  | require 'mocha/setup' | 
					
						
							| 
									
										
										
										
											2017-06-01 18:25:23 +00:00
										 |  |  | require 'fileutils' | 
					
						
							| 
									
										
										
										
											2014-01-24 16:26:50 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-19 19:42:49 +00:00
										 |  |  | Redmine::SudoMode.disable! | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-01 18:25:23 +00:00
										 |  |  | $redmine_tmp_attachments_directory = "#{Rails.root}/tmp/test/attachments" | 
					
						
							|  |  |  | FileUtils.mkdir_p $redmine_tmp_attachments_directory | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-10-22 17:37:16 +00:00
										 |  |  | class ActionView::TestCase | 
					
						
							|  |  |  |   helper :application | 
					
						
							|  |  |  |   include ApplicationHelper | 
					
						
							|  |  |  | end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-01-22 18:31:25 +00:00
										 |  |  |   self.use_transactional_fixtures = true | 
					
						
							| 
									
										
										
										
											2006-06-28 18:11:03 +00:00
										 |  |  |   self.use_instantiated_fixtures  = false | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-03 12:48:58 +00:00
										 |  |  |   def mock_file(options=nil) | 
					
						
							|  |  |  |     options ||= { | 
					
						
							|  |  |  |         :original_filename => 'a_file.png', | 
					
						
							|  |  |  |         :content_type => 'image/png', | 
					
						
							|  |  |  |         :size => 32
 | 
					
						
							|  |  |  |       } | 
					
						
							| 
									
										
										
										
											2010-08-19 22:24:42 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-03 12:48:58 +00:00
										 |  |  |     Redmine::MockFile.new(options) | 
					
						
							| 
									
										
										
										
											2010-08-19 22:24:42 +00:00
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-11-23 05:30:53 +00:00
										 |  |  |   def mock_file_with_options(options={}) | 
					
						
							| 
									
										
										
										
											2017-04-03 12:48:58 +00:00
										 |  |  |     mock_file(options) | 
					
						
							| 
									
										
										
										
											2011-11-23 05:30:53 +00:00
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-06-09 18:40:59 +00:00
										 |  |  |   # Use a temporary directory for attachment related tests | 
					
						
							|  |  |  |   def set_tmp_attachments_directory | 
					
						
							| 
									
										
										
										
											2017-06-01 18:25:23 +00:00
										 |  |  |     Attachment.storage_path = $redmine_tmp_attachments_directory | 
					
						
							| 
									
										
										
										
											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) | 
					
						
							| 
									
										
										
										
											2013-02-15 08:28:34 +00:00
										 |  |  |     saved_settings = options.keys.inject({}) do |h, k| | 
					
						
							|  |  |  |       h[k] = case Setting[k] | 
					
						
							|  |  |  |         when Symbol, false, true, nil | 
					
						
							|  |  |  |           Setting[k] | 
					
						
							|  |  |  |         else | 
					
						
							|  |  |  |           Setting[k].dup | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  |       h | 
					
						
							|  |  |  |     end | 
					
						
							| 
									
										
										
										
											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 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-15 09:18:01 +00:00
										 |  |  |   def with_locale(locale, &block) | 
					
						
							|  |  |  |     saved_localed = ::I18n.locale | 
					
						
							|  |  |  |     ::I18n.locale = locale | 
					
						
							|  |  |  |     yield | 
					
						
							|  |  |  |   ensure | 
					
						
							|  |  |  |     ::I18n.locale = saved_localed | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-02-16 16:40:50 +00:00
										 |  |  |   def self.ldap_configured? | 
					
						
							| 
									
										
										
										
											2016-01-22 18:10:26 +00:00
										 |  |  |     @test_ldap = Net::LDAP.new(:host => $redmine_test_ldap_server, :port => 389) | 
					
						
							| 
									
										
										
										
											2010-02-16 16:40:50 +00:00
										 |  |  |     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 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-26 11:26:24 +00:00
										 |  |  |   def convert_installed? | 
					
						
							|  |  |  |     self.class.convert_installed? | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-05-01 08:32:21 +00:00
										 |  |  |   # Returns the path to the test +vendor+ repository | 
					
						
							|  |  |  |   def self.repository_path(vendor) | 
					
						
							| 
									
										
										
										
											2014-10-22 17:37:16 +00:00
										 |  |  |     path = Rails.root.join("tmp/test/#{vendor.downcase}_repository").to_s | 
					
						
							|  |  |  |     # Unlike ruby, JRuby returns Rails.root with backslashes under Windows | 
					
						
							|  |  |  |     path.tr("\\", "/") | 
					
						
							| 
									
										
										
										
											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 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-14 16:31:11 +00:00
										 |  |  |   def sqlite? | 
					
						
							|  |  |  |     ActiveRecord::Base.connection.adapter_name =~ /sqlite/i | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-15 19:22:13 +00:00
										 |  |  |   def mysql? | 
					
						
							|  |  |  |     ActiveRecord::Base.connection.adapter_name =~ /mysql/i | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-08 15:13:57 +00:00
										 |  |  |   def postgresql? | 
					
						
							|  |  |  |     ActiveRecord::Base.connection.adapter_name =~ /postgresql/i | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-08 16:01:20 +00:00
										 |  |  |   def quoted_date(date) | 
					
						
							|  |  |  |     date = Date.parse(date) if date.is_a?(String) | 
					
						
							|  |  |  |     ActiveRecord::Base.connection.quoted_date(date) | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-05 13:52:02 +00:00
										 |  |  |   # Asserts that a new record for the given class is created | 
					
						
							|  |  |  |   # and returns it | 
					
						
							| 
									
										
										
										
											2015-06-29 16:06:37 +00:00
										 |  |  |   def new_record(klass, &block) | 
					
						
							| 
									
										
										
										
											2015-08-14 08:20:32 +00:00
										 |  |  |     new_records(klass, 1, &block).first | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   # Asserts that count new records for the given class are created | 
					
						
							|  |  |  |   # and returns them as an array order by object id | 
					
						
							|  |  |  |   def new_records(klass, count, &block) | 
					
						
							|  |  |  |     assert_difference "#{klass}.count", count do | 
					
						
							| 
									
										
										
										
											2015-06-29 16:06:37 +00:00
										 |  |  |       yield | 
					
						
							|  |  |  |     end | 
					
						
							| 
									
										
										
										
											2015-08-14 08:20:32 +00:00
										 |  |  |     klass.order(:id => :desc).limit(count).to_a.reverse | 
					
						
							| 
									
										
										
										
											2015-06-29 16:06:37 +00:00
										 |  |  |   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 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-22 09:38:21 +00:00
										 |  |  |   def assert_select_error(arg) | 
					
						
							|  |  |  |     assert_select '#errorExplanation', :text => arg | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-13 09:20:11 +00:00
										 |  |  |   def assert_not_include(expected, s, message=nil) | 
					
						
							|  |  |  |     assert !s.include?(expected), (message || "\"#{expected}\" found in \"#{s}\"") | 
					
						
							| 
									
										
										
										
											2012-03-04 13:28:18 +00:00
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-10-20 14:49:32 +00:00
										 |  |  |   def assert_select_in(text, *args, &block) | 
					
						
							| 
									
										
										
										
											2015-01-17 17:02:55 +00:00
										 |  |  |     d = Nokogiri::HTML(CGI::unescapeHTML(String.new(text))).root | 
					
						
							| 
									
										
										
										
											2012-10-20 14:49:32 +00:00
										 |  |  |     assert_select(d, *args, &block) | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-17 17:02:55 +00:00
										 |  |  |   def assert_select_email(*args, &block) | 
					
						
							|  |  |  |     email = ActionMailer::Base.deliveries.last | 
					
						
							|  |  |  |     assert_not_nil email | 
					
						
							|  |  |  |     html_body = email.parts.detect {|part| part.content_type.include?('text/html')}.try(&:body) | 
					
						
							|  |  |  |     assert_not_nil html_body | 
					
						
							|  |  |  |     assert_select_in html_body.encoded, *args, &block | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-13 09:20:11 +00:00
										 |  |  |   def assert_mail_body_match(expected, mail, message=nil) | 
					
						
							| 
									
										
										
										
											2012-03-04 13:28:18 +00:00
										 |  |  |     if expected.is_a?(String) | 
					
						
							| 
									
										
										
										
											2013-07-13 09:20:11 +00:00
										 |  |  |       assert_include expected, mail_body(mail), message | 
					
						
							| 
									
										
										
										
											2012-03-04 13:28:18 +00:00
										 |  |  |     else | 
					
						
							| 
									
										
										
										
											2013-07-13 09:20:11 +00:00
										 |  |  |       assert_match expected, mail_body(mail), message | 
					
						
							| 
									
										
										
										
											2012-03-04 13:28:18 +00:00
										 |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-13 09:20:11 +00:00
										 |  |  |   def assert_mail_body_no_match(expected, mail, message=nil) | 
					
						
							| 
									
										
										
										
											2012-03-04 13:28:18 +00:00
										 |  |  |     if expected.is_a?(String) | 
					
						
							| 
									
										
										
										
											2013-07-13 09:20:11 +00:00
										 |  |  |       assert_not_include expected, mail_body(mail), message | 
					
						
							| 
									
										
										
										
											2012-03-04 13:28:18 +00:00
										 |  |  |     else | 
					
						
							| 
									
										
										
										
											2013-07-13 09:20:11 +00:00
										 |  |  |       assert_no_match expected, mail_body(mail), message | 
					
						
							| 
									
										
										
										
											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 | 
					
						
							| 
									
										
										
										
											2014-01-24 16:26:50 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-08 06:20:32 +00:00
										 |  |  |   # Returns the lft value for a new root issue | 
					
						
							| 
									
										
										
										
											2014-01-24 16:26:50 +00:00
										 |  |  |   def new_issue_lft | 
					
						
							| 
									
										
										
										
											2015-01-07 20:19:49 +00:00
										 |  |  |     1
 | 
					
						
							| 
									
										
										
										
											2014-01-24 16:26:50 +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 | 
					
						
							| 
									
										
										
										
											2017-04-03 12:48:58 +00:00
										 |  |  |   class MockFile | 
					
						
							|  |  |  |     attr_reader :size, :original_filename, :content_type | 
					
						
							|  |  |  |    | 
					
						
							|  |  |  |     def initialize(options={}) | 
					
						
							|  |  |  |       @size = options[:size] || 32
 | 
					
						
							|  |  |  |       @original_filename = options[:original_filename] || options[:filename] | 
					
						
							|  |  |  |       @content_type = options[:content_type] | 
					
						
							|  |  |  |       @content = options[:content] || 'x'*size | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |    | 
					
						
							|  |  |  |     def read(*args) | 
					
						
							|  |  |  |       if @eof | 
					
						
							|  |  |  |         false | 
					
						
							|  |  |  |       else | 
					
						
							|  |  |  |         @eof = true | 
					
						
							|  |  |  |         @content | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-16 10:54:33 +00:00
										 |  |  |   class RoutingTest < ActionDispatch::IntegrationTest | 
					
						
							|  |  |  |     def should_route(arg) | 
					
						
							|  |  |  |       arg = arg.dup | 
					
						
							|  |  |  |       request = arg.keys.detect {|key| key.is_a?(String)} | 
					
						
							|  |  |  |       raise ArgumentError unless request | 
					
						
							|  |  |  |       options = arg.slice!(request) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       raise ArgumentError unless request =~ /\A(GET|POST|PUT|PATCH|DELETE)\s+(.+)\z/ | 
					
						
							|  |  |  |       method, path = $1.downcase.to_sym, $2 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       raise ArgumentError unless arg.values.first =~ /\A(.+)#(.+)\z/ | 
					
						
							|  |  |  |       controller, action = $1, $2 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       assert_routing( | 
					
						
							|  |  |  |         {:method => method, :path => path}, | 
					
						
							|  |  |  |         options.merge(:controller => controller, :action => action) | 
					
						
							|  |  |  |       ) | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-16 11:36:57 +00:00
										 |  |  |   class HelperTest < ActionView::TestCase | 
					
						
							| 
									
										
										
										
											2017-01-19 23:32:04 +00:00
										 |  |  |     include Redmine::I18n | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-19 23:22:26 +00:00
										 |  |  |     def setup | 
					
						
							|  |  |  |       super | 
					
						
							|  |  |  |       User.current = nil | 
					
						
							| 
									
										
										
										
											2017-01-19 23:32:04 +00:00
										 |  |  |       ::I18n.locale = 'en' | 
					
						
							| 
									
										
										
										
											2017-01-19 23:22:26 +00:00
										 |  |  |     end | 
					
						
							| 
									
										
										
										
											2016-07-16 11:36:57 +00:00
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-14 09:35:49 +00:00
										 |  |  |   class ControllerTest < ActionController::TestCase | 
					
						
							| 
									
										
										
										
											2016-07-18 21:26:30 +00:00
										 |  |  |     # Returns the issues that are displayed in the list in the same order | 
					
						
							|  |  |  |     def issues_in_list | 
					
						
							|  |  |  |       ids = css_select('tr.issue td.id').map(&:text).map(&:to_i) | 
					
						
							|  |  |  |       Issue.where(:id => ids).sort_by {|issue| ids.index(issue.id)} | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |    | 
					
						
							|  |  |  |     # Return the columns that are displayed in the list | 
					
						
							|  |  |  |     def columns_in_issues_list | 
					
						
							|  |  |  |       css_select('table.issues thead th:not(.checkbox)').map(&:text) | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |    | 
					
						
							|  |  |  |     # Verifies that the query filters match the expected filters | 
					
						
							|  |  |  |     def assert_query_filters(expected_filters) | 
					
						
							|  |  |  |       response.body =~ /initFilters\(\);\s*((addFilter\(.+\);\s*)*)/ | 
					
						
							|  |  |  |       filter_init = $1.to_s | 
					
						
							|  |  |  |    | 
					
						
							|  |  |  |       expected_filters.each do |field, operator, values| | 
					
						
							|  |  |  |         s = "addFilter(#{field.to_json}, #{operator.to_json}, #{Array(values).to_json});" | 
					
						
							|  |  |  |         assert_include s, filter_init | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  |       assert_equal expected_filters.size, filter_init.scan("addFilter").size, "filters counts don't match" | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-31 17:32:34 +00:00
										 |  |  |     def process(action, http_method = 'GET', *args) | 
					
						
							|  |  |  |       parameters, session, flash = *args | 
					
						
							|  |  |  |       if args.size == 1 && parameters[:xhr] == true | 
					
						
							|  |  |  |         xhr http_method.downcase.to_sym, action, parameters.except(:xhr) | 
					
						
							|  |  |  |       elsif parameters && (parameters.key?(:params) || parameters.key?(:session) || parameters.key?(:flash)) | 
					
						
							|  |  |  |         super action, http_method, parameters[:params], parameters[:session], parameters[:flash] | 
					
						
							| 
									
										
										
										
											2016-07-16 06:19:52 +00:00
										 |  |  |       else | 
					
						
							|  |  |  |         super | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							| 
									
										
										
										
											2016-07-14 09:35:49 +00:00
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-30 15:16:43 +00:00
										 |  |  |   class IntegrationTest < ActionDispatch::IntegrationTest | 
					
						
							|  |  |  |     def log_user(login, password) | 
					
						
							|  |  |  |       User.anonymous | 
					
						
							|  |  |  |       get "/login" | 
					
						
							| 
									
										
										
										
											2016-12-11 15:22:29 +00:00
										 |  |  |       assert_nil session[:user_id] | 
					
						
							| 
									
										
										
										
											2014-11-30 15:16:43 +00:00
										 |  |  |       assert_response :success | 
					
						
							| 
									
										
										
										
											2016-07-21 17:50:00 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-30 15:16:43 +00:00
										 |  |  |       post "/login", :username => login, :password => password | 
					
						
							|  |  |  |       assert_equal login, User.find(session[:user_id]).login | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-01 18:17:27 +00:00
										 |  |  |     %w(get post patch put delete head).each do |http_method| | 
					
						
							|  |  |  |       class_eval %Q"
 | 
					
						
							|  |  |  |         def #{http_method}(path, parameters = nil, headers_or_env = nil) | 
					
						
							|  |  |  |           if headers_or_env.nil? && parameters.is_a?(Hash) && (parameters.key?(:params) || parameters.key?(:headers)) | 
					
						
							|  |  |  |             super path, parameters[:params], parameters[:headers] | 
					
						
							|  |  |  |           else | 
					
						
							|  |  |  |             super | 
					
						
							|  |  |  |           end | 
					
						
							|  |  |  |         end"
 | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-30 15:16:43 +00:00
										 |  |  |     def credentials(user, password=nil) | 
					
						
							|  |  |  |       {'HTTP_AUTHORIZATION' => ActionController::HttpAuthentication::Basic.encode_credentials(user, password || user)} | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-01-22 18:26:04 +00:00
										 |  |  |   module ApiTest | 
					
						
							| 
									
										
										
										
											2014-11-16 10:54:33 +00:00
										 |  |  |     API_FORMATS = %w(json xml).freeze | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-01-22 18:26:04 +00:00
										 |  |  |     # Base class for API tests | 
					
						
							| 
									
										
										
										
											2014-11-30 15:16:43 +00:00
										 |  |  |     class Base < Redmine::IntegrationTest | 
					
						
							| 
									
										
										
										
											2014-11-30 15:10:56 +00:00
										 |  |  |       def setup | 
					
						
							|  |  |  |         Setting.rest_api_enabled = '1' | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       def teardown | 
					
						
							|  |  |  |         Setting.rest_api_enabled = '0' | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2014-12-22 13:41:51 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |       # Uploads content using the XML API and returns the attachment token | 
					
						
							|  |  |  |       def xml_upload(content, credentials) | 
					
						
							|  |  |  |         upload('xml', content, credentials) | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       # Uploads content using the JSON API and returns the attachment token | 
					
						
							|  |  |  |       def json_upload(content, credentials) | 
					
						
							|  |  |  |         upload('json', content, credentials) | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       def upload(format, content, credentials) | 
					
						
							|  |  |  |         set_tmp_attachments_directory | 
					
						
							|  |  |  |         assert_difference 'Attachment.count' do | 
					
						
							|  |  |  |           post "/uploads.#{format}", content, {"CONTENT_TYPE" => 'application/octet-stream'}.merge(credentials) | 
					
						
							|  |  |  |           assert_response :created | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  |         data = response_data | 
					
						
							|  |  |  |         assert_kind_of Hash, data['upload'] | 
					
						
							|  |  |  |         token = data['upload']['token'] | 
					
						
							|  |  |  |         assert_not_nil token | 
					
						
							|  |  |  |         token | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       # Parses the response body based on its content type | 
					
						
							|  |  |  |       def response_data | 
					
						
							|  |  |  |         unless response.content_type.to_s =~ /^application\/(.+)/ | 
					
						
							|  |  |  |           raise "Unexpected response type: #{response.content_type}" | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  |         format = $1 | 
					
						
							|  |  |  |         case format | 
					
						
							|  |  |  |         when 'xml' | 
					
						
							|  |  |  |           Hash.from_xml(response.body) | 
					
						
							|  |  |  |         when 'json' | 
					
						
							|  |  |  |           ActiveSupport::JSON.decode(response.body) | 
					
						
							|  |  |  |         else | 
					
						
							|  |  |  |           raise "Unknown response format: #{format}" | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2010-12-23 14:49:14 +00:00
										 |  |  |     end | 
					
						
							| 
									
										
										
										
											2014-11-16 10:54:33 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     class Routing < Redmine::RoutingTest | 
					
						
							|  |  |  |       def should_route(arg) | 
					
						
							|  |  |  |         arg = arg.dup | 
					
						
							|  |  |  |         request = arg.keys.detect {|key| key.is_a?(String)} | 
					
						
							|  |  |  |         raise ArgumentError unless request | 
					
						
							|  |  |  |         options = arg.slice!(request) | 
					
						
							|  |  |  |    | 
					
						
							|  |  |  |         API_FORMATS.each do |format| | 
					
						
							|  |  |  |           format_request = request.sub /$/, ".#{format}" | 
					
						
							|  |  |  |           super options.merge(format_request => arg[request], :format => format) | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							| 
									
										
										
										
											2010-11-01 15:45:03 +00:00
										 |  |  |   end | 
					
						
							| 
									
										
										
										
											2010-11-01 15:26:05 +00:00
										 |  |  | end |