From 41ebe13126a28b95dbe5acc5db3939ee9dae7e4b Mon Sep 17 00:00:00 2001 From: Benjamin Neff Date: Wed, 26 Apr 2017 02:08:38 +0200 Subject: [PATCH] Make Participation entity non-relayable Fixes #35 --- .../entities/participation.rb | 48 ++++++++--------- .../schemas/federation_entities.json | 26 +++++----- lib/diaspora_federation/test/factories.rb | 4 +- .../entities/participation_spec.rb | 51 +++---------------- 4 files changed, 41 insertions(+), 88 deletions(-) diff --git a/lib/diaspora_federation/entities/participation.rb b/lib/diaspora_federation/entities/participation.rb index d42fd53..d0cc815 100644 --- a/lib/diaspora_federation/entities/participation.rb +++ b/lib/diaspora_federation/entities/participation.rb @@ -4,11 +4,22 @@ module DiasporaFederation # # @see Validators::Participation class Participation < Entity - # Old signature order - # @deprecated - LEGACY_SIGNATURE_ORDER = %i(guid parent_type parent_guid author).freeze + # @!attribute [r] author + # The diaspora* ID of the author + # @see Person#author + # @return [String] diaspora* ID + property :author, :string, xml_name: :diaspora_handle - include Relayable + # @!attribute [r] guid + # A random string of at least 16 chars + # @see Validation::Rule::Guid + # @return [String] guid + property :guid, :string + + # @!attribute [r] parent_guid + # @see StatusMessage#guid + # @return [String] parent guid + property :parent_guid, :string # @!attribute [r] parent_type # A string describing a type of the target to subscribe on @@ -16,18 +27,9 @@ module DiasporaFederation # @return [String] parent type property :parent_type, :string, xml_name: :target_type - # It is only valid to receive a {Participation} from the author themself. - # @deprecated remove after {Participation} doesn't include {Relayable} anymore - def sender_valid?(sender) - sender == author - end - - # hackaround hacky from_hash override - # @deprecated remove after {Participation} doesn't include {Relayable} anymore - def enriched_properties - super.tap {|hash| - hash.delete(:parent) if hash[:parent].nil? - } + # @return [String] string representation of this object + def to_s + "#{super}:#{parent_type}:#{parent_guid}" end # Validates that the parent exists and the parent author is local @@ -36,22 +38,12 @@ module DiasporaFederation raise ParentNotLocal, "obj=#{self}" unless parent && parent.local end - # Don't verify signatures for a {Participation}. Validate that the parent is local. + # Validate that the parent is local. # @see Entity.from_hash # @param [Hash] hash entity initialization hash # @return [Entity] instance def self.from_hash(hash) - new(hash.merge(parent: nil)).tap(&:validate_parent) - end - - # @deprecated remove after {Participation} doesn't include {Relayable} anymore - private_class_method def self.xml_parser_class - DiasporaFederation::Parsers::XmlParser - end - - # @deprecated remove after {Participation} doesn't include {Relayable} anymore - private_class_method def self.json_parser_class - DiasporaFederation::Parsers::JsonParser + super.tap(&:validate_parent) end # Raised, if the parent is not owned by the receiving pod. diff --git a/lib/diaspora_federation/schemas/federation_entities.json b/lib/diaspora_federation/schemas/federation_entities.json index 98ec53a..a243efb 100644 --- a/lib/diaspora_federation/schemas/federation_entities.json +++ b/lib/diaspora_federation/schemas/federation_entities.json @@ -110,24 +110,22 @@ }, "participation": { - "allOf": [ - {"$ref": "#/definitions/relayable"}, - { + "type": "object", + "properties": { + "entity_type": { + "type": "string", + "pattern": "^participation$" + }, + "entity_data": { "type": "object", "properties": { - "entity_type": { - "type": "string", - "pattern": "^participation$" - }, - "entity_data": { - "type": "object", - "properties": { - "parent_type": {"enum": ["Post"]} - } - } + "author": { "type": "string" }, + "guid": { "$ref": "#/definitions/guid" }, + "parent_guid": { "$ref": "#/definitions/guid" }, + "parent_type": {"enum": ["Post"]} } } - ] + } }, "poll_participation": { diff --git a/lib/diaspora_federation/test/factories.rb b/lib/diaspora_federation/test/factories.rb index fed4be5..8e6fd41 100644 --- a/lib/diaspora_federation/test/factories.rb +++ b/lib/diaspora_federation/test/factories.rb @@ -93,10 +93,10 @@ module DiasporaFederation parent { Fabricate(:related_entity) } end - Fabricator(:participation_entity, - class_name: DiasporaFederation::Entities::Participation, from: :relayable_entity) do + Fabricator(:participation_entity, class_name: DiasporaFederation::Entities::Participation) do author { Fabricate.sequence(:diaspora_id) } guid { Fabricate.sequence(:guid) } + parent_guid { Fabricate.sequence(:guid) } parent_type "Post" end diff --git a/spec/lib/diaspora_federation/entities/participation_spec.rb b/spec/lib/diaspora_federation/entities/participation_spec.rb index cc1276e..bf1da7b 100644 --- a/spec/lib/diaspora_federation/entities/participation_spec.rb +++ b/spec/lib/diaspora_federation/entities/participation_spec.rb @@ -1,25 +1,21 @@ module DiasporaFederation describe Entities::Participation do let(:parent) { Fabricate(:post, author: bob) } - let(:parent_entity) { Fabricate(:related_entity, author: bob.diaspora_id) } let(:data) { Fabricate.attributes_for( :participation_entity, author: alice.diaspora_id, parent_guid: parent.guid, - parent_type: parent.entity_type, - parent: parent_entity - ).tap {|hash| add_signatures(hash) } + parent_type: parent.entity_type + ) } let(:xml) { <<-XML } - #{data[:guid]} - #{parent.entity_type} - #{parent.guid} #{data[:author]} - #{data[:author_signature]} - #{data[:parent_author_signature]} + #{data[:guid]} + #{parent.guid} + #{parent.entity_type} XML @@ -30,16 +26,8 @@ XML "author": "#{data[:author]}", "guid": "#{data[:guid]}", "parent_guid": "#{parent.guid}", - "author_signature": "#{data[:author_signature]}", - "parent_author_signature": "#{data[:parent_author_signature]}", "parent_type": "#{parent.entity_type}" - }, - "property_order": [ - "guid", - "parent_type", - "parent_guid", - "author" - ] + } } JSON @@ -47,36 +35,11 @@ JSON it_behaves_like "an Entity subclass" - it_behaves_like "an XML Entity", [:parent] + it_behaves_like "an XML Entity" it_behaves_like "a JSON Entity" - it_behaves_like "a relayable Entity" - - it_behaves_like "a relayable JSON entity" - - describe "#sender_valid?" do - let(:entity) { Entities::Participation.new(data) } - - it "allows the author" do - expect(entity.sender_valid?(alice.diaspora_id)).to be_truthy - end - - it "does not allow the parent author" do - expect(entity.sender_valid?(bob.diaspora_id)).to be_falsey - end - end - context "parse xml" do - it "does not verify the signature" do - data.merge!(author_signature: "aa", parent_author_signature: "bb") - xml = Entities::Participation.new(data).to_xml - - expect { - Entities::Participation.from_xml(xml) - }.not_to raise_error - end - describe "#validate_parent" do let(:participation) { allow(DiasporaFederation.callbacks).to receive(:trigger).and_call_original