Chris@1517: # Redmine - project management software Chris@1517: # Copyright (C) 2006-2014 Jean-Philippe Lang Chris@1517: # Chris@1517: # This program is free software; you can redistribute it and/or Chris@1517: # modify it under the terms of the GNU General Public License Chris@1517: # as published by the Free Software Foundation; either version 2 Chris@1517: # of the License, or (at your option) any later version. Chris@1517: # Chris@1517: # This program is distributed in the hope that it will be useful, Chris@1517: # but WITHOUT ANY WARRANTY; without even the implied warranty of Chris@1517: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Chris@1517: # GNU General Public License for more details. Chris@1517: # Chris@1517: # You should have received a copy of the GNU General Public License Chris@1517: # along with this program; if not, write to the Free Software Chris@1517: # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. Chris@1517: Chris@1517: #require 'shoulda' Chris@1517: ENV["RAILS_ENV"] = "test" Chris@1517: require File.expand_path(File.dirname(__FILE__) + "/../config/environment") Chris@1517: require 'rails/test_help' Chris@1517: require Rails.root.join('test', 'mocks', 'open_id_authentication_mock.rb').to_s Chris@1517: Chris@1517: require File.expand_path(File.dirname(__FILE__) + '/object_helpers') Chris@1517: include ObjectHelpers Chris@1517: Chris@1517: require 'awesome_nested_set/version' Chris@1517: Chris@1517: class ActiveSupport::TestCase Chris@1517: include ActionDispatch::TestProcess Chris@1517: Chris@1517: self.use_transactional_fixtures = true Chris@1517: self.use_instantiated_fixtures = false Chris@1517: Chris@1517: ESCAPED_CANT = 'can't' Chris@1517: ESCAPED_UCANT = 'Can't' Chris@1517: # Rails 4.0.2 Chris@1517: #ESCAPED_CANT = 'can't' Chris@1517: #ESCAPED_UCANT = 'Can't' Chris@1517: Chris@1517: def log_user(login, password) Chris@1517: User.anonymous Chris@1517: get "/login" Chris@1517: assert_equal nil, session[:user_id] Chris@1517: assert_response :success Chris@1517: assert_template "account/login" Chris@1517: post "/login", :username => login, :password => password Chris@1517: assert_equal login, User.find(session[:user_id]).login Chris@1517: end Chris@1517: Chris@1517: def uploaded_test_file(name, mime) Chris@1517: fixture_file_upload("files/#{name}", mime, true) Chris@1517: end Chris@1517: Chris@1517: def credentials(user, password=nil) Chris@1517: {'HTTP_AUTHORIZATION' => ActionController::HttpAuthentication::Basic.encode_credentials(user, password || user)} Chris@1517: end Chris@1517: Chris@1517: # Mock out a file Chris@1517: def self.mock_file Chris@1517: file = 'a_file.png' Chris@1517: file.stubs(:size).returns(32) Chris@1517: file.stubs(:original_filename).returns('a_file.png') Chris@1517: file.stubs(:content_type).returns('image/png') Chris@1517: file.stubs(:read).returns(false) Chris@1517: file Chris@1517: end Chris@1517: Chris@1517: def mock_file Chris@1517: self.class.mock_file Chris@1517: end Chris@1517: Chris@1517: def mock_file_with_options(options={}) Chris@1517: file = '' Chris@1517: file.stubs(:size).returns(32) Chris@1517: original_filename = options[:original_filename] || nil Chris@1517: file.stubs(:original_filename).returns(original_filename) Chris@1517: content_type = options[:content_type] || nil Chris@1517: file.stubs(:content_type).returns(content_type) Chris@1517: file.stubs(:read).returns(false) Chris@1517: file Chris@1517: end Chris@1517: Chris@1517: # Use a temporary directory for attachment related tests Chris@1517: def set_tmp_attachments_directory Chris@1517: Dir.mkdir "#{Rails.root}/tmp/test" unless File.directory?("#{Rails.root}/tmp/test") Chris@1517: unless File.directory?("#{Rails.root}/tmp/test/attachments") Chris@1517: Dir.mkdir "#{Rails.root}/tmp/test/attachments" Chris@1517: end Chris@1517: Attachment.storage_path = "#{Rails.root}/tmp/test/attachments" Chris@1517: end Chris@1517: Chris@1517: def set_fixtures_attachments_directory Chris@1517: Attachment.storage_path = "#{Rails.root}/test/fixtures/files" Chris@1517: end Chris@1517: Chris@1517: def with_settings(options, &block) Chris@1517: saved_settings = options.keys.inject({}) do |h, k| Chris@1517: h[k] = case Setting[k] Chris@1517: when Symbol, false, true, nil Chris@1517: Setting[k] Chris@1517: else Chris@1517: Setting[k].dup Chris@1517: end Chris@1517: h Chris@1517: end Chris@1517: options.each {|k, v| Setting[k] = v} Chris@1517: yield Chris@1517: ensure Chris@1517: saved_settings.each {|k, v| Setting[k] = v} if saved_settings Chris@1517: end Chris@1517: Chris@1517: # Yields the block with user as the current user Chris@1517: def with_current_user(user, &block) Chris@1517: saved_user = User.current Chris@1517: User.current = user Chris@1517: yield Chris@1517: ensure Chris@1517: User.current = saved_user Chris@1517: end Chris@1517: Chris@1517: def with_locale(locale, &block) Chris@1517: saved_localed = ::I18n.locale Chris@1517: ::I18n.locale = locale Chris@1517: yield Chris@1517: ensure Chris@1517: ::I18n.locale = saved_localed Chris@1517: end Chris@1517: Chris@1517: def change_user_password(login, new_password) Chris@1517: user = User.where(:login => login).first Chris@1517: user.password, user.password_confirmation = new_password, new_password Chris@1517: user.save! Chris@1517: end Chris@1517: Chris@1517: def self.ldap_configured? Chris@1517: @test_ldap = Net::LDAP.new(:host => '127.0.0.1', :port => 389) Chris@1517: return @test_ldap.bind Chris@1517: rescue Exception => e Chris@1517: # LDAP is not listening Chris@1517: return nil Chris@1517: end Chris@1517: Chris@1517: def self.convert_installed? Chris@1517: Redmine::Thumbnail.convert_available? Chris@1517: end Chris@1517: Chris@1517: # Returns the path to the test +vendor+ repository Chris@1517: def self.repository_path(vendor) Chris@1517: Rails.root.join("tmp/test/#{vendor.downcase}_repository").to_s Chris@1517: end Chris@1517: Chris@1517: # Returns the url of the subversion test repository Chris@1517: def self.subversion_repository_url Chris@1517: path = repository_path('subversion') Chris@1517: path = '/' + path unless path.starts_with?('/') Chris@1517: "file://#{path}" Chris@1517: end Chris@1517: Chris@1517: # Returns true if the +vendor+ test repository is configured Chris@1517: def self.repository_configured?(vendor) Chris@1517: File.directory?(repository_path(vendor)) Chris@1517: end Chris@1517: Chris@1517: def repository_path_hash(arr) Chris@1517: hs = {} Chris@1517: hs[:path] = arr.join("/") Chris@1517: hs[:param] = arr.join("/") Chris@1517: hs Chris@1517: end Chris@1517: Chris@1517: def assert_save(object) Chris@1517: saved = object.save Chris@1517: message = "#{object.class} could not be saved" Chris@1517: errors = object.errors.full_messages.map {|m| "- #{m}"} Chris@1517: message << ":\n#{errors.join("\n")}" if errors.any? Chris@1517: assert_equal true, saved, message Chris@1517: end Chris@1517: Chris@1517: def assert_error_tag(options={}) Chris@1517: assert_tag({:attributes => { :id => 'errorExplanation' }}.merge(options)) Chris@1517: end Chris@1517: Chris@1517: def assert_include(expected, s, message=nil) Chris@1517: assert s.include?(expected), (message || "\"#{expected}\" not found in \"#{s}\"") Chris@1517: end Chris@1517: Chris@1517: def assert_not_include(expected, s, message=nil) Chris@1517: assert !s.include?(expected), (message || "\"#{expected}\" found in \"#{s}\"") Chris@1517: end Chris@1517: Chris@1517: def assert_select_in(text, *args, &block) Chris@1517: d = HTML::Document.new(CGI::unescapeHTML(String.new(text))).root Chris@1517: assert_select(d, *args, &block) Chris@1517: end Chris@1517: Chris@1517: def assert_mail_body_match(expected, mail, message=nil) Chris@1517: if expected.is_a?(String) Chris@1517: assert_include expected, mail_body(mail), message Chris@1517: else Chris@1517: assert_match expected, mail_body(mail), message Chris@1517: end Chris@1517: end Chris@1517: Chris@1517: def assert_mail_body_no_match(expected, mail, message=nil) Chris@1517: if expected.is_a?(String) Chris@1517: assert_not_include expected, mail_body(mail), message Chris@1517: else Chris@1517: assert_no_match expected, mail_body(mail), message Chris@1517: end Chris@1517: end Chris@1517: Chris@1517: def mail_body(mail) Chris@1517: mail.parts.first.body.encoded Chris@1517: end Chris@1517: Chris@1517: # awesome_nested_set new node lft and rgt value changed this refactor revision. Chris@1517: # https://github.com/collectiveidea/awesome_nested_set/commit/199fca9bb938e40200cd90714dc69247ef017c61 Chris@1517: # The reason of behavior change is that "self.class.base_class.unscoped" was added to this line. Chris@1517: # https://github.com/collectiveidea/awesome_nested_set/commit/199fca9bb9#diff-f61b59a5e6319024e211b0ffdd0e4ef1R273 Chris@1517: # It seems correct behavior because of this line comment. Chris@1517: # https://github.com/collectiveidea/awesome_nested_set/blame/199fca9bb9/lib/awesome_nested_set/model.rb#L278 Chris@1517: def new_issue_lft Chris@1517: ::AwesomeNestedSet::VERSION > "2.1.6" ? Issue.maximum(:rgt) + 1 : 1 Chris@1517: end Chris@1517: end Chris@1517: Chris@1517: module Redmine Chris@1517: module ApiTest Chris@1517: # Base class for API tests Chris@1517: class Base < ActionDispatch::IntegrationTest Chris@1517: # Test that a request allows the three types of API authentication Chris@1517: # Chris@1517: # * HTTP Basic with username and password Chris@1517: # * HTTP Basic with an api key for the username Chris@1517: # * Key based with the key=X parameter Chris@1517: # Chris@1517: # @param [Symbol] http_method the HTTP method for request (:get, :post, :put, :delete) Chris@1517: # @param [String] url the request url Chris@1517: # @param [optional, Hash] parameters additional request parameters Chris@1517: # @param [optional, Hash] options additional options Chris@1517: # @option options [Symbol] :success_code Successful response code (:success) Chris@1517: # @option options [Symbol] :failure_code Failure response code (:unauthorized) Chris@1517: def self.should_allow_api_authentication(http_method, url, parameters={}, options={}) Chris@1517: should_allow_http_basic_auth_with_username_and_password(http_method, url, parameters, options) Chris@1517: should_allow_http_basic_auth_with_key(http_method, url, parameters, options) Chris@1517: should_allow_key_based_auth(http_method, url, parameters, options) Chris@1517: end Chris@1517: Chris@1517: # Test that a request allows the username and password for HTTP BASIC Chris@1517: # Chris@1517: # @param [Symbol] http_method the HTTP method for request (:get, :post, :put, :delete) Chris@1517: # @param [String] url the request url Chris@1517: # @param [optional, Hash] parameters additional request parameters Chris@1517: # @param [optional, Hash] options additional options Chris@1517: # @option options [Symbol] :success_code Successful response code (:success) Chris@1517: # @option options [Symbol] :failure_code Failure response code (:unauthorized) Chris@1517: def self.should_allow_http_basic_auth_with_username_and_password(http_method, url, parameters={}, options={}) Chris@1517: success_code = options[:success_code] || :success Chris@1517: failure_code = options[:failure_code] || :unauthorized Chris@1517: Chris@1517: context "should allow http basic auth using a username and password for #{http_method} #{url}" do Chris@1517: context "with a valid HTTP authentication" do Chris@1517: setup do Chris@1517: @user = User.generate! do |user| Chris@1517: user.admin = true Chris@1517: user.password = 'my_password' Chris@1517: end Chris@1517: send(http_method, url, parameters, credentials(@user.login, 'my_password')) Chris@1517: end Chris@1517: Chris@1517: should_respond_with success_code Chris@1517: should_respond_with_content_type_based_on_url(url) Chris@1517: should "login as the user" do Chris@1517: assert_equal @user, User.current Chris@1517: end Chris@1517: end Chris@1517: Chris@1517: context "with an invalid HTTP authentication" do Chris@1517: setup do Chris@1517: @user = User.generate! Chris@1517: send(http_method, url, parameters, credentials(@user.login, 'wrong_password')) Chris@1517: end Chris@1517: Chris@1517: should_respond_with failure_code Chris@1517: should_respond_with_content_type_based_on_url(url) Chris@1517: should "not login as the user" do Chris@1517: assert_equal User.anonymous, User.current Chris@1517: end Chris@1517: end Chris@1517: Chris@1517: context "without credentials" do Chris@1517: setup do Chris@1517: send(http_method, url, parameters) Chris@1517: end Chris@1517: Chris@1517: should_respond_with failure_code Chris@1517: should_respond_with_content_type_based_on_url(url) Chris@1517: should "include_www_authenticate_header" do Chris@1517: assert @controller.response.headers.has_key?('WWW-Authenticate') Chris@1517: end Chris@1517: end Chris@1517: end Chris@1517: end Chris@1517: Chris@1517: # Test that a request allows the API key with HTTP BASIC Chris@1517: # Chris@1517: # @param [Symbol] http_method the HTTP method for request (:get, :post, :put, :delete) Chris@1517: # @param [String] url the request url Chris@1517: # @param [optional, Hash] parameters additional request parameters Chris@1517: # @param [optional, Hash] options additional options Chris@1517: # @option options [Symbol] :success_code Successful response code (:success) Chris@1517: # @option options [Symbol] :failure_code Failure response code (:unauthorized) Chris@1517: def self.should_allow_http_basic_auth_with_key(http_method, url, parameters={}, options={}) Chris@1517: success_code = options[:success_code] || :success Chris@1517: failure_code = options[:failure_code] || :unauthorized Chris@1517: Chris@1517: context "should allow http basic auth with a key for #{http_method} #{url}" do Chris@1517: context "with a valid HTTP authentication using the API token" do Chris@1517: setup do Chris@1517: @user = User.generate! do |user| Chris@1517: user.admin = true Chris@1517: end Chris@1517: @token = Token.create!(:user => @user, :action => 'api') Chris@1517: send(http_method, url, parameters, credentials(@token.value, 'X')) Chris@1517: end Chris@1517: should_respond_with success_code Chris@1517: should_respond_with_content_type_based_on_url(url) Chris@1517: should_be_a_valid_response_string_based_on_url(url) Chris@1517: should "login as the user" do Chris@1517: assert_equal @user, User.current Chris@1517: end Chris@1517: end Chris@1517: Chris@1517: context "with an invalid HTTP authentication" do Chris@1517: setup do Chris@1517: @user = User.generate! Chris@1517: @token = Token.create!(:user => @user, :action => 'feeds') Chris@1517: send(http_method, url, parameters, credentials(@token.value, 'X')) Chris@1517: end Chris@1517: should_respond_with failure_code Chris@1517: should_respond_with_content_type_based_on_url(url) Chris@1517: should "not login as the user" do Chris@1517: assert_equal User.anonymous, User.current Chris@1517: end Chris@1517: end Chris@1517: end Chris@1517: end Chris@1517: Chris@1517: # Test that a request allows full key authentication Chris@1517: # Chris@1517: # @param [Symbol] http_method the HTTP method for request (:get, :post, :put, :delete) Chris@1517: # @param [String] url the request url, without the key=ZXY parameter Chris@1517: # @param [optional, Hash] parameters additional request parameters Chris@1517: # @param [optional, Hash] options additional options Chris@1517: # @option options [Symbol] :success_code Successful response code (:success) Chris@1517: # @option options [Symbol] :failure_code Failure response code (:unauthorized) Chris@1517: def self.should_allow_key_based_auth(http_method, url, parameters={}, options={}) Chris@1517: success_code = options[:success_code] || :success Chris@1517: failure_code = options[:failure_code] || :unauthorized Chris@1517: Chris@1517: context "should allow key based auth using key=X for #{http_method} #{url}" do Chris@1517: context "with a valid api token" do Chris@1517: setup do Chris@1517: @user = User.generate! do |user| Chris@1517: user.admin = true Chris@1517: end Chris@1517: @token = Token.create!(:user => @user, :action => 'api') Chris@1517: # Simple url parse to add on ?key= or &key= Chris@1517: request_url = if url.match(/\?/) Chris@1517: url + "&key=#{@token.value}" Chris@1517: else Chris@1517: url + "?key=#{@token.value}" Chris@1517: end Chris@1517: send(http_method, request_url, parameters) Chris@1517: end Chris@1517: should_respond_with success_code Chris@1517: should_respond_with_content_type_based_on_url(url) Chris@1517: should_be_a_valid_response_string_based_on_url(url) Chris@1517: should "login as the user" do Chris@1517: assert_equal @user, User.current Chris@1517: end Chris@1517: end Chris@1517: Chris@1517: context "with an invalid api token" do Chris@1517: setup do Chris@1517: @user = User.generate! do |user| Chris@1517: user.admin = true Chris@1517: end Chris@1517: @token = Token.create!(:user => @user, :action => 'feeds') Chris@1517: # Simple url parse to add on ?key= or &key= Chris@1517: request_url = if url.match(/\?/) Chris@1517: url + "&key=#{@token.value}" Chris@1517: else Chris@1517: url + "?key=#{@token.value}" Chris@1517: end Chris@1517: send(http_method, request_url, parameters) Chris@1517: end Chris@1517: should_respond_with failure_code Chris@1517: should_respond_with_content_type_based_on_url(url) Chris@1517: should "not login as the user" do Chris@1517: assert_equal User.anonymous, User.current Chris@1517: end Chris@1517: end Chris@1517: end Chris@1517: Chris@1517: context "should allow key based auth using X-Redmine-API-Key header for #{http_method} #{url}" do Chris@1517: setup do Chris@1517: @user = User.generate! do |user| Chris@1517: user.admin = true Chris@1517: end Chris@1517: @token = Token.create!(:user => @user, :action => 'api') Chris@1517: send(http_method, url, parameters, {'X-Redmine-API-Key' => @token.value.to_s}) Chris@1517: end Chris@1517: should_respond_with success_code Chris@1517: should_respond_with_content_type_based_on_url(url) Chris@1517: should_be_a_valid_response_string_based_on_url(url) Chris@1517: should "login as the user" do Chris@1517: assert_equal @user, User.current Chris@1517: end Chris@1517: end Chris@1517: end Chris@1517: Chris@1517: # Uses should_respond_with_content_type based on what's in the url: Chris@1517: # Chris@1517: # '/project/issues.xml' => should_respond_with_content_type :xml Chris@1517: # '/project/issues.json' => should_respond_with_content_type :json Chris@1517: # Chris@1517: # @param [String] url Request Chris@1517: def self.should_respond_with_content_type_based_on_url(url) Chris@1517: case Chris@1517: when url.match(/xml/i) Chris@1517: should "respond with XML" do Chris@1517: assert_equal 'application/xml', @response.content_type Chris@1517: end Chris@1517: when url.match(/json/i) Chris@1517: should "respond with JSON" do Chris@1517: assert_equal 'application/json', @response.content_type Chris@1517: end Chris@1517: else Chris@1517: raise "Unknown content type for should_respond_with_content_type_based_on_url: #{url}" Chris@1517: end Chris@1517: end Chris@1517: Chris@1517: # Uses the url to assert which format the response should be in Chris@1517: # Chris@1517: # '/project/issues.xml' => should_be_a_valid_xml_string Chris@1517: # '/project/issues.json' => should_be_a_valid_json_string Chris@1517: # Chris@1517: # @param [String] url Request Chris@1517: def self.should_be_a_valid_response_string_based_on_url(url) Chris@1517: case Chris@1517: when url.match(/xml/i) Chris@1517: should_be_a_valid_xml_string Chris@1517: when url.match(/json/i) Chris@1517: should_be_a_valid_json_string Chris@1517: else Chris@1517: raise "Unknown content type for should_be_a_valid_response_based_on_url: #{url}" Chris@1517: end Chris@1517: end Chris@1517: Chris@1517: # Checks that the response is a valid JSON string Chris@1517: def self.should_be_a_valid_json_string Chris@1517: should "be a valid JSON string (or empty)" do Chris@1517: assert(response.body.blank? || ActiveSupport::JSON.decode(response.body)) Chris@1517: end Chris@1517: end Chris@1517: Chris@1517: # Checks that the response is a valid XML string Chris@1517: def self.should_be_a_valid_xml_string Chris@1517: should "be a valid XML string" do Chris@1517: assert REXML::Document.new(response.body) Chris@1517: end Chris@1517: end Chris@1517: Chris@1517: def self.should_respond_with(status) Chris@1517: should "respond with #{status}" do Chris@1517: assert_response status Chris@1517: end Chris@1517: end Chris@1517: end Chris@1517: end Chris@1517: end Chris@1517: Chris@1517: # URL helpers do not work with config.threadsafe! Chris@1517: # https://github.com/rspec/rspec-rails/issues/476#issuecomment-4705454 Chris@1517: ActionView::TestCase::TestController.instance_eval do Chris@1517: helper Rails.application.routes.url_helpers Chris@1517: end Chris@1517: ActionView::TestCase::TestController.class_eval do Chris@1517: def _routes Chris@1517: Rails.application.routes Chris@1517: end Chris@1517: end