authentication_test.rb 5.69 KB
Newer Older
1
# frozen_string_literal: true
2

3
# Redmine - project management software
jplang's avatar
jplang committed
4
# Copyright (C) 2006-2017  Jean-Philippe Lang
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
#
# 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 File.expand_path('../../../test_helper', __FILE__)

22
class Redmine::ApiTest::AuthenticationTest < Redmine::ApiTest::Base
23 24
  fixtures :users

25 26 27 28
  def teardown
    User.current = nil
  end

jplang's avatar
jplang committed
29
  def test_api_should_deny_without_credentials
30
    get '/users/current.xml'
jplang's avatar
jplang committed
31 32 33 34 35 36 37 38 39
    assert_response 401
    assert_equal User.anonymous, User.current
    assert response.headers.has_key?('WWW-Authenticate')
  end

  def test_api_should_accept_http_basic_auth_using_username_and_password
    user = User.generate! do |user|
      user.password = 'my_password'
    end
40
    get '/users/current.xml', :headers => credentials(user.login, 'my_password')
jplang's avatar
jplang committed
41 42 43 44 45 46 47 48
    assert_response 200
    assert_equal user, User.current
  end

  def test_api_should_deny_http_basic_auth_using_username_and_wrong_password
    user = User.generate! do |user|
      user.password = 'my_password'
    end
49
    get '/users/current.xml', :headers => credentials(user.login, 'wrong_password')
jplang's avatar
jplang committed
50 51 52 53 54 55 56
    assert_response 401
    assert_equal User.anonymous, User.current
  end

  def test_api_should_accept_http_basic_auth_using_api_key
    user = User.generate!
    token = Token.create!(:user => user, :action => 'api')
57
    get '/users/current.xml', :headers => credentials(token.value, 'X')
jplang's avatar
jplang committed
58 59 60 61 62 63 64
    assert_response 200
    assert_equal user, User.current
  end

  def test_api_should_deny_http_basic_auth_using_wrong_api_key
    user = User.generate!
    token = Token.create!(:user => user, :action => 'feeds') # not the API key
65
    get '/users/current.xml', :headers => credentials(token.value, 'X')
jplang's avatar
jplang committed
66 67 68 69 70 71 72
    assert_response 401
    assert_equal User.anonymous, User.current
  end

  def test_api_should_accept_auth_using_api_key_as_parameter
    user = User.generate!
    token = Token.create!(:user => user, :action => 'api')
73
    get "/users/current.xml?key=#{token.value}"
jplang's avatar
jplang committed
74 75 76 77 78 79 80
    assert_response 200
    assert_equal user, User.current
  end

  def test_api_should_deny_auth_using_wrong_api_key_as_parameter
    user = User.generate!
    token = Token.create!(:user => user, :action => 'feeds') # not the API key
81
    get "/users/current.xml?key=#{token.value}"
jplang's avatar
jplang committed
82 83 84 85 86 87 88
    assert_response 401
    assert_equal User.anonymous, User.current
  end

  def test_api_should_accept_auth_using_api_key_as_request_header
    user = User.generate!
    token = Token.create!(:user => user, :action => 'api')
89
    get "/users/current.xml", :headers => {'X-Redmine-API-Key' => token.value.to_s}
jplang's avatar
jplang committed
90 91 92 93 94 95 96
    assert_response 200
    assert_equal user, User.current
  end

  def test_api_should_deny_auth_using_wrong_api_key_as_request_header
    user = User.generate!
    token = Token.create!(:user => user, :action => 'feeds') # not the API key
97
    get "/users/current.xml", :headers => {'X-Redmine-API-Key' => token.value.to_s}
jplang's avatar
jplang committed
98 99 100 101
    assert_response 401
    assert_equal User.anonymous, User.current
  end

102 103
  def test_api_should_trigger_basic_http_auth_with_basic_authorization_header
    ApplicationController.any_instance.expects(:authenticate_with_http_basic).once
104
    get '/users/current.xml', :headers => credentials('jsmith')
105
    assert_response 401
106 107 108 109
  end

  def test_api_should_not_trigger_basic_http_auth_with_non_basic_authorization_header
    ApplicationController.any_instance.expects(:authenticate_with_http_basic).never
110
    get '/users/current.xml', :headers => {'HTTP_AUTHORIZATION' => 'Digest foo bar'}
111
    assert_response 401
112 113
  end

114
  def test_invalid_utf8_credentials_should_not_trigger_an_error
115
    invalid_utf8 = "\x82"
116
    assert !invalid_utf8.valid_encoding?
117
    assert_nothing_raised do
118
      get '/users/current.xml', :headers => credentials(invalid_utf8, "foo")
119 120 121
    end
  end

122 123 124 125 126 127 128 129 130
  def test_api_request_should_not_use_user_session
    log_user('jsmith', 'jsmith')

    get '/users/current'
    assert_response :success

    get '/users/current.json'
    assert_response 401
  end
131 132 133 134 135

  def test_api_should_accept_switch_user_header_for_admin_user
    user = User.find(1)
    su = User.find(4)

136
    get '/users/current', :headers => {'X-Redmine-API-Key' => user.api_key, 'X-Redmine-Switch-User' => su.login}
137
    assert_response :success
138
    assert_select 'h2', :text => su.name
139 140 141 142
    assert_equal su, User.current
  end

  def test_api_should_respond_with_412_when_trying_to_switch_to_a_invalid_user
143
    get '/users/current', :headers => {'X-Redmine-API-Key' => User.find(1).api_key, 'X-Redmine-Switch-User' => 'foobar'}
144 145 146 147 148 149 150
    assert_response 412
  end

  def test_api_should_respond_with_412_when_trying_to_switch_to_a_locked_user
    user = User.find(5)
    assert user.locked?

151
    get '/users/current', :headers => {'X-Redmine-API-Key' => User.find(1).api_key, 'X-Redmine-Switch-User' => user.login}
152 153 154 155 156 157 158
    assert_response 412
  end

  def test_api_should_not_accept_switch_user_header_for_non_admin_user
    user = User.find(2)
    su = User.find(4)

159
    get '/users/current', :headers => {'X-Redmine-API-Key' => user.api_key, 'X-Redmine-Switch-User' => su.login}
160
    assert_response :success
161
    assert_select 'h2', :text => user.name
162 163
    assert_equal user, User.current
  end
164
end