Chris@0: # redMine - project management software Chris@0: # Copyright (C) 2006 Jean-Philippe Lang Chris@0: # Chris@0: # This program is free software; you can redistribute it and/or Chris@0: # modify it under the terms of the GNU General Public License Chris@0: # as published by the Free Software Foundation; either version 2 Chris@0: # of the License, or (at your option) any later version. Chris@0: # Chris@0: # This program is distributed in the hope that it will be useful, Chris@0: # but WITHOUT ANY WARRANTY; without even the implied warranty of Chris@0: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Chris@0: # GNU General Public License for more details. Chris@0: # Chris@0: # You should have received a copy of the GNU General Public License Chris@0: # along with this program; if not, write to the Free Software Chris@0: # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. Chris@0: Chris@0: ENV["RAILS_ENV"] = "test" Chris@0: require File.expand_path(File.dirname(__FILE__) + "/../config/environment") Chris@0: require 'test_help' Chris@0: require File.expand_path(File.dirname(__FILE__) + '/helper_testcase') Chris@0: require File.join(RAILS_ROOT,'test', 'mocks', 'open_id_authentication_mock.rb') Chris@0: Chris@0: require File.expand_path(File.dirname(__FILE__) + '/object_daddy_helpers') Chris@0: include ObjectDaddyHelpers Chris@0: Chris@0: class ActiveSupport::TestCase Chris@0: # Transactional fixtures accelerate your tests by wrapping each test method Chris@0: # in a transaction that's rolled back on completion. This ensures that the Chris@0: # test database remains unchanged so your fixtures don't have to be reloaded Chris@0: # between every test method. Fewer database queries means faster tests. Chris@0: # Chris@0: # Read Mike Clark's excellent walkthrough at Chris@0: # http://clarkware.com/cgi/blosxom/2005/10/24#Rails10FastTesting Chris@0: # Chris@0: # Every Active Record database supports transactions except MyISAM tables Chris@0: # in MySQL. Turn off transactional fixtures in this case; however, if you Chris@0: # don't care one way or the other, switching from MyISAM to InnoDB tables Chris@0: # is recommended. Chris@0: self.use_transactional_fixtures = true Chris@0: Chris@0: # Instantiated fixtures are slow, but give you @david where otherwise you Chris@0: # would need people(:david). If you don't want to migrate your existing Chris@0: # test cases which use the @david style and don't mind the speed hit (each Chris@0: # instantiated fixtures translates to a database query per test method), Chris@0: # then set this back to true. Chris@0: self.use_instantiated_fixtures = false Chris@0: Chris@0: # Add more helper methods to be used by all tests here... Chris@0: Chris@0: def log_user(login, password) Chris@0: User.anonymous Chris@0: get "/login" Chris@0: assert_equal nil, session[:user_id] Chris@0: assert_response :success Chris@0: assert_template "account/login" Chris@0: post "/login", :username => login, :password => password Chris@0: assert_equal login, User.find(session[:user_id]).login Chris@0: end Chris@0: Chris@0: def uploaded_test_file(name, mime) Chris@0: ActionController::TestUploadedFile.new(ActiveSupport::TestCase.fixture_path + "/files/#{name}", mime) Chris@0: end Chris@0: Chris@0: # Mock out a file Chris@14: def self.mock_file Chris@0: file = 'a_file.png' Chris@0: file.stubs(:size).returns(32) Chris@0: file.stubs(:original_filename).returns('a_file.png') Chris@0: file.stubs(:content_type).returns('image/png') Chris@0: file.stubs(:read).returns(false) Chris@0: file Chris@0: end Chris@14: Chris@14: def mock_file Chris@14: self.class.mock_file Chris@14: end Chris@14: Chris@0: # Use a temporary directory for attachment related tests Chris@0: def set_tmp_attachments_directory Chris@0: Dir.mkdir "#{RAILS_ROOT}/tmp/test" unless File.directory?("#{RAILS_ROOT}/tmp/test") Chris@0: Dir.mkdir "#{RAILS_ROOT}/tmp/test/attachments" unless File.directory?("#{RAILS_ROOT}/tmp/test/attachments") Chris@0: Attachment.storage_path = "#{RAILS_ROOT}/tmp/test/attachments" Chris@0: end Chris@0: Chris@0: def with_settings(options, &block) Chris@0: saved_settings = options.keys.inject({}) {|h, k| h[k] = Setting[k].dup; h} Chris@0: options.each {|k, v| Setting[k] = v} Chris@0: yield Chris@0: saved_settings.each {|k, v| Setting[k] = v} Chris@0: end Chris@0: Chris@14: def change_user_password(login, new_password) Chris@14: user = User.first(:conditions => {:login => login}) Chris@14: user.password, user.password_confirmation = new_password, new_password Chris@14: user.save! Chris@14: end Chris@14: Chris@0: def self.ldap_configured? Chris@0: @test_ldap = Net::LDAP.new(:host => '127.0.0.1', :port => 389) Chris@0: return @test_ldap.bind Chris@0: rescue Exception => e Chris@0: # LDAP is not listening Chris@0: return nil Chris@0: end Chris@0: Chris@0: # Returns the path to the test +vendor+ repository Chris@0: def self.repository_path(vendor) Chris@0: File.join(RAILS_ROOT.gsub(%r{config\/\.\.}, ''), "/tmp/test/#{vendor.downcase}_repository") Chris@0: end Chris@0: Chris@0: # Returns true if the +vendor+ test repository is configured Chris@0: def self.repository_configured?(vendor) Chris@0: File.directory?(repository_path(vendor)) Chris@0: end Chris@0: Chris@0: # Shoulda macros Chris@0: def self.should_render_404 Chris@0: should_respond_with :not_found Chris@0: should_render_template 'common/404' Chris@0: end Chris@0: Chris@0: def self.should_have_before_filter(expected_method, options = {}) Chris@0: should_have_filter('before', expected_method, options) Chris@0: end Chris@0: Chris@0: def self.should_have_after_filter(expected_method, options = {}) Chris@0: should_have_filter('after', expected_method, options) Chris@0: end Chris@0: Chris@0: def self.should_have_filter(filter_type, expected_method, options) Chris@0: description = "have #{filter_type}_filter :#{expected_method}" Chris@0: description << " with #{options.inspect}" unless options.empty? Chris@0: Chris@0: should description do Chris@0: klass = "action_controller/filters/#{filter_type}_filter".classify.constantize Chris@0: expected = klass.new(:filter, expected_method.to_sym, options) Chris@0: assert_equal 1, @controller.class.filter_chain.select { |filter| Chris@0: filter.method == expected.method && filter.kind == expected.kind && Chris@0: filter.options == expected.options && filter.class == expected.class Chris@0: }.size Chris@0: end Chris@0: end Chris@0: Chris@0: def self.should_show_the_old_and_new_values_for(prop_key, model, &block) Chris@0: context "" do Chris@0: setup do Chris@0: if block_given? Chris@0: instance_eval &block Chris@0: else Chris@0: @old_value = model.generate! Chris@0: @new_value = model.generate! Chris@0: end Chris@0: end Chris@0: Chris@0: should "use the new value's name" do Chris@0: @detail = JournalDetail.generate!(:property => 'attr', Chris@0: :old_value => @old_value.id, Chris@0: :value => @new_value.id, Chris@0: :prop_key => prop_key) Chris@0: Chris@0: assert_match @new_value.name, show_detail(@detail, true) Chris@0: end Chris@0: Chris@0: should "use the old value's name" do Chris@0: @detail = JournalDetail.generate!(:property => 'attr', Chris@0: :old_value => @old_value.id, Chris@0: :value => @new_value.id, Chris@0: :prop_key => prop_key) Chris@0: Chris@0: assert_match @old_value.name, show_detail(@detail, true) Chris@0: end Chris@0: end Chris@0: end Chris@14: Chris@14: def self.should_create_a_new_user(&block) Chris@14: should "create a new user" do Chris@14: user = instance_eval &block Chris@14: assert user Chris@14: assert_kind_of User, user Chris@14: assert !user.new_record? Chris@14: end Chris@14: end Chris@0: end