annotate .svn/pristine/2f/2f549bc1e936040c7191cdf4cf1d592cb479c086.svn-base @ 1524:82fac3dcf466 redmine-2.5-integration

Fix failure to interpret Javascript when autocompleting members for project
author Chris Cannam <chris.cannam@soundsoftware.ac.uk>
date Thu, 11 Sep 2014 10:24:38 +0100
parents e248c7af89ec
children
rev   line source
Chris@1494 1 # Redmine - project management software
Chris@1494 2 # Copyright (C) 2006-2014 Jean-Philippe Lang
Chris@1494 3 #
Chris@1494 4 # This program is free software; you can redistribute it and/or
Chris@1494 5 # modify it under the terms of the GNU General Public License
Chris@1494 6 # as published by the Free Software Foundation; either version 2
Chris@1494 7 # of the License, or (at your option) any later version.
Chris@1494 8 #
Chris@1494 9 # This program is distributed in the hope that it will be useful,
Chris@1494 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@1494 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@1494 12 # GNU General Public License for more details.
Chris@1494 13 #
Chris@1494 14 # You should have received a copy of the GNU General Public License
Chris@1494 15 # along with this program; if not, write to the Free Software
Chris@1494 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Chris@1494 17
Chris@1494 18 require File.expand_path('../../test_helper', __FILE__)
Chris@1494 19
Chris@1494 20 class AuthSourceLdapTest < ActiveSupport::TestCase
Chris@1494 21 include Redmine::I18n
Chris@1494 22 fixtures :auth_sources
Chris@1494 23
Chris@1494 24 def setup
Chris@1494 25 end
Chris@1494 26
Chris@1494 27 def test_create
Chris@1494 28 a = AuthSourceLdap.new(:name => 'My LDAP', :host => 'ldap.example.net', :port => 389, :base_dn => 'dc=example,dc=net', :attr_login => 'sAMAccountName')
Chris@1494 29 assert a.save
Chris@1494 30 end
Chris@1494 31
Chris@1494 32 def test_should_strip_ldap_attributes
Chris@1494 33 a = AuthSourceLdap.new(:name => 'My LDAP', :host => 'ldap.example.net', :port => 389, :base_dn => 'dc=example,dc=net', :attr_login => 'sAMAccountName',
Chris@1494 34 :attr_firstname => 'givenName ')
Chris@1494 35 assert a.save
Chris@1494 36 assert_equal 'givenName', a.reload.attr_firstname
Chris@1494 37 end
Chris@1494 38
Chris@1494 39 def test_replace_port_zero_to_389
Chris@1494 40 a = AuthSourceLdap.new(
Chris@1494 41 :name => 'My LDAP', :host => 'ldap.example.net', :port => 0,
Chris@1494 42 :base_dn => 'dc=example,dc=net', :attr_login => 'sAMAccountName',
Chris@1494 43 :attr_firstname => 'givenName ')
Chris@1494 44 assert a.save
Chris@1494 45 assert_equal 389, a.port
Chris@1494 46 end
Chris@1494 47
Chris@1494 48 def test_filter_should_be_validated
Chris@1494 49 set_language_if_valid 'en'
Chris@1494 50
Chris@1494 51 a = AuthSourceLdap.new(:name => 'My LDAP', :host => 'ldap.example.net', :port => 389, :attr_login => 'sn')
Chris@1494 52 a.filter = "(mail=*@redmine.org"
Chris@1494 53 assert !a.valid?
Chris@1494 54 assert_include "LDAP filter is invalid", a.errors.full_messages
Chris@1494 55
Chris@1494 56 a.filter = "(mail=*@redmine.org)"
Chris@1494 57 assert a.valid?
Chris@1494 58 end
Chris@1494 59
Chris@1494 60 if ldap_configured?
Chris@1494 61 test '#authenticate with a valid LDAP user should return the user attributes' do
Chris@1494 62 auth = AuthSourceLdap.find(1)
Chris@1494 63 auth.update_attribute :onthefly_register, true
Chris@1494 64
Chris@1494 65 attributes = auth.authenticate('example1','123456')
Chris@1494 66 assert attributes.is_a?(Hash), "An hash was not returned"
Chris@1494 67 assert_equal 'Example', attributes[:firstname]
Chris@1494 68 assert_equal 'One', attributes[:lastname]
Chris@1494 69 assert_equal 'example1@redmine.org', attributes[:mail]
Chris@1494 70 assert_equal auth.id, attributes[:auth_source_id]
Chris@1494 71 attributes.keys.each do |attribute|
Chris@1494 72 assert User.new.respond_to?("#{attribute}="), "Unexpected :#{attribute} attribute returned"
Chris@1494 73 end
Chris@1494 74 end
Chris@1494 75
Chris@1494 76 test '#authenticate with an invalid LDAP user should return nil' do
Chris@1494 77 auth = AuthSourceLdap.find(1)
Chris@1494 78 assert_equal nil, auth.authenticate('nouser','123456')
Chris@1494 79 end
Chris@1494 80
Chris@1494 81 test '#authenticate without a login should return nil' do
Chris@1494 82 auth = AuthSourceLdap.find(1)
Chris@1494 83 assert_equal nil, auth.authenticate('','123456')
Chris@1494 84 end
Chris@1494 85
Chris@1494 86 test '#authenticate without a password should return nil' do
Chris@1494 87 auth = AuthSourceLdap.find(1)
Chris@1494 88 assert_equal nil, auth.authenticate('edavis','')
Chris@1494 89 end
Chris@1494 90
Chris@1494 91 test '#authenticate without filter should return any user' do
Chris@1494 92 auth = AuthSourceLdap.find(1)
Chris@1494 93 assert auth.authenticate('example1','123456')
Chris@1494 94 assert auth.authenticate('edavis', '123456')
Chris@1494 95 end
Chris@1494 96
Chris@1494 97 test '#authenticate with filter should return user who matches the filter only' do
Chris@1494 98 auth = AuthSourceLdap.find(1)
Chris@1494 99 auth.filter = "(mail=*@redmine.org)"
Chris@1494 100
Chris@1494 101 assert auth.authenticate('example1','123456')
Chris@1494 102 assert_nil auth.authenticate('edavis', '123456')
Chris@1494 103 end
Chris@1494 104
Chris@1494 105 def test_authenticate_should_timeout
Chris@1494 106 auth_source = AuthSourceLdap.find(1)
Chris@1494 107 auth_source.timeout = 1
Chris@1494 108 def auth_source.initialize_ldap_con(*args); sleep(5); end
Chris@1494 109
Chris@1494 110 assert_raise AuthSourceTimeoutException do
Chris@1494 111 auth_source.authenticate 'example1', '123456'
Chris@1494 112 end
Chris@1494 113 end
Chris@1494 114
Chris@1494 115 def test_search_should_return_matching_entries
Chris@1494 116 results = AuthSource.search("exa")
Chris@1494 117 assert_equal 1, results.size
Chris@1494 118 result = results.first
Chris@1494 119 assert_kind_of Hash, result
Chris@1494 120 assert_equal "example1", result[:login]
Chris@1494 121 assert_equal "Example", result[:firstname]
Chris@1494 122 assert_equal "One", result[:lastname]
Chris@1494 123 assert_equal "example1@redmine.org", result[:mail]
Chris@1494 124 assert_equal 1, result[:auth_source_id]
Chris@1494 125 end
Chris@1494 126
Chris@1494 127 def test_search_with_no_match_should_return_an_empty_array
Chris@1494 128 results = AuthSource.search("wro")
Chris@1494 129 assert_equal [], results
Chris@1494 130 end
Chris@1494 131
Chris@1494 132 def test_search_with_exception_should_return_an_empty_array
Chris@1494 133 Net::LDAP.stubs(:new).raises(Net::LDAP::LdapError, 'Cannot connect')
Chris@1494 134
Chris@1494 135 results = AuthSource.search("exa")
Chris@1494 136 assert_equal [], results
Chris@1494 137 end
Chris@1494 138 else
Chris@1494 139 puts '(Test LDAP server not configured)'
Chris@1494 140 end
Chris@1494 141 end