Rename xml_order to signature_order

Because it's also used for signatures in JSON.
This commit is contained in:
Benjamin Neff 2017-05-01 21:30:26 +02:00
parent ba01882cb2
commit b510ed868f
No known key found for this signature in database
GPG key ID: 971464C3F1A90194
3 changed files with 33 additions and 34 deletions

View file

@ -18,12 +18,10 @@ module DiasporaFederation
# @return [Time] creation time
property :created_at, :timestamp, default: -> { Time.now.utc }
private
# Remove "created_at" when no order was received.
# @deprecated TODO: Remove this, this will break compatibility with pods older than 0.6.3.0.
def signature_order
super.tap {|order| order.delete(:created_at) unless xml_order }
@signature_order || super.tap {|order| order.delete(:created_at) }
end
end
end

View file

@ -7,10 +7,6 @@ module DiasporaFederation
module Relayable
include Signable
# Order from the parsed xml for signature
# @return [Array] order from xml
attr_reader :xml_order
# Additional properties from parsed input object
# @return [Hash] additional elements
attr_reader :additional_data
@ -65,11 +61,11 @@ module DiasporaFederation
# Initializes a new relayable Entity with order and additional xml elements
#
# @param [Hash] data entity data
# @param [Array] xml_order order from xml
# @param [Array] signature_order order for the signature
# @param [Hash] additional_data additional xml elements
# @see DiasporaFederation::Entity#initialize
def initialize(data, xml_order=nil, additional_data={})
@xml_order = xml_order.reject {|name| name =~ /signature/ } if xml_order
def initialize(data, signature_order=nil, additional_data={})
self.signature_order = signature_order if signature_order
@additional_data = additional_data
super(data)
@ -103,6 +99,12 @@ module DiasporaFederation
}
end
# The order for signing
# @return [Array]
def signature_order
@signature_order || self.class.class_props.keys - %i(author_signature parent_author_signature parent)
end
private
# Sign with author key
@ -147,15 +149,10 @@ module DiasporaFederation
order.map {|element| [element, data[element] || ""] }.to_h
end
# The order for signing
# @return [Array]
def signature_order
if xml_order
prop_names = self.class.class_props.keys.map(&:to_s)
xml_order.map {|name| prop_names.include?(name) ? name.to_sym : name }
else
self.class.class_props.keys - %i(author_signature parent_author_signature parent)
end
def signature_order=(order)
prop_names = self.class.class_props.keys.map(&:to_s)
@signature_order = order.reject {|name| name =~ /signature/ }
.map {|name| prop_names.include?(name) ? name.to_sym : name }
end
# @return [String] signature data string

View file

@ -18,9 +18,9 @@ module DiasporaFederation
describe "#initialize" do
it "filters signatures from order" do
xml_order = [:author, :guid, :parent_guid, :property, "new_property", :author_signature]
signature_order = [:author, :guid, :parent_guid, :property, "new_property", :author_signature]
expect(Entities::SomeRelayable.new(hash, xml_order).xml_order)
expect(Entities::SomeRelayable.new(hash, signature_order).signature_order)
.to eq([:author, :guid, :parent_guid, :property, "new_property"])
end
end
@ -144,23 +144,27 @@ module DiasporaFederation
XML
it "adds new unknown xml elements to the xml again" do
xml_order = [:author, :guid, :parent_guid, :property, "new_property"]
xml = Entities::SomeRelayable.new(hash_with_fake_signatures, xml_order, "new_property" => new_property).to_xml
signature_order = [:author, :guid, :parent_guid, :property, "new_property"]
xml = Entities::SomeRelayable.new(
hash_with_fake_signatures, signature_order, "new_property" => new_property
).to_xml
expect(xml.to_s.strip).to eq(expected_xml.strip)
end
it "converts strings in xml_order to symbol if needed" do
xml_order = %w(author guid parent_guid property new_property)
xml = Entities::SomeRelayable.new(hash_with_fake_signatures, xml_order, "new_property" => new_property).to_xml
it "accepts string names of known properties in signature_order" do
signature_order = %w(author guid parent_guid property new_property)
xml = Entities::SomeRelayable.new(
hash_with_fake_signatures, signature_order, "new_property" => new_property
).to_xml
expect(xml.to_s.strip).to eq(expected_xml.strip)
end
it "adds missing properties from xml_order to xml" do
xml_order = [:author, :guid, :parent_guid, :property, "new_property"]
it "adds missing properties from signature_order to xml" do
signature_order = [:author, :guid, :parent_guid, :property, "new_property"]
xml = Entities::SomeRelayable.new(hash_with_fake_signatures, xml_order).to_xml
xml = Entities::SomeRelayable.new(hash_with_fake_signatures, signature_order).to_xml
expect(xml.at_xpath("new_property").text).to be_empty
end
@ -182,10 +186,10 @@ XML
expect_callback(:fetch_private_key, author).and_return(author_pkey)
expect_callback(:fetch_private_key, local_parent.author).and_return(parent_pkey)
xml_order = [:author, :guid, :parent_guid, "new_property", :property]
signature_order = [:author, :guid, :parent_guid, "new_property", :property]
signature_data_with_new_property = "#{author};#{guid};#{parent_guid};#{new_property};#{property}"
xml = Entities::SomeRelayable.new(hash, xml_order, "new_property" => new_property).to_xml
xml = Entities::SomeRelayable.new(hash, signature_order, "new_property" => new_property).to_xml
author_signature = xml.at_xpath("author_signature").text
parent_author_signature = xml.at_xpath("parent_author_signature").text
@ -253,7 +257,7 @@ XML
it "hand over the order in the xml to the instance without signatures" do
entity = Entities::SomeRelayable.from_xml(Nokogiri::XML(new_xml).root)
expect(entity.xml_order).to eq([:author, :guid, :parent_guid, "new_property", :property])
expect(entity.signature_order).to eq([:author, :guid, :parent_guid, "new_property", :property])
end
it "creates Entity with empty 'additional_data' if the xml has only known properties" do
@ -297,7 +301,7 @@ XML
expect(json).to include_json(property_order: property_order.map(&:to_s))
end
it "uses property order for filling property_order when no xml_order supplied" do
it "uses property order for filling property_order when no signature_order supplied" do
entity = entity_class.new(hash_with_fake_signatures)
expect(
entity.to_json.to_json
@ -408,7 +412,7 @@ XML
it "hands over the order in the data to the instance without signatures" do
entity = Entities::SomeRelayable.from_hash(entity_data, property_order)
expect(entity.xml_order).to eq(%w(author guid parent_guid new_property property))
expect(entity.signature_order).to eq([:author, :guid, :parent_guid, "new_property", :property])
end
it "calls a constructor of the entity of the appropriate type" do