Only add optional nil values to relayable XML when needed for signature order

This commit is contained in:
Benjamin Neff 2017-07-29 22:26:10 +02:00
parent f5ff4a71e6
commit bb40d6190d
No known key found for this signature in database
GPG key ID: 971464C3F1A90194
3 changed files with 41 additions and 2 deletions

View file

@ -100,7 +100,9 @@ module DiasporaFederation
# The order for signing # The order for signing
# @return [Array] # @return [Array]
def signature_order def signature_order
@signature_order || self.class.class_props.keys - %i[author_signature parent_author_signature parent] @signature_order || self.class.class_props.keys.reject {|key|
self.class.optional_props.include?(key) && public_send(key).nil?
} - %i[author_signature parent_author_signature parent]
end end
private private

View file

@ -74,7 +74,7 @@ module DiasporaFederation
include Entities::Relayable include Entities::Relayable
property :property, :string property :property, :string, optional: true
end end
end end

View file

@ -148,6 +148,43 @@ XML
expect(xml.at_xpath("new_property").text).to be_empty expect(xml.at_xpath("new_property").text).to be_empty
end end
it "adds nil properties to xml when needed for signature_order" do
expected_xml = <<-XML
<some_relayable>
<author>#{author}</author>
<guid>#{guid}</guid>
<parent_guid>#{parent_guid}</parent_guid>
<property/>
<new_property>#{new_property}</new_property>
<author_signature>aa</author_signature>
<parent_author_signature>bb</parent_author_signature>
</some_relayable>
XML
signature_order = [:author, :guid, :parent_guid, :property, "new_property"]
xml = Entities::SomeRelayable.new(
hash_with_fake_signatures.merge(property: nil), signature_order, "new_property" => new_property
).to_xml
expect(xml.to_s.strip).to eq(expected_xml.strip)
end
it "does not add nil properties to xml when not needed for signature_order" do
expected_xml = <<-XML
<some_relayable>
<author>#{author}</author>
<guid>#{guid}</guid>
<parent_guid>#{parent_guid}</parent_guid>
<author_signature>aa</author_signature>
<parent_author_signature>bb</parent_author_signature>
</some_relayable>
XML
xml = Entities::SomeRelayable.new(hash_with_fake_signatures.merge(property: nil)).to_xml
expect(xml.to_s.strip).to eq(expected_xml.strip)
end
it "computes correct signatures for the entity" do it "computes correct signatures for the entity" do
expect_callback(:fetch_private_key, author).and_return(author_pkey) expect_callback(:fetch_private_key, author).and_return(author_pkey)
expect_callback(:fetch_private_key, local_parent.author).and_return(parent_pkey) expect_callback(:fetch_private_key, local_parent.author).and_return(parent_pkey)