Permalink
Browse files

more xml parsing

  • Loading branch information...
1 parent 2148b83 commit e05f0e51565a0f4ad7b76a2590ef23342a6d862e @aeden committed Aug 5, 2008
Showing with 50 additions and 3 deletions.
  1. +13 −3 lib/rsaml/assertion.rb
  2. +6 −0 lib/rsaml/identifier/issuer.rb
  3. +31 −0 test/assertion_test.rb
View
16 lib/rsaml/assertion.rb
@@ -95,9 +95,8 @@ class Assertion
# The subject of the statement(s) in the assertion.
attr_accessor :subject
- # Conditions that MUST be evaluated when assessing the validity of and/or when using the assertion.
- #
- # conditions should contain a single Conditions object
+ # Conditions that MUST be evaluated when assessing the validity of and/or when using the assertion.
+ # Note: conditions should contain a single Conditions instance, not an array of Condition instances.
attr_accessor :conditions
# Construct a new assertion from the given issuer
@@ -170,6 +169,17 @@ def to_xml(xml=Builder::XmlMarkup.new)
}
end
+ # Construct an Action instance from the given XML Element or fragment.
+ def self.from_xml(element)
+ element = REXML::Document.new(element).root if element.is_a?(String)
+ issuer = Identifier::Issuer.from_xml(element.get_elements('saml:Issuer').first)
+ assertion = Assertion.new(issuer)
+ if (subject = element.get_elements('saml:Subject').first)
+ assertion.subject = Subject.from_xml(subject)
+ end
+ assertion
+ end
+
protected
def assertion_cache
@assertion_cache ||= []
View
6 lib/rsaml/identifier/issuer.rb
@@ -17,6 +17,12 @@ def to_xml(xml=Builder::XmlMarkup.new)
attributes['SPProvidedID'] = sp_provided_id unless sp_provided_id.nil?
xml.tag!('saml:Issuer', value, attributes)
end
+
+ # Construct an Issuer instance from the given XML Element or fragment.
+ def self.from_xml(element)
+ element = REXML::Document.new(element).root if element.is_a?(String)
+ Issuer.new(element.text)
+ end
end
end
end
View
31 test/assertion_test.rb
@@ -102,6 +102,37 @@ class AssertionTest < Test::Unit::TestCase
assert_match(/<saml:AuthnStatement AuthnInstant="#{date_match}"/, xml)
end
end
+
+ context "when consuming xml" do
+ should "return a valid Assertion instance" do
+ xml_fragment = %Q(
+ <saml:Assertion>
+ <saml:Issuer>Example</saml:Issuer>
+ <saml:Subject>Anthony</saml:Subject>
+ </saml:Assertion>
+ )
+ assertion = Assertion.from_xml(xml_fragment)
+ assert_not_nil assertion
+ assert_not_nil assertion.issuer
+ assert_equal 'Example', assertion.issuer.value
+ assert_nothing_raised do
+ assertion.validate
+ end
+ end
+ context "where there is no saml:Subject element" do
+ should "raise a validation error" do
+ xml_fragment = %Q(
+ <saml:Assertion>
+ <saml:Issuer>Example</saml:Issuer>
+ </saml:Assertion>
+ )
+ assertion = Assertion.from_xml(xml_fragment)
+ assert_raise ValidationError do
+ assertion.validate
+ end
+ end
+ end
+ end
end
context "an assertion URI ref" do
setup do

0 comments on commit e05f0e5

Please sign in to comment.