Skip to content

Commit

Permalink
more xml parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
aeden committed Aug 5, 2008
1 parent 2148b83 commit e05f0e5
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 3 deletions.
16 changes: 13 additions & 3 deletions lib/rsaml/assertion.rb
Expand Up @@ -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
Expand Down Expand Up @@ -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 ||= []
Expand Down
6 changes: 6 additions & 0 deletions lib/rsaml/identifier/issuer.rb
Expand Up @@ -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
31 changes: 31 additions & 0 deletions test/assertion_test.rb
Expand Up @@ -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
Expand Down

0 comments on commit e05f0e5

Please sign in to comment.