require_relative '../spec_helper' module SAML2 describe Attribute do def serialize(attribute) doc = Nokogiri::XML::Builder.new do |builder| builder['saml'].Root('xmlns:saml' => Namespaces::SAML) do |root| attribute.build(root) root.parent.child['xmlns:saml'] = Namespaces::SAML end end.doc doc.root.child.to_s end let(:eduPersonPrincipalNameXML) { < user@domain XML } it "should auto-parse X500 attributes" do attr = Attribute.from_xml(Nokogiri::XML(eduPersonPrincipalNameXML).root) expect(attr).to be_instance_of Attribute::X500 expect(attr.value).to eq "user@domain" expect(attr.name).to eq Attribute::X500::EduPerson::PRINCIPAL_NAME expect(attr.friendly_name).to eq 'eduPersonPrincipalName' expect(attr.name_format).to eq Attribute::NameFormats::URI end it "should serialize an X500 attribute correctly" do attr = Attribute.create('eduPersonPrincipalName', 'user@domain') expect(attr).to be_instance_of Attribute::X500 expect(attr.value).to eq "user@domain" expect(attr.name).to eq Attribute::X500::EduPerson::PRINCIPAL_NAME expect(attr.friendly_name).to eq 'eduPersonPrincipalName' expect(attr.name_format).to eq Attribute::NameFormats::URI expect(serialize(attr)).to eq eduPersonPrincipalNameXML end it "should parse and serialize boolean values" do xml = < 1 XML stmt = AttributeStatement.from_xml(Nokogiri::XML(xml).root) expect(stmt.attributes.first.value).to eq true # serializes canonically expect(serialize(stmt)).to eq(xml.sub('>1<', '>true<')) end it "should parse and serialize dateTime values" do xml = < 2015-06-29T18:37:03Z XML stmt = AttributeStatement.from_xml(Nokogiri::XML(xml).root) expect(stmt.attributes.first.value).to eq Time.at(1435603023) # serializes canonically expect(serialize(stmt)).to eq xml end it "should parse values with different namespace prefixes" do xml = < 0 XML attr = Attribute.from_xml(Nokogiri::XML(xml).root) expect(attr.value).to eq false end it "should parse untagged values" do xml = < something XML attr = Attribute.from_xml(Nokogiri::XML(xml).root) expect(attr.value).to eq "something" end end describe AttributeStatement do describe "#to_h" do it "works" do attr_statement = Response.parse(fixture("response_with_attribute_signed.xml")).assertions.first.attribute_statements.first expect(attr_statement.to_h).to eq('givenName' => 'cody') expect(attr_statement.to_h(:name)).to eq("urn:oid:2.5.4.42" => 'cody') end it "infers friendly names if possible" do attr_statement = Response.parse(fixture("test3-response.xml")).assertions.first.attribute_statements.first expect(attr_statement.to_h).to eq({ 'eduPersonAffiliation' => 'member', 'eduPersonPrincipalName' => 'student@example.edu'}) end end end end