Commit 8dfded9fc2839934fb1a43de88c99e52e7594448

Authored by Aurélio A. Heckert
Committed by Rodrigo Souto
1 parent 378e98ed

Plugin LDAP: more data testing

plugins/ldap/lib/ldap_authentication.rb
... ... @@ -136,8 +136,16 @@ class LdapAuthentication
136 136 def self.get_attr(entry, attr_name)
137 137 if !attr_name.blank?
138 138 val = entry[attr_name].is_a?(Array) ? entry[attr_name].first : entry[attr_name]
139   - charset = Magic.guess_string_mime_encoding(val)
140   - val.encode 'utf-8', charset, invalid: :replace, undef: :replace
  139 + if val.nil?
  140 + Rails.logger.warn "LDAP entry #{entry.dn} has no attr #{attr_name}."
  141 + nil
  142 + elsif val == '' || val == ' '
  143 + Rails.logger.warn "LDAP entry #{entry.dn} has attr #{attr_name} empty."
  144 + ''
  145 + else
  146 + charset = Magic.guess_string_mime_encoding(val)
  147 + val.encode 'utf-8', charset, invalid: :replace, undef: :replace
  148 + end
141 149 end
142 150 end
143 151  
... ...
plugins/ldap/test/unit/ldap_authentication_test.rb
... ... @@ -3,6 +3,12 @@ require File.dirname(__FILE__) + '/../test_helper'
3 3  
4 4 class LdapAuthenticationTest < ActiveSupport::TestCase
5 5  
  6 + def pseudoEntry(data)
  7 + entry = data.clone
  8 + def entry.dn; 'testDN'; end
  9 + entry
  10 + end
  11 +
6 12 def setup
7 13 @ldap_config = load_ldap_config
8 14 end
... ... @@ -139,11 +145,35 @@ class LdapAuthenticationTest &lt; ActiveSupport::TestCase
139 145 end
140 146  
141 147 should 'detect and convert non utf-8 charset from ldap' do
142   - entry = { 'name' => "Jos\xE9 Jo\xE3o" }
  148 + entry = pseudoEntry('name' => "Jos\xE9 Jo\xE3o")
143 149 name = LdapAuthentication.get_attr entry, 'name'
144 150 assert_equal name, 'José João'
145 151 end
146 152  
  153 + should 'dont crash when entry key is empty string' do
  154 + entry = pseudoEntry('name' => "")
  155 + name = LdapAuthentication.get_attr entry, 'name'
  156 + assert_equal name, ''
  157 + end
  158 +
  159 + should 'dont crash when entry key has only a space char' do
  160 + entry = pseudoEntry('name' => " ")
  161 + name = LdapAuthentication.get_attr entry, 'name'
  162 + assert_equal name, ''
  163 + end
  164 +
  165 + should 'dont crash when entry key is nil' do
  166 + entry = pseudoEntry('name' => nil)
  167 + name = LdapAuthentication.get_attr entry, 'name'
  168 + assert_equal name, nil
  169 + end
  170 +
  171 + should 'dont crash when entry key does not exists' do
  172 + entry = pseudoEntry({})
  173 + name = LdapAuthentication.get_attr entry, 'name'
  174 + assert_equal name, nil
  175 + end
  176 +
147 177 if ldap_configured?
148 178 should 'return the user attributes' do
149 179 auth = LdapAuthentication.new(@ldap_config['server'])
... ...