##// END OF EJS Templates
Restore settings even if a test failure occurs....
Jean-Philippe Lang -
r5107:3ef5daaf3518
parent child
Show More
@@ -1,441 +1,442
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 22 require File.join(RAILS_ROOT,'test', 'mocks', 'open_id_authentication_mock.rb')
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 Dir.mkdir "#{RAILS_ROOT}/tmp/test/attachments" unless File.directory?("#{RAILS_ROOT}/tmp/test/attachments")
83 83 Attachment.storage_path = "#{RAILS_ROOT}/tmp/test/attachments"
84 84 end
85 85
86 86 def with_settings(options, &block)
87 87 saved_settings = options.keys.inject({}) {|h, k| h[k] = Setting[k].dup; h}
88 88 options.each {|k, v| Setting[k] = v}
89 89 yield
90 ensure
90 91 saved_settings.each {|k, v| Setting[k] = v}
91 92 end
92 93
93 94 def change_user_password(login, new_password)
94 95 user = User.first(:conditions => {:login => login})
95 96 user.password, user.password_confirmation = new_password, new_password
96 97 user.save!
97 98 end
98 99
99 100 def self.ldap_configured?
100 101 @test_ldap = Net::LDAP.new(:host => '127.0.0.1', :port => 389)
101 102 return @test_ldap.bind
102 103 rescue Exception => e
103 104 # LDAP is not listening
104 105 return nil
105 106 end
106 107
107 108 # Returns the path to the test +vendor+ repository
108 109 def self.repository_path(vendor)
109 110 File.join(RAILS_ROOT.gsub(%r{config\/\.\.}, ''), "/tmp/test/#{vendor.downcase}_repository")
110 111 end
111 112
112 113 # Returns the url of the subversion test repository
113 114 def self.subversion_repository_url
114 115 path = repository_path('subversion')
115 116 path = '/' + path unless path.starts_with?('/')
116 117 "file://#{path}"
117 118 end
118 119
119 120 # Returns true if the +vendor+ test repository is configured
120 121 def self.repository_configured?(vendor)
121 122 File.directory?(repository_path(vendor))
122 123 end
123 124
124 125 def assert_error_tag(options={})
125 126 assert_tag({:attributes => { :id => 'errorExplanation' }}.merge(options))
126 127 end
127 128
128 129 # Shoulda macros
129 130 def self.should_render_404
130 131 should_respond_with :not_found
131 132 should_render_template 'common/error'
132 133 end
133 134
134 135 def self.should_have_before_filter(expected_method, options = {})
135 136 should_have_filter('before', expected_method, options)
136 137 end
137 138
138 139 def self.should_have_after_filter(expected_method, options = {})
139 140 should_have_filter('after', expected_method, options)
140 141 end
141 142
142 143 def self.should_have_filter(filter_type, expected_method, options)
143 144 description = "have #{filter_type}_filter :#{expected_method}"
144 145 description << " with #{options.inspect}" unless options.empty?
145 146
146 147 should description do
147 148 klass = "action_controller/filters/#{filter_type}_filter".classify.constantize
148 149 expected = klass.new(:filter, expected_method.to_sym, options)
149 150 assert_equal 1, @controller.class.filter_chain.select { |filter|
150 151 filter.method == expected.method && filter.kind == expected.kind &&
151 152 filter.options == expected.options && filter.class == expected.class
152 153 }.size
153 154 end
154 155 end
155 156
156 157 def self.should_show_the_old_and_new_values_for(prop_key, model, &block)
157 158 context "" do
158 159 setup do
159 160 if block_given?
160 161 instance_eval &block
161 162 else
162 163 @old_value = model.generate!
163 164 @new_value = model.generate!
164 165 end
165 166 end
166 167
167 168 should "use the new value's name" do
168 169 @detail = JournalDetail.generate!(:property => 'attr',
169 170 :old_value => @old_value.id,
170 171 :value => @new_value.id,
171 172 :prop_key => prop_key)
172 173
173 174 assert_match @new_value.name, show_detail(@detail, true)
174 175 end
175 176
176 177 should "use the old value's name" do
177 178 @detail = JournalDetail.generate!(:property => 'attr',
178 179 :old_value => @old_value.id,
179 180 :value => @new_value.id,
180 181 :prop_key => prop_key)
181 182
182 183 assert_match @old_value.name, show_detail(@detail, true)
183 184 end
184 185 end
185 186 end
186 187
187 188 def self.should_create_a_new_user(&block)
188 189 should "create a new user" do
189 190 user = instance_eval &block
190 191 assert user
191 192 assert_kind_of User, user
192 193 assert !user.new_record?
193 194 end
194 195 end
195 196
196 197 # Test that a request allows the three types of API authentication
197 198 #
198 199 # * HTTP Basic with username and password
199 200 # * HTTP Basic with an api key for the username
200 201 # * Key based with the key=X parameter
201 202 #
202 203 # @param [Symbol] http_method the HTTP method for request (:get, :post, :put, :delete)
203 204 # @param [String] url the request url
204 205 # @param [optional, Hash] parameters additional request parameters
205 206 # @param [optional, Hash] options additional options
206 207 # @option options [Symbol] :success_code Successful response code (:success)
207 208 # @option options [Symbol] :failure_code Failure response code (:unauthorized)
208 209 def self.should_allow_api_authentication(http_method, url, parameters={}, options={})
209 210 should_allow_http_basic_auth_with_username_and_password(http_method, url, parameters, options)
210 211 should_allow_http_basic_auth_with_key(http_method, url, parameters, options)
211 212 should_allow_key_based_auth(http_method, url, parameters, options)
212 213 end
213 214
214 215 # Test that a request allows the username and password for HTTP BASIC
215 216 #
216 217 # @param [Symbol] http_method the HTTP method for request (:get, :post, :put, :delete)
217 218 # @param [String] url the request url
218 219 # @param [optional, Hash] parameters additional request parameters
219 220 # @param [optional, Hash] options additional options
220 221 # @option options [Symbol] :success_code Successful response code (:success)
221 222 # @option options [Symbol] :failure_code Failure response code (:unauthorized)
222 223 def self.should_allow_http_basic_auth_with_username_and_password(http_method, url, parameters={}, options={})
223 224 success_code = options[:success_code] || :success
224 225 failure_code = options[:failure_code] || :unauthorized
225 226
226 227 context "should allow http basic auth using a username and password for #{http_method} #{url}" do
227 228 context "with a valid HTTP authentication" do
228 229 setup do
229 230 @user = User.generate_with_protected!(:password => 'my_password', :password_confirmation => 'my_password', :admin => true) # Admin so they can access the project
230 231 @authorization = ActionController::HttpAuthentication::Basic.encode_credentials(@user.login, 'my_password')
231 232 send(http_method, url, parameters, {:authorization => @authorization})
232 233 end
233 234
234 235 should_respond_with success_code
235 236 should_respond_with_content_type_based_on_url(url)
236 237 should "login as the user" do
237 238 assert_equal @user, User.current
238 239 end
239 240 end
240 241
241 242 context "with an invalid HTTP authentication" do
242 243 setup do
243 244 @user = User.generate_with_protected!
244 245 @authorization = ActionController::HttpAuthentication::Basic.encode_credentials(@user.login, 'wrong_password')
245 246 send(http_method, url, parameters, {:authorization => @authorization})
246 247 end
247 248
248 249 should_respond_with failure_code
249 250 should_respond_with_content_type_based_on_url(url)
250 251 should "not login as the user" do
251 252 assert_equal User.anonymous, User.current
252 253 end
253 254 end
254 255
255 256 context "without credentials" do
256 257 setup do
257 258 send(http_method, url, parameters, {:authorization => ''})
258 259 end
259 260
260 261 should_respond_with failure_code
261 262 should_respond_with_content_type_based_on_url(url)
262 263 should "include_www_authenticate_header" do
263 264 assert @controller.response.headers.has_key?('WWW-Authenticate')
264 265 end
265 266 end
266 267 end
267 268
268 269 end
269 270
270 271 # Test that a request allows the API key with HTTP BASIC
271 272 #
272 273 # @param [Symbol] http_method the HTTP method for request (:get, :post, :put, :delete)
273 274 # @param [String] url the request url
274 275 # @param [optional, Hash] parameters additional request parameters
275 276 # @param [optional, Hash] options additional options
276 277 # @option options [Symbol] :success_code Successful response code (:success)
277 278 # @option options [Symbol] :failure_code Failure response code (:unauthorized)
278 279 def self.should_allow_http_basic_auth_with_key(http_method, url, parameters={}, options={})
279 280 success_code = options[:success_code] || :success
280 281 failure_code = options[:failure_code] || :unauthorized
281 282
282 283 context "should allow http basic auth with a key for #{http_method} #{url}" do
283 284 context "with a valid HTTP authentication using the API token" do
284 285 setup do
285 286 @user = User.generate_with_protected!(:admin => true)
286 287 @token = Token.generate!(:user => @user, :action => 'api')
287 288 @authorization = ActionController::HttpAuthentication::Basic.encode_credentials(@token.value, 'X')
288 289 send(http_method, url, parameters, {:authorization => @authorization})
289 290 end
290 291
291 292 should_respond_with success_code
292 293 should_respond_with_content_type_based_on_url(url)
293 294 should_be_a_valid_response_string_based_on_url(url)
294 295 should "login as the user" do
295 296 assert_equal @user, User.current
296 297 end
297 298 end
298 299
299 300 context "with an invalid HTTP authentication" do
300 301 setup do
301 302 @user = User.generate_with_protected!
302 303 @token = Token.generate!(:user => @user, :action => 'feeds')
303 304 @authorization = ActionController::HttpAuthentication::Basic.encode_credentials(@token.value, 'X')
304 305 send(http_method, url, parameters, {:authorization => @authorization})
305 306 end
306 307
307 308 should_respond_with failure_code
308 309 should_respond_with_content_type_based_on_url(url)
309 310 should "not login as the user" do
310 311 assert_equal User.anonymous, User.current
311 312 end
312 313 end
313 314 end
314 315 end
315 316
316 317 # Test that a request allows full key authentication
317 318 #
318 319 # @param [Symbol] http_method the HTTP method for request (:get, :post, :put, :delete)
319 320 # @param [String] url the request url, without the key=ZXY parameter
320 321 # @param [optional, Hash] parameters additional request parameters
321 322 # @param [optional, Hash] options additional options
322 323 # @option options [Symbol] :success_code Successful response code (:success)
323 324 # @option options [Symbol] :failure_code Failure response code (:unauthorized)
324 325 def self.should_allow_key_based_auth(http_method, url, parameters={}, options={})
325 326 success_code = options[:success_code] || :success
326 327 failure_code = options[:failure_code] || :unauthorized
327 328
328 329 context "should allow key based auth using key=X for #{http_method} #{url}" do
329 330 context "with a valid api token" do
330 331 setup do
331 332 @user = User.generate_with_protected!(:admin => true)
332 333 @token = Token.generate!(:user => @user, :action => 'api')
333 334 # Simple url parse to add on ?key= or &key=
334 335 request_url = if url.match(/\?/)
335 336 url + "&key=#{@token.value}"
336 337 else
337 338 url + "?key=#{@token.value}"
338 339 end
339 340 send(http_method, request_url, parameters)
340 341 end
341 342
342 343 should_respond_with success_code
343 344 should_respond_with_content_type_based_on_url(url)
344 345 should_be_a_valid_response_string_based_on_url(url)
345 346 should "login as the user" do
346 347 assert_equal @user, User.current
347 348 end
348 349 end
349 350
350 351 context "with an invalid api token" do
351 352 setup do
352 353 @user = User.generate_with_protected!
353 354 @token = Token.generate!(:user => @user, :action => 'feeds')
354 355 # Simple url parse to add on ?key= or &key=
355 356 request_url = if url.match(/\?/)
356 357 url + "&key=#{@token.value}"
357 358 else
358 359 url + "?key=#{@token.value}"
359 360 end
360 361 send(http_method, request_url, parameters)
361 362 end
362 363
363 364 should_respond_with failure_code
364 365 should_respond_with_content_type_based_on_url(url)
365 366 should "not login as the user" do
366 367 assert_equal User.anonymous, User.current
367 368 end
368 369 end
369 370 end
370 371
371 372 context "should allow key based auth using X-Redmine-API-Key header for #{http_method} #{url}" do
372 373 setup do
373 374 @user = User.generate_with_protected!(:admin => true)
374 375 @token = Token.generate!(:user => @user, :action => 'api')
375 376 send(http_method, url, parameters, {'X-Redmine-API-Key' => @token.value.to_s})
376 377 end
377 378
378 379 should_respond_with success_code
379 380 should_respond_with_content_type_based_on_url(url)
380 381 should_be_a_valid_response_string_based_on_url(url)
381 382 should "login as the user" do
382 383 assert_equal @user, User.current
383 384 end
384 385 end
385 386 end
386 387
387 388 # Uses should_respond_with_content_type based on what's in the url:
388 389 #
389 390 # '/project/issues.xml' => should_respond_with_content_type :xml
390 391 # '/project/issues.json' => should_respond_with_content_type :json
391 392 #
392 393 # @param [String] url Request
393 394 def self.should_respond_with_content_type_based_on_url(url)
394 395 case
395 396 when url.match(/xml/i)
396 397 should_respond_with_content_type :xml
397 398 when url.match(/json/i)
398 399 should_respond_with_content_type :json
399 400 else
400 401 raise "Unknown content type for should_respond_with_content_type_based_on_url: #{url}"
401 402 end
402 403
403 404 end
404 405
405 406 # Uses the url to assert which format the response should be in
406 407 #
407 408 # '/project/issues.xml' => should_be_a_valid_xml_string
408 409 # '/project/issues.json' => should_be_a_valid_json_string
409 410 #
410 411 # @param [String] url Request
411 412 def self.should_be_a_valid_response_string_based_on_url(url)
412 413 case
413 414 when url.match(/xml/i)
414 415 should_be_a_valid_xml_string
415 416 when url.match(/json/i)
416 417 should_be_a_valid_json_string
417 418 else
418 419 raise "Unknown content type for should_be_a_valid_response_based_on_url: #{url}"
419 420 end
420 421
421 422 end
422 423
423 424 # Checks that the response is a valid JSON string
424 425 def self.should_be_a_valid_json_string
425 426 should "be a valid JSON string (or empty)" do
426 427 assert(response.body.blank? || ActiveSupport::JSON.decode(response.body))
427 428 end
428 429 end
429 430
430 431 # Checks that the response is a valid XML string
431 432 def self.should_be_a_valid_xml_string
432 433 should "be a valid XML string" do
433 434 assert REXML::Document.new(response.body)
434 435 end
435 436 end
436 437
437 438 end
438 439
439 440 # Simple module to "namespace" all of the API tests
440 441 module ApiTest
441 442 end
General Comments 0
You need to be logged in to leave comments. Login now