To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.

Statistics Download as Zip
| Branch: | Tag: | Revision:

root / .svn / pristine / 34 / 349bb4552fd5e3ee5085280e36098a4598dd3ace.svn-base @ 1298:4f746d8966dd

History | View | Annotate | Download (4.26 KB)

1
# Redmine - project management software
2
# Copyright (C) 2006-2012  Jean-Philippe Lang
3
#
4
# This program is free software; you can redistribute it and/or
5
# modify it under the terms of the GNU General Public License
6
# as published by the Free Software Foundation; either version 2
7
# of the License, or (at your option) any later version.
8
#
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
# GNU General Public License for more details.
13
#
14
# You should have received a copy of the GNU General Public License
15
# along with this program; if not, write to the Free Software
16
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
17

    
18
require File.expand_path('../../test_helper', __FILE__)
19

    
20
class AuthSourceLdapTest < ActiveSupport::TestCase
21
  include Redmine::I18n
22
  fixtures :auth_sources
23

    
24
  def setup
25
  end
26

    
27
  def test_create
28
    a = AuthSourceLdap.new(:name => 'My LDAP', :host => 'ldap.example.net', :port => 389, :base_dn => 'dc=example,dc=net', :attr_login => 'sAMAccountName')
29
    assert a.save
30
  end
31

    
32
  def test_should_strip_ldap_attributes
33
    a = AuthSourceLdap.new(:name => 'My LDAP', :host => 'ldap.example.net', :port => 389, :base_dn => 'dc=example,dc=net', :attr_login => 'sAMAccountName',
34
                           :attr_firstname => 'givenName ')
35
    assert a.save
36
    assert_equal 'givenName', a.reload.attr_firstname
37
  end
38

    
39
  def test_replace_port_zero_to_389
40
    a = AuthSourceLdap.new(
41
           :name => 'My LDAP', :host => 'ldap.example.net', :port => 0,
42
           :base_dn => 'dc=example,dc=net', :attr_login => 'sAMAccountName',
43
           :attr_firstname => 'givenName ')
44
    assert a.save
45
    assert_equal 389, a.port
46
  end
47

    
48
  def test_filter_should_be_validated
49
    set_language_if_valid 'en'
50

    
51
    a = AuthSourceLdap.new(:name => 'My LDAP', :host => 'ldap.example.net', :port => 389, :attr_login => 'sn')
52
    a.filter = "(mail=*@redmine.org"
53
    assert !a.valid?
54
    assert_include "LDAP filter is invalid", a.errors.full_messages
55

    
56
    a.filter = "(mail=*@redmine.org)"
57
    assert a.valid?
58
  end
59

    
60
  if ldap_configured?
61
    context '#authenticate' do
62
      setup do
63
        @auth = AuthSourceLdap.find(1)
64
        @auth.update_attribute :onthefly_register, true
65
      end
66

    
67
      context 'with a valid LDAP user' do
68
        should 'return the user attributes' do
69
          attributes =  @auth.authenticate('example1','123456')
70
          assert attributes.is_a?(Hash), "An hash was not returned"
71
          assert_equal 'Example', attributes[:firstname]
72
          assert_equal 'One', attributes[:lastname]
73
          assert_equal 'example1@redmine.org', attributes[:mail]
74
          assert_equal @auth.id, attributes[:auth_source_id]
75
          attributes.keys.each do |attribute|
76
            assert User.new.respond_to?("#{attribute}="), "Unexpected :#{attribute} attribute returned"
77
          end
78
        end
79
      end
80

    
81
      context 'with an invalid LDAP user' do
82
        should 'return nil' do
83
          assert_equal nil, @auth.authenticate('nouser','123456')
84
        end
85
      end
86

    
87
      context 'without a login' do
88
        should 'return nil' do
89
          assert_equal nil, @auth.authenticate('','123456')
90
        end
91
      end
92

    
93
      context 'without a password' do
94
        should 'return nil' do
95
          assert_equal nil, @auth.authenticate('edavis','')
96
        end
97
      end
98

    
99
      context 'without filter' do
100
        should 'return any user' do
101
          assert @auth.authenticate('example1','123456')
102
          assert @auth.authenticate('edavis', '123456')
103
        end
104
      end
105

    
106
      context 'with filter' do
107
        setup do
108
          @auth.filter = "(mail=*@redmine.org)"
109
        end
110

    
111
        should 'return user who matches the filter only' do
112
          assert @auth.authenticate('example1','123456')
113
          assert_nil @auth.authenticate('edavis', '123456')
114
        end
115
      end
116
    end
117

    
118
    def test_authenticate_should_timeout
119
      auth_source = AuthSourceLdap.find(1)
120
      auth_source.timeout = 1
121
      def auth_source.initialize_ldap_con(*args); sleep(5); end
122

    
123
      assert_raise AuthSourceTimeoutException do
124
        auth_source.authenticate 'example1', '123456'
125
      end
126
    end
127
  else
128
    puts '(Test LDAP server not configured)'
129
  end
130
end