##// END OF EJS Templates
Ruby 1.9: fix tests error due to require path at test/test_helper.rb...
Toshi MARUYAMA -
r7682:c54b33ddca34
parent child
Show More
@@ -1,448 +1,448
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2011 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 ENV["RAILS_ENV"] = "test"
19 19 require File.expand_path(File.dirname(__FILE__) + "/../config/environment")
20 20 require 'test_help'
21 21 require File.expand_path(File.dirname(__FILE__) + '/helper_testcase')
22 require Rails.root.join('test', 'mocks', 'open_id_authentication_mock.rb')
22 require Rails.root.join('test', 'mocks', 'open_id_authentication_mock.rb').to_s
23 23
24 24 require File.expand_path(File.dirname(__FILE__) + '/object_daddy_helpers')
25 25 include ObjectDaddyHelpers
26 26
27 27 class ActiveSupport::TestCase
28 28 # Transactional fixtures accelerate your tests by wrapping each test method
29 29 # in a transaction that's rolled back on completion. This ensures that the
30 30 # test database remains unchanged so your fixtures don't have to be reloaded
31 31 # between every test method. Fewer database queries means faster tests.
32 32 #
33 33 # Read Mike Clark's excellent walkthrough at
34 34 # http://clarkware.com/cgi/blosxom/2005/10/24#Rails10FastTesting
35 35 #
36 36 # Every Active Record database supports transactions except MyISAM tables
37 37 # in MySQL. Turn off transactional fixtures in this case; however, if you
38 38 # don't care one way or the other, switching from MyISAM to InnoDB tables
39 39 # is recommended.
40 40 self.use_transactional_fixtures = true
41 41
42 42 # Instantiated fixtures are slow, but give you @david where otherwise you
43 43 # would need people(:david). If you don't want to migrate your existing
44 44 # test cases which use the @david style and don't mind the speed hit (each
45 45 # instantiated fixtures translates to a database query per test method),
46 46 # then set this back to true.
47 47 self.use_instantiated_fixtures = false
48 48
49 49 # Add more helper methods to be used by all tests here...
50 50
51 51 def log_user(login, password)
52 52 User.anonymous
53 53 get "/login"
54 54 assert_equal nil, session[:user_id]
55 55 assert_response :success
56 56 assert_template "account/login"
57 57 post "/login", :username => login, :password => password
58 58 assert_equal login, User.find(session[:user_id]).login
59 59 end
60 60
61 61 def uploaded_test_file(name, mime)
62 62 ActionController::TestUploadedFile.new(ActiveSupport::TestCase.fixture_path + "/files/#{name}", mime, true)
63 63 end
64 64
65 65 # Mock out a file
66 66 def self.mock_file
67 67 file = 'a_file.png'
68 68 file.stubs(:size).returns(32)
69 69 file.stubs(:original_filename).returns('a_file.png')
70 70 file.stubs(:content_type).returns('image/png')
71 71 file.stubs(:read).returns(false)
72 72 file
73 73 end
74 74
75 75 def mock_file
76 76 self.class.mock_file
77 77 end
78 78
79 79 # Use a temporary directory for attachment related tests
80 80 def set_tmp_attachments_directory
81 81 Dir.mkdir "#{Rails.root}/tmp/test" unless File.directory?("#{Rails.root}/tmp/test")
82 82 unless File.directory?("#{Rails.root}/tmp/test/attachments")
83 83 Dir.mkdir "#{Rails.root}/tmp/test/attachments"
84 84 end
85 85 Attachment.storage_path = "#{Rails.root}/tmp/test/attachments"
86 86 end
87 87
88 88 def with_settings(options, &block)
89 89 saved_settings = options.keys.inject({}) {|h, k| h[k] = Setting[k].dup; h}
90 90 options.each {|k, v| Setting[k] = v}
91 91 yield
92 92 ensure
93 93 saved_settings.each {|k, v| Setting[k] = v}
94 94 end
95 95
96 96 def change_user_password(login, new_password)
97 97 user = User.first(:conditions => {:login => login})
98 98 user.password, user.password_confirmation = new_password, new_password
99 99 user.save!
100 100 end
101 101
102 102 def self.ldap_configured?
103 103 @test_ldap = Net::LDAP.new(:host => '127.0.0.1', :port => 389)
104 104 return @test_ldap.bind
105 105 rescue Exception => e
106 106 # LDAP is not listening
107 107 return nil
108 108 end
109 109
110 110 # Returns the path to the test +vendor+ repository
111 111 def self.repository_path(vendor)
112 112 Rails.root.join("tmp/test/#{vendor.downcase}_repository").to_s
113 113 end
114 114
115 115 # Returns the url of the subversion test repository
116 116 def self.subversion_repository_url
117 117 path = repository_path('subversion')
118 118 path = '/' + path unless path.starts_with?('/')
119 119 "file://#{path}"
120 120 end
121 121
122 122 # Returns true if the +vendor+ test repository is configured
123 123 def self.repository_configured?(vendor)
124 124 File.directory?(repository_path(vendor))
125 125 end
126 126
127 127 def assert_error_tag(options={})
128 128 assert_tag({:attributes => { :id => 'errorExplanation' }}.merge(options))
129 129 end
130 130
131 131 def assert_include(expected, s)
132 132 assert s.include?(expected), "\"#{expected}\" not found in \"#{s}\""
133 133 end
134 134
135 135 # Shoulda macros
136 136 def self.should_render_404
137 137 should_respond_with :not_found
138 138 should_render_template 'common/error'
139 139 end
140 140
141 141 def self.should_have_before_filter(expected_method, options = {})
142 142 should_have_filter('before', expected_method, options)
143 143 end
144 144
145 145 def self.should_have_after_filter(expected_method, options = {})
146 146 should_have_filter('after', expected_method, options)
147 147 end
148 148
149 149 def self.should_have_filter(filter_type, expected_method, options)
150 150 description = "have #{filter_type}_filter :#{expected_method}"
151 151 description << " with #{options.inspect}" unless options.empty?
152 152
153 153 should description do
154 154 klass = "action_controller/filters/#{filter_type}_filter".classify.constantize
155 155 expected = klass.new(:filter, expected_method.to_sym, options)
156 156 assert_equal 1, @controller.class.filter_chain.select { |filter|
157 157 filter.method == expected.method && filter.kind == expected.kind &&
158 158 filter.options == expected.options && filter.class == expected.class
159 159 }.size
160 160 end
161 161 end
162 162
163 163 def self.should_show_the_old_and_new_values_for(prop_key, model, &block)
164 164 context "" do
165 165 setup do
166 166 if block_given?
167 167 instance_eval &block
168 168 else
169 169 @old_value = model.generate!
170 170 @new_value = model.generate!
171 171 end
172 172 end
173 173
174 174 should "use the new value's name" do
175 175 @detail = JournalDetail.generate!(:property => 'attr',
176 176 :old_value => @old_value.id,
177 177 :value => @new_value.id,
178 178 :prop_key => prop_key)
179 179
180 180 assert_match @new_value.name, show_detail(@detail, true)
181 181 end
182 182
183 183 should "use the old value's name" do
184 184 @detail = JournalDetail.generate!(:property => 'attr',
185 185 :old_value => @old_value.id,
186 186 :value => @new_value.id,
187 187 :prop_key => prop_key)
188 188
189 189 assert_match @old_value.name, show_detail(@detail, true)
190 190 end
191 191 end
192 192 end
193 193
194 194 def self.should_create_a_new_user(&block)
195 195 should "create a new user" do
196 196 user = instance_eval &block
197 197 assert user
198 198 assert_kind_of User, user
199 199 assert !user.new_record?
200 200 end
201 201 end
202 202
203 203 # Test that a request allows the three types of API authentication
204 204 #
205 205 # * HTTP Basic with username and password
206 206 # * HTTP Basic with an api key for the username
207 207 # * Key based with the key=X parameter
208 208 #
209 209 # @param [Symbol] http_method the HTTP method for request (:get, :post, :put, :delete)
210 210 # @param [String] url the request url
211 211 # @param [optional, Hash] parameters additional request parameters
212 212 # @param [optional, Hash] options additional options
213 213 # @option options [Symbol] :success_code Successful response code (:success)
214 214 # @option options [Symbol] :failure_code Failure response code (:unauthorized)
215 215 def self.should_allow_api_authentication(http_method, url, parameters={}, options={})
216 216 should_allow_http_basic_auth_with_username_and_password(http_method, url, parameters, options)
217 217 should_allow_http_basic_auth_with_key(http_method, url, parameters, options)
218 218 should_allow_key_based_auth(http_method, url, parameters, options)
219 219 end
220 220
221 221 # Test that a request allows the username and password for HTTP BASIC
222 222 #
223 223 # @param [Symbol] http_method the HTTP method for request (:get, :post, :put, :delete)
224 224 # @param [String] url the request url
225 225 # @param [optional, Hash] parameters additional request parameters
226 226 # @param [optional, Hash] options additional options
227 227 # @option options [Symbol] :success_code Successful response code (:success)
228 228 # @option options [Symbol] :failure_code Failure response code (:unauthorized)
229 229 def self.should_allow_http_basic_auth_with_username_and_password(http_method, url, parameters={}, options={})
230 230 success_code = options[:success_code] || :success
231 231 failure_code = options[:failure_code] || :unauthorized
232 232
233 233 context "should allow http basic auth using a username and password for #{http_method} #{url}" do
234 234 context "with a valid HTTP authentication" do
235 235 setup do
236 236 @user = User.generate_with_protected!(:password => 'my_password', :password_confirmation => 'my_password', :admin => true) # Admin so they can access the project
237 237 @authorization = ActionController::HttpAuthentication::Basic.encode_credentials(@user.login, 'my_password')
238 238 send(http_method, url, parameters, {:authorization => @authorization})
239 239 end
240 240
241 241 should_respond_with success_code
242 242 should_respond_with_content_type_based_on_url(url)
243 243 should "login as the user" do
244 244 assert_equal @user, User.current
245 245 end
246 246 end
247 247
248 248 context "with an invalid HTTP authentication" do
249 249 setup do
250 250 @user = User.generate_with_protected!
251 251 @authorization = ActionController::HttpAuthentication::Basic.encode_credentials(@user.login, 'wrong_password')
252 252 send(http_method, url, parameters, {:authorization => @authorization})
253 253 end
254 254
255 255 should_respond_with failure_code
256 256 should_respond_with_content_type_based_on_url(url)
257 257 should "not login as the user" do
258 258 assert_equal User.anonymous, User.current
259 259 end
260 260 end
261 261
262 262 context "without credentials" do
263 263 setup do
264 264 send(http_method, url, parameters, {:authorization => ''})
265 265 end
266 266
267 267 should_respond_with failure_code
268 268 should_respond_with_content_type_based_on_url(url)
269 269 should "include_www_authenticate_header" do
270 270 assert @controller.response.headers.has_key?('WWW-Authenticate')
271 271 end
272 272 end
273 273 end
274 274
275 275 end
276 276
277 277 # Test that a request allows the API key with HTTP BASIC
278 278 #
279 279 # @param [Symbol] http_method the HTTP method for request (:get, :post, :put, :delete)
280 280 # @param [String] url the request url
281 281 # @param [optional, Hash] parameters additional request parameters
282 282 # @param [optional, Hash] options additional options
283 283 # @option options [Symbol] :success_code Successful response code (:success)
284 284 # @option options [Symbol] :failure_code Failure response code (:unauthorized)
285 285 def self.should_allow_http_basic_auth_with_key(http_method, url, parameters={}, options={})
286 286 success_code = options[:success_code] || :success
287 287 failure_code = options[:failure_code] || :unauthorized
288 288
289 289 context "should allow http basic auth with a key for #{http_method} #{url}" do
290 290 context "with a valid HTTP authentication using the API token" do
291 291 setup do
292 292 @user = User.generate_with_protected!(:admin => true)
293 293 @token = Token.generate!(:user => @user, :action => 'api')
294 294 @authorization = ActionController::HttpAuthentication::Basic.encode_credentials(@token.value, 'X')
295 295 send(http_method, url, parameters, {:authorization => @authorization})
296 296 end
297 297
298 298 should_respond_with success_code
299 299 should_respond_with_content_type_based_on_url(url)
300 300 should_be_a_valid_response_string_based_on_url(url)
301 301 should "login as the user" do
302 302 assert_equal @user, User.current
303 303 end
304 304 end
305 305
306 306 context "with an invalid HTTP authentication" do
307 307 setup do
308 308 @user = User.generate_with_protected!
309 309 @token = Token.generate!(:user => @user, :action => 'feeds')
310 310 @authorization = ActionController::HttpAuthentication::Basic.encode_credentials(@token.value, 'X')
311 311 send(http_method, url, parameters, {:authorization => @authorization})
312 312 end
313 313
314 314 should_respond_with failure_code
315 315 should_respond_with_content_type_based_on_url(url)
316 316 should "not login as the user" do
317 317 assert_equal User.anonymous, User.current
318 318 end
319 319 end
320 320 end
321 321 end
322 322
323 323 # Test that a request allows full key authentication
324 324 #
325 325 # @param [Symbol] http_method the HTTP method for request (:get, :post, :put, :delete)
326 326 # @param [String] url the request url, without the key=ZXY parameter
327 327 # @param [optional, Hash] parameters additional request parameters
328 328 # @param [optional, Hash] options additional options
329 329 # @option options [Symbol] :success_code Successful response code (:success)
330 330 # @option options [Symbol] :failure_code Failure response code (:unauthorized)
331 331 def self.should_allow_key_based_auth(http_method, url, parameters={}, options={})
332 332 success_code = options[:success_code] || :success
333 333 failure_code = options[:failure_code] || :unauthorized
334 334
335 335 context "should allow key based auth using key=X for #{http_method} #{url}" do
336 336 context "with a valid api token" do
337 337 setup do
338 338 @user = User.generate_with_protected!(:admin => true)
339 339 @token = Token.generate!(:user => @user, :action => 'api')
340 340 # Simple url parse to add on ?key= or &key=
341 341 request_url = if url.match(/\?/)
342 342 url + "&key=#{@token.value}"
343 343 else
344 344 url + "?key=#{@token.value}"
345 345 end
346 346 send(http_method, request_url, parameters)
347 347 end
348 348
349 349 should_respond_with success_code
350 350 should_respond_with_content_type_based_on_url(url)
351 351 should_be_a_valid_response_string_based_on_url(url)
352 352 should "login as the user" do
353 353 assert_equal @user, User.current
354 354 end
355 355 end
356 356
357 357 context "with an invalid api token" do
358 358 setup do
359 359 @user = User.generate_with_protected!
360 360 @token = Token.generate!(:user => @user, :action => 'feeds')
361 361 # Simple url parse to add on ?key= or &key=
362 362 request_url = if url.match(/\?/)
363 363 url + "&key=#{@token.value}"
364 364 else
365 365 url + "?key=#{@token.value}"
366 366 end
367 367 send(http_method, request_url, parameters)
368 368 end
369 369
370 370 should_respond_with failure_code
371 371 should_respond_with_content_type_based_on_url(url)
372 372 should "not login as the user" do
373 373 assert_equal User.anonymous, User.current
374 374 end
375 375 end
376 376 end
377 377
378 378 context "should allow key based auth using X-Redmine-API-Key header for #{http_method} #{url}" do
379 379 setup do
380 380 @user = User.generate_with_protected!(:admin => true)
381 381 @token = Token.generate!(:user => @user, :action => 'api')
382 382 send(http_method, url, parameters, {'X-Redmine-API-Key' => @token.value.to_s})
383 383 end
384 384
385 385 should_respond_with success_code
386 386 should_respond_with_content_type_based_on_url(url)
387 387 should_be_a_valid_response_string_based_on_url(url)
388 388 should "login as the user" do
389 389 assert_equal @user, User.current
390 390 end
391 391 end
392 392 end
393 393
394 394 # Uses should_respond_with_content_type based on what's in the url:
395 395 #
396 396 # '/project/issues.xml' => should_respond_with_content_type :xml
397 397 # '/project/issues.json' => should_respond_with_content_type :json
398 398 #
399 399 # @param [String] url Request
400 400 def self.should_respond_with_content_type_based_on_url(url)
401 401 case
402 402 when url.match(/xml/i)
403 403 should_respond_with_content_type :xml
404 404 when url.match(/json/i)
405 405 should_respond_with_content_type :json
406 406 else
407 407 raise "Unknown content type for should_respond_with_content_type_based_on_url: #{url}"
408 408 end
409 409
410 410 end
411 411
412 412 # Uses the url to assert which format the response should be in
413 413 #
414 414 # '/project/issues.xml' => should_be_a_valid_xml_string
415 415 # '/project/issues.json' => should_be_a_valid_json_string
416 416 #
417 417 # @param [String] url Request
418 418 def self.should_be_a_valid_response_string_based_on_url(url)
419 419 case
420 420 when url.match(/xml/i)
421 421 should_be_a_valid_xml_string
422 422 when url.match(/json/i)
423 423 should_be_a_valid_json_string
424 424 else
425 425 raise "Unknown content type for should_be_a_valid_response_based_on_url: #{url}"
426 426 end
427 427
428 428 end
429 429
430 430 # Checks that the response is a valid JSON string
431 431 def self.should_be_a_valid_json_string
432 432 should "be a valid JSON string (or empty)" do
433 433 assert(response.body.blank? || ActiveSupport::JSON.decode(response.body))
434 434 end
435 435 end
436 436
437 437 # Checks that the response is a valid XML string
438 438 def self.should_be_a_valid_xml_string
439 439 should "be a valid XML string" do
440 440 assert REXML::Document.new(response.body)
441 441 end
442 442 end
443 443
444 444 end
445 445
446 446 # Simple module to "namespace" all of the API tests
447 447 module ApiTest
448 448 end
General Comments 0
You need to be logged in to leave comments. Login now