Chris@0
|
1 # Redmine - project management software
|
Chris@0
|
2 # Copyright (C) 2006-2008 Jean-Philippe Lang
|
Chris@0
|
3 #
|
Chris@0
|
4 # This program is free software; you can redistribute it and/or
|
Chris@0
|
5 # modify it under the terms of the GNU General Public License
|
Chris@0
|
6 # as published by the Free Software Foundation; either version 2
|
Chris@0
|
7 # of the License, or (at your option) any later version.
|
Chris@0
|
8 #
|
Chris@0
|
9 # This program is distributed in the hope that it will be useful,
|
Chris@0
|
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
|
Chris@0
|
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
Chris@0
|
12 # GNU General Public License for more details.
|
Chris@0
|
13 #
|
Chris@0
|
14 # You should have received a copy of the GNU General Public License
|
Chris@0
|
15 # along with this program; if not, write to the Free Software
|
Chris@0
|
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
Chris@0
|
17
|
Chris@117
|
18 require File.expand_path('../../test_helper', __FILE__)
|
Chris@0
|
19
|
Chris@0
|
20 class AuthSourceLdapTest < ActiveSupport::TestCase
|
Chris@0
|
21 fixtures :auth_sources
|
Chris@0
|
22
|
Chris@0
|
23 def setup
|
Chris@0
|
24 end
|
Chris@0
|
25
|
Chris@0
|
26 def test_create
|
Chris@0
|
27 a = AuthSourceLdap.new(:name => 'My LDAP', :host => 'ldap.example.net', :port => 389, :base_dn => 'dc=example,dc=net', :attr_login => 'sAMAccountName')
|
Chris@0
|
28 assert a.save
|
Chris@0
|
29 end
|
Chris@0
|
30
|
Chris@0
|
31 def test_should_strip_ldap_attributes
|
Chris@0
|
32 a = AuthSourceLdap.new(:name => 'My LDAP', :host => 'ldap.example.net', :port => 389, :base_dn => 'dc=example,dc=net', :attr_login => 'sAMAccountName',
|
Chris@0
|
33 :attr_firstname => 'givenName ')
|
Chris@0
|
34 assert a.save
|
Chris@0
|
35 assert_equal 'givenName', a.reload.attr_firstname
|
Chris@0
|
36 end
|
Chris@0
|
37
|
Chris@0
|
38 if ldap_configured?
|
Chris@0
|
39 context '#authenticate' do
|
Chris@0
|
40 setup do
|
Chris@0
|
41 @auth = AuthSourceLdap.find(1)
|
Chris@0
|
42 end
|
Chris@0
|
43
|
Chris@0
|
44 context 'with a valid LDAP user' do
|
Chris@0
|
45 should 'return the user attributes' do
|
Chris@0
|
46 attributes = @auth.authenticate('example1','123456')
|
Chris@0
|
47 assert attributes.is_a?(Hash), "An hash was not returned"
|
Chris@0
|
48 assert_equal 'Example', attributes[:firstname]
|
Chris@0
|
49 assert_equal 'One', attributes[:lastname]
|
Chris@0
|
50 assert_equal 'example1@redmine.org', attributes[:mail]
|
Chris@0
|
51 assert_equal @auth.id, attributes[:auth_source_id]
|
Chris@0
|
52 attributes.keys.each do |attribute|
|
Chris@0
|
53 assert User.new.respond_to?("#{attribute}="), "Unexpected :#{attribute} attribute returned"
|
Chris@0
|
54 end
|
Chris@0
|
55 end
|
Chris@0
|
56 end
|
Chris@0
|
57
|
Chris@0
|
58 context 'with an invalid LDAP user' do
|
Chris@0
|
59 should 'return nil' do
|
Chris@0
|
60 assert_equal nil, @auth.authenticate('nouser','123456')
|
Chris@0
|
61 end
|
Chris@0
|
62 end
|
Chris@0
|
63
|
Chris@0
|
64 context 'without a login' do
|
Chris@0
|
65 should 'return nil' do
|
Chris@0
|
66 assert_equal nil, @auth.authenticate('','123456')
|
Chris@0
|
67 end
|
Chris@0
|
68 end
|
Chris@0
|
69
|
Chris@0
|
70 context 'without a password' do
|
Chris@0
|
71 should 'return nil' do
|
Chris@0
|
72 assert_equal nil, @auth.authenticate('edavis','')
|
Chris@0
|
73 end
|
Chris@0
|
74 end
|
Chris@0
|
75
|
Chris@0
|
76 end
|
Chris@0
|
77 else
|
Chris@0
|
78 puts '(Test LDAP server not configured)'
|
Chris@0
|
79 end
|
Chris@0
|
80 end
|