annotate .svn/pristine/c8/c8ead97161810444d935cbc0189f12e56246f822.svn-base @ 1298:4f746d8966dd redmine_2.3_integration

Merge from redmine-2.3 branch to create new branch redmine-2.3-integration
author Chris Cannam
date Fri, 14 Jun 2013 09:28:30 +0100
parents 622f24f53b42
children
rev   line source
Chris@1295 1 # Redmine - project management software
Chris@1295 2 # Copyright (C) 2006-2012 Jean-Philippe Lang
Chris@1295 3 #
Chris@1295 4 # This program is free software; you can redistribute it and/or
Chris@1295 5 # modify it under the terms of the GNU General Public License
Chris@1295 6 # as published by the Free Software Foundation; either version 2
Chris@1295 7 # of the License, or (at your option) any later version.
Chris@1295 8 #
Chris@1295 9 # This program is distributed in the hope that it will be useful,
Chris@1295 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@1295 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@1295 12 # GNU General Public License for more details.
Chris@1295 13 #
Chris@1295 14 # You should have received a copy of the GNU General Public License
Chris@1295 15 # along with this program; if not, write to the Free Software
Chris@1295 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Chris@1295 17
Chris@1295 18 #require 'shoulda'
Chris@1295 19 ENV["RAILS_ENV"] = "test"
Chris@1295 20 require File.expand_path(File.dirname(__FILE__) + "/../config/environment")
Chris@1295 21 require 'rails/test_help'
Chris@1295 22 require Rails.root.join('test', 'mocks', 'open_id_authentication_mock.rb').to_s
Chris@1295 23
Chris@1295 24 require File.expand_path(File.dirname(__FILE__) + '/object_helpers')
Chris@1295 25 include ObjectHelpers
Chris@1295 26
Chris@1295 27 class ActiveSupport::TestCase
Chris@1295 28 include ActionDispatch::TestProcess
Chris@1295 29
Chris@1295 30 # Transactional fixtures accelerate your tests by wrapping each test method
Chris@1295 31 # in a transaction that's rolled back on completion. This ensures that the
Chris@1295 32 # test database remains unchanged so your fixtures don't have to be reloaded
Chris@1295 33 # between every test method. Fewer database queries means faster tests.
Chris@1295 34 #
Chris@1295 35 # Read Mike Clark's excellent walkthrough at
Chris@1295 36 # http://clarkware.com/cgi/blosxom/2005/10/24#Rails10FastTesting
Chris@1295 37 #
Chris@1295 38 # Every Active Record database supports transactions except MyISAM tables
Chris@1295 39 # in MySQL. Turn off transactional fixtures in this case; however, if you
Chris@1295 40 # don't care one way or the other, switching from MyISAM to InnoDB tables
Chris@1295 41 # is recommended.
Chris@1295 42 self.use_transactional_fixtures = true
Chris@1295 43
Chris@1295 44 # Instantiated fixtures are slow, but give you @david where otherwise you
Chris@1295 45 # would need people(:david). If you don't want to migrate your existing
Chris@1295 46 # test cases which use the @david style and don't mind the speed hit (each
Chris@1295 47 # instantiated fixtures translates to a database query per test method),
Chris@1295 48 # then set this back to true.
Chris@1295 49 self.use_instantiated_fixtures = false
Chris@1295 50
Chris@1295 51 # Add more helper methods to be used by all tests here...
Chris@1295 52
Chris@1295 53 def log_user(login, password)
Chris@1295 54 User.anonymous
Chris@1295 55 get "/login"
Chris@1295 56 assert_equal nil, session[:user_id]
Chris@1295 57 assert_response :success
Chris@1295 58 assert_template "account/login"
Chris@1295 59 post "/login", :username => login, :password => password
Chris@1295 60 assert_equal login, User.find(session[:user_id]).login
Chris@1295 61 end
Chris@1295 62
Chris@1295 63 def uploaded_test_file(name, mime)
Chris@1295 64 fixture_file_upload("files/#{name}", mime, true)
Chris@1295 65 end
Chris@1295 66
Chris@1295 67 def credentials(user, password=nil)
Chris@1295 68 {'HTTP_AUTHORIZATION' => ActionController::HttpAuthentication::Basic.encode_credentials(user, password || user)}
Chris@1295 69 end
Chris@1295 70
Chris@1295 71 # Mock out a file
Chris@1295 72 def self.mock_file
Chris@1295 73 file = 'a_file.png'
Chris@1295 74 file.stubs(:size).returns(32)
Chris@1295 75 file.stubs(:original_filename).returns('a_file.png')
Chris@1295 76 file.stubs(:content_type).returns('image/png')
Chris@1295 77 file.stubs(:read).returns(false)
Chris@1295 78 file
Chris@1295 79 end
Chris@1295 80
Chris@1295 81 def mock_file
Chris@1295 82 self.class.mock_file
Chris@1295 83 end
Chris@1295 84
Chris@1295 85 def mock_file_with_options(options={})
Chris@1295 86 file = ''
Chris@1295 87 file.stubs(:size).returns(32)
Chris@1295 88 original_filename = options[:original_filename] || nil
Chris@1295 89 file.stubs(:original_filename).returns(original_filename)
Chris@1295 90 content_type = options[:content_type] || nil
Chris@1295 91 file.stubs(:content_type).returns(content_type)
Chris@1295 92 file.stubs(:read).returns(false)
Chris@1295 93 file
Chris@1295 94 end
Chris@1295 95
Chris@1295 96 # Use a temporary directory for attachment related tests
Chris@1295 97 def set_tmp_attachments_directory
Chris@1295 98 Dir.mkdir "#{Rails.root}/tmp/test" unless File.directory?("#{Rails.root}/tmp/test")
Chris@1295 99 unless File.directory?("#{Rails.root}/tmp/test/attachments")
Chris@1295 100 Dir.mkdir "#{Rails.root}/tmp/test/attachments"
Chris@1295 101 end
Chris@1295 102 Attachment.storage_path = "#{Rails.root}/tmp/test/attachments"
Chris@1295 103 end
Chris@1295 104
Chris@1295 105 def set_fixtures_attachments_directory
Chris@1295 106 Attachment.storage_path = "#{Rails.root}/test/fixtures/files"
Chris@1295 107 end
Chris@1295 108
Chris@1295 109 def with_settings(options, &block)
Chris@1295 110 saved_settings = options.keys.inject({}) {|h, k| h[k] = Setting[k].is_a?(Symbol) ? Setting[k] : Setting[k].dup; h}
Chris@1295 111 options.each {|k, v| Setting[k] = v}
Chris@1295 112 yield
Chris@1295 113 ensure
Chris@1295 114 saved_settings.each {|k, v| Setting[k] = v} if saved_settings
Chris@1295 115 end
Chris@1295 116
Chris@1295 117 # Yields the block with user as the current user
Chris@1295 118 def with_current_user(user, &block)
Chris@1295 119 saved_user = User.current
Chris@1295 120 User.current = user
Chris@1295 121 yield
Chris@1295 122 ensure
Chris@1295 123 User.current = saved_user
Chris@1295 124 end
Chris@1295 125
Chris@1295 126 def change_user_password(login, new_password)
Chris@1295 127 user = User.first(:conditions => {:login => login})
Chris@1295 128 user.password, user.password_confirmation = new_password, new_password
Chris@1295 129 user.save!
Chris@1295 130 end
Chris@1295 131
Chris@1295 132 def self.ldap_configured?
Chris@1295 133 @test_ldap = Net::LDAP.new(:host => '127.0.0.1', :port => 389)
Chris@1295 134 return @test_ldap.bind
Chris@1295 135 rescue Exception => e
Chris@1295 136 # LDAP is not listening
Chris@1295 137 return nil
Chris@1295 138 end
Chris@1295 139
Chris@1295 140 def self.convert_installed?
Chris@1295 141 Redmine::Thumbnail.convert_available?
Chris@1295 142 end
Chris@1295 143
Chris@1295 144 # Returns the path to the test +vendor+ repository
Chris@1295 145 def self.repository_path(vendor)
Chris@1295 146 Rails.root.join("tmp/test/#{vendor.downcase}_repository").to_s
Chris@1295 147 end
Chris@1295 148
Chris@1295 149 # Returns the url of the subversion test repository
Chris@1295 150 def self.subversion_repository_url
Chris@1295 151 path = repository_path('subversion')
Chris@1295 152 path = '/' + path unless path.starts_with?('/')
Chris@1295 153 "file://#{path}"
Chris@1295 154 end
Chris@1295 155
Chris@1295 156 # Returns true if the +vendor+ test repository is configured
Chris@1295 157 def self.repository_configured?(vendor)
Chris@1295 158 File.directory?(repository_path(vendor))
Chris@1295 159 end
Chris@1295 160
Chris@1295 161 def repository_path_hash(arr)
Chris@1295 162 hs = {}
Chris@1295 163 hs[:path] = arr.join("/")
Chris@1295 164 hs[:param] = arr.join("/")
Chris@1295 165 hs
Chris@1295 166 end
Chris@1295 167
Chris@1295 168 def assert_save(object)
Chris@1295 169 saved = object.save
Chris@1295 170 message = "#{object.class} could not be saved"
Chris@1295 171 errors = object.errors.full_messages.map {|m| "- #{m}"}
Chris@1295 172 message << ":\n#{errors.join("\n")}" if errors.any?
Chris@1295 173 assert_equal true, saved, message
Chris@1295 174 end
Chris@1295 175
Chris@1295 176 def assert_error_tag(options={})
Chris@1295 177 assert_tag({:attributes => { :id => 'errorExplanation' }}.merge(options))
Chris@1295 178 end
Chris@1295 179
Chris@1295 180 def assert_include(expected, s, message=nil)
Chris@1295 181 assert s.include?(expected), (message || "\"#{expected}\" not found in \"#{s}\"")
Chris@1295 182 end
Chris@1295 183
Chris@1295 184 def assert_not_include(expected, s)
Chris@1295 185 assert !s.include?(expected), "\"#{expected}\" found in \"#{s}\""
Chris@1295 186 end
Chris@1295 187
Chris@1295 188 def assert_select_in(text, *args, &block)
Chris@1295 189 d = HTML::Document.new(CGI::unescapeHTML(String.new(text))).root
Chris@1295 190 assert_select(d, *args, &block)
Chris@1295 191 end
Chris@1295 192
Chris@1295 193 def assert_mail_body_match(expected, mail)
Chris@1295 194 if expected.is_a?(String)
Chris@1295 195 assert_include expected, mail_body(mail)
Chris@1295 196 else
Chris@1295 197 assert_match expected, mail_body(mail)
Chris@1295 198 end
Chris@1295 199 end
Chris@1295 200
Chris@1295 201 def assert_mail_body_no_match(expected, mail)
Chris@1295 202 if expected.is_a?(String)
Chris@1295 203 assert_not_include expected, mail_body(mail)
Chris@1295 204 else
Chris@1295 205 assert_no_match expected, mail_body(mail)
Chris@1295 206 end
Chris@1295 207 end
Chris@1295 208
Chris@1295 209 def mail_body(mail)
Chris@1295 210 mail.parts.first.body.encoded
Chris@1295 211 end
Chris@1295 212
Chris@1295 213 # Shoulda macros
Chris@1295 214 def self.should_render_404
Chris@1295 215 should_respond_with :not_found
Chris@1295 216 should_render_template 'common/error'
Chris@1295 217 end
Chris@1295 218
Chris@1295 219 def self.should_have_before_filter(expected_method, options = {})
Chris@1295 220 should_have_filter('before', expected_method, options)
Chris@1295 221 end
Chris@1295 222
Chris@1295 223 def self.should_have_after_filter(expected_method, options = {})
Chris@1295 224 should_have_filter('after', expected_method, options)
Chris@1295 225 end
Chris@1295 226
Chris@1295 227 def self.should_have_filter(filter_type, expected_method, options)
Chris@1295 228 description = "have #{filter_type}_filter :#{expected_method}"
Chris@1295 229 description << " with #{options.inspect}" unless options.empty?
Chris@1295 230
Chris@1295 231 should description do
Chris@1295 232 klass = "action_controller/filters/#{filter_type}_filter".classify.constantize
Chris@1295 233 expected = klass.new(:filter, expected_method.to_sym, options)
Chris@1295 234 assert_equal 1, @controller.class.filter_chain.select { |filter|
Chris@1295 235 filter.method == expected.method && filter.kind == expected.kind &&
Chris@1295 236 filter.options == expected.options && filter.class == expected.class
Chris@1295 237 }.size
Chris@1295 238 end
Chris@1295 239 end
Chris@1295 240
Chris@1295 241 # Test that a request allows the three types of API authentication
Chris@1295 242 #
Chris@1295 243 # * HTTP Basic with username and password
Chris@1295 244 # * HTTP Basic with an api key for the username
Chris@1295 245 # * Key based with the key=X parameter
Chris@1295 246 #
Chris@1295 247 # @param [Symbol] http_method the HTTP method for request (:get, :post, :put, :delete)
Chris@1295 248 # @param [String] url the request url
Chris@1295 249 # @param [optional, Hash] parameters additional request parameters
Chris@1295 250 # @param [optional, Hash] options additional options
Chris@1295 251 # @option options [Symbol] :success_code Successful response code (:success)
Chris@1295 252 # @option options [Symbol] :failure_code Failure response code (:unauthorized)
Chris@1295 253 def self.should_allow_api_authentication(http_method, url, parameters={}, options={})
Chris@1295 254 should_allow_http_basic_auth_with_username_and_password(http_method, url, parameters, options)
Chris@1295 255 should_allow_http_basic_auth_with_key(http_method, url, parameters, options)
Chris@1295 256 should_allow_key_based_auth(http_method, url, parameters, options)
Chris@1295 257 end
Chris@1295 258
Chris@1295 259 # Test that a request allows the username and password for HTTP BASIC
Chris@1295 260 #
Chris@1295 261 # @param [Symbol] http_method the HTTP method for request (:get, :post, :put, :delete)
Chris@1295 262 # @param [String] url the request url
Chris@1295 263 # @param [optional, Hash] parameters additional request parameters
Chris@1295 264 # @param [optional, Hash] options additional options
Chris@1295 265 # @option options [Symbol] :success_code Successful response code (:success)
Chris@1295 266 # @option options [Symbol] :failure_code Failure response code (:unauthorized)
Chris@1295 267 def self.should_allow_http_basic_auth_with_username_and_password(http_method, url, parameters={}, options={})
Chris@1295 268 success_code = options[:success_code] || :success
Chris@1295 269 failure_code = options[:failure_code] || :unauthorized
Chris@1295 270
Chris@1295 271 context "should allow http basic auth using a username and password for #{http_method} #{url}" do
Chris@1295 272 context "with a valid HTTP authentication" do
Chris@1295 273 setup do
Chris@1295 274 @user = User.generate! do |user|
Chris@1295 275 user.admin = true
Chris@1295 276 user.password = 'my_password'
Chris@1295 277 end
Chris@1295 278 send(http_method, url, parameters, credentials(@user.login, 'my_password'))
Chris@1295 279 end
Chris@1295 280
Chris@1295 281 should_respond_with success_code
Chris@1295 282 should_respond_with_content_type_based_on_url(url)
Chris@1295 283 should "login as the user" do
Chris@1295 284 assert_equal @user, User.current
Chris@1295 285 end
Chris@1295 286 end
Chris@1295 287
Chris@1295 288 context "with an invalid HTTP authentication" do
Chris@1295 289 setup do
Chris@1295 290 @user = User.generate!
Chris@1295 291 send(http_method, url, parameters, credentials(@user.login, 'wrong_password'))
Chris@1295 292 end
Chris@1295 293
Chris@1295 294 should_respond_with failure_code
Chris@1295 295 should_respond_with_content_type_based_on_url(url)
Chris@1295 296 should "not login as the user" do
Chris@1295 297 assert_equal User.anonymous, User.current
Chris@1295 298 end
Chris@1295 299 end
Chris@1295 300
Chris@1295 301 context "without credentials" do
Chris@1295 302 setup do
Chris@1295 303 send(http_method, url, parameters)
Chris@1295 304 end
Chris@1295 305
Chris@1295 306 should_respond_with failure_code
Chris@1295 307 should_respond_with_content_type_based_on_url(url)
Chris@1295 308 should "include_www_authenticate_header" do
Chris@1295 309 assert @controller.response.headers.has_key?('WWW-Authenticate')
Chris@1295 310 end
Chris@1295 311 end
Chris@1295 312 end
Chris@1295 313 end
Chris@1295 314
Chris@1295 315 # Test that a request allows the API key with HTTP BASIC
Chris@1295 316 #
Chris@1295 317 # @param [Symbol] http_method the HTTP method for request (:get, :post, :put, :delete)
Chris@1295 318 # @param [String] url the request url
Chris@1295 319 # @param [optional, Hash] parameters additional request parameters
Chris@1295 320 # @param [optional, Hash] options additional options
Chris@1295 321 # @option options [Symbol] :success_code Successful response code (:success)
Chris@1295 322 # @option options [Symbol] :failure_code Failure response code (:unauthorized)
Chris@1295 323 def self.should_allow_http_basic_auth_with_key(http_method, url, parameters={}, options={})
Chris@1295 324 success_code = options[:success_code] || :success
Chris@1295 325 failure_code = options[:failure_code] || :unauthorized
Chris@1295 326
Chris@1295 327 context "should allow http basic auth with a key for #{http_method} #{url}" do
Chris@1295 328 context "with a valid HTTP authentication using the API token" do
Chris@1295 329 setup do
Chris@1295 330 @user = User.generate! do |user|
Chris@1295 331 user.admin = true
Chris@1295 332 end
Chris@1295 333 @token = Token.create!(:user => @user, :action => 'api')
Chris@1295 334 send(http_method, url, parameters, credentials(@token.value, 'X'))
Chris@1295 335 end
Chris@1295 336 should_respond_with success_code
Chris@1295 337 should_respond_with_content_type_based_on_url(url)
Chris@1295 338 should_be_a_valid_response_string_based_on_url(url)
Chris@1295 339 should "login as the user" do
Chris@1295 340 assert_equal @user, User.current
Chris@1295 341 end
Chris@1295 342 end
Chris@1295 343
Chris@1295 344 context "with an invalid HTTP authentication" do
Chris@1295 345 setup do
Chris@1295 346 @user = User.generate!
Chris@1295 347 @token = Token.create!(:user => @user, :action => 'feeds')
Chris@1295 348 send(http_method, url, parameters, credentials(@token.value, 'X'))
Chris@1295 349 end
Chris@1295 350 should_respond_with failure_code
Chris@1295 351 should_respond_with_content_type_based_on_url(url)
Chris@1295 352 should "not login as the user" do
Chris@1295 353 assert_equal User.anonymous, User.current
Chris@1295 354 end
Chris@1295 355 end
Chris@1295 356 end
Chris@1295 357 end
Chris@1295 358
Chris@1295 359 # Test that a request allows full key authentication
Chris@1295 360 #
Chris@1295 361 # @param [Symbol] http_method the HTTP method for request (:get, :post, :put, :delete)
Chris@1295 362 # @param [String] url the request url, without the key=ZXY parameter
Chris@1295 363 # @param [optional, Hash] parameters additional request parameters
Chris@1295 364 # @param [optional, Hash] options additional options
Chris@1295 365 # @option options [Symbol] :success_code Successful response code (:success)
Chris@1295 366 # @option options [Symbol] :failure_code Failure response code (:unauthorized)
Chris@1295 367 def self.should_allow_key_based_auth(http_method, url, parameters={}, options={})
Chris@1295 368 success_code = options[:success_code] || :success
Chris@1295 369 failure_code = options[:failure_code] || :unauthorized
Chris@1295 370
Chris@1295 371 context "should allow key based auth using key=X for #{http_method} #{url}" do
Chris@1295 372 context "with a valid api token" do
Chris@1295 373 setup do
Chris@1295 374 @user = User.generate! do |user|
Chris@1295 375 user.admin = true
Chris@1295 376 end
Chris@1295 377 @token = Token.create!(:user => @user, :action => 'api')
Chris@1295 378 # Simple url parse to add on ?key= or &key=
Chris@1295 379 request_url = if url.match(/\?/)
Chris@1295 380 url + "&key=#{@token.value}"
Chris@1295 381 else
Chris@1295 382 url + "?key=#{@token.value}"
Chris@1295 383 end
Chris@1295 384 send(http_method, request_url, parameters)
Chris@1295 385 end
Chris@1295 386 should_respond_with success_code
Chris@1295 387 should_respond_with_content_type_based_on_url(url)
Chris@1295 388 should_be_a_valid_response_string_based_on_url(url)
Chris@1295 389 should "login as the user" do
Chris@1295 390 assert_equal @user, User.current
Chris@1295 391 end
Chris@1295 392 end
Chris@1295 393
Chris@1295 394 context "with an invalid api token" do
Chris@1295 395 setup do
Chris@1295 396 @user = User.generate! do |user|
Chris@1295 397 user.admin = true
Chris@1295 398 end
Chris@1295 399 @token = Token.create!(:user => @user, :action => 'feeds')
Chris@1295 400 # Simple url parse to add on ?key= or &key=
Chris@1295 401 request_url = if url.match(/\?/)
Chris@1295 402 url + "&key=#{@token.value}"
Chris@1295 403 else
Chris@1295 404 url + "?key=#{@token.value}"
Chris@1295 405 end
Chris@1295 406 send(http_method, request_url, parameters)
Chris@1295 407 end
Chris@1295 408 should_respond_with failure_code
Chris@1295 409 should_respond_with_content_type_based_on_url(url)
Chris@1295 410 should "not login as the user" do
Chris@1295 411 assert_equal User.anonymous, User.current
Chris@1295 412 end
Chris@1295 413 end
Chris@1295 414 end
Chris@1295 415
Chris@1295 416 context "should allow key based auth using X-Redmine-API-Key header for #{http_method} #{url}" do
Chris@1295 417 setup do
Chris@1295 418 @user = User.generate! do |user|
Chris@1295 419 user.admin = true
Chris@1295 420 end
Chris@1295 421 @token = Token.create!(:user => @user, :action => 'api')
Chris@1295 422 send(http_method, url, parameters, {'X-Redmine-API-Key' => @token.value.to_s})
Chris@1295 423 end
Chris@1295 424 should_respond_with success_code
Chris@1295 425 should_respond_with_content_type_based_on_url(url)
Chris@1295 426 should_be_a_valid_response_string_based_on_url(url)
Chris@1295 427 should "login as the user" do
Chris@1295 428 assert_equal @user, User.current
Chris@1295 429 end
Chris@1295 430 end
Chris@1295 431 end
Chris@1295 432
Chris@1295 433 # Uses should_respond_with_content_type based on what's in the url:
Chris@1295 434 #
Chris@1295 435 # '/project/issues.xml' => should_respond_with_content_type :xml
Chris@1295 436 # '/project/issues.json' => should_respond_with_content_type :json
Chris@1295 437 #
Chris@1295 438 # @param [String] url Request
Chris@1295 439 def self.should_respond_with_content_type_based_on_url(url)
Chris@1295 440 case
Chris@1295 441 when url.match(/xml/i)
Chris@1295 442 should "respond with XML" do
Chris@1295 443 assert_equal 'application/xml', @response.content_type
Chris@1295 444 end
Chris@1295 445 when url.match(/json/i)
Chris@1295 446 should "respond with JSON" do
Chris@1295 447 assert_equal 'application/json', @response.content_type
Chris@1295 448 end
Chris@1295 449 else
Chris@1295 450 raise "Unknown content type for should_respond_with_content_type_based_on_url: #{url}"
Chris@1295 451 end
Chris@1295 452 end
Chris@1295 453
Chris@1295 454 # Uses the url to assert which format the response should be in
Chris@1295 455 #
Chris@1295 456 # '/project/issues.xml' => should_be_a_valid_xml_string
Chris@1295 457 # '/project/issues.json' => should_be_a_valid_json_string
Chris@1295 458 #
Chris@1295 459 # @param [String] url Request
Chris@1295 460 def self.should_be_a_valid_response_string_based_on_url(url)
Chris@1295 461 case
Chris@1295 462 when url.match(/xml/i)
Chris@1295 463 should_be_a_valid_xml_string
Chris@1295 464 when url.match(/json/i)
Chris@1295 465 should_be_a_valid_json_string
Chris@1295 466 else
Chris@1295 467 raise "Unknown content type for should_be_a_valid_response_based_on_url: #{url}"
Chris@1295 468 end
Chris@1295 469 end
Chris@1295 470
Chris@1295 471 # Checks that the response is a valid JSON string
Chris@1295 472 def self.should_be_a_valid_json_string
Chris@1295 473 should "be a valid JSON string (or empty)" do
Chris@1295 474 assert(response.body.blank? || ActiveSupport::JSON.decode(response.body))
Chris@1295 475 end
Chris@1295 476 end
Chris@1295 477
Chris@1295 478 # Checks that the response is a valid XML string
Chris@1295 479 def self.should_be_a_valid_xml_string
Chris@1295 480 should "be a valid XML string" do
Chris@1295 481 assert REXML::Document.new(response.body)
Chris@1295 482 end
Chris@1295 483 end
Chris@1295 484
Chris@1295 485 def self.should_respond_with(status)
Chris@1295 486 should "respond with #{status}" do
Chris@1295 487 assert_response status
Chris@1295 488 end
Chris@1295 489 end
Chris@1295 490 end
Chris@1295 491
Chris@1295 492 # Simple module to "namespace" all of the API tests
Chris@1295 493 module ApiTest
Chris@1295 494 end