From 4caca8db703660fd3c94488ecb04556c9bb14edf Mon Sep 17 00:00:00 2001 From: lukemelia Date: Mon, 6 Nov 2006 07:32:48 +0000 Subject: [PATCH] Improved reliability of ldap test (somewhat... it's still very environment specific). Fixed validations in User model. Updated environment.rb.tmpl to be a little easier to follow. A note for upgraders. Be sure to include the following line in your environment.rb: AUTHENTICATION_SCHEMES = ['database'] See environment.rb.tmpl for details git-svn-id: http://www.rousette.org.uk/svn/tracks-repos/trunk@337 a4c988fc-2ded-0310-b66e-134b36920a42 --- tracks/app/models/user.rb | 8 ++++++-- tracks/config/environment.rb.tmpl | 5 ++++- tracks/test/integration/ldap_auth_test.rb | 23 ++++++++++++++++++++++- 3 files changed, 32 insertions(+), 4 deletions(-) diff --git a/tracks/app/models/user.rb b/tracks/app/models/user.rb index 98c583ad..45f08a73 100644 --- a/tracks/app/models/user.rb +++ b/tracks/app/models/user.rb @@ -56,10 +56,14 @@ protected def crypt_password write_attribute("password", self.class.sha1(password)) if password == @password_confirmation end + + def password_required? + auth_type == 'database' + end validates_presence_of :login - validates_presence_of :password, :if => Proc.new{|user| user.auth_type == 'database'} - validates_length_of :password, :within => 5..40 + validates_presence_of :password, :if => :password_required? + validates_length_of :password, :within => 5..40, :if => :password_required? validates_confirmation_of :password validates_length_of :login, :within => 3..80 validates_uniqueness_of :login, :on => :create diff --git a/tracks/config/environment.rb.tmpl b/tracks/config/environment.rb.tmpl index 7d99bd99..ec658d9d 100644 --- a/tracks/config/environment.rb.tmpl +++ b/tracks/config/environment.rb.tmpl @@ -58,6 +58,10 @@ SALT = "change-me" # e.g. if you are in the Eastern time zone of the US, set the value below. # ENV['TZ'] = 'US/Eastern' +# Leave this alone or set it to one or more of ['database', 'ldap', 'open_id']. +# If you choose ldap, see the additional configuration options further down. +AUTHENTICATION_SCHEMES = ['database'] + require 'acts_as_namepart_finder' require 'acts_as_todo_container' require 'config' @@ -67,7 +71,6 @@ ActiveRecord::Base.class_eval do include Tracks::Acts::TodoContainer end -AUTHENTICATION_SCHEMES = ['database'] #one or more of ['database', 'ldap', 'open_id'] if (AUTHENTICATION_SCHEMES.include? 'ldap') require 'net/ldap' #requires ruby-net-ldap gem be installed require 'simple_ldap_authenticator' diff --git a/tracks/test/integration/ldap_auth_test.rb b/tracks/test/integration/ldap_auth_test.rb index 57363787..60697c68 100755 --- a/tracks/test/integration/ldap_auth_test.rb +++ b/tracks/test/integration/ldap_auth_test.rb @@ -2,6 +2,17 @@ require "#{File.dirname(__FILE__)}/../test_helper" require 'tempfile' require 'user' +module Tracks + class Config + def self.salt + "change-me" + end + def self.auth_schemes + ['database','ldap'] + end + end +end + class LdapAuthTest < Test::Unit::TestCase fixtures :users @@ -10,6 +21,15 @@ class LdapAuthTest < Test::Unit::TestCase SLAPD_SCHEMA_DIR = "/etc/openldap/schema/" #You may need to adjust this SLAPD_TEST_PORT = 10389 OUTPUT_DEBUG_INFO = false + + require 'net/ldap' #requires ruby-net-ldap gem be installed + require 'simple_ldap_authenticator' + SimpleLdapAuthenticator.ldap_library = 'net/ldap' + SimpleLdapAuthenticator.servers = %w'localhost' + SimpleLdapAuthenticator.use_ssl = false + SimpleLdapAuthenticator.login_format = 'cn=%s,dc=lukemelia,dc=com' + SimpleLdapAuthenticator.port = 10389 + SimpleLdapAuthenticator.logger = RAILS_DEFAULT_LOGGER def setup assert_equal "test", ENV['RAILS_ENV'] @@ -25,6 +45,7 @@ class LdapAuthTest < Test::Unit::TestCase def test_authenticate_against_ldap add_ldap_user_to_ldap_repository + assert SimpleLdapAuthenticator.valid?('john', 'deere') user = User.authenticate('john', 'deere') assert_not_nil(user) assert_equal user.login, 'john' @@ -41,7 +62,7 @@ class LdapAuthTest < Test::Unit::TestCase def start_ldap_server t = Thread.new(@slapd_conf.path) { |slapd_conf_path| puts "starting slapd..." if OUTPUT_DEBUG_INFO - run_cmd %Q{/usr/libexec/slapd -f #{slapd_conf_path} -h "ldap://127.0.0.1:10389/"} + run_cmd %Q{/usr/libexec/slapd -f #{slapd_conf_path} -h "ldap://127.0.0.1:10389/" -d0} } sleep(2) run_cmd %Q{ldapsearch -H "ldap://127.0.0.1:10389/" -x -b '' -s base '(objectclass=*)' namingContexts}