Chris@0
|
1 # Redmine - project management software
|
Chris@909
|
2 # Copyright (C) 2006-2011 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@909
|
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@909
|
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@119
|
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@909
|
22
|
Chris@0
|
23 def setup
|
Chris@0
|
24 end
|
Chris@909
|
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@909
|
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@909
|
38 def test_replace_port_zero_to_389
|
Chris@909
|
39 a = AuthSourceLdap.new(
|
Chris@909
|
40 :name => 'My LDAP', :host => 'ldap.example.net', :port => 0,
|
Chris@909
|
41 :base_dn => 'dc=example,dc=net', :attr_login => 'sAMAccountName',
|
Chris@909
|
42 :attr_firstname => 'givenName ')
|
Chris@909
|
43 assert a.save
|
Chris@909
|
44 assert_equal 389, a.port
|
Chris@909
|
45 end
|
Chris@909
|
46
|
Chris@0
|
47 if ldap_configured?
|
Chris@0
|
48 context '#authenticate' do
|
Chris@0
|
49 setup do
|
Chris@0
|
50 @auth = AuthSourceLdap.find(1)
|
Chris@0
|
51 end
|
Chris@0
|
52
|
Chris@0
|
53 context 'with a valid LDAP user' do
|
Chris@0
|
54 should 'return the user attributes' do
|
Chris@0
|
55 attributes = @auth.authenticate('example1','123456')
|
Chris@0
|
56 assert attributes.is_a?(Hash), "An hash was not returned"
|
Chris@0
|
57 assert_equal 'Example', attributes[:firstname]
|
Chris@0
|
58 assert_equal 'One', attributes[:lastname]
|
Chris@0
|
59 assert_equal 'example1@redmine.org', attributes[:mail]
|
Chris@0
|
60 assert_equal @auth.id, attributes[:auth_source_id]
|
Chris@0
|
61 attributes.keys.each do |attribute|
|
Chris@0
|
62 assert User.new.respond_to?("#{attribute}="), "Unexpected :#{attribute} attribute returned"
|
Chris@0
|
63 end
|
Chris@0
|
64 end
|
Chris@0
|
65 end
|
Chris@0
|
66
|
Chris@0
|
67 context 'with an invalid LDAP user' do
|
Chris@0
|
68 should 'return nil' do
|
Chris@0
|
69 assert_equal nil, @auth.authenticate('nouser','123456')
|
Chris@0
|
70 end
|
Chris@0
|
71 end
|
Chris@0
|
72
|
Chris@0
|
73 context 'without a login' do
|
Chris@0
|
74 should 'return nil' do
|
Chris@0
|
75 assert_equal nil, @auth.authenticate('','123456')
|
Chris@0
|
76 end
|
Chris@0
|
77 end
|
Chris@0
|
78
|
Chris@0
|
79 context 'without a password' do
|
Chris@0
|
80 should 'return nil' do
|
Chris@0
|
81 assert_equal nil, @auth.authenticate('edavis','')
|
Chris@0
|
82 end
|
Chris@0
|
83 end
|
Chris@909
|
84
|
Chris@0
|
85 end
|
Chris@0
|
86 else
|
Chris@0
|
87 puts '(Test LDAP server not configured)'
|
Chris@0
|
88 end
|
Chris@0
|
89 end
|