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 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. # Redmine - project management software
  2. # Copyright (C) 2006-2014 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. test '#authenticate with a valid LDAP user should return the user attributes' do
  52. auth = AuthSourceLdap.find(1)
  53. auth.update_attribute :onthefly_register, true
  54. attributes = auth.authenticate('example1','123456')
  55. assert attributes.is_a?(Hash), "An hash was not returned"
  56. assert_equal 'Example', attributes[:firstname]
  57. assert_equal 'One', attributes[:lastname]
  58. assert_equal 'example1@redmine.org', attributes[:mail]
  59. assert_equal auth.id, attributes[:auth_source_id]
  60. attributes.keys.each do |attribute|
  61. assert User.new.respond_to?("#{attribute}="), "Unexpected :#{attribute} attribute returned"
  62. end
  63. end
  64. test '#authenticate with an invalid LDAP user should return nil' do
  65. auth = AuthSourceLdap.find(1)
  66. assert_equal nil, auth.authenticate('nouser','123456')
  67. end
  68. test '#authenticate without a login should return nil' do
  69. auth = AuthSourceLdap.find(1)
  70. assert_equal nil, auth.authenticate('','123456')
  71. end
  72. test '#authenticate without a password should return nil' do
  73. auth = AuthSourceLdap.find(1)
  74. assert_equal nil, auth.authenticate('edavis','')
  75. end
  76. test '#authenticate without filter should return any user' do
  77. auth = AuthSourceLdap.find(1)
  78. assert auth.authenticate('example1','123456')
  79. assert auth.authenticate('edavis', '123456')
  80. end
  81. test '#authenticate with filter should return user who matches the filter only' do
  82. auth = AuthSourceLdap.find(1)
  83. auth.filter = "(mail=*@redmine.org)"
  84. assert auth.authenticate('example1','123456')
  85. assert_nil auth.authenticate('edavis', '123456')
  86. end
  87. def test_authenticate_should_timeout
  88. auth_source = AuthSourceLdap.find(1)
  89. auth_source.timeout = 1
  90. def auth_source.initialize_ldap_con(*args); sleep(5); end
  91. assert_raise AuthSourceTimeoutException do
  92. auth_source.authenticate 'example1', '123456'
  93. end
  94. end
  95. def test_search_should_return_matching_entries
  96. results = AuthSource.search("exa")
  97. assert_equal 1, results.size
  98. result = results.first
  99. assert_kind_of Hash, result
  100. assert_equal "example1", result[:login]
  101. assert_equal "Example", result[:firstname]
  102. assert_equal "One", result[:lastname]
  103. assert_equal "example1@redmine.org", result[:mail]
  104. assert_equal 1, result[:auth_source_id]
  105. end
  106. def test_search_with_no_match_should_return_an_empty_array
  107. results = AuthSource.search("wro")
  108. assert_equal [], results
  109. end
  110. def test_search_with_exception_should_return_an_empty_array
  111. Net::LDAP.stubs(:new).raises(Net::LDAP::LdapError, 'Cannot connect')
  112. results = AuthSource.search("exa")
  113. assert_equal [], results
  114. end
  115. else
  116. puts '(Test LDAP server not configured)'
  117. end
  118. end