comparison test/test_helper.rb @ 1526:404aa68d4227

Merge from live branch
author Chris Cannam
date Thu, 11 Sep 2014 12:46:20 +0100
parents fb9a13467253
children
comparison
equal deleted inserted replaced
1493:a5f2bdf3b486 1526:404aa68d4227
1 # Redmine - project management software 1 # Redmine - project management software
2 # Copyright (C) 2006-2012 Jean-Philippe Lang 2 # Copyright (C) 2006-2014 Jean-Philippe Lang
3 # 3 #
4 # This program is free software; you can redistribute it and/or 4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License 5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2 6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version. 7 # of the License, or (at your option) any later version.
22 require Rails.root.join('test', 'mocks', 'open_id_authentication_mock.rb').to_s 22 require Rails.root.join('test', 'mocks', 'open_id_authentication_mock.rb').to_s
23 23
24 require File.expand_path(File.dirname(__FILE__) + '/object_helpers') 24 require File.expand_path(File.dirname(__FILE__) + '/object_helpers')
25 include ObjectHelpers 25 include ObjectHelpers
26 26
27 require 'awesome_nested_set/version'
28
27 class ActiveSupport::TestCase 29 class ActiveSupport::TestCase
28 include ActionDispatch::TestProcess 30 include ActionDispatch::TestProcess
29 31
30 # Transactional fixtures accelerate your tests by wrapping each test method
31 # in a transaction that's rolled back on completion. This ensures that the
32 # test database remains unchanged so your fixtures don't have to be reloaded
33 # between every test method. Fewer database queries means faster tests.
34 #
35 # Read Mike Clark's excellent walkthrough at
36 # http://clarkware.com/cgi/blosxom/2005/10/24#Rails10FastTesting
37 #
38 # Every Active Record database supports transactions except MyISAM tables
39 # in MySQL. Turn off transactional fixtures in this case; however, if you
40 # don't care one way or the other, switching from MyISAM to InnoDB tables
41 # is recommended.
42 self.use_transactional_fixtures = true 32 self.use_transactional_fixtures = true
43
44 # Instantiated fixtures are slow, but give you @david where otherwise you
45 # would need people(:david). If you don't want to migrate your existing
46 # test cases which use the @david style and don't mind the speed hit (each
47 # instantiated fixtures translates to a database query per test method),
48 # then set this back to true.
49 self.use_instantiated_fixtures = false 33 self.use_instantiated_fixtures = false
50 34
51 # Add more helper methods to be used by all tests here... 35 ESCAPED_CANT = 'can't'
36 ESCAPED_UCANT = 'Can't'
37 # Rails 4.0.2
38 #ESCAPED_CANT = 'can't'
39 #ESCAPED_UCANT = 'Can't'
52 40
53 def log_user(login, password) 41 def log_user(login, password)
54 User.anonymous 42 User.anonymous
55 get "/login" 43 get "/login"
56 assert_equal nil, session[:user_id] 44 assert_equal nil, session[:user_id]
105 def set_fixtures_attachments_directory 93 def set_fixtures_attachments_directory
106 Attachment.storage_path = "#{Rails.root}/test/fixtures/files" 94 Attachment.storage_path = "#{Rails.root}/test/fixtures/files"
107 end 95 end
108 96
109 def with_settings(options, &block) 97 def with_settings(options, &block)
110 saved_settings = options.keys.inject({}) {|h, k| h[k] = Setting[k].is_a?(Symbol) ? Setting[k] : Setting[k].dup; h} 98 saved_settings = options.keys.inject({}) do |h, k|
99 h[k] = case Setting[k]
100 when Symbol, false, true, nil
101 Setting[k]
102 else
103 Setting[k].dup
104 end
105 h
106 end
111 options.each {|k, v| Setting[k] = v} 107 options.each {|k, v| Setting[k] = v}
112 yield 108 yield
113 ensure 109 ensure
114 saved_settings.each {|k, v| Setting[k] = v} if saved_settings 110 saved_settings.each {|k, v| Setting[k] = v} if saved_settings
115 end 111 end
121 yield 117 yield
122 ensure 118 ensure
123 User.current = saved_user 119 User.current = saved_user
124 end 120 end
125 121
122 def with_locale(locale, &block)
123 saved_localed = ::I18n.locale
124 ::I18n.locale = locale
125 yield
126 ensure
127 ::I18n.locale = saved_localed
128 end
129
126 def change_user_password(login, new_password) 130 def change_user_password(login, new_password)
127 user = User.first(:conditions => {:login => login}) 131 user = User.where(:login => login).first
128 user.password, user.password_confirmation = new_password, new_password 132 user.password, user.password_confirmation = new_password, new_password
129 user.save! 133 user.save!
130 end 134 end
131 135
132 def self.ldap_configured? 136 def self.ldap_configured?
179 183
180 def assert_include(expected, s, message=nil) 184 def assert_include(expected, s, message=nil)
181 assert s.include?(expected), (message || "\"#{expected}\" not found in \"#{s}\"") 185 assert s.include?(expected), (message || "\"#{expected}\" not found in \"#{s}\"")
182 end 186 end
183 187
184 def assert_not_include(expected, s) 188 def assert_not_include(expected, s, message=nil)
185 assert !s.include?(expected), "\"#{expected}\" found in \"#{s}\"" 189 assert !s.include?(expected), (message || "\"#{expected}\" found in \"#{s}\"")
186 end 190 end
187 191
188 def assert_select_in(text, *args, &block) 192 def assert_select_in(text, *args, &block)
189 d = HTML::Document.new(CGI::unescapeHTML(String.new(text))).root 193 d = HTML::Document.new(CGI::unescapeHTML(String.new(text))).root
190 assert_select(d, *args, &block) 194 assert_select(d, *args, &block)
191 end 195 end
192 196
193 def assert_mail_body_match(expected, mail) 197 def assert_mail_body_match(expected, mail, message=nil)
194 if expected.is_a?(String) 198 if expected.is_a?(String)
195 assert_include expected, mail_body(mail) 199 assert_include expected, mail_body(mail), message
196 else 200 else
197 assert_match expected, mail_body(mail) 201 assert_match expected, mail_body(mail), message
198 end 202 end
199 end 203 end
200 204
201 def assert_mail_body_no_match(expected, mail) 205 def assert_mail_body_no_match(expected, mail, message=nil)
202 if expected.is_a?(String) 206 if expected.is_a?(String)
203 assert_not_include expected, mail_body(mail) 207 assert_not_include expected, mail_body(mail), message
204 else 208 else
205 assert_no_match expected, mail_body(mail) 209 assert_no_match expected, mail_body(mail), message
206 end 210 end
207 end 211 end
208 212
209 def mail_body(mail) 213 def mail_body(mail)
210 mail.parts.first.body.encoded 214 mail.parts.first.body.encoded
211 end 215 end
212 216
213 # Shoulda macros 217 # awesome_nested_set new node lft and rgt value changed this refactor revision.
214 def self.should_render_404 218 # https://github.com/collectiveidea/awesome_nested_set/commit/199fca9bb938e40200cd90714dc69247ef017c61
215 should_respond_with :not_found 219 # The reason of behavior change is that "self.class.base_class.unscoped" was added to this line.
216 should_render_template 'common/error' 220 # https://github.com/collectiveidea/awesome_nested_set/commit/199fca9bb9#diff-f61b59a5e6319024e211b0ffdd0e4ef1R273
217 end 221 # It seems correct behavior because of this line comment.
218 222 # https://github.com/collectiveidea/awesome_nested_set/blame/199fca9bb9/lib/awesome_nested_set/model.rb#L278
219 def self.should_have_before_filter(expected_method, options = {}) 223 def new_issue_lft
220 should_have_filter('before', expected_method, options) 224 ::AwesomeNestedSet::VERSION > "2.1.6" ? Issue.maximum(:rgt) + 1 : 1
221 end 225 end
222 226 end
223 def self.should_have_after_filter(expected_method, options = {}) 227
224 should_have_filter('after', expected_method, options) 228 module Redmine
225 end 229 module ApiTest
226 230 # Base class for API tests
227 def self.should_have_filter(filter_type, expected_method, options) 231 class Base < ActionDispatch::IntegrationTest
228 description = "have #{filter_type}_filter :#{expected_method}" 232 # Test that a request allows the three types of API authentication
229 description << " with #{options.inspect}" unless options.empty? 233 #
230 234 # * HTTP Basic with username and password
231 should description do 235 # * HTTP Basic with an api key for the username
232 klass = "action_controller/filters/#{filter_type}_filter".classify.constantize 236 # * Key based with the key=X parameter
233 expected = klass.new(:filter, expected_method.to_sym, options) 237 #
234 assert_equal 1, @controller.class.filter_chain.select { |filter| 238 # @param [Symbol] http_method the HTTP method for request (:get, :post, :put, :delete)
235 filter.method == expected.method && filter.kind == expected.kind && 239 # @param [String] url the request url
236 filter.options == expected.options && filter.class == expected.class 240 # @param [optional, Hash] parameters additional request parameters
237 }.size 241 # @param [optional, Hash] options additional options
242 # @option options [Symbol] :success_code Successful response code (:success)
243 # @option options [Symbol] :failure_code Failure response code (:unauthorized)
244 def self.should_allow_api_authentication(http_method, url, parameters={}, options={})
245 should_allow_http_basic_auth_with_username_and_password(http_method, url, parameters, options)
246 should_allow_http_basic_auth_with_key(http_method, url, parameters, options)
247 should_allow_key_based_auth(http_method, url, parameters, options)
248 end
249
250 # Test that a request allows the username and password for HTTP BASIC
251 #
252 # @param [Symbol] http_method the HTTP method for request (:get, :post, :put, :delete)
253 # @param [String] url the request url
254 # @param [optional, Hash] parameters additional request parameters
255 # @param [optional, Hash] options additional options
256 # @option options [Symbol] :success_code Successful response code (:success)
257 # @option options [Symbol] :failure_code Failure response code (:unauthorized)
258 def self.should_allow_http_basic_auth_with_username_and_password(http_method, url, parameters={}, options={})
259 success_code = options[:success_code] || :success
260 failure_code = options[:failure_code] || :unauthorized
261
262 context "should allow http basic auth using a username and password for #{http_method} #{url}" do
263 context "with a valid HTTP authentication" do
264 setup do
265 @user = User.generate! do |user|
266 user.admin = true
267 user.password = 'my_password'
268 end
269 send(http_method, url, parameters, credentials(@user.login, 'my_password'))
270 end
271
272 should_respond_with success_code
273 should_respond_with_content_type_based_on_url(url)
274 should "login as the user" do
275 assert_equal @user, User.current
276 end
277 end
278
279 context "with an invalid HTTP authentication" do
280 setup do
281 @user = User.generate!
282 send(http_method, url, parameters, credentials(@user.login, 'wrong_password'))
283 end
284
285 should_respond_with failure_code
286 should_respond_with_content_type_based_on_url(url)
287 should "not login as the user" do
288 assert_equal User.anonymous, User.current
289 end
290 end
291
292 context "without credentials" do
293 setup do
294 send(http_method, url, parameters)
295 end
296
297 should_respond_with failure_code
298 should_respond_with_content_type_based_on_url(url)
299 should "include_www_authenticate_header" do
300 assert @controller.response.headers.has_key?('WWW-Authenticate')
301 end
302 end
303 end
304 end
305
306 # Test that a request allows the API key with HTTP BASIC
307 #
308 # @param [Symbol] http_method the HTTP method for request (:get, :post, :put, :delete)
309 # @param [String] url the request url
310 # @param [optional, Hash] parameters additional request parameters
311 # @param [optional, Hash] options additional options
312 # @option options [Symbol] :success_code Successful response code (:success)
313 # @option options [Symbol] :failure_code Failure response code (:unauthorized)
314 def self.should_allow_http_basic_auth_with_key(http_method, url, parameters={}, options={})
315 success_code = options[:success_code] || :success
316 failure_code = options[:failure_code] || :unauthorized
317
318 context "should allow http basic auth with a key for #{http_method} #{url}" do
319 context "with a valid HTTP authentication using the API token" do
320 setup do
321 @user = User.generate! do |user|
322 user.admin = true
323 end
324 @token = Token.create!(:user => @user, :action => 'api')
325 send(http_method, url, parameters, credentials(@token.value, 'X'))
326 end
327 should_respond_with success_code
328 should_respond_with_content_type_based_on_url(url)
329 should_be_a_valid_response_string_based_on_url(url)
330 should "login as the user" do
331 assert_equal @user, User.current
332 end
333 end
334
335 context "with an invalid HTTP authentication" do
336 setup do
337 @user = User.generate!
338 @token = Token.create!(:user => @user, :action => 'feeds')
339 send(http_method, url, parameters, credentials(@token.value, 'X'))
340 end
341 should_respond_with failure_code
342 should_respond_with_content_type_based_on_url(url)
343 should "not login as the user" do
344 assert_equal User.anonymous, User.current
345 end
346 end
347 end
348 end
349
350 # Test that a request allows full key authentication
351 #
352 # @param [Symbol] http_method the HTTP method for request (:get, :post, :put, :delete)
353 # @param [String] url the request url, without the key=ZXY parameter
354 # @param [optional, Hash] parameters additional request parameters
355 # @param [optional, Hash] options additional options
356 # @option options [Symbol] :success_code Successful response code (:success)
357 # @option options [Symbol] :failure_code Failure response code (:unauthorized)
358 def self.should_allow_key_based_auth(http_method, url, parameters={}, options={})
359 success_code = options[:success_code] || :success
360 failure_code = options[:failure_code] || :unauthorized
361
362 context "should allow key based auth using key=X for #{http_method} #{url}" do
363 context "with a valid api token" do
364 setup do
365 @user = User.generate! do |user|
366 user.admin = true
367 end
368 @token = Token.create!(:user => @user, :action => 'api')
369 # Simple url parse to add on ?key= or &key=
370 request_url = if url.match(/\?/)
371 url + "&key=#{@token.value}"
372 else
373 url + "?key=#{@token.value}"
374 end
375 send(http_method, request_url, parameters)
376 end
377 should_respond_with success_code
378 should_respond_with_content_type_based_on_url(url)
379 should_be_a_valid_response_string_based_on_url(url)
380 should "login as the user" do
381 assert_equal @user, User.current
382 end
383 end
384
385 context "with an invalid api token" do
386 setup do
387 @user = User.generate! do |user|
388 user.admin = true
389 end
390 @token = Token.create!(:user => @user, :action => 'feeds')
391 # Simple url parse to add on ?key= or &key=
392 request_url = if url.match(/\?/)
393 url + "&key=#{@token.value}"
394 else
395 url + "?key=#{@token.value}"
396 end
397 send(http_method, request_url, parameters)
398 end
399 should_respond_with failure_code
400 should_respond_with_content_type_based_on_url(url)
401 should "not login as the user" do
402 assert_equal User.anonymous, User.current
403 end
404 end
405 end
406
407 context "should allow key based auth using X-Redmine-API-Key header for #{http_method} #{url}" do
408 setup do
409 @user = User.generate! do |user|
410 user.admin = true
411 end
412 @token = Token.create!(:user => @user, :action => 'api')
413 send(http_method, url, parameters, {'X-Redmine-API-Key' => @token.value.to_s})
414 end
415 should_respond_with success_code
416 should_respond_with_content_type_based_on_url(url)
417 should_be_a_valid_response_string_based_on_url(url)
418 should "login as the user" do
419 assert_equal @user, User.current
420 end
421 end
422 end
423
424 # Uses should_respond_with_content_type based on what's in the url:
425 #
426 # '/project/issues.xml' => should_respond_with_content_type :xml
427 # '/project/issues.json' => should_respond_with_content_type :json
428 #
429 # @param [String] url Request
430 def self.should_respond_with_content_type_based_on_url(url)
431 case
432 when url.match(/xml/i)
433 should "respond with XML" do
434 assert_equal 'application/xml', @response.content_type
435 end
436 when url.match(/json/i)
437 should "respond with JSON" do
438 assert_equal 'application/json', @response.content_type
439 end
440 else
441 raise "Unknown content type for should_respond_with_content_type_based_on_url: #{url}"
442 end
443 end
444
445 # Uses the url to assert which format the response should be in
446 #
447 # '/project/issues.xml' => should_be_a_valid_xml_string
448 # '/project/issues.json' => should_be_a_valid_json_string
449 #
450 # @param [String] url Request
451 def self.should_be_a_valid_response_string_based_on_url(url)
452 case
453 when url.match(/xml/i)
454 should_be_a_valid_xml_string
455 when url.match(/json/i)
456 should_be_a_valid_json_string
457 else
458 raise "Unknown content type for should_be_a_valid_response_based_on_url: #{url}"
459 end
460 end
461
462 # Checks that the response is a valid JSON string
463 def self.should_be_a_valid_json_string
464 should "be a valid JSON string (or empty)" do
465 assert(response.body.blank? || ActiveSupport::JSON.decode(response.body))
466 end
467 end
468
469 # Checks that the response is a valid XML string
470 def self.should_be_a_valid_xml_string
471 should "be a valid XML string" do
472 assert REXML::Document.new(response.body)
473 end
474 end
475
476 def self.should_respond_with(status)
477 should "respond with #{status}" do
478 assert_response status
479 end
480 end
238 end 481 end
239 end 482 end
240
241 # Test that a request allows the three types of API authentication
242 #
243 # * HTTP Basic with username and password
244 # * HTTP Basic with an api key for the username
245 # * Key based with the key=X parameter
246 #
247 # @param [Symbol] http_method the HTTP method for request (:get, :post, :put, :delete)
248 # @param [String] url the request url
249 # @param [optional, Hash] parameters additional request parameters
250 # @param [optional, Hash] options additional options
251 # @option options [Symbol] :success_code Successful response code (:success)
252 # @option options [Symbol] :failure_code Failure response code (:unauthorized)
253 def self.should_allow_api_authentication(http_method, url, parameters={}, options={})
254 should_allow_http_basic_auth_with_username_and_password(http_method, url, parameters, options)
255 should_allow_http_basic_auth_with_key(http_method, url, parameters, options)
256 should_allow_key_based_auth(http_method, url, parameters, options)
257 end
258
259 # Test that a request allows the username and password for HTTP BASIC
260 #
261 # @param [Symbol] http_method the HTTP method for request (:get, :post, :put, :delete)
262 # @param [String] url the request url
263 # @param [optional, Hash] parameters additional request parameters
264 # @param [optional, Hash] options additional options
265 # @option options [Symbol] :success_code Successful response code (:success)
266 # @option options [Symbol] :failure_code Failure response code (:unauthorized)
267 def self.should_allow_http_basic_auth_with_username_and_password(http_method, url, parameters={}, options={})
268 success_code = options[:success_code] || :success
269 failure_code = options[:failure_code] || :unauthorized
270
271 context "should allow http basic auth using a username and password for #{http_method} #{url}" do
272 context "with a valid HTTP authentication" do
273 setup do
274 @user = User.generate! do |user|
275 user.admin = true
276 user.password = 'my_password'
277 end
278 send(http_method, url, parameters, credentials(@user.login, 'my_password'))
279 end
280
281 should_respond_with success_code
282 should_respond_with_content_type_based_on_url(url)
283 should "login as the user" do
284 assert_equal @user, User.current
285 end
286 end
287
288 context "with an invalid HTTP authentication" do
289 setup do
290 @user = User.generate!
291 send(http_method, url, parameters, credentials(@user.login, 'wrong_password'))
292 end
293
294 should_respond_with failure_code
295 should_respond_with_content_type_based_on_url(url)
296 should "not login as the user" do
297 assert_equal User.anonymous, User.current
298 end
299 end
300
301 context "without credentials" do
302 setup do
303 send(http_method, url, parameters)
304 end
305
306 should_respond_with failure_code
307 should_respond_with_content_type_based_on_url(url)
308 should "include_www_authenticate_header" do
309 assert @controller.response.headers.has_key?('WWW-Authenticate')
310 end
311 end
312 end
313 end
314
315 # Test that a request allows the API key with HTTP BASIC
316 #
317 # @param [Symbol] http_method the HTTP method for request (:get, :post, :put, :delete)
318 # @param [String] url the request url
319 # @param [optional, Hash] parameters additional request parameters
320 # @param [optional, Hash] options additional options
321 # @option options [Symbol] :success_code Successful response code (:success)
322 # @option options [Symbol] :failure_code Failure response code (:unauthorized)
323 def self.should_allow_http_basic_auth_with_key(http_method, url, parameters={}, options={})
324 success_code = options[:success_code] || :success
325 failure_code = options[:failure_code] || :unauthorized
326
327 context "should allow http basic auth with a key for #{http_method} #{url}" do
328 context "with a valid HTTP authentication using the API token" do
329 setup do
330 @user = User.generate! do |user|
331 user.admin = true
332 end
333 @token = Token.create!(:user => @user, :action => 'api')
334 send(http_method, url, parameters, credentials(@token.value, 'X'))
335 end
336 should_respond_with success_code
337 should_respond_with_content_type_based_on_url(url)
338 should_be_a_valid_response_string_based_on_url(url)
339 should "login as the user" do
340 assert_equal @user, User.current
341 end
342 end
343
344 context "with an invalid HTTP authentication" do
345 setup do
346 @user = User.generate!
347 @token = Token.create!(:user => @user, :action => 'feeds')
348 send(http_method, url, parameters, credentials(@token.value, 'X'))
349 end
350 should_respond_with failure_code
351 should_respond_with_content_type_based_on_url(url)
352 should "not login as the user" do
353 assert_equal User.anonymous, User.current
354 end
355 end
356 end
357 end
358
359 # Test that a request allows full key authentication
360 #
361 # @param [Symbol] http_method the HTTP method for request (:get, :post, :put, :delete)
362 # @param [String] url the request url, without the key=ZXY parameter
363 # @param [optional, Hash] parameters additional request parameters
364 # @param [optional, Hash] options additional options
365 # @option options [Symbol] :success_code Successful response code (:success)
366 # @option options [Symbol] :failure_code Failure response code (:unauthorized)
367 def self.should_allow_key_based_auth(http_method, url, parameters={}, options={})
368 success_code = options[:success_code] || :success
369 failure_code = options[:failure_code] || :unauthorized
370
371 context "should allow key based auth using key=X for #{http_method} #{url}" do
372 context "with a valid api token" do
373 setup do
374 @user = User.generate! do |user|
375 user.admin = true
376 end
377 @token = Token.create!(:user => @user, :action => 'api')
378 # Simple url parse to add on ?key= or &key=
379 request_url = if url.match(/\?/)
380 url + "&key=#{@token.value}"
381 else
382 url + "?key=#{@token.value}"
383 end
384 send(http_method, request_url, parameters)
385 end
386 should_respond_with success_code
387 should_respond_with_content_type_based_on_url(url)
388 should_be_a_valid_response_string_based_on_url(url)
389 should "login as the user" do
390 assert_equal @user, User.current
391 end
392 end
393
394 context "with an invalid api token" do
395 setup do
396 @user = User.generate! do |user|
397 user.admin = true
398 end
399 @token = Token.create!(:user => @user, :action => 'feeds')
400 # Simple url parse to add on ?key= or &key=
401 request_url = if url.match(/\?/)
402 url + "&key=#{@token.value}"
403 else
404 url + "?key=#{@token.value}"
405 end
406 send(http_method, request_url, parameters)
407 end
408 should_respond_with failure_code
409 should_respond_with_content_type_based_on_url(url)
410 should "not login as the user" do
411 assert_equal User.anonymous, User.current
412 end
413 end
414 end
415
416 context "should allow key based auth using X-Redmine-API-Key header for #{http_method} #{url}" do
417 setup do
418 @user = User.generate! do |user|
419 user.admin = true
420 end
421 @token = Token.create!(:user => @user, :action => 'api')
422 send(http_method, url, parameters, {'X-Redmine-API-Key' => @token.value.to_s})
423 end
424 should_respond_with success_code
425 should_respond_with_content_type_based_on_url(url)
426 should_be_a_valid_response_string_based_on_url(url)
427 should "login as the user" do
428 assert_equal @user, User.current
429 end
430 end
431 end
432
433 # Uses should_respond_with_content_type based on what's in the url:
434 #
435 # '/project/issues.xml' => should_respond_with_content_type :xml
436 # '/project/issues.json' => should_respond_with_content_type :json
437 #
438 # @param [String] url Request
439 def self.should_respond_with_content_type_based_on_url(url)
440 case
441 when url.match(/xml/i)
442 should "respond with XML" do
443 assert_equal 'application/xml', @response.content_type
444 end
445 when url.match(/json/i)
446 should "respond with JSON" do
447 assert_equal 'application/json', @response.content_type
448 end
449 else
450 raise "Unknown content type for should_respond_with_content_type_based_on_url: #{url}"
451 end
452 end
453
454 # Uses the url to assert which format the response should be in
455 #
456 # '/project/issues.xml' => should_be_a_valid_xml_string
457 # '/project/issues.json' => should_be_a_valid_json_string
458 #
459 # @param [String] url Request
460 def self.should_be_a_valid_response_string_based_on_url(url)
461 case
462 when url.match(/xml/i)
463 should_be_a_valid_xml_string
464 when url.match(/json/i)
465 should_be_a_valid_json_string
466 else
467 raise "Unknown content type for should_be_a_valid_response_based_on_url: #{url}"
468 end
469 end
470
471 # Checks that the response is a valid JSON string
472 def self.should_be_a_valid_json_string
473 should "be a valid JSON string (or empty)" do
474 assert(response.body.blank? || ActiveSupport::JSON.decode(response.body))
475 end
476 end
477
478 # Checks that the response is a valid XML string
479 def self.should_be_a_valid_xml_string
480 should "be a valid XML string" do
481 assert REXML::Document.new(response.body)
482 end
483 end
484
485 def self.should_respond_with(status)
486 should "respond with #{status}" do
487 assert_response status
488 end
489 end
490 end 483 end
491 484
492 # Simple module to "namespace" all of the API tests 485 # URL helpers do not work with config.threadsafe!
493 module ApiTest 486 # https://github.com/rspec/rspec-rails/issues/476#issuecomment-4705454
487 ActionView::TestCase::TestController.instance_eval do
488 helper Rails.application.routes.url_helpers
494 end 489 end
490 ActionView::TestCase::TestController.class_eval do
491 def _routes
492 Rails.application.routes
493 end
494 end