annotate test/.svn/text-base/test_helper.rb.svn-base @ 8:0c83d98252d9 yuya

* Add custom repo prefix and proper auth realm, remove auth cache (seems like an unwise feature), pass DB handle around, various other bits of tidying
author Chris Cannam
date Thu, 12 Aug 2010 15:31:37 +0100
parents 513646585e45
children 1d32c0a0efbf
rev   line source
Chris@0 1 # redMine - project management software
Chris@0 2 # Copyright (C) 2006 Jean-Philippe Lang
Chris@0 3 #
Chris@0 4 # This program is free software; you can redistribute it and/or
Chris@0 5 # modify it under the terms of the GNU General Public License
Chris@0 6 # as published by the Free Software Foundation; either version 2
Chris@0 7 # of the License, or (at your option) any later version.
Chris@0 8 #
Chris@0 9 # This program is distributed in the hope that it will be useful,
Chris@0 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@0 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@0 12 # GNU General Public License for more details.
Chris@0 13 #
Chris@0 14 # You should have received a copy of the GNU General Public License
Chris@0 15 # along with this program; if not, write to the Free Software
Chris@0 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Chris@0 17
Chris@0 18 ENV["RAILS_ENV"] = "test"
Chris@0 19 require File.expand_path(File.dirname(__FILE__) + "/../config/environment")
Chris@0 20 require 'test_help'
Chris@0 21 require File.expand_path(File.dirname(__FILE__) + '/helper_testcase')
Chris@0 22 require File.join(RAILS_ROOT,'test', 'mocks', 'open_id_authentication_mock.rb')
Chris@0 23
Chris@0 24 require File.expand_path(File.dirname(__FILE__) + '/object_daddy_helpers')
Chris@0 25 include ObjectDaddyHelpers
Chris@0 26
Chris@0 27 class ActiveSupport::TestCase
Chris@0 28 # Transactional fixtures accelerate your tests by wrapping each test method
Chris@0 29 # in a transaction that's rolled back on completion. This ensures that the
Chris@0 30 # test database remains unchanged so your fixtures don't have to be reloaded
Chris@0 31 # between every test method. Fewer database queries means faster tests.
Chris@0 32 #
Chris@0 33 # Read Mike Clark's excellent walkthrough at
Chris@0 34 # http://clarkware.com/cgi/blosxom/2005/10/24#Rails10FastTesting
Chris@0 35 #
Chris@0 36 # Every Active Record database supports transactions except MyISAM tables
Chris@0 37 # in MySQL. Turn off transactional fixtures in this case; however, if you
Chris@0 38 # don't care one way or the other, switching from MyISAM to InnoDB tables
Chris@0 39 # is recommended.
Chris@0 40 self.use_transactional_fixtures = true
Chris@0 41
Chris@0 42 # Instantiated fixtures are slow, but give you @david where otherwise you
Chris@0 43 # would need people(:david). If you don't want to migrate your existing
Chris@0 44 # test cases which use the @david style and don't mind the speed hit (each
Chris@0 45 # instantiated fixtures translates to a database query per test method),
Chris@0 46 # then set this back to true.
Chris@0 47 self.use_instantiated_fixtures = false
Chris@0 48
Chris@0 49 # Add more helper methods to be used by all tests here...
Chris@0 50
Chris@0 51 def log_user(login, password)
Chris@0 52 User.anonymous
Chris@0 53 get "/login"
Chris@0 54 assert_equal nil, session[:user_id]
Chris@0 55 assert_response :success
Chris@0 56 assert_template "account/login"
Chris@0 57 post "/login", :username => login, :password => password
Chris@0 58 assert_equal login, User.find(session[:user_id]).login
Chris@0 59 end
Chris@0 60
Chris@0 61 def uploaded_test_file(name, mime)
Chris@0 62 ActionController::TestUploadedFile.new(ActiveSupport::TestCase.fixture_path + "/files/#{name}", mime)
Chris@0 63 end
Chris@0 64
Chris@0 65 # Mock out a file
Chris@0 66 def mock_file
Chris@0 67 file = 'a_file.png'
Chris@0 68 file.stubs(:size).returns(32)
Chris@0 69 file.stubs(:original_filename).returns('a_file.png')
Chris@0 70 file.stubs(:content_type).returns('image/png')
Chris@0 71 file.stubs(:read).returns(false)
Chris@0 72 file
Chris@0 73 end
Chris@0 74
Chris@0 75 # Use a temporary directory for attachment related tests
Chris@0 76 def set_tmp_attachments_directory
Chris@0 77 Dir.mkdir "#{RAILS_ROOT}/tmp/test" unless File.directory?("#{RAILS_ROOT}/tmp/test")
Chris@0 78 Dir.mkdir "#{RAILS_ROOT}/tmp/test/attachments" unless File.directory?("#{RAILS_ROOT}/tmp/test/attachments")
Chris@0 79 Attachment.storage_path = "#{RAILS_ROOT}/tmp/test/attachments"
Chris@0 80 end
Chris@0 81
Chris@0 82 def with_settings(options, &block)
Chris@0 83 saved_settings = options.keys.inject({}) {|h, k| h[k] = Setting[k].dup; h}
Chris@0 84 options.each {|k, v| Setting[k] = v}
Chris@0 85 yield
Chris@0 86 saved_settings.each {|k, v| Setting[k] = v}
Chris@0 87 end
Chris@0 88
Chris@0 89 def self.ldap_configured?
Chris@0 90 @test_ldap = Net::LDAP.new(:host => '127.0.0.1', :port => 389)
Chris@0 91 return @test_ldap.bind
Chris@0 92 rescue Exception => e
Chris@0 93 # LDAP is not listening
Chris@0 94 return nil
Chris@0 95 end
Chris@0 96
Chris@0 97 # Returns the path to the test +vendor+ repository
Chris@0 98 def self.repository_path(vendor)
Chris@0 99 File.join(RAILS_ROOT.gsub(%r{config\/\.\.}, ''), "/tmp/test/#{vendor.downcase}_repository")
Chris@0 100 end
Chris@0 101
Chris@0 102 # Returns true if the +vendor+ test repository is configured
Chris@0 103 def self.repository_configured?(vendor)
Chris@0 104 File.directory?(repository_path(vendor))
Chris@0 105 end
Chris@0 106
Chris@0 107 # Shoulda macros
Chris@0 108 def self.should_render_404
Chris@0 109 should_respond_with :not_found
Chris@0 110 should_render_template 'common/404'
Chris@0 111 end
Chris@0 112
Chris@0 113 def self.should_have_before_filter(expected_method, options = {})
Chris@0 114 should_have_filter('before', expected_method, options)
Chris@0 115 end
Chris@0 116
Chris@0 117 def self.should_have_after_filter(expected_method, options = {})
Chris@0 118 should_have_filter('after', expected_method, options)
Chris@0 119 end
Chris@0 120
Chris@0 121 def self.should_have_filter(filter_type, expected_method, options)
Chris@0 122 description = "have #{filter_type}_filter :#{expected_method}"
Chris@0 123 description << " with #{options.inspect}" unless options.empty?
Chris@0 124
Chris@0 125 should description do
Chris@0 126 klass = "action_controller/filters/#{filter_type}_filter".classify.constantize
Chris@0 127 expected = klass.new(:filter, expected_method.to_sym, options)
Chris@0 128 assert_equal 1, @controller.class.filter_chain.select { |filter|
Chris@0 129 filter.method == expected.method && filter.kind == expected.kind &&
Chris@0 130 filter.options == expected.options && filter.class == expected.class
Chris@0 131 }.size
Chris@0 132 end
Chris@0 133 end
Chris@0 134
Chris@0 135 def self.should_show_the_old_and_new_values_for(prop_key, model, &block)
Chris@0 136 context "" do
Chris@0 137 setup do
Chris@0 138 if block_given?
Chris@0 139 instance_eval &block
Chris@0 140 else
Chris@0 141 @old_value = model.generate!
Chris@0 142 @new_value = model.generate!
Chris@0 143 end
Chris@0 144 end
Chris@0 145
Chris@0 146 should "use the new value's name" do
Chris@0 147 @detail = JournalDetail.generate!(:property => 'attr',
Chris@0 148 :old_value => @old_value.id,
Chris@0 149 :value => @new_value.id,
Chris@0 150 :prop_key => prop_key)
Chris@0 151
Chris@0 152 assert_match @new_value.name, show_detail(@detail, true)
Chris@0 153 end
Chris@0 154
Chris@0 155 should "use the old value's name" do
Chris@0 156 @detail = JournalDetail.generate!(:property => 'attr',
Chris@0 157 :old_value => @old_value.id,
Chris@0 158 :value => @new_value.id,
Chris@0 159 :prop_key => prop_key)
Chris@0 160
Chris@0 161 assert_match @old_value.name, show_detail(@detail, true)
Chris@0 162 end
Chris@0 163 end
Chris@0 164 end
Chris@0 165 end