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 # @return [Time] creation time
property :created_at, :timestamp, default: -> { Time.now.utc } property :created_at, :timestamp, default: -> { Time.now.utc }
private
# Remove "created_at" when no order was received. # Remove "created_at" when no order was received.
# @deprecated TODO: Remove this, this will break compatibility with pods older than 0.6.3.0. # @deprecated TODO: Remove this, this will break compatibility with pods older than 0.6.3.0.
def signature_order 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 end
end end

View file

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

View file

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