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