comparison .svn/pristine/84/844e1f7efcdb7d2e09fc680929a5a6cad80561f3.svn-base @ 1298:4f746d8966dd redmine_2.3_integration

Merge from redmine-2.3 branch to create new branch redmine-2.3-integration
author Chris Cannam
date Fri, 14 Jun 2013 09:28:30 +0100
parents 622f24f53b42
children
comparison
equal deleted inserted replaced
1297:0a574315af3e 1298:4f746d8966dd
1 # Redmine - project management software
2 # Copyright (C) 2006-2013 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 test '#authenticate with a valid LDAP user should return the user attributes' do
62 auth = AuthSourceLdap.find(1)
63 auth.update_attribute :onthefly_register, true
64
65 attributes = auth.authenticate('example1','123456')
66 assert attributes.is_a?(Hash), "An hash was not returned"
67 assert_equal 'Example', attributes[:firstname]
68 assert_equal 'One', attributes[:lastname]
69 assert_equal 'example1@redmine.org', attributes[:mail]
70 assert_equal auth.id, attributes[:auth_source_id]
71 attributes.keys.each do |attribute|
72 assert User.new.respond_to?("#{attribute}="), "Unexpected :#{attribute} attribute returned"
73 end
74 end
75
76 test '#authenticate with an invalid LDAP user should return nil' do
77 auth = AuthSourceLdap.find(1)
78 assert_equal nil, auth.authenticate('nouser','123456')
79 end
80
81 test '#authenticate without a login should return nil' do
82 auth = AuthSourceLdap.find(1)
83 assert_equal nil, auth.authenticate('','123456')
84 end
85
86 test '#authenticate without a password should return nil' do
87 auth = AuthSourceLdap.find(1)
88 assert_equal nil, auth.authenticate('edavis','')
89 end
90
91 test '#authenticate without filter should return any user' do
92 auth = AuthSourceLdap.find(1)
93 assert auth.authenticate('example1','123456')
94 assert auth.authenticate('edavis', '123456')
95 end
96
97 test '#authenticate with filter should return user who matches the filter only' do
98 auth = AuthSourceLdap.find(1)
99 auth.filter = "(mail=*@redmine.org)"
100
101 assert auth.authenticate('example1','123456')
102 assert_nil auth.authenticate('edavis', '123456')
103 end
104
105 def test_authenticate_should_timeout
106 auth_source = AuthSourceLdap.find(1)
107 auth_source.timeout = 1
108 def auth_source.initialize_ldap_con(*args); sleep(5); end
109
110 assert_raise AuthSourceTimeoutException do
111 auth_source.authenticate 'example1', '123456'
112 end
113 end
114
115 def test_search_should_return_matching_entries
116 results = AuthSource.search("exa")
117 assert_equal 1, results.size
118 result = results.first
119 assert_kind_of Hash, result
120 assert_equal "example1", result[:login]
121 assert_equal "Example", result[:firstname]
122 assert_equal "One", result[:lastname]
123 assert_equal "example1@redmine.org", result[:mail]
124 assert_equal 1, result[:auth_source_id]
125 end
126
127 def test_search_with_no_match_should_return_an_empty_array
128 results = AuthSource.search("wro")
129 assert_equal [], results
130 end
131
132 def test_search_with_exception_should_return_an_empty_array
133 Net::LDAP.stubs(:new).raises(Net::LDAP::LdapError, 'Cannot connect')
134
135 results = AuthSource.search("exa")
136 assert_equal [], results
137 end
138 else
139 puts '(Test LDAP server not configured)'
140 end
141 end