Mercurial > hg > soundsoftware-site
diff test/test_helper.rb @ 909:cbb26bc654de redmine-1.3
Update to Redmine 1.3-stable branch (Redmine SVN rev 8964)
author | Chris Cannam |
---|---|
date | Fri, 24 Feb 2012 19:09:32 +0000 |
parents | cbce1fd3b1b7 |
children | 5e80956cc792 433d4f72a19b |
line wrap: on
line diff
--- a/test/test_helper.rb Fri Feb 24 18:36:29 2012 +0000 +++ b/test/test_helper.rb Fri Feb 24 19:09:32 2012 +0000 @@ -5,12 +5,12 @@ # 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. -# +# # 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. -# +# # 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. @@ -19,7 +19,7 @@ require File.expand_path(File.dirname(__FILE__) + "/../config/environment") require 'test_help' require File.expand_path(File.dirname(__FILE__) + '/helper_testcase') -require File.join(RAILS_ROOT,'test', 'mocks', 'open_id_authentication_mock.rb') +require Rails.root.join('test', 'mocks', 'open_id_authentication_mock.rb').to_s require File.expand_path(File.dirname(__FILE__) + '/object_daddy_helpers') include ObjectDaddyHelpers @@ -47,7 +47,7 @@ self.use_instantiated_fixtures = false # Add more helper methods to be used by all tests here... - + def log_user(login, password) User.anonymous get "/login" @@ -57,9 +57,10 @@ post "/login", :username => login, :password => password assert_equal login, User.find(session[:user_id]).login end - + def uploaded_test_file(name, mime) - ActionController::TestUploadedFile.new(ActiveSupport::TestCase.fixture_path + "/files/#{name}", mime, true) + ActionController::TestUploadedFile.new( + ActiveSupport::TestCase.fixture_path + "/files/#{name}", mime, true) end # Mock out a file @@ -76,19 +77,32 @@ self.class.mock_file end + 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 + # Use a temporary directory for attachment related tests def set_tmp_attachments_directory - Dir.mkdir "#{RAILS_ROOT}/tmp/test" unless File.directory?("#{RAILS_ROOT}/tmp/test") - Dir.mkdir "#{RAILS_ROOT}/tmp/test/attachments" unless File.directory?("#{RAILS_ROOT}/tmp/test/attachments") - Attachment.storage_path = "#{RAILS_ROOT}/tmp/test/attachments" + 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" end - + def with_settings(options, &block) - saved_settings = options.keys.inject({}) {|h, k| h[k] = Setting[k].dup; h} + saved_settings = options.keys.inject({}) {|h, k| h[k] = Setting[k].is_a?(Symbol) ? Setting[k] : Setting[k].dup; h} options.each {|k, v| Setting[k] = v} yield ensure - saved_settings.each {|k, v| Setting[k] = v} + saved_settings.each {|k, v| Setting[k] = v} if saved_settings end def change_user_password(login, new_password) @@ -104,28 +118,32 @@ # LDAP is not listening return nil end - + # Returns the path to the test +vendor+ repository def self.repository_path(vendor) - File.join(RAILS_ROOT.gsub(%r{config\/\.\.}, ''), "/tmp/test/#{vendor.downcase}_repository") + Rails.root.join("tmp/test/#{vendor.downcase}_repository").to_s end - + # Returns the url of the subversion test repository def self.subversion_repository_url path = repository_path('subversion') path = '/' + path unless path.starts_with?('/') "file://#{path}" end - + # Returns true if the +vendor+ test repository is configured def self.repository_configured?(vendor) File.directory?(repository_path(vendor)) end - + def assert_error_tag(options={}) assert_tag({:attributes => { :id => 'errorExplanation' }}.merge(options)) end + def assert_include(expected, s) + assert s.include?(expected), "\"#{expected}\" not found in \"#{s}\"" + end + # Shoulda macros def self.should_render_404 should_respond_with :not_found @@ -170,7 +188,7 @@ :old_value => @old_value.id, :value => @new_value.id, :prop_key => prop_key) - + assert_match @new_value.name, show_detail(@detail, true) end @@ -179,7 +197,7 @@ :old_value => @old_value.id, :value => @new_value.id, :prop_key => prop_key) - + assert_match @old_value.name, show_detail(@detail, true) end end @@ -223,7 +241,7 @@ 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 @@ -231,7 +249,7 @@ @authorization = ActionController::HttpAuthentication::Basic.encode_credentials(@user.login, 'my_password') send(http_method, url, parameters, {:authorization => @authorization}) end - + should_respond_with success_code should_respond_with_content_type_based_on_url(url) should "login as the user" do @@ -245,14 +263,14 @@ @authorization = ActionController::HttpAuthentication::Basic.encode_credentials(@user.login, 'wrong_password') send(http_method, url, parameters, {:authorization => @authorization}) 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, {:authorization => ''}) @@ -288,7 +306,7 @@ @authorization = ActionController::HttpAuthentication::Basic.encode_credentials(@token.value, 'X') send(http_method, url, parameters, {:authorization => @authorization}) 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) @@ -313,7 +331,7 @@ end end end - + # Test that a request allows full key authentication # # @param [Symbol] http_method the HTTP method for request (:get, :post, :put, :delete) @@ -339,7 +357,7 @@ 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) @@ -360,7 +378,7 @@ 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 @@ -368,14 +386,14 @@ end end end - + context "should allow key based auth using X-Redmine-API-Key header for #{http_method} #{url}" do setup do @user = User.generate_with_protected!(:admin => true) @token = Token.generate!(: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) @@ -400,7 +418,7 @@ else raise "Unknown content type for should_respond_with_content_type_based_on_url: #{url}" end - + end # Uses the url to assert which format the response should be in @@ -418,9 +436,9 @@ else raise "Unknown content type for should_be_a_valid_response_based_on_url: #{url}" end - + end - + # 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 @@ -434,7 +452,7 @@ assert REXML::Document.new(response.body) end end - + end # Simple module to "namespace" all of the API tests