Commit 18eaf81

mo <mo.khan@gmail.com>
2017-12-24 04:39:20
add error handing for invalid docs.
1 parent a447676
lib/saml/kit/document.rb
@@ -81,6 +81,8 @@ module Saml
             AuthenticationRequest.new(xml, configuration: configuration)
           elsif hash['LogoutRequest'].present?
             LogoutRequest.new(xml, configuration: configuration)
+          else
+            InvalidDocument.new(xml)
           end
         rescue => error
           Saml::Kit.logger.error(error)
lib/saml/kit/invalid_document.rb
@@ -9,6 +9,12 @@ module Saml
       def initialize(xml)
         super(xml, name: "InvalidDocument")
       end
+
+      def to_h
+        super
+      rescue
+        {}
+      end
     end
   end
 end
spec/saml/document_spec.rb
@@ -0,0 +1,47 @@
+RSpec.describe Saml::Kit::Document do
+  describe ".to_saml_document" do
+    subject { described_class }
+    let(:user) { double(:user, name_id_for: SecureRandom.uuid, assertion_attributes_for: { id: SecureRandom.uuid }) }
+    let(:request) { instance_double(Saml::Kit::AuthenticationRequest, id: Saml::Kit::Id.generate, issuer: FFaker::Internet.http_url, assertion_consumer_service_url: FFaker::Internet.http_url, name_id_format: Saml::Kit::Namespaces::PERSISTENT, provider: nil, signed?: true, trusted?: true) }
+
+    it 'returns a Response' do
+      xml = Saml::Kit::Response.build_xml(user, request)
+      result = subject.to_saml_document(xml)
+      expect(result).to be_instance_of(Saml::Kit::Response)
+    end
+
+    it 'returns a LogoutResponse' do
+      xml = Saml::Kit::LogoutResponse.build_xml(request)
+      result = subject.to_saml_document(xml)
+      expect(result).to be_instance_of(Saml::Kit::LogoutResponse)
+    end
+
+    it 'returns an AuthenticationRequest' do
+      xml = Saml::Kit::AuthenticationRequest.build_xml
+      result = subject.to_saml_document(xml)
+      expect(result).to be_instance_of(Saml::Kit::AuthenticationRequest)
+    end
+
+    it 'returns a LogoutRequest' do
+      xml = Saml::Kit::LogoutRequest.build_xml(user)
+      result = subject.to_saml_document(xml)
+      expect(result).to be_instance_of(Saml::Kit::LogoutRequest)
+    end
+
+    it 'returns an invalid document' do
+      xml = <<-XML
+      <html>
+        <head></head>
+        <body></body>
+      </html>
+      XML
+      result = subject.to_saml_document(xml)
+      expect(result).to be_instance_of(Saml::Kit::InvalidDocument)
+    end
+
+    it 'returns an invalid document when the xml is not XML' do
+      result = subject.to_saml_document("NOT XML")
+      expect(result).to be_instance_of(Saml::Kit::InvalidDocument)
+    end
+  end
+end
spec/saml/invalid_document_spec.rb
@@ -1,8 +1,12 @@
 RSpec.describe Saml::Kit::InvalidDocument do
-  subject { described_class.new(xml) }
-  let(:xml) { "<xml></xml>" }
-
   it 'is invalid' do
+    subject = described_class.new("<xml></xml>")
+    expect(subject).to be_invalid
+    expect(subject.errors[:base]).to be_present
+  end
+
+  it 'is invalid with something that not xml' do
+    subject = described_class.new("NOT XML")
     expect(subject).to be_invalid
     expect(subject.errors[:base]).to be_present
   end