annotate .svn/pristine/34/349bb4552fd5e3ee5085280e36098a4598dd3ace.svn-base @ 1295:622f24f53b42 redmine-2.3

Update to Redmine SVN revision 11972 on 2.3-stable branch
author Chris Cannam
date Fri, 14 Jun 2013 09:02:21 +0100
parents
children
rev   line source
Chris@1295 1 # Redmine - project management software
Chris@1295 2 # Copyright (C) 2006-2012 Jean-Philippe Lang
Chris@1295 3 #
Chris@1295 4 # This program is free software; you can redistribute it and/or
Chris@1295 5 # modify it under the terms of the GNU General Public License
Chris@1295 6 # as published by the Free Software Foundation; either version 2
Chris@1295 7 # of the License, or (at your option) any later version.
Chris@1295 8 #
Chris@1295 9 # This program is distributed in the hope that it will be useful,
Chris@1295 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@1295 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@1295 12 # GNU General Public License for more details.
Chris@1295 13 #
Chris@1295 14 # You should have received a copy of the GNU General Public License
Chris@1295 15 # along with this program; if not, write to the Free Software
Chris@1295 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Chris@1295 17
Chris@1295 18 require File.expand_path('../../test_helper', __FILE__)
Chris@1295 19
Chris@1295 20 class AuthSourceLdapTest < ActiveSupport::TestCase
Chris@1295 21 include Redmine::I18n
Chris@1295 22 fixtures :auth_sources
Chris@1295 23
Chris@1295 24 def setup
Chris@1295 25 end
Chris@1295 26
Chris@1295 27 def test_create
Chris@1295 28 a = AuthSourceLdap.new(:name => 'My LDAP', :host => 'ldap.example.net', :port => 389, :base_dn => 'dc=example,dc=net', :attr_login => 'sAMAccountName')
Chris@1295 29 assert a.save
Chris@1295 30 end
Chris@1295 31
Chris@1295 32 def test_should_strip_ldap_attributes
Chris@1295 33 a = AuthSourceLdap.new(:name => 'My LDAP', :host => 'ldap.example.net', :port => 389, :base_dn => 'dc=example,dc=net', :attr_login => 'sAMAccountName',
Chris@1295 34 :attr_firstname => 'givenName ')
Chris@1295 35 assert a.save
Chris@1295 36 assert_equal 'givenName', a.reload.attr_firstname
Chris@1295 37 end
Chris@1295 38
Chris@1295 39 def test_replace_port_zero_to_389
Chris@1295 40 a = AuthSourceLdap.new(
Chris@1295 41 :name => 'My LDAP', :host => 'ldap.example.net', :port => 0,
Chris@1295 42 :base_dn => 'dc=example,dc=net', :attr_login => 'sAMAccountName',
Chris@1295 43 :attr_firstname => 'givenName ')
Chris@1295 44 assert a.save
Chris@1295 45 assert_equal 389, a.port
Chris@1295 46 end
Chris@1295 47
Chris@1295 48 def test_filter_should_be_validated
Chris@1295 49 set_language_if_valid 'en'
Chris@1295 50
Chris@1295 51 a = AuthSourceLdap.new(:name => 'My LDAP', :host => 'ldap.example.net', :port => 389, :attr_login => 'sn')
Chris@1295 52 a.filter = "(mail=*@redmine.org"
Chris@1295 53 assert !a.valid?
Chris@1295 54 assert_include "LDAP filter is invalid", a.errors.full_messages
Chris@1295 55
Chris@1295 56 a.filter = "(mail=*@redmine.org)"
Chris@1295 57 assert a.valid?
Chris@1295 58 end
Chris@1295 59
Chris@1295 60 if ldap_configured?
Chris@1295 61 context '#authenticate' do
Chris@1295 62 setup do
Chris@1295 63 @auth = AuthSourceLdap.find(1)
Chris@1295 64 @auth.update_attribute :onthefly_register, true
Chris@1295 65 end
Chris@1295 66
Chris@1295 67 context 'with a valid LDAP user' do
Chris@1295 68 should 'return the user attributes' do
Chris@1295 69 attributes = @auth.authenticate('example1','123456')
Chris@1295 70 assert attributes.is_a?(Hash), "An hash was not returned"
Chris@1295 71 assert_equal 'Example', attributes[:firstname]
Chris@1295 72 assert_equal 'One', attributes[:lastname]
Chris@1295 73 assert_equal 'example1@redmine.org', attributes[:mail]
Chris@1295 74 assert_equal @auth.id, attributes[:auth_source_id]
Chris@1295 75 attributes.keys.each do |attribute|
Chris@1295 76 assert User.new.respond_to?("#{attribute}="), "Unexpected :#{attribute} attribute returned"
Chris@1295 77 end
Chris@1295 78 end
Chris@1295 79 end
Chris@1295 80
Chris@1295 81 context 'with an invalid LDAP user' do
Chris@1295 82 should 'return nil' do
Chris@1295 83 assert_equal nil, @auth.authenticate('nouser','123456')
Chris@1295 84 end
Chris@1295 85 end
Chris@1295 86
Chris@1295 87 context 'without a login' do
Chris@1295 88 should 'return nil' do
Chris@1295 89 assert_equal nil, @auth.authenticate('','123456')
Chris@1295 90 end
Chris@1295 91 end
Chris@1295 92
Chris@1295 93 context 'without a password' do
Chris@1295 94 should 'return nil' do
Chris@1295 95 assert_equal nil, @auth.authenticate('edavis','')
Chris@1295 96 end
Chris@1295 97 end
Chris@1295 98
Chris@1295 99 context 'without filter' do
Chris@1295 100 should 'return any user' do
Chris@1295 101 assert @auth.authenticate('example1','123456')
Chris@1295 102 assert @auth.authenticate('edavis', '123456')
Chris@1295 103 end
Chris@1295 104 end
Chris@1295 105
Chris@1295 106 context 'with filter' do
Chris@1295 107 setup do
Chris@1295 108 @auth.filter = "(mail=*@redmine.org)"
Chris@1295 109 end
Chris@1295 110
Chris@1295 111 should 'return user who matches the filter only' do
Chris@1295 112 assert @auth.authenticate('example1','123456')
Chris@1295 113 assert_nil @auth.authenticate('edavis', '123456')
Chris@1295 114 end
Chris@1295 115 end
Chris@1295 116 end
Chris@1295 117
Chris@1295 118 def test_authenticate_should_timeout
Chris@1295 119 auth_source = AuthSourceLdap.find(1)
Chris@1295 120 auth_source.timeout = 1
Chris@1295 121 def auth_source.initialize_ldap_con(*args); sleep(5); end
Chris@1295 122
Chris@1295 123 assert_raise AuthSourceTimeoutException do
Chris@1295 124 auth_source.authenticate 'example1', '123456'
Chris@1295 125 end
Chris@1295 126 end
Chris@1295 127 else
Chris@1295 128 puts '(Test LDAP server not configured)'
Chris@1295 129 end
Chris@1295 130 end