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