Send unwrapped entities
Remove the <XML><post>...</post></XML> wrapper. Second step of #28
This commit is contained in:
parent
52a8c89d4c
commit
221d87d7fe
6 changed files with 27 additions and 68 deletions
|
|
@ -90,7 +90,6 @@ module DiasporaFederation
|
||||||
# {http://www.rubydoc.info/gems/nokogiri/Nokogiri/XML/Element Nokogiri::XML::Element}s
|
# {http://www.rubydoc.info/gems/nokogiri/Nokogiri/XML/Element Nokogiri::XML::Element}s
|
||||||
#
|
#
|
||||||
# @see Nokogiri::XML::Node.to_xml
|
# @see Nokogiri::XML::Node.to_xml
|
||||||
# @see XmlPayload#pack
|
|
||||||
#
|
#
|
||||||
# @return [Nokogiri::XML::Element] root element containing properties as child elements
|
# @return [Nokogiri::XML::Element] root element containing properties as child elements
|
||||||
def to_xml
|
def to_xml
|
||||||
|
|
|
||||||
|
|
@ -136,7 +136,7 @@ module DiasporaFederation
|
||||||
# The payload data as string
|
# The payload data as string
|
||||||
# @return [String] payload data
|
# @return [String] payload data
|
||||||
def payload_data
|
def payload_data
|
||||||
@payload_data ||= XmlPayload.pack(@payload).to_xml.strip.tap do |data|
|
@payload_data ||= payload.to_xml.to_xml.strip.tap do |data|
|
||||||
logger.debug "send payload:\n#{data}"
|
logger.debug "send payload:\n#{data}"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -13,25 +13,6 @@ module DiasporaFederation
|
||||||
# (The +post+ element is there for historic reasons...)
|
# (The +post+ element is there for historic reasons...)
|
||||||
# @deprecated
|
# @deprecated
|
||||||
module XmlPayload
|
module XmlPayload
|
||||||
# Encapsulates an Entity inside the wrapping xml structure
|
|
||||||
# and returns the XML Object.
|
|
||||||
#
|
|
||||||
# @param [Entity] entity subject
|
|
||||||
# @return [Nokogiri::XML::Element] XML root node
|
|
||||||
# @raise [ArgumentError] if the argument is not an Entity subclass
|
|
||||||
def self.pack(entity)
|
|
||||||
raise ArgumentError, "only instances of DiasporaFederation::Entity allowed" unless entity.is_a?(Entity)
|
|
||||||
|
|
||||||
entity_xml = entity.to_xml
|
|
||||||
doc = entity_xml.document
|
|
||||||
wrap = Nokogiri::XML::Element.new("XML", doc)
|
|
||||||
wrap_post = Nokogiri::XML::Element.new("post", doc)
|
|
||||||
entity_xml.parent = wrap_post
|
|
||||||
wrap << wrap_post
|
|
||||||
|
|
||||||
wrap
|
|
||||||
end
|
|
||||||
|
|
||||||
# Extracts the Entity XML from the wrapping XML structure, parses the entity
|
# Extracts the Entity XML from the wrapping XML structure, parses the entity
|
||||||
# XML and returns a new instance of the Entity that was packed inside the
|
# XML and returns a new instance of the Entity that was packed inside the
|
||||||
# given payload.
|
# given payload.
|
||||||
|
|
|
||||||
|
|
@ -112,6 +112,16 @@ XML
|
||||||
</comment>
|
</comment>
|
||||||
</post>
|
</post>
|
||||||
</XML>
|
</XML>
|
||||||
|
XML
|
||||||
|
let(:legacy_signature_comment_xml_bob) { <<-XML }
|
||||||
|
<comment>
|
||||||
|
<guid>e21589b0b41101333b870f77ba60fa73</guid>
|
||||||
|
<parent_guid>9e269ae0b41201333b8c0f77ba60fa73</parent_guid>
|
||||||
|
<text>this is a very informative comment</text>
|
||||||
|
<author>alice@pod-a.org</author>
|
||||||
|
<author_signature>XU5X1uqTh8SY6JMG9uhEVR5Rg7FURV6lpRwl/HYOu6DJ3Hd9tpA2aSFFibUxxsMgJXKNrrc5SykrrEdTiQoEei+j0QqZf3B5R7r84qgK7M46KazwIpqRPwVl2MdA/0DdQyYJLA/oavNj1nwll9vtR87M7e/C94qG6P+iQTMBQzo=</author_signature>
|
||||||
|
<parent_author_signature>QqWSdwpb+/dcJUxuKKVe7aiz1NivXzlIdWZ71xyrxnhFxFYd+7EIittyTcp1cVehjg96pwDbn++P/rWyCffqenWu025DHvUfSmQkC93Z0dX6r3OIUlZqwEggtOdbunybiE++F3BVsGt5wC4YbAESB5ZFuhFVhBXh1X+EaZ/qoKo=</parent_author_signature>
|
||||||
|
</comment>
|
||||||
XML
|
XML
|
||||||
let(:legacy_new_signature_comment_xml_bob) { <<-XML }
|
let(:legacy_new_signature_comment_xml_bob) { <<-XML }
|
||||||
<XML>
|
<XML>
|
||||||
|
|
@ -185,7 +195,6 @@ XML
|
||||||
|
|
||||||
context "relaying on bobs pod" do
|
context "relaying on bobs pod" do
|
||||||
before do
|
before do
|
||||||
skip("TODO: expect new format as output")
|
|
||||||
expect_callback(:fetch_public_key, author).and_return(author_key.public_key)
|
expect_callback(:fetch_public_key, author).and_return(author_key.public_key)
|
||||||
expect_callback(:fetch_private_key, parent.author).and_return(parent_key)
|
expect_callback(:fetch_private_key, parent.author).and_return(parent_key)
|
||||||
expect_callback(:fetch_related_entity, "Post", parent_guid).and_return(parent)
|
expect_callback(:fetch_related_entity, "Post", parent_guid).and_return(parent)
|
||||||
|
|
@ -194,19 +203,19 @@ XML
|
||||||
it "relays legacy signatures and xml" do
|
it "relays legacy signatures and xml" do
|
||||||
xml = Nokogiri::XML::Document.parse(legacy_comment_xml_alice).root
|
xml = Nokogiri::XML::Document.parse(legacy_comment_xml_alice).root
|
||||||
entity = Salmon::XmlPayload.unpack(xml)
|
entity = Salmon::XmlPayload.unpack(xml)
|
||||||
expect(Salmon::XmlPayload.pack(entity).to_xml).to eq(legacy_comment_xml_bob.strip)
|
expect(entity.to_xml.to_xml).to eq(legacy_signature_comment_xml_bob.strip)
|
||||||
end
|
end
|
||||||
|
|
||||||
it "relays new signatures and xml" do
|
it "relays new signatures and xml" do
|
||||||
xml = Nokogiri::XML::Document.parse(new_signature_comment_xml_alice).root
|
xml = Nokogiri::XML::Document.parse(new_signature_comment_xml_alice).root
|
||||||
entity = Salmon::XmlPayload.unpack(xml)
|
entity = Salmon::XmlPayload.unpack(xml)
|
||||||
expect(Salmon::XmlPayload.pack(entity).to_xml).to eq(legacy_new_signature_comment_xml_bob.strip)
|
expect(entity.to_xml.to_xml).to eq(new_signature_comment_xml_bob.strip)
|
||||||
end
|
end
|
||||||
|
|
||||||
it "relays new signatures with new data" do
|
it "relays new signatures with new data" do
|
||||||
xml = Nokogiri::XML::Document.parse(new_data_comment_xml_alice).root
|
xml = Nokogiri::XML::Document.parse(new_data_comment_xml_alice).root
|
||||||
entity = Salmon::XmlPayload.unpack(xml)
|
entity = Salmon::XmlPayload.unpack(xml)
|
||||||
expect(Salmon::XmlPayload.pack(entity).to_xml).to eq(legacy_new_data_comment_xml_bob.strip)
|
expect(entity.to_xml.to_xml).to eq(new_data_comment_xml_bob.strip)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
@ -227,6 +236,14 @@ XML
|
||||||
expect(entity.text).to eq(text)
|
expect(entity.text).to eq(text)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "parses legacy signatures and with new xml" do
|
||||||
|
xml = Nokogiri::XML::Document.parse(legacy_signature_comment_xml_bob).root
|
||||||
|
entity = Salmon::XmlPayload.unpack(xml)
|
||||||
|
|
||||||
|
expect(entity.author).to eq(author)
|
||||||
|
expect(entity.text).to eq(text)
|
||||||
|
end
|
||||||
|
|
||||||
it "parses new signatures with legacy xml" do
|
it "parses new signatures with legacy xml" do
|
||||||
xml = Nokogiri::XML::Document.parse(legacy_new_signature_comment_xml_bob).root
|
xml = Nokogiri::XML::Document.parse(legacy_new_signature_comment_xml_bob).root
|
||||||
entity = Salmon::XmlPayload.unpack(xml)
|
entity = Salmon::XmlPayload.unpack(xml)
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,6 @@
|
||||||
module DiasporaFederation
|
module DiasporaFederation
|
||||||
describe Salmon::XmlPayload do
|
describe Salmon::XmlPayload do
|
||||||
let(:entity) { Entities::TestEntity.new(test: "asdf") }
|
let(:entity) { Entities::TestEntity.new(test: "asdf") }
|
||||||
let(:payload) { Salmon::XmlPayload.pack(entity) }
|
|
||||||
let(:entity_xml) { <<-XML.strip }
|
let(:entity_xml) { <<-XML.strip }
|
||||||
<XML>
|
<XML>
|
||||||
<post>
|
<post>
|
||||||
|
|
@ -12,48 +11,11 @@ module DiasporaFederation
|
||||||
</XML>
|
</XML>
|
||||||
XML
|
XML
|
||||||
|
|
||||||
describe ".pack" do
|
|
||||||
it "expects an Entity as param" do
|
|
||||||
expect {
|
|
||||||
Salmon::XmlPayload.pack(entity)
|
|
||||||
}.not_to raise_error
|
|
||||||
end
|
|
||||||
|
|
||||||
it "raises an error when the param is not an Entity" do
|
|
||||||
["asdf", 1234, true, :test, payload].each do |val|
|
|
||||||
expect {
|
|
||||||
Salmon::XmlPayload.pack(val)
|
|
||||||
}.to raise_error ArgumentError
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
context "returned xml" do
|
|
||||||
subject { Salmon::XmlPayload.pack(entity) }
|
|
||||||
|
|
||||||
it "returns an xml wrapper" do
|
|
||||||
expect(subject).to be_an_instance_of Nokogiri::XML::Element
|
|
||||||
expect(subject.name).to eq("XML")
|
|
||||||
expect(subject.children).to have(1).item
|
|
||||||
expect(subject.children[0].name).to eq("post")
|
|
||||||
expect(subject.children[0].children).to have(1).item
|
|
||||||
end
|
|
||||||
|
|
||||||
it "returns the entity xml inside the wrapper" do
|
|
||||||
expect(subject.children[0].children[0].name).to eq("test_entity")
|
|
||||||
expect(subject.children[0].children[0].children).to have(1).item
|
|
||||||
end
|
|
||||||
|
|
||||||
it "produces the expected XML" do
|
|
||||||
expect(subject.to_xml).to eq(entity_xml)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
describe ".unpack" do
|
describe ".unpack" do
|
||||||
context "sanity" do
|
context "sanity" do
|
||||||
it "expects an Nokogiri::XML::Element as param" do
|
it "expects an Nokogiri::XML::Element as param" do
|
||||||
expect {
|
expect {
|
||||||
Salmon::XmlPayload.unpack(payload)
|
Salmon::XmlPayload.unpack(Nokogiri::XML::Document.parse(entity_xml).root)
|
||||||
}.not_to raise_error
|
}.not_to raise_error
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -63,7 +63,7 @@ shared_examples "an XML Entity" do |ignored_props=[]|
|
||||||
|
|
||||||
context "parsing" do
|
context "parsing" do
|
||||||
it "reads its own output" do
|
it "reads its own output" do
|
||||||
packed_xml = DiasporaFederation::Salmon::XmlPayload.pack(instance)
|
packed_xml = instance.to_xml
|
||||||
parsed_instance = DiasporaFederation::Salmon::XmlPayload.unpack(packed_xml)
|
parsed_instance = DiasporaFederation::Salmon::XmlPayload.unpack(packed_xml)
|
||||||
|
|
||||||
check_entity(instance, parsed_instance, ignored_props)
|
check_entity(instance, parsed_instance, ignored_props)
|
||||||
|
|
@ -109,10 +109,10 @@ shared_examples "a relayable Entity" do
|
||||||
order = described_class.class_props.keys - %i(author_signature parent_author_signature parent created_at)
|
order = described_class.class_props.keys - %i(author_signature parent_author_signature parent created_at)
|
||||||
signed_string = order.map {|name| data[name] }.join(";")
|
signed_string = order.map {|name| data[name] }.join(";")
|
||||||
|
|
||||||
xml = DiasporaFederation::Salmon::XmlPayload.pack(instance)
|
xml = instance.to_xml
|
||||||
|
|
||||||
author_signature = xml.at_xpath("post/*[1]/author_signature").text
|
author_signature = xml.at_xpath("author_signature").text
|
||||||
parent_author_signature = xml.at_xpath("post/*[1]/parent_author_signature").text
|
parent_author_signature = xml.at_xpath("parent_author_signature").text
|
||||||
|
|
||||||
expect(verify_signature(alice.public_key, author_signature, signed_string)).to be_truthy
|
expect(verify_signature(alice.public_key, author_signature, signed_string)).to be_truthy
|
||||||
expect(verify_signature(bob.public_key, parent_author_signature, signed_string)).to be_truthy
|
expect(verify_signature(bob.public_key, parent_author_signature, signed_string)).to be_truthy
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue