2011-03-20 21:25:38 +03:00
|
|
|
# Redmine - project management software
|
2013-01-12 13:29:31 +04:00
|
|
|
# Copyright (C) 2006-2013 Jean-Philippe Lang
|
2006-07-09 20:30:01 +04:00
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or
|
|
|
|
# modify it under the terms of the GNU General Public License
|
|
|
|
# as published by the Free Software Foundation; either version 2
|
|
|
|
# of the License, or (at your option) any later version.
|
2011-06-23 15:19:05 +04:00
|
|
|
#
|
2006-07-09 20:30:01 +04:00
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
2011-06-23 15:19:05 +04:00
|
|
|
#
|
2006-07-09 20:30:01 +04:00
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program; if not, write to the Free Software
|
|
|
|
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
2012-04-25 21:17:49 +04:00
|
|
|
#require 'shoulda'
|
2010-07-09 17:46:20 +04:00
|
|
|
ENV["RAILS_ENV"] = "test"
|
2006-06-28 22:11:03 +04:00
|
|
|
require File.expand_path(File.dirname(__FILE__) + "/../config/environment")
|
2012-04-25 21:17:49 +04:00
|
|
|
require 'rails/test_help'
|
2011-11-13 15:47:08 +04:00
|
|
|
require Rails.root.join('test', 'mocks', 'open_id_authentication_mock.rb').to_s
|
2006-06-28 22:11:03 +04:00
|
|
|
|
2012-04-22 15:58:05 +04:00
|
|
|
require File.expand_path(File.dirname(__FILE__) + '/object_helpers')
|
|
|
|
include ObjectHelpers
|
2009-10-20 04:36:51 +04:00
|
|
|
|
2009-09-13 21:14:35 +04:00
|
|
|
class ActiveSupport::TestCase
|
2012-04-25 21:17:49 +04:00
|
|
|
include ActionDispatch::TestProcess
|
2006-06-28 22:11:03 +04:00
|
|
|
|
2013-01-22 22:31:25 +04:00
|
|
|
self.use_transactional_fixtures = true
|
2006-06-28 22:11:03 +04:00
|
|
|
self.use_instantiated_fixtures = false
|
|
|
|
|
2006-07-09 20:30:01 +04:00
|
|
|
def log_user(login, password)
|
2009-12-06 13:28:20 +03:00
|
|
|
User.anonymous
|
2009-02-21 14:04:50 +03:00
|
|
|
get "/login"
|
2006-07-29 13:32:58 +04:00
|
|
|
assert_equal nil, session[:user_id]
|
2006-07-09 20:30:01 +04:00
|
|
|
assert_response :success
|
|
|
|
assert_template "account/login"
|
2009-02-21 14:04:50 +03:00
|
|
|
post "/login", :username => login, :password => password
|
2006-07-29 13:32:58 +04:00
|
|
|
assert_equal login, User.find(session[:user_id]).login
|
2006-07-09 20:30:01 +04:00
|
|
|
end
|
2011-09-01 04:50:14 +04:00
|
|
|
|
2009-10-10 18:56:29 +04:00
|
|
|
def uploaded_test_file(name, mime)
|
2012-04-25 21:17:49 +04:00
|
|
|
fixture_file_upload("files/#{name}", mime, true)
|
2008-01-06 20:06:14 +03:00
|
|
|
end
|
2010-06-19 07:54:23 +04:00
|
|
|
|
2012-01-02 23:44:37 +04:00
|
|
|
def credentials(user, password=nil)
|
2012-04-25 21:17:49 +04:00
|
|
|
{'HTTP_AUTHORIZATION' => ActionController::HttpAuthentication::Basic.encode_credentials(user, password || user)}
|
2012-01-02 23:44:37 +04:00
|
|
|
end
|
|
|
|
|
2010-06-19 07:54:23 +04:00
|
|
|
# Mock out a file
|
2010-08-20 02:24:42 +04:00
|
|
|
def self.mock_file
|
2010-06-19 07:54:23 +04:00
|
|
|
file = 'a_file.png'
|
|
|
|
file.stubs(:size).returns(32)
|
|
|
|
file.stubs(:original_filename).returns('a_file.png')
|
|
|
|
file.stubs(:content_type).returns('image/png')
|
|
|
|
file.stubs(:read).returns(false)
|
|
|
|
file
|
|
|
|
end
|
2010-08-20 02:24:42 +04:00
|
|
|
|
|
|
|
def mock_file
|
|
|
|
self.class.mock_file
|
|
|
|
end
|
|
|
|
|
2011-11-23 09:30:53 +04:00
|
|
|
def mock_file_with_options(options={})
|
|
|
|
file = ''
|
|
|
|
file.stubs(:size).returns(32)
|
|
|
|
original_filename = options[:original_filename] || nil
|
|
|
|
file.stubs(:original_filename).returns(original_filename)
|
|
|
|
content_type = options[:content_type] || nil
|
|
|
|
file.stubs(:content_type).returns(content_type)
|
|
|
|
file.stubs(:read).returns(false)
|
|
|
|
file
|
|
|
|
end
|
|
|
|
|
2008-06-09 22:40:59 +04:00
|
|
|
# Use a temporary directory for attachment related tests
|
|
|
|
def set_tmp_attachments_directory
|
2011-06-16 08:02:31 +04:00
|
|
|
Dir.mkdir "#{Rails.root}/tmp/test" unless File.directory?("#{Rails.root}/tmp/test")
|
|
|
|
unless File.directory?("#{Rails.root}/tmp/test/attachments")
|
|
|
|
Dir.mkdir "#{Rails.root}/tmp/test/attachments"
|
|
|
|
end
|
|
|
|
Attachment.storage_path = "#{Rails.root}/tmp/test/attachments"
|
2008-06-09 22:40:59 +04:00
|
|
|
end
|
2011-06-16 08:02:31 +04:00
|
|
|
|
2011-12-16 21:52:47 +04:00
|
|
|
def set_fixtures_attachments_directory
|
|
|
|
Attachment.storage_path = "#{Rails.root}/test/fixtures/files"
|
|
|
|
end
|
|
|
|
|
2009-02-21 19:04:51 +03:00
|
|
|
def with_settings(options, &block)
|
2011-11-26 21:37:20 +04:00
|
|
|
saved_settings = options.keys.inject({}) {|h, k| h[k] = Setting[k].is_a?(Symbol) ? Setting[k] : Setting[k].dup; h}
|
2009-02-21 19:04:51 +03:00
|
|
|
options.each {|k, v| Setting[k] = v}
|
|
|
|
yield
|
2011-03-27 19:41:05 +04:00
|
|
|
ensure
|
2011-11-26 21:37:20 +04:00
|
|
|
saved_settings.each {|k, v| Setting[k] = v} if saved_settings
|
2009-02-21 19:04:51 +03:00
|
|
|
end
|
2009-12-30 22:42:19 +03:00
|
|
|
|
2012-10-18 21:08:42 +04:00
|
|
|
# Yields the block with user as the current user
|
|
|
|
def with_current_user(user, &block)
|
|
|
|
saved_user = User.current
|
|
|
|
User.current = user
|
|
|
|
yield
|
|
|
|
ensure
|
|
|
|
User.current = saved_user
|
|
|
|
end
|
|
|
|
|
2010-08-19 05:28:33 +04:00
|
|
|
def change_user_password(login, new_password)
|
|
|
|
user = User.first(:conditions => {:login => login})
|
|
|
|
user.password, user.password_confirmation = new_password, new_password
|
|
|
|
user.save!
|
|
|
|
end
|
|
|
|
|
2010-02-16 19:40:50 +03:00
|
|
|
def self.ldap_configured?
|
|
|
|
@test_ldap = Net::LDAP.new(:host => '127.0.0.1', :port => 389)
|
|
|
|
return @test_ldap.bind
|
2010-02-17 21:08:15 +03:00
|
|
|
rescue Exception => e
|
|
|
|
# LDAP is not listening
|
|
|
|
return nil
|
2010-02-16 19:40:50 +03:00
|
|
|
end
|
2011-06-20 16:33:58 +04:00
|
|
|
|
2012-07-07 17:48:07 +04:00
|
|
|
def self.convert_installed?
|
2012-07-16 21:37:24 +04:00
|
|
|
Redmine::Thumbnail.convert_available?
|
2012-07-07 17:48:07 +04:00
|
|
|
end
|
|
|
|
|
2010-05-01 12:32:21 +04:00
|
|
|
# Returns the path to the test +vendor+ repository
|
|
|
|
def self.repository_path(vendor)
|
2011-06-20 16:33:58 +04:00
|
|
|
Rails.root.join("tmp/test/#{vendor.downcase}_repository").to_s
|
2010-05-01 12:32:21 +04:00
|
|
|
end
|
2011-06-20 16:33:58 +04:00
|
|
|
|
2011-03-21 02:10:11 +03:00
|
|
|
# Returns the url of the subversion test repository
|
2011-03-20 21:25:38 +03:00
|
|
|
def self.subversion_repository_url
|
|
|
|
path = repository_path('subversion')
|
|
|
|
path = '/' + path unless path.starts_with?('/')
|
|
|
|
"file://#{path}"
|
|
|
|
end
|
2011-06-20 16:33:58 +04:00
|
|
|
|
2010-05-01 12:32:21 +04:00
|
|
|
# Returns true if the +vendor+ test repository is configured
|
|
|
|
def self.repository_configured?(vendor)
|
|
|
|
File.directory?(repository_path(vendor))
|
|
|
|
end
|
2011-06-20 16:33:58 +04:00
|
|
|
|
2012-02-23 10:11:59 +04:00
|
|
|
def repository_path_hash(arr)
|
|
|
|
hs = {}
|
|
|
|
hs[:path] = arr.join("/")
|
2012-04-25 21:17:49 +04:00
|
|
|
hs[:param] = arr.join("/")
|
2012-02-23 10:11:59 +04:00
|
|
|
hs
|
|
|
|
end
|
|
|
|
|
2012-10-10 21:38:17 +04:00
|
|
|
def assert_save(object)
|
|
|
|
saved = object.save
|
|
|
|
message = "#{object.class} could not be saved"
|
|
|
|
errors = object.errors.full_messages.map {|m| "- #{m}"}
|
|
|
|
message << ":\n#{errors.join("\n")}" if errors.any?
|
|
|
|
assert_equal true, saved, message
|
|
|
|
end
|
|
|
|
|
2010-10-23 15:07:04 +04:00
|
|
|
def assert_error_tag(options={})
|
2010-11-27 13:34:44 +03:00
|
|
|
assert_tag({:attributes => { :id => 'errorExplanation' }}.merge(options))
|
2010-10-23 15:07:04 +04:00
|
|
|
end
|
2011-09-01 04:50:14 +04:00
|
|
|
|
2012-07-21 10:35:49 +04:00
|
|
|
def assert_include(expected, s, message=nil)
|
|
|
|
assert s.include?(expected), (message || "\"#{expected}\" not found in \"#{s}\"")
|
2011-07-10 12:00:25 +04:00
|
|
|
end
|
2010-02-16 19:40:50 +03:00
|
|
|
|
2012-03-04 17:28:18 +04:00
|
|
|
def assert_not_include(expected, s)
|
|
|
|
assert !s.include?(expected), "\"#{expected}\" found in \"#{s}\""
|
|
|
|
end
|
|
|
|
|
2012-10-20 18:49:32 +04:00
|
|
|
def assert_select_in(text, *args, &block)
|
|
|
|
d = HTML::Document.new(CGI::unescapeHTML(String.new(text))).root
|
|
|
|
assert_select(d, *args, &block)
|
|
|
|
end
|
|
|
|
|
2012-03-04 17:28:18 +04:00
|
|
|
def assert_mail_body_match(expected, mail)
|
|
|
|
if expected.is_a?(String)
|
2012-03-04 18:01:54 +04:00
|
|
|
assert_include expected, mail_body(mail)
|
2012-03-04 17:28:18 +04:00
|
|
|
else
|
2012-03-04 18:01:54 +04:00
|
|
|
assert_match expected, mail_body(mail)
|
2012-03-04 17:28:18 +04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def assert_mail_body_no_match(expected, mail)
|
|
|
|
if expected.is_a?(String)
|
2012-03-04 18:01:54 +04:00
|
|
|
assert_not_include expected, mail_body(mail)
|
2012-03-04 17:28:18 +04:00
|
|
|
else
|
2012-03-04 18:01:54 +04:00
|
|
|
assert_no_match expected, mail_body(mail)
|
2012-03-04 17:28:18 +04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-03-04 18:01:54 +04:00
|
|
|
def mail_body(mail)
|
2012-04-25 21:17:49 +04:00
|
|
|
mail.parts.first.body.encoded
|
2012-03-04 18:01:54 +04:00
|
|
|
end
|
2013-01-22 22:26:04 +04:00
|
|
|
end
|
2010-03-08 19:47:47 +03:00
|
|
|
|
2013-01-22 22:26:04 +04:00
|
|
|
module Redmine
|
|
|
|
module ApiTest
|
|
|
|
# Base class for API tests
|
|
|
|
class Base < ActionDispatch::IntegrationTest
|
|
|
|
# Test that a request allows the three types of API authentication
|
|
|
|
#
|
|
|
|
# * HTTP Basic with username and password
|
|
|
|
# * HTTP Basic with an api key for the username
|
|
|
|
# * Key based with the key=X parameter
|
|
|
|
#
|
|
|
|
# @param [Symbol] http_method the HTTP method for request (:get, :post, :put, :delete)
|
|
|
|
# @param [String] url the request url
|
|
|
|
# @param [optional, Hash] parameters additional request parameters
|
|
|
|
# @param [optional, Hash] options additional options
|
|
|
|
# @option options [Symbol] :success_code Successful response code (:success)
|
|
|
|
# @option options [Symbol] :failure_code Failure response code (:unauthorized)
|
|
|
|
def self.should_allow_api_authentication(http_method, url, parameters={}, options={})
|
|
|
|
should_allow_http_basic_auth_with_username_and_password(http_method, url, parameters, options)
|
|
|
|
should_allow_http_basic_auth_with_key(http_method, url, parameters, options)
|
|
|
|
should_allow_key_based_auth(http_method, url, parameters, options)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Test that a request allows the username and password for HTTP BASIC
|
|
|
|
#
|
|
|
|
# @param [Symbol] http_method the HTTP method for request (:get, :post, :put, :delete)
|
|
|
|
# @param [String] url the request url
|
|
|
|
# @param [optional, Hash] parameters additional request parameters
|
|
|
|
# @param [optional, Hash] options additional options
|
|
|
|
# @option options [Symbol] :success_code Successful response code (:success)
|
|
|
|
# @option options [Symbol] :failure_code Failure response code (:unauthorized)
|
|
|
|
def self.should_allow_http_basic_auth_with_username_and_password(http_method, url, parameters={}, options={})
|
|
|
|
success_code = options[:success_code] || :success
|
|
|
|
failure_code = options[:failure_code] || :unauthorized
|
|
|
|
|
|
|
|
context "should allow http basic auth using a username and password for #{http_method} #{url}" do
|
|
|
|
context "with a valid HTTP authentication" do
|
|
|
|
setup do
|
|
|
|
@user = User.generate! do |user|
|
|
|
|
user.admin = true
|
|
|
|
user.password = 'my_password'
|
|
|
|
end
|
|
|
|
send(http_method, url, parameters, credentials(@user.login, 'my_password'))
|
|
|
|
end
|
|
|
|
|
|
|
|
should_respond_with success_code
|
|
|
|
should_respond_with_content_type_based_on_url(url)
|
|
|
|
should "login as the user" do
|
|
|
|
assert_equal @user, User.current
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "with an invalid HTTP authentication" do
|
|
|
|
setup do
|
|
|
|
@user = User.generate!
|
|
|
|
send(http_method, url, parameters, credentials(@user.login, 'wrong_password'))
|
|
|
|
end
|
|
|
|
|
|
|
|
should_respond_with failure_code
|
|
|
|
should_respond_with_content_type_based_on_url(url)
|
|
|
|
should "not login as the user" do
|
|
|
|
assert_equal User.anonymous, User.current
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "without credentials" do
|
|
|
|
setup do
|
|
|
|
send(http_method, url, parameters)
|
|
|
|
end
|
|
|
|
|
|
|
|
should_respond_with failure_code
|
|
|
|
should_respond_with_content_type_based_on_url(url)
|
|
|
|
should "include_www_authenticate_header" do
|
|
|
|
assert @controller.response.headers.has_key?('WWW-Authenticate')
|
|
|
|
end
|
2012-04-22 15:56:47 +04:00
|
|
|
end
|
2010-11-02 18:52:06 +03:00
|
|
|
end
|
|
|
|
end
|
2013-01-22 22:26:04 +04:00
|
|
|
|
|
|
|
# Test that a request allows the API key with HTTP BASIC
|
|
|
|
#
|
|
|
|
# @param [Symbol] http_method the HTTP method for request (:get, :post, :put, :delete)
|
|
|
|
# @param [String] url the request url
|
|
|
|
# @param [optional, Hash] parameters additional request parameters
|
|
|
|
# @param [optional, Hash] options additional options
|
|
|
|
# @option options [Symbol] :success_code Successful response code (:success)
|
|
|
|
# @option options [Symbol] :failure_code Failure response code (:unauthorized)
|
|
|
|
def self.should_allow_http_basic_auth_with_key(http_method, url, parameters={}, options={})
|
|
|
|
success_code = options[:success_code] || :success
|
|
|
|
failure_code = options[:failure_code] || :unauthorized
|
|
|
|
|
|
|
|
context "should allow http basic auth with a key for #{http_method} #{url}" do
|
|
|
|
context "with a valid HTTP authentication using the API token" do
|
|
|
|
setup do
|
|
|
|
@user = User.generate! do |user|
|
|
|
|
user.admin = true
|
|
|
|
end
|
|
|
|
@token = Token.create!(:user => @user, :action => 'api')
|
|
|
|
send(http_method, url, parameters, credentials(@token.value, 'X'))
|
|
|
|
end
|
|
|
|
should_respond_with success_code
|
|
|
|
should_respond_with_content_type_based_on_url(url)
|
|
|
|
should_be_a_valid_response_string_based_on_url(url)
|
|
|
|
should "login as the user" do
|
|
|
|
assert_equal @user, User.current
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "with an invalid HTTP authentication" do
|
|
|
|
setup do
|
|
|
|
@user = User.generate!
|
|
|
|
@token = Token.create!(:user => @user, :action => 'feeds')
|
|
|
|
send(http_method, url, parameters, credentials(@token.value, 'X'))
|
|
|
|
end
|
|
|
|
should_respond_with failure_code
|
|
|
|
should_respond_with_content_type_based_on_url(url)
|
|
|
|
should "not login as the user" do
|
|
|
|
assert_equal User.anonymous, User.current
|
|
|
|
end
|
|
|
|
end
|
2010-11-02 18:52:06 +03:00
|
|
|
end
|
|
|
|
end
|
2013-01-22 22:26:04 +04:00
|
|
|
|
|
|
|
# Test that a request allows full key authentication
|
|
|
|
#
|
|
|
|
# @param [Symbol] http_method the HTTP method for request (:get, :post, :put, :delete)
|
|
|
|
# @param [String] url the request url, without the key=ZXY parameter
|
|
|
|
# @param [optional, Hash] parameters additional request parameters
|
|
|
|
# @param [optional, Hash] options additional options
|
|
|
|
# @option options [Symbol] :success_code Successful response code (:success)
|
|
|
|
# @option options [Symbol] :failure_code Failure response code (:unauthorized)
|
|
|
|
def self.should_allow_key_based_auth(http_method, url, parameters={}, options={})
|
|
|
|
success_code = options[:success_code] || :success
|
|
|
|
failure_code = options[:failure_code] || :unauthorized
|
|
|
|
|
|
|
|
context "should allow key based auth using key=X for #{http_method} #{url}" do
|
|
|
|
context "with a valid api token" do
|
|
|
|
setup do
|
|
|
|
@user = User.generate! do |user|
|
|
|
|
user.admin = true
|
|
|
|
end
|
|
|
|
@token = Token.create!(:user => @user, :action => 'api')
|
|
|
|
# Simple url parse to add on ?key= or &key=
|
|
|
|
request_url = if url.match(/\?/)
|
|
|
|
url + "&key=#{@token.value}"
|
|
|
|
else
|
|
|
|
url + "?key=#{@token.value}"
|
|
|
|
end
|
|
|
|
send(http_method, request_url, parameters)
|
|
|
|
end
|
|
|
|
should_respond_with success_code
|
|
|
|
should_respond_with_content_type_based_on_url(url)
|
|
|
|
should_be_a_valid_response_string_based_on_url(url)
|
|
|
|
should "login as the user" do
|
|
|
|
assert_equal @user, User.current
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "with an invalid api token" do
|
|
|
|
setup do
|
|
|
|
@user = User.generate! do |user|
|
|
|
|
user.admin = true
|
|
|
|
end
|
|
|
|
@token = Token.create!(:user => @user, :action => 'feeds')
|
|
|
|
# Simple url parse to add on ?key= or &key=
|
|
|
|
request_url = if url.match(/\?/)
|
|
|
|
url + "&key=#{@token.value}"
|
|
|
|
else
|
|
|
|
url + "?key=#{@token.value}"
|
|
|
|
end
|
|
|
|
send(http_method, request_url, parameters)
|
|
|
|
end
|
|
|
|
should_respond_with failure_code
|
|
|
|
should_respond_with_content_type_based_on_url(url)
|
|
|
|
should "not login as the user" do
|
|
|
|
assert_equal User.anonymous, User.current
|
|
|
|
end
|
|
|
|
end
|
2010-11-02 18:52:06 +03:00
|
|
|
end
|
2013-01-22 22:26:04 +04:00
|
|
|
|
|
|
|
context "should allow key based auth using X-Redmine-API-Key header for #{http_method} #{url}" do
|
|
|
|
setup do
|
|
|
|
@user = User.generate! do |user|
|
|
|
|
user.admin = true
|
|
|
|
end
|
|
|
|
@token = Token.create!(:user => @user, :action => 'api')
|
|
|
|
send(http_method, url, parameters, {'X-Redmine-API-Key' => @token.value.to_s})
|
|
|
|
end
|
|
|
|
should_respond_with success_code
|
|
|
|
should_respond_with_content_type_based_on_url(url)
|
|
|
|
should_be_a_valid_response_string_based_on_url(url)
|
|
|
|
should "login as the user" do
|
|
|
|
assert_equal @user, User.current
|
|
|
|
end
|
2010-11-02 18:52:06 +03:00
|
|
|
end
|
|
|
|
end
|
2013-01-22 22:26:04 +04:00
|
|
|
|
|
|
|
# Uses should_respond_with_content_type based on what's in the url:
|
|
|
|
#
|
|
|
|
# '/project/issues.xml' => should_respond_with_content_type :xml
|
|
|
|
# '/project/issues.json' => should_respond_with_content_type :json
|
|
|
|
#
|
|
|
|
# @param [String] url Request
|
|
|
|
def self.should_respond_with_content_type_based_on_url(url)
|
|
|
|
case
|
|
|
|
when url.match(/xml/i)
|
|
|
|
should "respond with XML" do
|
|
|
|
assert_equal 'application/xml', @response.content_type
|
2012-04-22 15:56:47 +04:00
|
|
|
end
|
2013-01-22 22:26:04 +04:00
|
|
|
when url.match(/json/i)
|
|
|
|
should "respond with JSON" do
|
|
|
|
assert_equal 'application/json', @response.content_type
|
|
|
|
end
|
|
|
|
else
|
|
|
|
raise "Unknown content type for should_respond_with_content_type_based_on_url: #{url}"
|
2010-11-03 19:48:23 +03:00
|
|
|
end
|
|
|
|
end
|
2013-01-22 22:26:04 +04:00
|
|
|
|
|
|
|
# Uses the url to assert which format the response should be in
|
|
|
|
#
|
|
|
|
# '/project/issues.xml' => should_be_a_valid_xml_string
|
|
|
|
# '/project/issues.json' => should_be_a_valid_json_string
|
|
|
|
#
|
|
|
|
# @param [String] url Request
|
|
|
|
def self.should_be_a_valid_response_string_based_on_url(url)
|
|
|
|
case
|
|
|
|
when url.match(/xml/i)
|
|
|
|
should_be_a_valid_xml_string
|
|
|
|
when url.match(/json/i)
|
|
|
|
should_be_a_valid_json_string
|
|
|
|
else
|
|
|
|
raise "Unknown content type for should_be_a_valid_response_based_on_url: #{url}"
|
2010-11-03 19:48:23 +03:00
|
|
|
end
|
|
|
|
end
|
2013-01-22 22:26:04 +04:00
|
|
|
|
|
|
|
# Checks that the response is a valid JSON string
|
|
|
|
def self.should_be_a_valid_json_string
|
|
|
|
should "be a valid JSON string (or empty)" do
|
|
|
|
assert(response.body.blank? || ActiveSupport::JSON.decode(response.body))
|
2010-11-01 18:45:03 +03:00
|
|
|
end
|
|
|
|
end
|
2013-01-22 22:26:04 +04:00
|
|
|
|
|
|
|
# Checks that the response is a valid XML string
|
|
|
|
def self.should_be_a_valid_xml_string
|
|
|
|
should "be a valid XML string" do
|
|
|
|
assert REXML::Document.new(response.body)
|
2010-11-01 18:45:03 +03:00
|
|
|
end
|
|
|
|
end
|
2013-01-22 22:26:04 +04:00
|
|
|
|
|
|
|
def self.should_respond_with(status)
|
|
|
|
should "respond with #{status}" do
|
|
|
|
assert_response status
|
2012-04-22 15:56:47 +04:00
|
|
|
end
|
2010-12-23 17:49:14 +03:00
|
|
|
end
|
|
|
|
end
|
2010-11-01 18:45:03 +03:00
|
|
|
end
|
2010-11-01 18:26:05 +03:00
|
|
|
end
|
2012-12-02 22:07:35 +04:00
|
|
|
|
|
|
|
# URL helpers do not work with config.threadsafe!
|
|
|
|
# https://github.com/rspec/rspec-rails/issues/476#issuecomment-4705454
|
|
|
|
ActionView::TestCase::TestController.instance_eval do
|
|
|
|
helper Rails.application.routes.url_helpers
|
|
|
|
end
|
|
|
|
ActionView::TestCase::TestController.class_eval do
|
|
|
|
def _routes
|
|
|
|
Rails.application.routes
|
|
|
|
end
|
|
|
|
end
|