Remove legacy xml unwrap code

Closes #28
This commit is contained in:
Benjamin Neff 2019-10-29 02:54:07 +01:00
parent 0163963849
commit 0ccd15cd53
No known key found for this signature in database
GPG key ID: 971464C3F1A90194
12 changed files with 21 additions and 206 deletions

View file

@ -13,6 +13,5 @@ require "base64"
require "diaspora_federation/salmon/aes"
require "diaspora_federation/salmon/exceptions"
require "diaspora_federation/salmon/xml_payload"
require "diaspora_federation/salmon/magic_envelope"
require "diaspora_federation/salmon/encrypted_magic_envelope"

View file

@ -114,7 +114,8 @@ module DiasporaFederation
logger.debug "unenvelop message from #{sender}:\n#{data}"
new(XmlPayload.unpack(Nokogiri::XML(data).root), sender)
xml = Nokogiri::XML(data).root
new(Entity.entity_class(xml.name).from_xml(xml), sender)
end
private

View file

@ -1,43 +0,0 @@
# frozen_string_literal: true
module DiasporaFederation
module Salmon
# +XmlPayload+ provides methods to wrap a XML-serialized {Entity} inside a
# common XML structure that will become the payload for federation messages.
#
# The wrapper looks like so:
# <XML>
# <post>
# {data}
# </post>
# </XML>
#
# (The +post+ element is there for historic reasons...)
# @deprecated
module XmlPayload
# 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.
#
# @param [Nokogiri::XML::Element] xml payload XML root node
# @return [Entity] re-constructed Entity instance
# @raise [ArgumentError] if the argument is not an
# {http://www.rubydoc.info/gems/nokogiri/Nokogiri/XML/Element Nokogiri::XML::Element}
# @raise [UnknownEntity] if the class for the entity contained inside the
# XML can't be found
def self.unpack(xml)
raise ArgumentError, "only Nokogiri::XML::Element allowed" unless xml.instance_of?(Nokogiri::XML::Element)
data = xml_wrapped?(xml) ? xml.at_xpath("post/*[1]") : xml
Entity.entity_class(data.name).from_xml(data)
end
# @param [Nokogiri::XML::Element] element
private_class_method def self.xml_wrapped?(element)
(element.name == "XML" && !element.at_xpath("post").nil? &&
!element.at_xpath("post").children.empty?)
end
end
end
end

View file

