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@14
|
66 def self.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@14
|
74
|
Chris@14
|
75 def mock_file
|
Chris@14
|
76 self.class.mock_file
|
Chris@14
|
77 end
|
Chris@14
|
78
|
Chris@0
|
79 # Use a temporary directory for attachment related tests
|
Chris@0
|
80 def set_tmp_attachments_directory
|
Chris@0
|
81 Dir.mkdir "#{RAILS_ROOT}/tmp/test" unless File.directory?("#{RAILS_ROOT}/tmp/test")
|
Chris@0
|
82 Dir.mkdir "#{RAILS_ROOT}/tmp/test/attachments" unless File.directory?("#{RAILS_ROOT}/tmp/test/attachments")
|
Chris@0
|
83 Attachment.storage_path = "#{RAILS_ROOT}/tmp/test/attachments"
|
Chris@0
|
84 end
|
Chris@0
|
85
|
Chris@0
|
86 def with_settings(options, &block)
|
Chris@0
|
87 saved_settings = options.keys.inject({}) {|h, k| h[k] = Setting[k].dup; h}
|
Chris@0
|
88 options.each {|k, v| Setting[k] = v}
|
Chris@0
|
89 yield
|
Chris@0
|
90 saved_settings.each {|k, v| Setting[k] = v}
|
Chris@0
|
91 end
|
Chris@0
|
92
|
Chris@14
|
93 def change_user_password(login, new_password)
|
Chris@14
|
94 user = User.first(:conditions => {:login => login})
|
Chris@14
|
95 user.password, user.password_confirmation = new_password, new_password
|
Chris@14
|
96 user.save!
|
Chris@14
|
97 end
|
Chris@14
|
98
|
Chris@0
|
99 def self.ldap_configured?
|
Chris@0
|
100 @test_ldap = Net::LDAP.new(:host => '127.0.0.1', :port => 389)
|
Chris@0
|
101 return @test_ldap.bind
|
Chris@0
|
102 rescue Exception => e
|
Chris@0
|
103 # LDAP is not listening
|
Chris@0
|
104 return nil
|
Chris@0
|
105 end
|
Chris@0
|
106
|
Chris@0
|
107 # Returns the path to the test +vendor+ repository
|
Chris@0
|
108 def self.repository_path(vendor)
|
Chris@0
|
109 File.join(RAILS_ROOT.gsub(%r{config\/\.\.}, ''), "/tmp/test/#{vendor.downcase}_repository")
|
Chris@0
|
110 end
|
Chris@0
|
111
|
Chris@0
|
112 # Returns true if the +vendor+ test repository is configured
|
Chris@0
|
113 def self.repository_configured?(vendor)
|
Chris@0
|
114 File.directory?(repository_path(vendor))
|
Chris@0
|
115 end
|
Chris@0
|
116
|
Chris@0
|
117 # Shoulda macros
|
Chris@0
|
118 def self.should_render_404
|
Chris@0
|
119 should_respond_with :not_found
|
Chris@0
|
120 should_render_template 'common/404'
|
Chris@0
|
121 end
|
Chris@0
|
122
|
Chris@0
|
123 def self.should_have_before_filter(expected_method, options = {})
|
Chris@0
|
124 should_have_filter('before', expected_method, options)
|
Chris@0
|
125 end
|
Chris@0
|
126
|
Chris@0
|
127 def self.should_have_after_filter(expected_method, options = {})
|
Chris@0
|
128 should_have_filter('after', expected_method, options)
|
Chris@0
|
129 end
|
Chris@0
|
130
|
Chris@0
|
131 def self.should_have_filter(filter_type, expected_method, options)
|
Chris@0
|
132 description = "have #{filter_type}_filter :#{expected_method}"
|
Chris@0
|
133 description << " with #{options.inspect}" unless options.empty?
|
Chris@0
|
134
|
Chris@0
|
135 should description do
|
Chris@0
|
136 klass = "action_controller/filters/#{filter_type}_filter".classify.constantize
|
Chris@0
|
137 expected = klass.new(:filter, expected_method.to_sym, options)
|
Chris@0
|
138 assert_equal 1, @controller.class.filter_chain.select { |filter|
|
Chris@0
|
139 filter.method == expected.method && filter.kind == expected.kind &&
|
Chris@0
|
140 filter.options == expected.options && filter.class == expected.class
|
Chris@0
|
141 }.size
|
Chris@0
|
142 end
|
Chris@0
|
143 end
|
Chris@0
|
144
|
Chris@0
|
145 def self.should_show_the_old_and_new_values_for(prop_key, model, &block)
|
Chris@0
|
146 context "" do
|
Chris@0
|
147 setup do
|
Chris@0
|
148 if block_given?
|
Chris@0
|
149 instance_eval &block
|
Chris@0
|
150 else
|
Chris@0
|
151 @old_value = model.generate!
|
Chris@0
|
152 @new_value = model.generate!
|
Chris@0
|
153 end
|
Chris@0
|
154 end
|
Chris@0
|
155
|
Chris@0
|
156 should "use the new value's name" do
|
Chris@0
|
157 @detail = JournalDetail.generate!(:property => 'attr',
|
Chris@0
|
158 :old_value => @old_value.id,
|
Chris@0
|
159 :value => @new_value.id,
|
Chris@0
|
160 :prop_key => prop_key)
|
Chris@0
|
161
|
Chris@0
|
162 assert_match @new_value.name, show_detail(@detail, true)
|
Chris@0
|
163 end
|
Chris@0
|
164
|
Chris@0
|
165 should "use the old value's name" do
|
Chris@0
|
166 @detail = JournalDetail.generate!(:property => 'attr',
|
Chris@0
|
167 :old_value => @old_value.id,
|
Chris@0
|
168 :value => @new_value.id,
|
Chris@0
|
169 :prop_key => prop_key)
|
Chris@0
|
170
|
Chris@0
|
171 assert_match @old_value.name, show_detail(@detail, true)
|
Chris@0
|
172 end
|
Chris@0
|
173 end
|
Chris@0
|
174 end
|
Chris@14
|
175
|
Chris@14
|
176 def self.should_create_a_new_user(&block)
|
Chris@14
|
177 should "create a new user" do
|
Chris@14
|
178 user = instance_eval &block
|
Chris@14
|
179 assert user
|
Chris@14
|
180 assert_kind_of User, user
|
Chris@14
|
181 assert !user.new_record?
|
Chris@14
|
182 end
|
Chris@14
|
183 end
|
Chris@0
|
184 end
|