##// END OF EJS Templates
Replace tab introduced in r14388 in test/test_helper.rb with spaces (#20242)....
Jean-Philippe Lang -
r14030:e67bc3637e4a
parent child
Show More
@@ -1,355 +1,355
1 # Redmine - project management software
1 # Redmine - project management software
2 # Copyright (C) 2006-2015 Jean-Philippe Lang
2 # Copyright (C) 2006-2015 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 'net/ldap'
33 require 'net/ldap'
34 require 'mocha/setup'
34 require 'mocha/setup'
35
35
36 Redmine::SudoMode.disable!
36 Redmine::SudoMode.disable!
37
37
38 class ActionView::TestCase
38 class ActionView::TestCase
39 helper :application
39 helper :application
40 include ApplicationHelper
40 include ApplicationHelper
41 end
41 end
42
42
43 class ActiveSupport::TestCase
43 class ActiveSupport::TestCase
44 include ActionDispatch::TestProcess
44 include ActionDispatch::TestProcess
45
45
46 self.use_transactional_fixtures = true
46 self.use_transactional_fixtures = true
47 self.use_instantiated_fixtures = false
47 self.use_instantiated_fixtures = false
48
48
49 def uploaded_test_file(name, mime)
49 def uploaded_test_file(name, mime)
50 fixture_file_upload("files/#{name}", mime, true)
50 fixture_file_upload("files/#{name}", mime, true)
51 end
51 end
52
52
53 # Mock out a file
53 # Mock out a file
54 def self.mock_file
54 def self.mock_file
55 file = 'a_file.png'
55 file = 'a_file.png'
56 file.stubs(:size).returns(32)
56 file.stubs(:size).returns(32)
57 file.stubs(:original_filename).returns('a_file.png')
57 file.stubs(:original_filename).returns('a_file.png')
58 file.stubs(:content_type).returns('image/png')
58 file.stubs(:content_type).returns('image/png')
59 file.stubs(:read).returns(false)
59 file.stubs(:read).returns(false)
60 file
60 file
61 end
61 end
62
62
63 def mock_file
63 def mock_file
64 self.class.mock_file
64 self.class.mock_file
65 end
65 end
66
66
67 def mock_file_with_options(options={})
67 def mock_file_with_options(options={})
68 file = ''
68 file = ''
69 file.stubs(:size).returns(32)
69 file.stubs(:size).returns(32)
70 original_filename = options[:original_filename] || nil
70 original_filename = options[:original_filename] || nil
71 file.stubs(:original_filename).returns(original_filename)
71 file.stubs(:original_filename).returns(original_filename)
72 content_type = options[:content_type] || nil
72 content_type = options[:content_type] || nil
73 file.stubs(:content_type).returns(content_type)
73 file.stubs(:content_type).returns(content_type)
74 file.stubs(:read).returns(false)
74 file.stubs(:read).returns(false)
75 file
75 file
76 end
76 end
77
77
78 # Use a temporary directory for attachment related tests
78 # Use a temporary directory for attachment related tests
79 def set_tmp_attachments_directory
79 def set_tmp_attachments_directory
80 Dir.mkdir "#{Rails.root}/tmp/test" unless File.directory?("#{Rails.root}/tmp/test")
80 Dir.mkdir "#{Rails.root}/tmp/test" unless File.directory?("#{Rails.root}/tmp/test")
81 unless File.directory?("#{Rails.root}/tmp/test/attachments")
81 unless File.directory?("#{Rails.root}/tmp/test/attachments")
82 Dir.mkdir "#{Rails.root}/tmp/test/attachments"
82 Dir.mkdir "#{Rails.root}/tmp/test/attachments"
83 end
83 end
84 Attachment.storage_path = "#{Rails.root}/tmp/test/attachments"
84 Attachment.storage_path = "#{Rails.root}/tmp/test/attachments"
85 end
85 end
86
86
87 def set_fixtures_attachments_directory
87 def set_fixtures_attachments_directory
88 Attachment.storage_path = "#{Rails.root}/test/fixtures/files"
88 Attachment.storage_path = "#{Rails.root}/test/fixtures/files"
89 end
89 end
90
90
91 def with_settings(options, &block)
91 def with_settings(options, &block)
92 saved_settings = options.keys.inject({}) do |h, k|
92 saved_settings = options.keys.inject({}) do |h, k|
93 h[k] = case Setting[k]
93 h[k] = case Setting[k]
94 when Symbol, false, true, nil
94 when Symbol, false, true, nil
95 Setting[k]
95 Setting[k]
96 else
96 else
97 Setting[k].dup
97 Setting[k].dup
98 end
98 end
99 h
99 h
100 end
100 end
101 options.each {|k, v| Setting[k] = v}
101 options.each {|k, v| Setting[k] = v}
102 yield
102 yield
103 ensure
103 ensure
104 saved_settings.each {|k, v| Setting[k] = v} if saved_settings
104 saved_settings.each {|k, v| Setting[k] = v} if saved_settings
105 end
105 end
106
106
107 # Yields the block with user as the current user
107 # Yields the block with user as the current user
108 def with_current_user(user, &block)
108 def with_current_user(user, &block)
109 saved_user = User.current
109 saved_user = User.current
110 User.current = user
110 User.current = user
111 yield
111 yield
112 ensure
112 ensure
113 User.current = saved_user
113 User.current = saved_user
114 end
114 end
115
115
116 def with_locale(locale, &block)
116 def with_locale(locale, &block)
117 saved_localed = ::I18n.locale
117 saved_localed = ::I18n.locale
118 ::I18n.locale = locale
118 ::I18n.locale = locale
119 yield
119 yield
120 ensure
120 ensure
121 ::I18n.locale = saved_localed
121 ::I18n.locale = saved_localed
122 end
122 end
123
123
124 def self.ldap_configured?
124 def self.ldap_configured?
125 @test_ldap = Net::LDAP.new(:host => '127.0.0.1', :port => 389)
125 @test_ldap = Net::LDAP.new(:host => '127.0.0.1', :port => 389)
126 return @test_ldap.bind
126 return @test_ldap.bind
127 rescue Exception => e
127 rescue Exception => e
128 # LDAP is not listening
128 # LDAP is not listening
129 return nil
129 return nil
130 end
130 end
131
131
132 def self.convert_installed?
132 def self.convert_installed?
133 Redmine::Thumbnail.convert_available?
133 Redmine::Thumbnail.convert_available?
134 end
134 end
135
135
136 def convert_installed?
136 def convert_installed?
137 self.class.convert_installed?
137 self.class.convert_installed?
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?
166 def sqlite?
167 ActiveRecord::Base.connection.adapter_name =~ /sqlite/i
167 ActiveRecord::Base.connection.adapter_name =~ /sqlite/i
168 end
168 end
169
169
170 def mysql?
170 def mysql?
171 ActiveRecord::Base.connection.adapter_name =~ /mysql/i
171 ActiveRecord::Base.connection.adapter_name =~ /mysql/i
172 end
172 end
173
173
174 def postgresql?
174 def postgresql?
175 ActiveRecord::Base.connection.adapter_name =~ /postgresql/i
175 ActiveRecord::Base.connection.adapter_name =~ /postgresql/i
176 end
176 end
177
177
178 def quoted_date(date)
178 def quoted_date(date)
179 date = Date.parse(date) if date.is_a?(String)
179 date = Date.parse(date) if date.is_a?(String)
180 ActiveRecord::Base.connection.quoted_date(date)
180 ActiveRecord::Base.connection.quoted_date(date)
181 end
181 end
182
182
183 # Asserts that a new record for the given class is created
183 # Asserts that a new record for the given class is created
184 # and returns it
184 # and returns it
185 def new_record(klass, &block)
185 def new_record(klass, &block)
186 assert_difference "#{klass}.count" do
186 assert_difference "#{klass}.count" do
187 yield
187 yield
188 end
188 end
189 klass.order(:id => :desc).first
189 klass.order(:id => :desc).first
190 end
190 end
191
191
192 def assert_save(object)
192 def assert_save(object)
193 saved = object.save
193 saved = object.save
194 message = "#{object.class} could not be saved"
194 message = "#{object.class} could not be saved"
195 errors = object.errors.full_messages.map {|m| "- #{m}"}
195 errors = object.errors.full_messages.map {|m| "- #{m}"}
196 message << ":\n#{errors.join("\n")}" if errors.any?
196 message << ":\n#{errors.join("\n")}" if errors.any?
197 assert_equal true, saved, message
197 assert_equal true, saved, message
198 end
198 end
199
199
200 def assert_select_error(arg)
200 def assert_select_error(arg)
201 assert_select '#errorExplanation', :text => arg
201 assert_select '#errorExplanation', :text => arg
202 end
202 end
203
203
204 def assert_include(expected, s, message=nil)
204 def assert_include(expected, s, message=nil)
205 assert s.include?(expected), (message || "\"#{expected}\" not found in \"#{s}\"")
205 assert s.include?(expected), (message || "\"#{expected}\" not found in \"#{s}\"")
206 end
206 end
207
207
208 def assert_not_include(expected, s, message=nil)
208 def assert_not_include(expected, s, message=nil)
209 assert !s.include?(expected), (message || "\"#{expected}\" found in \"#{s}\"")
209 assert !s.include?(expected), (message || "\"#{expected}\" found in \"#{s}\"")
210 end
210 end
211
211
212 def assert_select_in(text, *args, &block)
212 def assert_select_in(text, *args, &block)
213 d = Nokogiri::HTML(CGI::unescapeHTML(String.new(text))).root
213 d = Nokogiri::HTML(CGI::unescapeHTML(String.new(text))).root
214 assert_select(d, *args, &block)
214 assert_select(d, *args, &block)
215 end
215 end
216
216
217 def assert_select_email(*args, &block)
217 def assert_select_email(*args, &block)
218 email = ActionMailer::Base.deliveries.last
218 email = ActionMailer::Base.deliveries.last
219 assert_not_nil email
219 assert_not_nil email
220 html_body = email.parts.detect {|part| part.content_type.include?('text/html')}.try(&:body)
220 html_body = email.parts.detect {|part| part.content_type.include?('text/html')}.try(&:body)
221 assert_not_nil html_body
221 assert_not_nil html_body
222 assert_select_in html_body.encoded, *args, &block
222 assert_select_in html_body.encoded, *args, &block
223 end
223 end
224
224
225 def assert_mail_body_match(expected, mail, message=nil)
225 def assert_mail_body_match(expected, mail, message=nil)
226 if expected.is_a?(String)
226 if expected.is_a?(String)
227 assert_include expected, mail_body(mail), message
227 assert_include expected, mail_body(mail), message
228 else
228 else
229 assert_match expected, mail_body(mail), message
229 assert_match expected, mail_body(mail), message
230 end
230 end
231 end
231 end
232
232
233 def assert_mail_body_no_match(expected, mail, message=nil)
233 def assert_mail_body_no_match(expected, mail, message=nil)
234 if expected.is_a?(String)
234 if expected.is_a?(String)
235 assert_not_include expected, mail_body(mail), message
235 assert_not_include expected, mail_body(mail), message
236 else
236 else
237 assert_no_match expected, mail_body(mail), message
237 assert_no_match expected, mail_body(mail), message
238 end
238 end
239 end
239 end
240
240
241 def mail_body(mail)
241 def mail_body(mail)
242 mail.parts.first.body.encoded
242 mail.parts.first.body.encoded
243 end
243 end
244
244
245 # Returns the lft value for a new root issue
245 # Returns the lft value for a new root issue
246 def new_issue_lft
246 def new_issue_lft
247 1
247 1
248 end
248 end
249 end
249 end
250
250
251 module Redmine
251 module Redmine
252 class RoutingTest < ActionDispatch::IntegrationTest
252 class RoutingTest < ActionDispatch::IntegrationTest
253 def should_route(arg)
253 def should_route(arg)
254 arg = arg.dup
254 arg = arg.dup
255 request = arg.keys.detect {|key| key.is_a?(String)}
255 request = arg.keys.detect {|key| key.is_a?(String)}
256 raise ArgumentError unless request
256 raise ArgumentError unless request
257 options = arg.slice!(request)
257 options = arg.slice!(request)
258
258
259 raise ArgumentError unless request =~ /\A(GET|POST|PUT|PATCH|DELETE)\s+(.+)\z/
259 raise ArgumentError unless request =~ /\A(GET|POST|PUT|PATCH|DELETE)\s+(.+)\z/
260 method, path = $1.downcase.to_sym, $2
260 method, path = $1.downcase.to_sym, $2
261
261
262 raise ArgumentError unless arg.values.first =~ /\A(.+)#(.+)\z/
262 raise ArgumentError unless arg.values.first =~ /\A(.+)#(.+)\z/
263 controller, action = $1, $2
263 controller, action = $1, $2
264
264
265 assert_routing(
265 assert_routing(
266 {:method => method, :path => path},
266 {:method => method, :path => path},
267 options.merge(:controller => controller, :action => action)
267 options.merge(:controller => controller, :action => action)
268 )
268 )
269 end
269 end
270 end
270 end
271
271
272 class IntegrationTest < ActionDispatch::IntegrationTest
272 class IntegrationTest < ActionDispatch::IntegrationTest
273 def log_user(login, password)
273 def log_user(login, password)
274 User.anonymous
274 User.anonymous
275 get "/login"
275 get "/login"
276 assert_equal nil, session[:user_id]
276 assert_equal nil, session[:user_id]
277 assert_response :success
277 assert_response :success
278 assert_template "account/login"
278 assert_template "account/login"
279 post "/login", :username => login, :password => password
279 post "/login", :username => login, :password => password
280 assert_equal login, User.find(session[:user_id]).login
280 assert_equal login, User.find(session[:user_id]).login
281 end
281 end
282
282
283 def credentials(user, password=nil)
283 def credentials(user, password=nil)
284 {'HTTP_AUTHORIZATION' => ActionController::HttpAuthentication::Basic.encode_credentials(user, password || user)}
284 {'HTTP_AUTHORIZATION' => ActionController::HttpAuthentication::Basic.encode_credentials(user, password || user)}
285 end
285 end
286 end
286 end
287
287
288 module ApiTest
288 module ApiTest
289 API_FORMATS = %w(json xml).freeze
289 API_FORMATS = %w(json xml).freeze
290
290
291 # Base class for API tests
291 # Base class for API tests
292 class Base < Redmine::IntegrationTest
292 class Base < Redmine::IntegrationTest
293 def setup
293 def setup
294 Setting.rest_api_enabled = '1'
294 Setting.rest_api_enabled = '1'
295 end
295 end
296
296
297 def teardown
297 def teardown
298 Setting.rest_api_enabled = '0'
298 Setting.rest_api_enabled = '0'
299 end
299 end
300
300
301 # Uploads content using the XML API and returns the attachment token
301 # Uploads content using the XML API and returns the attachment token
302 def xml_upload(content, credentials)
302 def xml_upload(content, credentials)
303 upload('xml', content, credentials)
303 upload('xml', content, credentials)
304 end
304 end
305
305
306 # Uploads content using the JSON API and returns the attachment token
306 # Uploads content using the JSON API and returns the attachment token
307 def json_upload(content, credentials)
307 def json_upload(content, credentials)
308 upload('json', content, credentials)
308 upload('json', content, credentials)
309 end
309 end
310
310
311 def upload(format, content, credentials)
311 def upload(format, content, credentials)
312 set_tmp_attachments_directory
312 set_tmp_attachments_directory
313 assert_difference 'Attachment.count' do
313 assert_difference 'Attachment.count' do
314 post "/uploads.#{format}", content, {"CONTENT_TYPE" => 'application/octet-stream'}.merge(credentials)
314 post "/uploads.#{format}", content, {"CONTENT_TYPE" => 'application/octet-stream'}.merge(credentials)
315 assert_response :created
315 assert_response :created
316 end
316 end
317 data = response_data
317 data = response_data
318 assert_kind_of Hash, data['upload']
318 assert_kind_of Hash, data['upload']
319 token = data['upload']['token']
319 token = data['upload']['token']
320 assert_not_nil token
320 assert_not_nil token
321 token
321 token
322 end
322 end
323
323
324 # Parses the response body based on its content type
324 # Parses the response body based on its content type
325 def response_data
325 def response_data
326 unless response.content_type.to_s =~ /^application\/(.+)/
326 unless response.content_type.to_s =~ /^application\/(.+)/
327 raise "Unexpected response type: #{response.content_type}"
327 raise "Unexpected response type: #{response.content_type}"
328 end
328 end
329 format = $1
329 format = $1
330 case format
330 case format
331 when 'xml'
331 when 'xml'
332 Hash.from_xml(response.body)
332 Hash.from_xml(response.body)
333 when 'json'
333 when 'json'
334 ActiveSupport::JSON.decode(response.body)
334 ActiveSupport::JSON.decode(response.body)
335 else
335 else
336 raise "Unknown response format: #{format}"
336 raise "Unknown response format: #{format}"
337 end
337 end
338 end
338 end
339 end
339 end
340
340
341 class Routing < Redmine::RoutingTest
341 class Routing < Redmine::RoutingTest
342 def should_route(arg)
342 def should_route(arg)
343 arg = arg.dup
343 arg = arg.dup
344 request = arg.keys.detect {|key| key.is_a?(String)}
344 request = arg.keys.detect {|key| key.is_a?(String)}
345 raise ArgumentError unless request
345 raise ArgumentError unless request
346 options = arg.slice!(request)
346 options = arg.slice!(request)
347
347
348 API_FORMATS.each do |format|
348 API_FORMATS.each do |format|
349 format_request = request.sub /$/, ".#{format}"
349 format_request = request.sub /$/, ".#{format}"
350 super options.merge(format_request => arg[request], :format => format)
350 super options.merge(format_request => arg[request], :format => format)
351 end
351 end
352 end
352 end
353 end
353 end
354 end
354 end
355 end
355 end
General Comments 0
You need to be logged in to leave comments. Login now