Send unwrapped entities

Remove the <XML><post>...</post></XML> wrapper.

Second step of #28
This commit is contained in:
Benjamin Neff 2017-04-27 23:47:33 +02:00
parent 52a8c89d4c
commit 221d87d7fe
No known key found for this signature in database
GPG key ID: 971464C3F1A90194
6 changed files with 27 additions and 68 deletions

View file

@ -90,7 +90,6 @@ module DiasporaFederation
# {http://www.rubydoc.info/gems/nokogiri/Nokogiri/XML/Element Nokogiri::XML::Element}s
#
# @see Nokogiri::XML::Node.to_xml
# @see XmlPayload#pack
#
# @return [Nokogiri::XML::Element] root element containing properties as child elements
def to_xml

View file

@ -136,7 +136,7 @@ module DiasporaFederation
# The payload data as string
# @return [String] 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}"
end
end

View file

@ -13,25 +13,6 @@ module DiasporaFederation
# (The +post+ element is there for historic reasons...)
# @deprecated
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
# XML and returns a new instance of the Entity that was packed inside the
# given payload.

View file

@ -112,6 +112,16 @@ XML
</comment>
</post>
</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
let(:legacy_new_signature_comment_xml_bob) { <<-XML }
<XML>
@ -185,7 +195,6 @@ XML
context "relaying on bobs pod" 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_private_key, parent.author).and_return(parent_key)
expect_callback(:fetch_related_entity, "Post", parent_guid).and_return(parent)
@ -194,19 +203,19 @@ XML
it "relays legacy signatures and xml" do
xml = Nokogiri::XML::Document.parse(legacy_comment_xml_alice).root
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
it "relays new signatures and xml" do
xml = Nokogiri::XML::Document.parse(new_signature_comment_xml_alice).root
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
it "relays new signatures with new data" do
xml = Nokogiri::XML::Document.parse(new_data_comment_xml_alice).root
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
@ -227,6 +236,14 @@ XML
expect(entity.text).to eq(text)
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
xml = Nokogiri::XML::Document.parse(legacy_new_signature_comment_xml_bob).root
entity = Salmon::XmlPayload.unpack(xml)

View file

@ -1,7 +1,6 @@
module DiasporaFederation
describe Salmon::XmlPayload do
let(:entity) { Entities::TestEntity.new(test: "asdf") }
let(:payload) { Salmon::XmlPayload.pack(entity) }
let(:entity_xml) { <<-XML.strip }
<XML>
<post>
@ -12,48 +11,11 @@ module DiasporaFederation
</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
context "sanity" do
it "expects an Nokogiri::XML::Element as param" do
expect {
Salmon::XmlPayload.unpack(payload)
Salmon::XmlPayload.unpack(Nokogiri::XML::Document.parse(entity_xml).root)
}.not_to raise_error
end

View file

@ -63,7 +63,7 @@ shared_examples "an XML Entity" do |ignored_props=[]|
context "parsing" 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)
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)
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
parent_author_signature = xml.at_xpath("post/*[1]/parent_author_signature").text
author_signature = xml.at_xpath("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(bob.public_key, parent_author_signature, signed_string)).to be_truthy