Chris@0
|
1 # Redmine - project management software
|
Chris@1115
|
2 # Copyright (C) 2006-2012 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@1115
|
21 include Redmine::I18n
|
Chris@0
|
22 fixtures :auth_sources
|
Chris@909
|
23
|
Chris@0
|
24 def setup
|
Chris@0
|
25 end
|
Chris@909
|
26
|
Chris@0
|
27 def test_create
|
Chris@0
|
28 a = AuthSourceLdap.new(:name => 'My LDAP', :host => 'ldap.example.net', :port => 389, :base_dn => 'dc=example,dc=net', :attr_login => 'sAMAccountName')
|
Chris@0
|
29 assert a.save
|
Chris@0
|
30 end
|
Chris@909
|
31
|
Chris@0
|
32 def test_should_strip_ldap_attributes
|
Chris@0
|
33 a = AuthSourceLdap.new(:name => 'My LDAP', :host => 'ldap.example.net', :port => 389, :base_dn => 'dc=example,dc=net', :attr_login => 'sAMAccountName',
|
Chris@0
|
34 :attr_firstname => 'givenName ')
|
Chris@0
|
35 assert a.save
|
Chris@0
|
36 assert_equal 'givenName', a.reload.attr_firstname
|
Chris@0
|
37 end
|
Chris@0
|
38
|
Chris@909
|
39 def test_replace_port_zero_to_389
|
Chris@909
|
40 a = AuthSourceLdap.new(
|
Chris@909
|
41 :name => 'My LDAP', :host => 'ldap.example.net', :port => 0,
|
Chris@909
|
42 :base_dn => 'dc=example,dc=net', :attr_login => 'sAMAccountName',
|
Chris@909
|
43 :attr_firstname => 'givenName ')
|
Chris@909
|
44 assert a.save
|
Chris@909
|
45 assert_equal 389, a.port
|
Chris@909
|
46 end
|
Chris@909
|
47
|
Chris@1115
|
48 def test_filter_should_be_validated
|
Chris@1115
|
49 set_language_if_valid 'en'
|
Chris@1115
|
50
|
Chris@1115
|
51 a = AuthSourceLdap.new(:name => 'My LDAP', :host => 'ldap.example.net', :port => 389, :attr_login => 'sn')
|
Chris@1115
|
52 a.filter = "(mail=*@redmine.org"
|
Chris@1115
|
53 assert !a.valid?
|
Chris@1115
|
54 assert_include "LDAP filter is invalid", a.errors.full_messages
|
Chris@1115
|
55
|
Chris@1115
|
56 a.filter = "(mail=*@redmine.org)"
|
Chris@1115
|
57 assert a.valid?
|
Chris@1115
|
58 end
|
Chris@1115
|
59
|
Chris@0
|
60 if ldap_configured?
|
Chris@0
|
61 context '#authenticate' do
|
Chris@0
|
62 setup do
|
Chris@0
|
63 @auth = AuthSourceLdap.find(1)
|
Chris@1115
|
64 @auth.update_attribute :onthefly_register, true
|
Chris@0
|
65 end
|
Chris@0
|
66
|
Chris@0
|
67 context 'with a valid LDAP user' do
|
Chris@0
|
68 should 'return the user attributes' do
|
Chris@0
|
69 attributes = @auth.authenticate('example1','123456')
|
Chris@0
|
70 assert attributes.is_a?(Hash), "An hash was not returned"
|
Chris@0
|
71 assert_equal 'Example', attributes[:firstname]
|
Chris@0
|
72 assert_equal 'One', attributes[:lastname]
|
Chris@0
|
73 assert_equal 'example1@redmine.org', attributes[:mail]
|
Chris@0
|
74 assert_equal @auth.id, attributes[:auth_source_id]
|
Chris@0
|
75 attributes.keys.each do |attribute|
|
Chris@0
|
76 assert User.new.respond_to?("#{attribute}="), "Unexpected :#{attribute} attribute returned"
|
Chris@0
|
77 end
|
Chris@0
|
78 end
|
Chris@0
|
79 end
|
Chris@0
|
80
|
Chris@0
|
81 context 'with an invalid LDAP user' do
|
Chris@0
|
82 should 'return nil' do
|
Chris@0
|
83 assert_equal nil, @auth.authenticate('nouser','123456')
|
Chris@0
|
84 end
|
Chris@0
|
85 end
|
Chris@0
|
86
|
Chris@0
|
87 context 'without a login' do
|
Chris@0
|
88 should 'return nil' do
|
Chris@0
|
89 assert_equal nil, @auth.authenticate('','123456')
|
Chris@0
|
90 end
|
Chris@0
|
91 end
|
Chris@0
|
92
|
Chris@0
|
93 context 'without a password' do
|
Chris@0
|
94 should 'return nil' do
|
Chris@0
|
95 assert_equal nil, @auth.authenticate('edavis','')
|
Chris@0
|
96 end
|
Chris@0
|
97 end
|
Chris@909
|
98
|
Chris@1115
|
99 context 'without filter' do
|
Chris@1115
|
100 should 'return any user' do
|
Chris@1115
|
101 assert @auth.authenticate('example1','123456')
|
Chris@1115
|
102 assert @auth.authenticate('edavis', '123456')
|
Chris@1115
|
103 end
|
Chris@1115
|
104 end
|
Chris@1115
|
105
|
Chris@1115
|
106 context 'with filter' do
|
Chris@1115
|
107 setup do
|
Chris@1115
|
108 @auth.filter = "(mail=*@redmine.org)"
|
Chris@1115
|
109 end
|
Chris@1115
|
110
|
Chris@1115
|
111 should 'return user who matches the filter only' do
|
Chris@1115
|
112 assert @auth.authenticate('example1','123456')
|
Chris@1115
|
113 assert_nil @auth.authenticate('edavis', '123456')
|
Chris@1115
|
114 end
|
Chris@1115
|
115 end
|
Chris@1115
|
116 end
|
Chris@1115
|
117
|
Chris@1115
|
118 def test_authenticate_should_timeout
|
Chris@1115
|
119 auth_source = AuthSourceLdap.find(1)
|
Chris@1115
|
120 auth_source.timeout = 1
|
Chris@1115
|
121 def auth_source.initialize_ldap_con(*args); sleep(5); end
|
Chris@1115
|
122
|
Chris@1115
|
123 assert_raise AuthSourceTimeoutException do
|
Chris@1115
|
124 auth_source.authenticate 'example1', '123456'
|
Chris@1115
|
125 end
|
Chris@0
|
126 end
|
Chris@0
|
127 else
|
Chris@0
|
128 puts '(Test LDAP server not configured)'
|
Chris@0
|
129 end
|
Chris@0
|
130 end
|