Skip to content

Commit 105fa47

Browse files
committed
Attributes method [] can raise NoMethodError #157
1 parent e1e5881 commit 105fa47

2 files changed

Lines changed: 60 additions & 4 deletions

File tree

lib/onelogin/ruby-saml/attributes.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ def include?(name)
4747

4848
# Return first value for an attribute
4949
def single(name)
50-
attributes[canonize_name(name)].first
50+
attributes[canonize_name(name)].first if include?(name)
5151
end
5252

5353
# Return all values for an attribute
@@ -106,4 +106,4 @@ def attributes
106106
end
107107
end
108108
end
109-
end
109+
end

test/response_test.rb

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -243,43 +243,99 @@ class RubySamlTest < Test::Unit::TestCase
243243
assert_equal "demo", response.attributes[:uid]
244244
end
245245

246+
should "extract single value as string in compatibility mode off" do
247+
response = OneLogin::RubySaml::Response.new(fixture(:response_with_multiple_attribute_values))
248+
OneLogin::RubySaml::Attributes.single_value_compatibility = false
249+
assert_equal ["demo"], response.attributes[:uid]
250+
# classes are not reloaded between tests so restore default
251+
OneLogin::RubySaml::Attributes.single_value_compatibility = true
252+
end
253+
246254
should "extract first of multiple values as string for b/w compatibility" do
247255
response = OneLogin::RubySaml::Response.new(fixture(:response_with_multiple_attribute_values))
248256
assert_equal 'value1', response.attributes[:another_value]
249257
end
250258

259+
should "extract first of multiple values as string for b/w compatibility in compatibility mode off" do
260+
response = OneLogin::RubySaml::Response.new(fixture(:response_with_multiple_attribute_values))
261+
OneLogin::RubySaml::Attributes.single_value_compatibility = false
262+
assert_equal ['value1', 'value2'], response.attributes[:another_value]
263+
OneLogin::RubySaml::Attributes.single_value_compatibility = true
264+
end
265+
251266
should "return array with all attributes when asked in XML order" do
252267
response = OneLogin::RubySaml::Response.new(fixture(:response_with_multiple_attribute_values))
253268
assert_equal ['value1', 'value2'], response.attributes.multi(:another_value)
254269
end
255270

271+
should "return array with all attributes when asked in XML order in compatibility mode off" do
272+
response = OneLogin::RubySaml::Response.new(fixture(:response_with_multiple_attribute_values))
273+
OneLogin::RubySaml::Attributes.single_value_compatibility = false
274+
assert_equal ['value1', 'value2'], response.attributes.multi(:another_value)
275+
OneLogin::RubySaml::Attributes.single_value_compatibility = true
276+
end
277+
256278
should "return first of multiple values when multiple Attribute tags in XML" do
257279
response = OneLogin::RubySaml::Response.new(fixture(:response_with_multiple_attribute_values))
258280
assert_equal 'role1', response.attributes[:role]
259281
end
260282

283+
should "return first of multiple values when multiple Attribute tags in XML in compatibility mode off" do
284+
response = OneLogin::RubySaml::Response.new(fixture(:response_with_multiple_attribute_values))
285+
OneLogin::RubySaml::Attributes.single_value_compatibility = false
286+
assert_equal ['role1', 'role2', 'role3'], response.attributes[:role]
287+
OneLogin::RubySaml::Attributes.single_value_compatibility = true
288+
end
289+
261290
should "return all of multiple values in reverse order when multiple Attribute tags in XML" do
262291
response = OneLogin::RubySaml::Response.new(fixture(:response_with_multiple_attribute_values))
263292
assert_equal ['role1', 'role2', 'role3'], response.attributes.multi(:role)
264293
end
265294

295+
should "return all of multiple values in reverse order when multiple Attribute tags in XML in compatibility mode off" do
296+
response = OneLogin::RubySaml::Response.new(fixture(:response_with_multiple_attribute_values))
297+
OneLogin::RubySaml::Attributes.single_value_compatibility = false
298+
assert_equal ['role1', 'role2', 'role3'], response.attributes.multi(:role)
299+
OneLogin::RubySaml::Attributes.single_value_compatibility = true
300+
end
301+
266302
should "return nil value correctly" do
267303
response = OneLogin::RubySaml::Response.new(fixture(:response_with_multiple_attribute_values))
268304
assert_nil response.attributes[:attribute_with_nil_value]
269305
end
270306

307+
should "return nil value correctly when not in compatibility mode off" do
308+
response = OneLogin::RubySaml::Response.new(fixture(:response_with_multiple_attribute_values))
309+
OneLogin::RubySaml::Attributes.single_value_compatibility = false
310+
assert_equal [nil], response.attributes[:attribute_with_nil_value]
311+
OneLogin::RubySaml::Attributes.single_value_compatibility = true
312+
end
313+
271314
should "return multiple values including nil and empty string" do
272315
response = OneLogin::RubySaml::Response.new(fixture(:response_with_multiple_attribute_values))
273316
assert_equal ["", "valuePresent", nil, nil], response.attributes.multi(:attribute_with_nils_and_empty_strings)
274317
end
275318

276-
should "return multiple values from [] when not in compatibility mode" do
319+
should "return multiple values from [] when not in compatibility mode off" do
277320
response = OneLogin::RubySaml::Response.new(fixture(:response_with_multiple_attribute_values))
278321
OneLogin::RubySaml::Attributes.single_value_compatibility = false
279322
assert_equal ["", "valuePresent", nil, nil], response.attributes[:attribute_with_nils_and_empty_strings]
280-
# classes are not reloaded between tests so restore default
281323
OneLogin::RubySaml::Attributes.single_value_compatibility = true
282324
end
325+
326+
should "check what happens when trying retrieve attribute that does not exists" do
327+
response = OneLogin::RubySaml::Response.new(fixture(:response_with_multiple_attribute_values))
328+
assert_equal nil, response.attributes[:attribute_not_exists]
329+
assert_equal nil, response.attributes.single(:attribute_not_exists)
330+
assert_equal nil, response.attributes.multi(:attribute_not_exists)
331+
332+
OneLogin::RubySaml::Attributes.single_value_compatibility = false
333+
assert_equal nil, response.attributes[:attribute_not_exists]
334+
assert_equal nil, response.attributes.single(:attribute_not_exists)
335+
assert_equal nil, response.attributes.multi(:attribute_not_exists)
336+
OneLogin::RubySaml::Attributes.single_value_compatibility = true
337+
end
338+
283339
end
284340
end
285341

0 commit comments

Comments
 (0)