##// END OF EJS Templates
Missing test helper (#18537)....
Jean-Philippe Lang -
r13379:90e16a35c5d8
parent child
Show More
@@ -1,288 +1,292
1 # Redmine - project management software
1 # Redmine - project management software
2 # Copyright (C) 2006-2014 Jean-Philippe Lang
2 # Copyright (C) 2006-2014 Jean-Philippe Lang
3 #
3 #
4 # This program is free software; you can redistribute it and/or
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
7 # of the License, or (at your option) any later version.
8 #
8 #
9 # This program is distributed in the hope that it will be useful,
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
12 # GNU General Public License for more details.
13 #
13 #
14 # You should have received a copy of the GNU General Public License
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
17
18 if ENV["COVERAGE"]
18 if ENV["COVERAGE"]
19 require 'simplecov'
19 require 'simplecov'
20 require File.expand_path(File.dirname(__FILE__) + "/coverage/html_formatter")
20 require File.expand_path(File.dirname(__FILE__) + "/coverage/html_formatter")
21 SimpleCov.formatter = Redmine::Coverage::HtmlFormatter
21 SimpleCov.formatter = Redmine::Coverage::HtmlFormatter
22 SimpleCov.start 'rails'
22 SimpleCov.start 'rails'
23 end
23 end
24
24
25 ENV["RAILS_ENV"] = "test"
25 ENV["RAILS_ENV"] = "test"
26 require File.expand_path(File.dirname(__FILE__) + "/../config/environment")
26 require File.expand_path(File.dirname(__FILE__) + "/../config/environment")
27 require 'rails/test_help'
27 require 'rails/test_help'
28 require Rails.root.join('test', 'mocks', 'open_id_authentication_mock.rb').to_s
28 require Rails.root.join('test', 'mocks', 'open_id_authentication_mock.rb').to_s
29
29
30 require File.expand_path(File.dirname(__FILE__) + '/object_helpers')
30 require File.expand_path(File.dirname(__FILE__) + '/object_helpers')
31 include ObjectHelpers
31 include ObjectHelpers
32
32
33 require 'awesome_nested_set/version'
33 require 'awesome_nested_set/version'
34 require 'net/ldap'
34 require 'net/ldap'
35
35
36 class ActionView::TestCase
36 class ActionView::TestCase
37 helper :application
37 helper :application
38 include ApplicationHelper
38 include ApplicationHelper
39 end
39 end
40
40
41 class ActiveSupport::TestCase
41 class ActiveSupport::TestCase
42 include ActionDispatch::TestProcess
42 include ActionDispatch::TestProcess
43
43
44 self.use_transactional_fixtures = true
44 self.use_transactional_fixtures = true
45 self.use_instantiated_fixtures = false
45 self.use_instantiated_fixtures = false
46
46
47 #ESCAPED_CANT = 'can't'
47 #ESCAPED_CANT = 'can't'
48 #ESCAPED_UCANT = 'Can't'
48 #ESCAPED_UCANT = 'Can't'
49 # Rails 4.0.2
49 # Rails 4.0.2
50 ESCAPED_CANT = 'can't'
50 ESCAPED_CANT = 'can't'
51 ESCAPED_UCANT = 'Can't'
51 ESCAPED_UCANT = 'Can't'
52
52
53 def uploaded_test_file(name, mime)
53 def uploaded_test_file(name, mime)
54 fixture_file_upload("files/#{name}", mime, true)
54 fixture_file_upload("files/#{name}", mime, true)
55 end
55 end
56
56
57 # Mock out a file
57 # Mock out a file
58 def self.mock_file
58 def self.mock_file
59 file = 'a_file.png'
59 file = 'a_file.png'
60 file.stubs(:size).returns(32)
60 file.stubs(:size).returns(32)
61 file.stubs(:original_filename).returns('a_file.png')
61 file.stubs(:original_filename).returns('a_file.png')
62 file.stubs(:content_type).returns('image/png')
62 file.stubs(:content_type).returns('image/png')
63 file.stubs(:read).returns(false)
63 file.stubs(:read).returns(false)
64 file
64 file
65 end
65 end
66
66
67 def mock_file
67 def mock_file
68 self.class.mock_file
68 self.class.mock_file
69 end
69 end
70
70
71 def mock_file_with_options(options={})
71 def mock_file_with_options(options={})
72 file = ''
72 file = ''
73 file.stubs(:size).returns(32)
73 file.stubs(:size).returns(32)
74 original_filename = options[:original_filename] || nil
74 original_filename = options[:original_filename] || nil
75 file.stubs(:original_filename).returns(original_filename)
75 file.stubs(:original_filename).returns(original_filename)
76 content_type = options[:content_type] || nil
76 content_type = options[:content_type] || nil
77 file.stubs(:content_type).returns(content_type)
77 file.stubs(:content_type).returns(content_type)
78 file.stubs(:read).returns(false)
78 file.stubs(:read).returns(false)
79 file
79 file
80 end
80 end
81
81
82 # Use a temporary directory for attachment related tests
82 # Use a temporary directory for attachment related tests
83 def set_tmp_attachments_directory
83 def set_tmp_attachments_directory
84 Dir.mkdir "#{Rails.root}/tmp/test" unless File.directory?("#{Rails.root}/tmp/test")
84 Dir.mkdir "#{Rails.root}/tmp/test" unless File.directory?("#{Rails.root}/tmp/test")
85 unless File.directory?("#{Rails.root}/tmp/test/attachments")
85 unless File.directory?("#{Rails.root}/tmp/test/attachments")
86 Dir.mkdir "#{Rails.root}/tmp/test/attachments"
86 Dir.mkdir "#{Rails.root}/tmp/test/attachments"
87 end
87 end
88 Attachment.storage_path = "#{Rails.root}/tmp/test/attachments"
88 Attachment.storage_path = "#{Rails.root}/tmp/test/attachments"
89 end
89 end
90
90
91 def set_fixtures_attachments_directory
91 def set_fixtures_attachments_directory
92 Attachment.storage_path = "#{Rails.root}/test/fixtures/files"
92 Attachment.storage_path = "#{Rails.root}/test/fixtures/files"
93 end
93 end
94
94
95 def with_settings(options, &block)
95 def with_settings(options, &block)
96 saved_settings = options.keys.inject({}) do |h, k|
96 saved_settings = options.keys.inject({}) do |h, k|
97 h[k] = case Setting[k]
97 h[k] = case Setting[k]
98 when Symbol, false, true, nil
98 when Symbol, false, true, nil
99 Setting[k]
99 Setting[k]
100 else
100 else
101 Setting[k].dup
101 Setting[k].dup
102 end
102 end
103 h
103 h
104 end
104 end
105 options.each {|k, v| Setting[k] = v}
105 options.each {|k, v| Setting[k] = v}
106 yield
106 yield
107 ensure
107 ensure
108 saved_settings.each {|k, v| Setting[k] = v} if saved_settings
108 saved_settings.each {|k, v| Setting[k] = v} if saved_settings
109 end
109 end
110
110
111 # Yields the block with user as the current user
111 # Yields the block with user as the current user
112 def with_current_user(user, &block)
112 def with_current_user(user, &block)
113 saved_user = User.current
113 saved_user = User.current
114 User.current = user
114 User.current = user
115 yield
115 yield
116 ensure
116 ensure
117 User.current = saved_user
117 User.current = saved_user
118 end
118 end
119
119
120 def with_locale(locale, &block)
120 def with_locale(locale, &block)
121 saved_localed = ::I18n.locale
121 saved_localed = ::I18n.locale
122 ::I18n.locale = locale
122 ::I18n.locale = locale
123 yield
123 yield
124 ensure
124 ensure
125 ::I18n.locale = saved_localed
125 ::I18n.locale = saved_localed
126 end
126 end
127
127
128 def self.ldap_configured?
128 def self.ldap_configured?
129 @test_ldap = Net::LDAP.new(:host => '127.0.0.1', :port => 389)
129 @test_ldap = Net::LDAP.new(:host => '127.0.0.1', :port => 389)
130 return @test_ldap.bind
130 return @test_ldap.bind
131 rescue Exception => e
131 rescue Exception => e
132 # LDAP is not listening
132 # LDAP is not listening
133 return nil
133 return nil
134 end
134 end
135
135
136 def self.convert_installed?
136 def self.convert_installed?
137 Redmine::Thumbnail.convert_available?
137 Redmine::Thumbnail.convert_available?
138 end
138 end
139
139
140 # Returns the path to the test +vendor+ repository
140 # Returns the path to the test +vendor+ repository
141 def self.repository_path(vendor)
141 def self.repository_path(vendor)
142 path = Rails.root.join("tmp/test/#{vendor.downcase}_repository").to_s
142 path = Rails.root.join("tmp/test/#{vendor.downcase}_repository").to_s
143 # Unlike ruby, JRuby returns Rails.root with backslashes under Windows
143 # Unlike ruby, JRuby returns Rails.root with backslashes under Windows
144 path.tr("\\", "/")
144 path.tr("\\", "/")
145 end
145 end
146
146
147 # Returns the url of the subversion test repository
147 # Returns the url of the subversion test repository
148 def self.subversion_repository_url
148 def self.subversion_repository_url
149 path = repository_path('subversion')
149 path = repository_path('subversion')
150 path = '/' + path unless path.starts_with?('/')
150 path = '/' + path unless path.starts_with?('/')
151 "file://#{path}"
151 "file://#{path}"
152 end
152 end
153
153
154 # Returns true if the +vendor+ test repository is configured
154 # Returns true if the +vendor+ test repository is configured
155 def self.repository_configured?(vendor)
155 def self.repository_configured?(vendor)
156 File.directory?(repository_path(vendor))
156 File.directory?(repository_path(vendor))
157 end
157 end
158
158
159 def repository_path_hash(arr)
159 def repository_path_hash(arr)
160 hs = {}
160 hs = {}
161 hs[:path] = arr.join("/")
161 hs[:path] = arr.join("/")
162 hs[:param] = arr.join("/")
162 hs[:param] = arr.join("/")
163 hs
163 hs
164 end
164 end
165
165
166 def sqlite?
167 ActiveRecord::Base.connection.adapter_name =~ /sqlite/i
168 end
169
166 def assert_save(object)
170 def assert_save(object)
167 saved = object.save
171 saved = object.save
168 message = "#{object.class} could not be saved"
172 message = "#{object.class} could not be saved"
169 errors = object.errors.full_messages.map {|m| "- #{m}"}
173 errors = object.errors.full_messages.map {|m| "- #{m}"}
170 message << ":\n#{errors.join("\n")}" if errors.any?
174 message << ":\n#{errors.join("\n")}" if errors.any?
171 assert_equal true, saved, message
175 assert_equal true, saved, message
172 end
176 end
173
177
174 def assert_select_error(arg)
178 def assert_select_error(arg)
175 assert_select '#errorExplanation', :text => arg
179 assert_select '#errorExplanation', :text => arg
176 end
180 end
177
181
178 def assert_include(expected, s, message=nil)
182 def assert_include(expected, s, message=nil)
179 assert s.include?(expected), (message || "\"#{expected}\" not found in \"#{s}\"")
183 assert s.include?(expected), (message || "\"#{expected}\" not found in \"#{s}\"")
180 end
184 end
181
185
182 def assert_not_include(expected, s, message=nil)
186 def assert_not_include(expected, s, message=nil)
183 assert !s.include?(expected), (message || "\"#{expected}\" found in \"#{s}\"")
187 assert !s.include?(expected), (message || "\"#{expected}\" found in \"#{s}\"")
184 end
188 end
185
189
186 def assert_select_in(text, *args, &block)
190 def assert_select_in(text, *args, &block)
187 d = HTML::Document.new(CGI::unescapeHTML(String.new(text))).root
191 d = HTML::Document.new(CGI::unescapeHTML(String.new(text))).root
188 assert_select(d, *args, &block)
192 assert_select(d, *args, &block)
189 end
193 end
190
194
191 def assert_mail_body_match(expected, mail, message=nil)
195 def assert_mail_body_match(expected, mail, message=nil)
192 if expected.is_a?(String)
196 if expected.is_a?(String)
193 assert_include expected, mail_body(mail), message
197 assert_include expected, mail_body(mail), message
194 else
198 else
195 assert_match expected, mail_body(mail), message
199 assert_match expected, mail_body(mail), message
196 end
200 end
197 end
201 end
198
202
199 def assert_mail_body_no_match(expected, mail, message=nil)
203 def assert_mail_body_no_match(expected, mail, message=nil)
200 if expected.is_a?(String)
204 if expected.is_a?(String)
201 assert_not_include expected, mail_body(mail), message
205 assert_not_include expected, mail_body(mail), message
202 else
206 else
203 assert_no_match expected, mail_body(mail), message
207 assert_no_match expected, mail_body(mail), message
204 end
208 end
205 end
209 end
206
210
207 def mail_body(mail)
211 def mail_body(mail)
208 mail.parts.first.body.encoded
212 mail.parts.first.body.encoded
209 end
213 end
210
214
211 # awesome_nested_set new node lft and rgt value changed this refactor revision.
215 # awesome_nested_set new node lft and rgt value changed this refactor revision.
212 # https://github.com/collectiveidea/awesome_nested_set/commit/199fca9bb938e40200cd90714dc69247ef017c61
216 # https://github.com/collectiveidea/awesome_nested_set/commit/199fca9bb938e40200cd90714dc69247ef017c61
213 # The reason of behavior change is that "self.class.base_class.unscoped" was added to this line.
217 # The reason of behavior change is that "self.class.base_class.unscoped" was added to this line.
214 # https://github.com/collectiveidea/awesome_nested_set/commit/199fca9bb9#diff-f61b59a5e6319024e211b0ffdd0e4ef1R273
218 # https://github.com/collectiveidea/awesome_nested_set/commit/199fca9bb9#diff-f61b59a5e6319024e211b0ffdd0e4ef1R273
215 # It seems correct behavior because of this line comment.
219 # It seems correct behavior because of this line comment.
216 # https://github.com/collectiveidea/awesome_nested_set/blame/199fca9bb9/lib/awesome_nested_set/model.rb#L278
220 # https://github.com/collectiveidea/awesome_nested_set/blame/199fca9bb9/lib/awesome_nested_set/model.rb#L278
217 def new_issue_lft
221 def new_issue_lft
218 # ::AwesomeNestedSet::VERSION > "2.1.6" ? Issue.maximum(:rgt) + 1 : 1
222 # ::AwesomeNestedSet::VERSION > "2.1.6" ? Issue.maximum(:rgt) + 1 : 1
219 Issue.maximum(:rgt) + 1
223 Issue.maximum(:rgt) + 1
220 end
224 end
221 end
225 end
222
226
223 module Redmine
227 module Redmine
224 class RoutingTest < ActionDispatch::IntegrationTest
228 class RoutingTest < ActionDispatch::IntegrationTest
225 def should_route(arg)
229 def should_route(arg)
226 arg = arg.dup
230 arg = arg.dup
227 request = arg.keys.detect {|key| key.is_a?(String)}
231 request = arg.keys.detect {|key| key.is_a?(String)}
228 raise ArgumentError unless request
232 raise ArgumentError unless request
229 options = arg.slice!(request)
233 options = arg.slice!(request)
230
234
231 raise ArgumentError unless request =~ /\A(GET|POST|PUT|PATCH|DELETE)\s+(.+)\z/
235 raise ArgumentError unless request =~ /\A(GET|POST|PUT|PATCH|DELETE)\s+(.+)\z/
232 method, path = $1.downcase.to_sym, $2
236 method, path = $1.downcase.to_sym, $2
233
237
234 raise ArgumentError unless arg.values.first =~ /\A(.+)#(.+)\z/
238 raise ArgumentError unless arg.values.first =~ /\A(.+)#(.+)\z/
235 controller, action = $1, $2
239 controller, action = $1, $2
236
240
237 assert_routing(
241 assert_routing(
238 {:method => method, :path => path},
242 {:method => method, :path => path},
239 options.merge(:controller => controller, :action => action)
243 options.merge(:controller => controller, :action => action)
240 )
244 )
241 end
245 end
242 end
246 end
243
247
244 class IntegrationTest < ActionDispatch::IntegrationTest
248 class IntegrationTest < ActionDispatch::IntegrationTest
245 def log_user(login, password)
249 def log_user(login, password)
246 User.anonymous
250 User.anonymous
247 get "/login"
251 get "/login"
248 assert_equal nil, session[:user_id]
252 assert_equal nil, session[:user_id]
249 assert_response :success
253 assert_response :success
250 assert_template "account/login"
254 assert_template "account/login"
251 post "/login", :username => login, :password => password
255 post "/login", :username => login, :password => password
252 assert_equal login, User.find(session[:user_id]).login
256 assert_equal login, User.find(session[:user_id]).login
253 end
257 end
254
258
255 def credentials(user, password=nil)
259 def credentials(user, password=nil)
256 {'HTTP_AUTHORIZATION' => ActionController::HttpAuthentication::Basic.encode_credentials(user, password || user)}
260 {'HTTP_AUTHORIZATION' => ActionController::HttpAuthentication::Basic.encode_credentials(user, password || user)}
257 end
261 end
258 end
262 end
259
263
260 module ApiTest
264 module ApiTest
261 API_FORMATS = %w(json xml).freeze
265 API_FORMATS = %w(json xml).freeze
262
266
263 # Base class for API tests
267 # Base class for API tests
264 class Base < Redmine::IntegrationTest
268 class Base < Redmine::IntegrationTest
265 def setup
269 def setup
266 Setting.rest_api_enabled = '1'
270 Setting.rest_api_enabled = '1'
267 end
271 end
268
272
269 def teardown
273 def teardown
270 Setting.rest_api_enabled = '0'
274 Setting.rest_api_enabled = '0'
271 end
275 end
272 end
276 end
273
277
274 class Routing < Redmine::RoutingTest
278 class Routing < Redmine::RoutingTest
275 def should_route(arg)
279 def should_route(arg)
276 arg = arg.dup
280 arg = arg.dup
277 request = arg.keys.detect {|key| key.is_a?(String)}
281 request = arg.keys.detect {|key| key.is_a?(String)}
278 raise ArgumentError unless request
282 raise ArgumentError unless request
279 options = arg.slice!(request)
283 options = arg.slice!(request)
280
284
281 API_FORMATS.each do |format|
285 API_FORMATS.each do |format|
282 format_request = request.sub /$/, ".#{format}"
286 format_request = request.sub /$/, ".#{format}"
283 super options.merge(format_request => arg[request], :format => format)
287 super options.merge(format_request => arg[request], :format => format)
284 end
288 end
285 end
289 end
286 end
290 end
287 end
291 end
288 end
292 end
General Comments 0
You need to be logged in to leave comments. Login now