annotate .svn/pristine/34/349bb4552fd5e3ee5085280e36098a4598dd3ace.svn-base @ 1327:287f201c2802 redmine-2.2-integration

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