@@ -1,234 +1,234 | |||
|
1 | 1 | # Redmine - project management software |
|
2 | 2 | # Copyright (C) 2006-2015 Jean-Philippe Lang |
|
3 | 3 | # |
|
4 | 4 | # This program is free software; you can redistribute it and/or |
|
5 | 5 | # modify it under the terms of the GNU General Public License |
|
6 | 6 | # as published by the Free Software Foundation; either version 2 |
|
7 | 7 | # of the License, or (at your option) any later version. |
|
8 | 8 | # |
|
9 | 9 | # This program is distributed in the hope that it will be useful, |
|
10 | 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 | 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 | 12 | # GNU General Public License for more details. |
|
13 | 13 | # |
|
14 | 14 | # You should have received a copy of the GNU General Public License |
|
15 | 15 | # along with this program; if not, write to the Free Software |
|
16 | 16 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
|
17 | 17 | |
|
18 | 18 | require File.expand_path('../../test_helper', __FILE__) |
|
19 | 19 | |
|
20 | 20 | class AuthSourceLdapTest < ActiveSupport::TestCase |
|
21 | 21 | include Redmine::I18n |
|
22 | 22 | fixtures :auth_sources |
|
23 | 23 | |
|
24 | 24 | def setup |
|
25 | 25 | end |
|
26 | 26 | |
|
27 | 27 | def test_initialize |
|
28 | 28 | auth_source = AuthSourceLdap.new |
|
29 | 29 | assert_nil auth_source.id |
|
30 | 30 | assert_equal "AuthSourceLdap", auth_source.type |
|
31 | 31 | assert_equal "", auth_source.name |
|
32 | 32 | assert_nil auth_source.host |
|
33 | 33 | assert_nil auth_source.port |
|
34 | 34 | assert_nil auth_source.account |
|
35 | 35 | assert_equal "", auth_source.account_password |
|
36 | 36 | assert_nil auth_source.base_dn |
|
37 | 37 | assert_nil auth_source.attr_login |
|
38 | 38 | assert_nil auth_source.attr_firstname |
|
39 | 39 | assert_nil auth_source.attr_lastname |
|
40 | 40 | assert_nil auth_source.attr_mail |
|
41 | 41 | assert_equal false, auth_source.onthefly_register |
|
42 | 42 | assert_equal false, auth_source.tls |
|
43 | 43 | assert_nil auth_source.filter |
|
44 | 44 | assert_nil auth_source.timeout |
|
45 | 45 | end |
|
46 | 46 | |
|
47 | 47 | def test_create |
|
48 | 48 | a = AuthSourceLdap.new(:name => 'My LDAP', :host => 'ldap.example.net', :port => 389, :base_dn => 'dc=example,dc=net', :attr_login => 'sAMAccountName') |
|
49 | 49 | assert a.save |
|
50 | 50 | end |
|
51 | 51 | |
|
52 | 52 | def test_should_strip_ldap_attributes |
|
53 | 53 | a = AuthSourceLdap.new(:name => 'My LDAP', :host => 'ldap.example.net', :port => 389, :base_dn => 'dc=example,dc=net', :attr_login => 'sAMAccountName', |
|
54 | 54 | :attr_firstname => 'givenName ') |
|
55 | 55 | assert a.save |
|
56 | 56 | assert_equal 'givenName', a.reload.attr_firstname |
|
57 | 57 | end |
|
58 | 58 | |
|
59 | 59 | def test_replace_port_zero_to_389 |
|
60 | 60 | a = AuthSourceLdap.new( |
|
61 | 61 | :name => 'My LDAP', :host => 'ldap.example.net', :port => 0, |
|
62 | 62 | :base_dn => 'dc=example,dc=net', :attr_login => 'sAMAccountName', |
|
63 | 63 | :attr_firstname => 'givenName ') |
|
64 | 64 | assert a.save |
|
65 | 65 | assert_equal 389, a.port |
|
66 | 66 | end |
|
67 | 67 | |
|
68 | 68 | def test_filter_should_be_validated |
|
69 | 69 | set_language_if_valid 'en' |
|
70 | 70 | |
|
71 | 71 | a = AuthSourceLdap.new(:name => 'My LDAP', :host => 'ldap.example.net', :port => 389, :attr_login => 'sn') |
|
72 | 72 | a.filter = "(mail=*@redmine.org" |
|
73 | 73 | assert !a.valid? |
|
74 | 74 | assert_include "LDAP filter is invalid", a.errors.full_messages |
|
75 | 75 | |
|
76 | 76 | a.filter = "(mail=*@redmine.org)" |
|
77 | 77 | assert a.valid? |
|
78 | 78 | end |
|
79 | 79 | |
|
80 | 80 | if ldap_configured? |
|
81 | 81 | test '#authenticate with a valid LDAP user should return the user attributes' do |
|
82 | 82 | auth = AuthSourceLdap.find(1) |
|
83 | 83 | auth.update_attribute :onthefly_register, true |
|
84 | 84 | |
|
85 | 85 | attributes = auth.authenticate('example1','123456') |
|
86 | 86 | assert attributes.is_a?(Hash), "An hash was not returned" |
|
87 | 87 | assert_equal 'Example', attributes[:firstname] |
|
88 | 88 | assert_equal 'One', attributes[:lastname] |
|
89 | 89 | assert_equal 'example1@redmine.org', attributes[:mail] |
|
90 | 90 | assert_equal auth.id, attributes[:auth_source_id] |
|
91 | 91 | attributes.keys.each do |attribute| |
|
92 | 92 | assert User.new.respond_to?("#{attribute}="), "Unexpected :#{attribute} attribute returned" |
|
93 | 93 | end |
|
94 | 94 | end |
|
95 | 95 | |
|
96 | 96 | test '#authenticate with an invalid LDAP user should return nil' do |
|
97 | 97 | auth = AuthSourceLdap.find(1) |
|
98 | 98 | assert_equal nil, auth.authenticate('nouser','123456') |
|
99 | 99 | end |
|
100 | 100 | |
|
101 | 101 | test '#authenticate without a login should return nil' do |
|
102 | 102 | auth = AuthSourceLdap.find(1) |
|
103 | 103 | assert_equal nil, auth.authenticate('','123456') |
|
104 | 104 | end |
|
105 | 105 | |
|
106 | 106 | test '#authenticate without a password should return nil' do |
|
107 | 107 | auth = AuthSourceLdap.find(1) |
|
108 | 108 | assert_equal nil, auth.authenticate('edavis','') |
|
109 | 109 | end |
|
110 | 110 | |
|
111 | 111 | test '#authenticate without filter should return any user' do |
|
112 | 112 | auth = AuthSourceLdap.find(1) |
|
113 | 113 | assert auth.authenticate('example1','123456') |
|
114 | 114 | assert auth.authenticate('edavis', '123456') |
|
115 | 115 | end |
|
116 | 116 | |
|
117 | 117 | test '#authenticate with filter should return user who matches the filter only' do |
|
118 | 118 | auth = AuthSourceLdap.find(1) |
|
119 | 119 | auth.filter = "(mail=*@redmine.org)" |
|
120 | 120 | |
|
121 | 121 | assert auth.authenticate('example1','123456') |
|
122 | 122 | assert_nil auth.authenticate('edavis', '123456') |
|
123 | 123 | end |
|
124 | 124 | |
|
125 | 125 | def test_authenticate_should_timeout |
|
126 | 126 | auth_source = AuthSourceLdap.find(1) |
|
127 | 127 | auth_source.timeout = 1 |
|
128 | 128 | def auth_source.initialize_ldap_con(*args); sleep(5); end |
|
129 | 129 | |
|
130 | 130 | assert_raise AuthSourceTimeoutException do |
|
131 | 131 | auth_source.authenticate 'example1', '123456' |
|
132 | 132 | end |
|
133 | 133 | end |
|
134 | 134 | |
|
135 | 135 | def test_search_should_return_matching_entries |
|
136 | 136 | results = AuthSource.search("exa") |
|
137 | 137 | assert_equal 1, results.size |
|
138 | 138 | result = results.first |
|
139 | 139 | assert_kind_of Hash, result |
|
140 | 140 | assert_equal "example1", result[:login] |
|
141 | 141 | assert_equal "Example", result[:firstname] |
|
142 | 142 | assert_equal "One", result[:lastname] |
|
143 | 143 | assert_equal "example1@redmine.org", result[:mail] |
|
144 | 144 | assert_equal 1, result[:auth_source_id] |
|
145 | 145 | end |
|
146 | 146 | |
|
147 | 147 | def test_search_with_no_match_should_return_an_empty_array |
|
148 | 148 | results = AuthSource.search("wro") |
|
149 | 149 | assert_equal [], results |
|
150 | 150 | end |
|
151 | 151 | |
|
152 | 152 | def test_search_with_exception_should_return_an_empty_array |
|
153 | 153 | Net::LDAP.stubs(:new).raises(Net::LDAP::LdapError, 'Cannot connect') |
|
154 | 154 | |
|
155 | 155 | results = AuthSource.search("exa") |
|
156 | 156 | assert_equal [], results |
|
157 | 157 | end |
|
158 | 158 | |
|
159 |
def test_ |
|
|
159 | def test_test_connection_with_correct_host_and_port | |
|
160 | 160 | auth_source = AuthSourceLdap.find(1) |
|
161 | 161 | |
|
162 | 162 | assert_nothing_raised Net::LDAP::Error do |
|
163 | 163 | auth_source.test_connection |
|
164 | 164 | end |
|
165 | 165 | end |
|
166 | 166 | |
|
167 |
def test_ |
|
|
167 | def test_test_connection_with_incorrect_host | |
|
168 | 168 | auth_source = AuthSourceLdap.find(1) |
|
169 | 169 | auth_source.host = "badhost" |
|
170 | 170 | auth_source.save! |
|
171 | 171 | |
|
172 | 172 | assert_raise Net::LDAP::Error do |
|
173 | 173 | auth_source.test_connection |
|
174 | 174 | end |
|
175 | 175 | end |
|
176 | 176 | |
|
177 |
def test_ |
|
|
177 | def test_test_connection_with_incorrect_port | |
|
178 | 178 | auth_source = AuthSourceLdap.find(1) |
|
179 | 179 | auth_source.port = 1234 |
|
180 | 180 | auth_source.save! |
|
181 | 181 | |
|
182 | 182 | assert_raise Net::LDAP::Error do |
|
183 | 183 | auth_source.test_connection |
|
184 | 184 | end |
|
185 | 185 | end |
|
186 | 186 | |
|
187 |
def test_ |
|
|
187 | def test_test_connection_bind_with_account_and_password | |
|
188 | 188 | auth_source = AuthSourceLdap.find(1) |
|
189 | 189 | auth_source.account = "cn=admin,dc=redmine,dc=org" |
|
190 | 190 | auth_source.account_password = "secret" |
|
191 | 191 | auth_source.save! |
|
192 | 192 | |
|
193 | 193 | assert_equal "cn=admin,dc=redmine,dc=org", auth_source.account |
|
194 | 194 | assert_equal "secret", auth_source.account_password |
|
195 | 195 | assert_nil auth_source.test_connection |
|
196 | 196 | end |
|
197 | 197 | |
|
198 |
def test_ |
|
|
198 | def test_test_connection_bind_without_account_and_password | |
|
199 | 199 | auth_source = AuthSourceLdap.find(1) |
|
200 | 200 | |
|
201 | 201 | assert_nil auth_source.account |
|
202 | 202 | assert_equal "", auth_source.account_password |
|
203 | 203 | assert_nil auth_source.test_connection |
|
204 | 204 | end |
|
205 | 205 | |
|
206 |
def test_ |
|
|
206 | def test_test_connection_bind_with_incorrect_account | |
|
207 | 207 | auth_source = AuthSourceLdap.find(1) |
|
208 | 208 | auth_source.account = "cn=baduser,dc=redmine,dc=org" |
|
209 | 209 | auth_source.account_password = "secret" |
|
210 | 210 | auth_source.save! |
|
211 | 211 | |
|
212 | 212 | assert_equal "cn=baduser,dc=redmine,dc=org", auth_source.account |
|
213 | 213 | assert_equal "secret", auth_source.account_password |
|
214 | 214 | assert_raise AuthSourceException do |
|
215 | 215 | auth_source.test_connection |
|
216 | 216 | end |
|
217 | 217 | end |
|
218 | 218 | |
|
219 |
def test_ |
|
|
219 | def test_test_connection_bind_with_incorrect_password | |
|
220 | 220 | auth_source = AuthSourceLdap.find(1) |
|
221 | 221 | auth_source.account = "cn=admin,dc=redmine,dc=org" |
|
222 | 222 | auth_source.account_password = "badpassword" |
|
223 | 223 | auth_source.save! |
|
224 | 224 | |
|
225 | 225 | assert_equal "cn=admin,dc=redmine,dc=org", auth_source.account |
|
226 | 226 | assert_equal "badpassword", auth_source.account_password |
|
227 | 227 | assert_raise AuthSourceException do |
|
228 | 228 | auth_source.test_connection |
|
229 | 229 | end |
|
230 | 230 | end |
|
231 | 231 | else |
|
232 | 232 | puts '(Test LDAP server not configured)' |
|
233 | 233 | end |
|
234 | 234 | end |
General Comments 0
You need to be logged in to leave comments.
Login now