@ -65,20 +65,6 @@ module DiasporaFederation
)
}
let(:legacy_format_comment_xml_alice) { <<~XML }
<XML>
<post>
<comment>
<guid>e21589b0b41101333b870f77ba60fa73</guid>
<parent_guid>9e269ae0b41201333b8c0f77ba60fa73</parent_guid>
<author_signature>XU5X1uqTh8SY6JMG9uhEVR5Rg7FURV6lpRwl/HYOu6DJ3Hd9tpA2aSFFibUxxsMgJXKNrrc5SykrrEdTiQoEei+j0QqZf3B5R7r84qgK7M46KazwIpqRPwVl2MdA/0DdQyYJLA/oavNj1nwll9vtR87M7e/C94qG6P+iQTMBQzo=</author_signature>
<parent_author_signature/>
<text>this is a very informative comment</text>
<diaspora_handle>alice@pod-a.org</diaspora_handle>
</comment>
</post>
</XML>
XML
let(:new_format_comment_xml_alice) { <<~XML }
<comment>
<author>alice@pod-a.org</author>
@ -101,20 +87,6 @@ module DiasporaFederation
</comment>
XML
let(:legacy_format_comment_xml_bob) { <<~XML }
<XML>
<post>
<comment>
<guid>e21589b0b41101333b870f77ba60fa73</guid>
<parent_guid>9e269ae0b41201333b8c0f77ba60fa73</parent_guid>
<text>this is a very informative comment</text>
<diaspora_handle>alice@pod-a.org</diaspora_handle>
<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>
</post>
</XML>
XML
let(:legacy_order_new_format_comment_xml_bob) { <<~XML }
<comment>
<guid>e21589b0b41101333b870f77ba60fa73</guid>
@ -125,20 +97,6 @@ module DiasporaFederation
<parent_author_signature>QqWSdwpb+/dcJUxuKKVe7aiz1NivXzlIdWZ71xyrxnhFxFYd+7EIittyTcp1cVehjg96pwDbn++P/rWyCffqenWu025DHvUfSmQkC93Z0dX6r3OIUlZqwEggtOdbunybiE++F3BVsGt5wC4YbAESB5ZFuhFVhBXh1X+EaZ/qoKo=</parent_author_signature>
</comment>
XML
let(:new_order_legacy_format_comment_xml_bob) { <<~XML }
<XML>
<post>
<comment>
<diaspora_handle>alice@pod-a.org</diaspora_handle>
<guid>e21589b0b41101333b870f77ba60fa73</guid>
<parent_guid>9e269ae0b41201333b8c0f77ba60fa73</parent_guid>
<text>this is a very informative comment</text>
<author_signature>SQbLeqsEpFmSl74G1fFJXKQcsq6jp5B2ZjmfEOF/LbBccYP2oZEyEqOq18K3Fa71OYTp6Nddb38hCmHWWHvnGUltGfxKBnQ0WHafJUi40VM4VmeRoU8cac6m+1hslwe5SNmK6oh47EV3mRCXlgGGjLIrw7iEwjKL2g9x1gkNp2s=</author_signature>
<parent_author_signature>hWsagsczmZD6d36d6MFdTt3hKAdnRtupSIU6464G2kkMJ+WlExxMgbF6kWR+jVCBTeKipWCYK3Arnj0YkuIZM9d14bJGVMTsW/ZzNfJ69bXZhsyawI8dPnZnLVydo+hU/XmGJBEuh2TOj9Emq6/HCYiWzPTF5qhYAtyJ1oxJ4Yk=</parent_author_signature>
</comment>
</post>
</XML>
XML
let(:new_format_comment_xml_bob) { <<~XML }
<comment>
<author>alice@pod-a.org</author>
@ -149,21 +107,6 @@ module DiasporaFederation
<parent_author_signature>hWsagsczmZD6d36d6MFdTt3hKAdnRtupSIU6464G2kkMJ+WlExxMgbF6kWR+jVCBTeKipWCYK3Arnj0YkuIZM9d14bJGVMTsW/ZzNfJ69bXZhsyawI8dPnZnLVydo+hU/XmGJBEuh2TOj9Emq6/HCYiWzPTF5qhYAtyJ1oxJ4Yk=</parent_author_signature>
</comment>
XML
let(:legacy_format_new_data_comment_xml_bob) { <<~XML }
<XML>
<post>
<comment>
<diaspora_handle>alice@pod-a.org</diaspora_handle>
<guid>e21589b0b41101333b870f77ba60fa73</guid>
<parent_guid>9e269ae0b41201333b8c0f77ba60fa73</parent_guid>
<text>this is a very informative comment</text>
<new_data>foobar</new_data>
<author_signature>SFYXSvCX/DhTFiOUALp2Nf1kfNkGKXrnoBPikAyhaIogGydVBm+8tIlu1U/vsnpyKO3yfC3JReJ00/UBd4J16VO1IxStntq8NUqbSv4me5A/6kdK9Xg6eYbXrqQGm8fUQ5Xuh2UzeB71p7SVySXX3OZHVe0dvHCxH/lsfSDpEjc=</author_signature>
<parent_author_signature>NxXuEUVeXwUMR77osIbaNlp2oB3bpl8rBEFgQoO6cnoN5ewDbiGADK0x6EhcmJptjwhGVcZiNJNpq7k3/pjJtKaH++3ToCAtcuZoIKwPDsneLnjPhVjE2GXM1TiZKwoHrq41qSp/8Vl5UPbtC6sPiOzIvPKaILXUG8XCiVWuB0M=</parent_author_signature>
</comment>
</post>
</XML>
XML
let(:new_data_comment_xml_bob) { <<~XML }
<comment>
<author>alice@pod-a.org</author>
@ -191,7 +134,7 @@ module DiasporaFederation
expect_callback(:fetch_related_entity, "Post", parent_guid).and_return(parent)
xml = Nokogiri::XML(new_data_comment_xml_alice).root
Salmon::XmlPayload.unpack(xml).to_xml
Entity.entity_class(xml.name).from_xml(xml).to_xml
end
end
@ -202,21 +145,15 @@ module DiasporaFederation
expect_callback(:fetch_related_entity, "Post", parent_guid).and_return(parent)
end
it "relays legacy order" do
xml = Nokogiri::XML(legacy_format_comment_xml_alice).root
entity = Salmon::XmlPayload.unpack(xml)
expect(entity.to_xml.to_xml).to eq(legacy_order_new_format_comment_xml_bob.strip)
end
it "relays new order" do
xml = Nokogiri::XML(new_format_comment_xml_alice).root
entity = Salmon::XmlPayload.unpack(xml)
entity = Entity.entity_class(xml.name).from_xml(xml)
expect(entity.to_xml.to_xml).to eq(new_format_comment_xml_bob.strip)
end
it "relays new data" do
xml = Nokogiri::XML(new_data_comment_xml_alice).root
entity = Salmon::XmlPayload.unpack(xml)
entity = Entity.entity_class(xml.name).from_xml(xml)
expect(entity.to_xml.to_xml).to eq(new_data_comment_xml_bob.strip)
end
end
@ -229,25 +166,9 @@ module DiasporaFederation
expect_callback(:fetch_related_entity, "Post", parent_guid).and_return(parent)
end
it "parses legacy format" do
xml = Nokogiri::XML(legacy_format_comment_xml_bob).root
entity = Salmon::XmlPayload.unpack(xml)
expect(entity.author).to eq(author)
expect(entity.text).to eq(text)
end
it "parses legacy order with new xml format" do
xml = Nokogiri::XML(legacy_order_new_format_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 order with legacy xml format" do
xml = Nokogiri::XML(new_order_legacy_format_comment_xml_bob).root
entity = Salmon::XmlPayload.unpack(xml)
entity = Entity.entity_class(xml.name).from_xml(xml)
expect(entity.author).to eq(author)
expect(entity.text).to eq(text)
@ -255,24 +176,15 @@ module DiasporaFederation
it "parses new xml format" do
xml = Nokogiri::XML(new_format_comment_xml_bob).root
entity = Salmon::XmlPayload.unpack(xml)
entity = Entity.entity_class(xml.name).from_xml(xml)
expect(entity.author).to eq(author)
expect(entity.text).to eq(text)
end
it "parses new data with legacy xml format" do
xml = Nokogiri::XML(legacy_format_new_data_comment_xml_bob).root
entity = Salmon::XmlPayload.unpack(xml)
expect(entity.author).to eq(author)
expect(entity.text).to eq(text)
expect(entity.additional_data["new_data"]).to eq(new_data)
end
it "parses new data with new xml format" do
xml = Nokogiri::XML(new_data_comment_xml_bob).root
entity = Salmon::XmlPayload.unpack(xml)
entity = Entity.entity_class(xml.name).from_xml(xml)
expect(entity.author).to eq(author)
expect(entity.text).to eq(text)

View file

@ -215,7 +215,8 @@ module DiasporaFederation
it "fails validation on parsing" do
expect {
DiasporaFederation::Salmon::XmlPayload.unpack(Nokogiri::XML(xml).root)
parsed_xml = Nokogiri::XML(xml).root
Entity.entity_class(parsed_xml.name).from_xml(parsed_xml)
}.to raise_error Entity::ValidationError
end
end

View file

@ -58,7 +58,8 @@ module DiasporaFederation
</conversation>
XML
parsed_instance = DiasporaFederation::Salmon::XmlPayload.unpack(Nokogiri::XML(minimal_xml).root)
parsed_xml = Nokogiri::XML(minimal_xml).root
parsed_instance = Entity.entity_class(parsed_xml.name).from_xml(parsed_xml)
expect(parsed_instance.messages).to eq([])
end
end

View file

@ -43,7 +43,8 @@ module DiasporaFederation
</event>
XML
parsed_instance = DiasporaFederation::Salmon::XmlPayload.unpack(Nokogiri::XML(minimal_xml).root)
parsed_xml = Nokogiri::XML(minimal_xml).root
parsed_instance = Entity.entity_class(parsed_xml.name).from_xml(parsed_xml)
expect(parsed_instance.end).to be_nil
expect(parsed_instance.all_day).to be_falsey
expect(parsed_instance.timezone).to be_nil

View file

@ -62,7 +62,8 @@ module DiasporaFederation
</photo>
XML
parsed_instance = DiasporaFederation::Salmon::XmlPayload.unpack(Nokogiri::XML(minimal_xml).root)
parsed_xml = Nokogiri::XML(minimal_xml).root
parsed_instance = Entity.entity_class(parsed_xml.name).from_xml(parsed_xml)
expect(parsed_instance.public).to be_falsey
expect(parsed_instance.text).to be_nil
end

View file

@ -63,7 +63,8 @@ module DiasporaFederation
</profile>
XML
parsed_instance = DiasporaFederation::Salmon::XmlPayload.unpack(Nokogiri::XML(minimal_xml).root)
parsed_xml = Nokogiri::XML(minimal_xml).root
parsed_instance = Entity.entity_class(parsed_xml.name).from_xml(parsed_xml)
expect(parsed_instance.full_name).to be_nil
expect(parsed_instance.first_name).to be_nil
expect(parsed_instance.last_name).to be_nil

View file

@ -136,7 +136,8 @@ module DiasporaFederation
</status_message>
XML
parsed_instance = DiasporaFederation::Salmon::XmlPayload.unpack(Nokogiri::XML(minimal_xml).root)
parsed_xml = Nokogiri::XML(minimal_xml).root
parsed_instance = Entity.entity_class(parsed_xml.name).from_xml(parsed_xml)
expect(parsed_instance.photos).to eq([])
expect(parsed_instance.location).to be_nil
expect(parsed_instance.poll).to be_nil

View file

@ -1,60 +0,0 @@
# frozen_string_literal: true
module DiasporaFederation
describe Salmon::XmlPayload do
let(:entity) { Entities::TestEntity.new(test: "asdf") }
let(:entity_xml) { <<~XML.strip }
<XML>
<post>
<test_entity>
<test>asdf</test>
</test_entity>
</post>
</XML>
XML
describe ".unpack" do
context "sanity" do
it "expects an Nokogiri::XML::Element as param" do
expect {
Salmon::XmlPayload.unpack(Nokogiri::XML(entity_xml).root)
}.not_to raise_error
end
it "raises and error when the param is not an Nokogiri::XML::Element" do
["asdf", 1234, true, :test, entity].each do |val|
expect {
Salmon::XmlPayload.unpack(val)
}.to raise_error ArgumentError, "only Nokogiri::XML::Element allowed"
end
end
end
context "returned object" do
subject { Salmon::XmlPayload.unpack(Nokogiri::XML(entity_xml).root) }
it "#to_h should match entity.to_h" do
expect(subject.to_h).to eq(entity.to_h)
end
it "returns an entity instance of the original class" do
expect(subject).to be_an_instance_of Entities::TestEntity
expect(subject.test).to eq("asdf")
end
it "allows unwrapped entities" do
xml = <<~XML
<test_entity>
<test>asdf</test>
</test_entity>
XML
entity = Salmon::XmlPayload.unpack(Nokogiri::XML(xml).root)
expect(entity).to be_an_instance_of Entities::TestEntity
expect(entity.test).to eq("asdf")
end
end
end
end
end

View file

@ -67,7 +67,7 @@ shared_examples "an XML Entity" do |ignored_props=[]|
context "parsing" do
it "reads its own output" do
packed_xml = instance.to_xml
parsed_instance = DiasporaFederation::Salmon::XmlPayload.unpack(packed_xml)
parsed_instance = DiasporaFederation::Entity.entity_class(packed_xml.name).from_xml(packed_xml)
check_entity(instance, parsed_instance, ignored_props)
end