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