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