Chris@909: # Redmine - project management software Chris@909: # Copyright (C) 2006-2011 Jean-Philippe Lang Chris@909: # Chris@909: # This program is free software; you can redistribute it and/or Chris@909: # modify it under the terms of the GNU General Public License Chris@909: # as published by the Free Software Foundation; either version 2 Chris@909: # of the License, or (at your option) any later version. Chris@909: # Chris@909: # This program is distributed in the hope that it will be useful, Chris@909: # but WITHOUT ANY WARRANTY; without even the implied warranty of Chris@909: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Chris@909: # GNU General Public License for more details. Chris@909: # Chris@909: # You should have received a copy of the GNU General Public License Chris@909: # along with this program; if not, write to the Free Software Chris@909: # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. Chris@909: Chris@909: require File.expand_path('../../test_helper', __FILE__) Chris@909: Chris@909: class AuthSourceLdapTest < ActiveSupport::TestCase Chris@909: fixtures :auth_sources Chris@909: Chris@909: def setup Chris@909: end Chris@909: Chris@909: def test_create Chris@909: a = AuthSourceLdap.new(:name => 'My LDAP', :host => 'ldap.example.net', :port => 389, :base_dn => 'dc=example,dc=net', :attr_login => 'sAMAccountName') Chris@909: assert a.save Chris@909: end Chris@909: Chris@909: def test_should_strip_ldap_attributes Chris@909: a = AuthSourceLdap.new(:name => 'My LDAP', :host => 'ldap.example.net', :port => 389, :base_dn => 'dc=example,dc=net', :attr_login => 'sAMAccountName', Chris@909: :attr_firstname => 'givenName ') Chris@909: assert a.save Chris@909: assert_equal 'givenName', a.reload.attr_firstname Chris@909: end Chris@909: Chris@909: def test_replace_port_zero_to_389 Chris@909: a = AuthSourceLdap.new( Chris@909: :name => 'My LDAP', :host => 'ldap.example.net', :port => 0, Chris@909: :base_dn => 'dc=example,dc=net', :attr_login => 'sAMAccountName', Chris@909: :attr_firstname => 'givenName ') Chris@909: assert a.save Chris@909: assert_equal 389, a.port Chris@909: end Chris@909: Chris@909: if ldap_configured? Chris@909: context '#authenticate' do Chris@909: setup do Chris@909: @auth = AuthSourceLdap.find(1) Chris@909: end Chris@909: Chris@909: context 'with a valid LDAP user' do Chris@909: should 'return the user attributes' do Chris@909: attributes = @auth.authenticate('example1','123456') Chris@909: assert attributes.is_a?(Hash), "An hash was not returned" Chris@909: assert_equal 'Example', attributes[:firstname] Chris@909: assert_equal 'One', attributes[:lastname] Chris@909: assert_equal 'example1@redmine.org', attributes[:mail] Chris@909: assert_equal @auth.id, attributes[:auth_source_id] Chris@909: attributes.keys.each do |attribute| Chris@909: assert User.new.respond_to?("#{attribute}="), "Unexpected :#{attribute} attribute returned" Chris@909: end Chris@909: end Chris@909: end Chris@909: Chris@909: context 'with an invalid LDAP user' do Chris@909: should 'return nil' do Chris@909: assert_equal nil, @auth.authenticate('nouser','123456') Chris@909: end Chris@909: end Chris@909: Chris@909: context 'without a login' do Chris@909: should 'return nil' do Chris@909: assert_equal nil, @auth.authenticate('','123456') Chris@909: end Chris@909: end Chris@909: Chris@909: context 'without a password' do Chris@909: should 'return nil' do Chris@909: assert_equal nil, @auth.authenticate('edavis','') Chris@909: end Chris@909: end Chris@909: Chris@909: end Chris@909: else Chris@909: puts '(Test LDAP server not configured)' Chris@909: end Chris@909: end