Skip to content

Commit

Permalink
Make Participation entity non-relayable
Browse files Browse the repository at this point in the history
Fixes #35
  • Loading branch information
SuperTux88 committed May 10, 2017
1 parent b7167b9 commit 41ebe13
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 88 deletions.
50 changes: 21 additions & 29 deletions lib/diaspora_federation/entities/participation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,32 @@ module Entities
#
# @see Validators::Participation
class Participation < Entity
# Old signature order
# @deprecated
LEGACY_SIGNATURE_ORDER = %i(guid parent_type parent_guid author).freeze

include Relayable
# @!attribute [r] author
# The diaspora* ID of the author
# @see Person#author
# @return [String] diaspora* ID
property :author, :string, xml_name: :diaspora_handle

# @!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
# Currently only "Post" is supported.
# @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
Expand All @@ -36,22 +38,12 @@ def validate_parent
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.
Expand Down
26 changes: 12 additions & 14 deletions lib/diaspora_federation/schemas/federation_entities.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
4 changes: 2 additions & 2 deletions lib/diaspora_federation/test/factories.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,10 @@ module Factories
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

Expand Down
49 changes: 6 additions & 43 deletions spec/lib/diaspora_federation/entities/participation_spec.rb
Original file line number Diff line number Diff line change
@@ -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 }
<participation>
<diaspora_handle>#{data[:author]}</diaspora_handle>
<guid>#{data[:guid]}</guid>
<target_type>#{parent.entity_type}</target_type>
<parent_guid>#{parent.guid}</parent_guid>
<diaspora_handle>#{data[:author]}</diaspora_handle>
<author_signature>#{data[:author_signature]}</author_signature>
<parent_author_signature>#{data[:parent_author_signature]}</parent_author_signature>
<target_type>#{parent.entity_type}</target_type>
</participation>
XML

Expand All @@ -30,53 +26,20 @@ module DiasporaFederation
"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

let(:string) { "Participation:#{data[:guid]}:Post:#{parent.guid}" }

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
Expand Down

0 comments on commit 41ebe13

Please sign in to comment.