comparison test/unit/auth_source_ldap_test.rb @ 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 e248c7af89ec
comparison
equal deleted inserted replaced
1297:0a574315af3e 1298:4f746d8966dd
1 # Redmine - project management software 1 # Redmine - project management software
2 # Copyright (C) 2006-2012 Jean-Philippe Lang 2 # Copyright (C) 2006-2013 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.
56 a.filter = "(mail=*@redmine.org)" 56 a.filter = "(mail=*@redmine.org)"
57 assert a.valid? 57 assert a.valid?
58 end 58 end
59 59
60 if ldap_configured? 60 if ldap_configured?
61 context '#authenticate' do 61 test '#authenticate with a valid LDAP user should return the user attributes' do
62 setup do 62 auth = AuthSourceLdap.find(1)
63 @auth = AuthSourceLdap.find(1) 63 auth.update_attribute :onthefly_register, true
64 @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"
65 end 73 end
74 end
66 75
67 context 'with a valid LDAP user' do 76 test '#authenticate with an invalid LDAP user should return nil' do
68 should 'return the user attributes' do 77 auth = AuthSourceLdap.find(1)
69 attributes = @auth.authenticate('example1','123456') 78 assert_equal nil, auth.authenticate('nouser','123456')
70 assert attributes.is_a?(Hash), "An hash was not returned" 79 end
71 assert_equal 'Example', attributes[:firstname]
72 assert_equal 'One', attributes[:lastname]
73 assert_equal 'example1@redmine.org', attributes[:mail]
74 assert_equal @auth.id, attributes[:auth_source_id]
75 attributes.keys.each do |attribute|
76 assert User.new.respond_to?("#{attribute}="), "Unexpected :#{attribute} attribute returned"
77 end
78 end
79 end
80 80
81 context 'with an invalid LDAP user' do 81 test '#authenticate without a login should return nil' do
82 should 'return nil' do 82 auth = AuthSourceLdap.find(1)
83 assert_equal nil, @auth.authenticate('nouser','123456') 83 assert_equal nil, auth.authenticate('','123456')
84 end 84 end
85 end
86 85
87 context 'without a login' do 86 test '#authenticate without a password should return nil' do
88 should 'return nil' do 87 auth = AuthSourceLdap.find(1)
89 assert_equal nil, @auth.authenticate('','123456') 88 assert_equal nil, auth.authenticate('edavis','')
90 end 89 end
91 end
92 90
93 context 'without a password' do 91 test '#authenticate without filter should return any user' do
94 should 'return nil' do 92 auth = AuthSourceLdap.find(1)
95 assert_equal nil, @auth.authenticate('edavis','') 93 assert auth.authenticate('example1','123456')
96 end 94 assert auth.authenticate('edavis', '123456')
97 end 95 end
98 96
99 context 'without filter' do 97 test '#authenticate with filter should return user who matches the filter only' do
100 should 'return any user' do 98 auth = AuthSourceLdap.find(1)
101 assert @auth.authenticate('example1','123456') 99 auth.filter = "(mail=*@redmine.org)"
102 assert @auth.authenticate('edavis', '123456')
103 end
104 end
105 100
106 context 'with filter' do 101 assert auth.authenticate('example1','123456')
107 setup do 102 assert_nil auth.authenticate('edavis', '123456')
108 @auth.filter = "(mail=*@redmine.org)"
109 end
110
111 should 'return user who matches the filter only' do
112 assert @auth.authenticate('example1','123456')
113 assert_nil @auth.authenticate('edavis', '123456')
114 end
115 end
116 end 103 end
117 104
118 def test_authenticate_should_timeout 105 def test_authenticate_should_timeout
119 auth_source = AuthSourceLdap.find(1) 106 auth_source = AuthSourceLdap.find(1)
120 auth_source.timeout = 1 107 auth_source.timeout = 1
122 109
123 assert_raise AuthSourceTimeoutException do 110 assert_raise AuthSourceTimeoutException do
124 auth_source.authenticate 'example1', '123456' 111 auth_source.authenticate 'example1', '123456'
125 end 112 end
126 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
127 else 138 else
128 puts '(Test LDAP server not configured)' 139 puts '(Test LDAP server not configured)'
129 end 140 end
130 end 141 end