# frozen_string_literal: true# Redmine - project management software# Copyright (C) 2006- Jean-Philippe Lang## 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.## 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.require_relative'../../test_helper'classRedmine::ApiTest::AuthenticationTest<Redmine::ApiTest::BasedefteardownUser.current=nilenddeftest_api_should_deny_without_credentialsget'/users/current.xml'assert_response:unauthorizedassertresponse.headers.has_key?('WWW-Authenticate')enddeftest_api_should_accept_http_basic_auth_using_username_and_passworduser=User.generate!do|user|user.password='my_password'endget'/users/current.xml',:headers=>credentials(user.login,'my_password')assert_response:okenddeftest_api_should_deny_http_basic_auth_using_username_and_wrong_passworduser=User.generate!do|user|user.password='my_password'endget'/users/current.xml',:headers=>credentials(user.login,'wrong_password')assert_response:unauthorizedenddeftest_api_should_deny_http_basic_auth_if_twofa_is_activeuser=User.generate!do|user|user.password='my_password'user.update(twofa_scheme:'totp')endget'/users/current.xml',:headers=>credentials(user.login,'my_password')assert_response:unauthorizedenddeftest_api_should_accept_http_basic_auth_using_api_keyuser=User.generate!token=Token.create!(:user=>user,:action=>'api')get'/users/current.xml',:headers=>credentials(token.value,'X')assert_response:okenddeftest_api_should_deny_http_basic_auth_using_wrong_api_keyuser=User.generate!token=Token.create!(:user=>user,:action=>'feeds')# not the API keyget'/users/current.xml',:headers=>credentials(token.value,'X')assert_response:unauthorizedenddeftest_api_should_accept_auth_using_api_key_as_parameteruser=User.generate!token=Token.create!(:user=>user,:action=>'api')get"/users/current.xml?key=#{token.value}"assert_response:okenddeftest_api_should_deny_auth_using_wrong_api_key_as_parameteruser=User.generate!token=Token.create!(:user=>user,:action=>'feeds')# not the API keyget"/users/current.xml?key=#{token.value}"assert_response:unauthorizedenddeftest_api_should_accept_auth_using_api_key_as_request_headeruser=User.generate!token=Token.create!(:user=>user,:action=>'api')get"/users/current.xml",:headers=>{'X-Redmine-API-Key'=>token.value.to_s}assert_response:okenddeftest_api_should_deny_auth_using_wrong_api_key_as_request_headeruser=User.generate!token=Token.create!(:user=>user,:action=>'feeds')# not the API keyget"/users/current.xml",:headers=>{'X-Redmine-API-Key'=>token.value.to_s}assert_response:unauthorizedenddeftest_api_should_trigger_basic_http_auth_with_basic_authorization_headerApplicationController.any_instance.expects(:authenticate_with_http_basic).onceget'/users/current.xml',:headers=>credentials('jsmith')assert_response:unauthorizedenddeftest_api_should_not_trigger_basic_http_auth_with_non_basic_authorization_headerApplicationController.any_instance.expects(:authenticate_with_http_basic).neverget'/users/current.xml',:headers=>{'HTTP_AUTHORIZATION'=>'Digest foo bar'}assert_response:unauthorizedenddeftest_invalid_utf8_credentials_should_not_trigger_an_errorinvalid_utf8="\x82"assert!invalid_utf8.valid_encoding?assert_nothing_raiseddoget'/users/current.xml',:headers=>credentials(invalid_utf8,"foo")endenddeftest_api_request_should_not_use_user_sessionlog_user('jsmith','jsmith')get'/users/current'assert_response:successget'/users/current.json'assert_response:unauthorizedenddeftest_api_should_accept_switch_user_header_for_admin_useruser=User.find(1)su=User.find(4)get'/users/current',:headers=>{'X-Redmine-API-Key'=>user.api_key,'X-Redmine-Switch-User'=>su.login}assert_response:successassert_select'h2',:text=>su.nameenddeftest_api_should_respond_with_412_when_trying_to_switch_to_a_invalid_userget'/users/current',:headers=>{'X-Redmine-API-Key'=>User.find(1).api_key,'X-Redmine-Switch-User'=>'foobar'}assert_response:precondition_failedenddeftest_api_should_respond_with_412_when_trying_to_switch_to_a_locked_useruser=User.find(5)assertuser.locked?get'/users/current',:headers=>{'X-Redmine-API-Key'=>User.find(1).api_key,'X-Redmine-Switch-User'=>user.login}assert_response:precondition_failedenddeftest_api_should_not_accept_switch_user_header_for_non_admin_useruser=User.find(2)su=User.find(4)get'/users/current',:headers=>{'X-Redmine-API-Key'=>user.api_key,'X-Redmine-Switch-User'=>su.login}assert_response:successassert_select'h2',:text=>user.nameendend