parent
b7167b9fde
commit
41ebe13126
4 changed files with 41 additions and 88 deletions
|
|
@ -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.
|
||||
|
|
|
|||
|
|
@ -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": {
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
||||
|
|
|
|||
|
|
@ -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>
|
||||
<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>
|
||||
<guid>#{data[:guid]}</guid>
|
||||
<parent_guid>#{parent.guid}</parent_guid>
|
||||
<target_type>#{parent.entity_type}</target_type>
|
||||
</participation>
|
||||
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
|
||||
|
|
|
|||
Loading…
Reference in a new issue