Commit eed4a73

mokha <mo@mokhan.ca>
2019-01-21 17:59:08
create a separate fixture for key extraction
1 parent 9c1f10c
Changed files (4)
spec/fixtures/xsd/item-extracted-key.xsd
@@ -0,0 +1,30 @@
+<?xml version="1.0" encoding="utf-8"?>
+<xsd:schema
+  xmlns:ds="http://www.w3.org/2000/09/xmldsig#"
+  xmlns:xenc="http://www.w3.org/2001/04/xmlenc#"
+  xmlns:xsd="http://www.w3.org/2001/XMLSchema"
+  xmlns="https://www.example.org/item#"
+  targetNamespace="https://www.example.org/item#"
+  elementFormDefault="qualified"
+  version="1.0">
+  <xsd:import namespace="http://www.w3.org/2000/09/xmldsig#" schemaLocation="xmldsig-core-schema.xsd"/>
+  <xsd:import namespace="http://www.w3.org/2001/04/xmlenc#" schemaLocation="xenc-schema.xsd"/>
+
+  <xsd:element name="Encrypted">
+    <xsd:complexType>
+      <xsd:sequence>
+        <xsd:element ref="xenc:EncryptedData"/>
+      </xsd:sequence>
+    </xsd:complexType>
+  </xsd:element>
+
+  <xsd:element name="Item">
+    <xsd:complexType>
+      <xsd:sequence>
+        <xsd:element ref="ds:Signature" />
+        <xsd:element ref="Encrypted"/>
+      </xsd:sequence>
+      <xsd:attribute name="ID" type="xsd:string" use="optional"/>
+    </xsd:complexType>
+  </xsd:element>
+</xsd:schema>
spec/fixtures/item-extracted-key.builder
@@ -0,0 +1,11 @@
+xml.instruct!
+xml.Item ID: id, xmlns: 'https://www.example.org/item#' do
+  signature_for reference_id: id, xml: xml
+  xml.Encrypted xmlns: 'https://www.example.org/item#' do
+    encryption_for xml: xml do |xml|
+      xml.EncryptMe do
+        xml.Secret "secret"
+      end
+    end
+  end
+end
spec/support/matchers/match_xsd.rb
@@ -1,13 +1,17 @@
 require 'rspec/expectations'
 
-RSpec::Matchers.define :match_xsd do
+RSpec::Matchers.define :match_xsd do |expected|
   match do |actual|
-    file = File.expand_path(File.join(__dir__, "../../fixtures/xsd/item.xsd"))
+    file = File.expand_path(File.join(__dir__, "../../fixtures/xsd/#{expected}.xsd"))
     xml = Nokogiri::XML(actual).document
     Dir.chdir(File.dirname(file)) do
       xsd = Nokogiri::XML::Schema(IO.read(file))
-      expect(xsd.validate(xml)).to be_empty
+      errors = xsd.validate(xml)
+      @actual = errors
+      expect(errors).to be_empty
     end
   end
+
+  diffable
 end
 
spec/xml/kit/templatable_spec.rb
@@ -94,6 +94,14 @@ RSpec.describe ::Xml::Kit::Templatable do
       end
     end
 
-    specify { expect(subject.to_xml).to match_xsd }
+    specify { expect(subject.to_xml).to match_xsd('item') }
+
+    context "with the key extracted to the header" do
+      before do
+        subject.template_path = './spec/fixtures/item-extracted-key.builder'
+      end
+
+      specify { expect(subject.to_xml).to match_xsd('item-extracted-key') }
+    end
   end
 end