Commit 8dfded9fc2839934fb1a43de88c99e52e7594448
Committed by
Rodrigo Souto
1 parent
378e98ed
Exists in
master
and in
21 other branches
Plugin LDAP: more data testing
Showing
2 changed files
with
41 additions
and
3 deletions
Show diff stats
plugins/ldap/lib/ldap_authentication.rb
@@ -136,8 +136,16 @@ class LdapAuthentication | @@ -136,8 +136,16 @@ class LdapAuthentication | ||
136 | def self.get_attr(entry, attr_name) | 136 | def self.get_attr(entry, attr_name) |
137 | if !attr_name.blank? | 137 | if !attr_name.blank? |
138 | val = entry[attr_name].is_a?(Array) ? entry[attr_name].first : entry[attr_name] | 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 | end | 149 | end |
142 | end | 150 | end |
143 | 151 |
plugins/ldap/test/unit/ldap_authentication_test.rb
@@ -3,6 +3,12 @@ require File.dirname(__FILE__) + '/../test_helper' | @@ -3,6 +3,12 @@ require File.dirname(__FILE__) + '/../test_helper' | ||
3 | 3 | ||
4 | class LdapAuthenticationTest < ActiveSupport::TestCase | 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 | def setup | 12 | def setup |
7 | @ldap_config = load_ldap_config | 13 | @ldap_config = load_ldap_config |
8 | end | 14 | end |
@@ -139,11 +145,35 @@ class LdapAuthenticationTest < ActiveSupport::TestCase | @@ -139,11 +145,35 @@ class LdapAuthenticationTest < ActiveSupport::TestCase | ||
139 | end | 145 | end |
140 | 146 | ||
141 | should 'detect and convert non utf-8 charset from ldap' do | 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 | name = LdapAuthentication.get_attr entry, 'name' | 149 | name = LdapAuthentication.get_attr entry, 'name' |
144 | assert_equal name, 'José João' | 150 | assert_equal name, 'José João' |
145 | end | 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 | if ldap_configured? | 177 | if ldap_configured? |
148 | should 'return the user attributes' do | 178 | should 'return the user attributes' do |
149 | auth = LdapAuthentication.new(@ldap_config['server']) | 179 | auth = LdapAuthentication.new(@ldap_config['server']) |