You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

auth_source_ldap_test.rb 5.0KB

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