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