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