move sender_valid? logic to Retraction and Relayable

This commit is contained in:
Benjamin Neff 2016-04-01 03:52:47 +02:00
parent 05cd90f073
commit c8be9083f0
5 changed files with 85 additions and 10 deletions

View file

@ -89,6 +89,10 @@ module DiasporaFederation
verify_parent_author_signature unless parent.local
end
def sender_valid?(sender)
sender == author || sender == parent.author
end
private
# this happens only on downstream federation

View file

@ -33,6 +33,15 @@ module DiasporaFederation
super(data)
end
def sender_valid?(sender)
case target_type
when "Comment", "Like", "PollParticipation"
sender == target.author || sender == target.parent.author
else
sender == target.author
end
end
# @param [Nokogiri::XML::Element] root_node xml nodes
# @return [Retraction] instance
def self.populate_entity(root_node)

View file

@ -27,16 +27,8 @@ module DiasporaFederation
end
def sender_valid?
case entity
when Entities::Retraction
case entity.target_type
when "Comment", "Like", "PollParticipation"
sender == entity.target.author || sender == entity.target.parent.author
else
sender == entity.target.author
end
when Entities::Relayable
sender == entity.author || sender == entity.parent.author
if entity.respond_to?(:sender_valid?)
entity.sender_valid?(sender)
else
sender == entity.author
end

View file

@ -323,5 +323,26 @@ XML
expect(entity.parent).to eq(remote_parent)
end
end
describe "#sender_valid?" do
it "allows author" do
entity = SomeRelayable.new(hash)
expect(entity.sender_valid?(author)).to be_truthy
end
it "allows parent author" do
entity = SomeRelayable.new(hash)
expect(entity.sender_valid?(local_parent.author)).to be_truthy
end
it "does not allow any random author" do
entity = SomeRelayable.new(hash)
invalid_author = FactoryGirl.generate(:diaspora_id)
expect(entity.sender_valid?(invalid_author)).to be_falsey
end
end
end
end

View file

@ -26,5 +26,54 @@ XML
it_behaves_like "an XML Entity"
it_behaves_like "a retraction"
describe "#sender_valid?" do
context "unrelayable target" do
it "allows target author" do
entity = Entities::Retraction.new(data)
expect(entity.sender_valid?(bob.diaspora_id)).to be_truthy
end
it "does not allow any random author" do
entity = Entities::Retraction.new(data)
invalid_author = FactoryGirl.generate(:diaspora_id)
expect(entity.sender_valid?(invalid_author)).to be_falsey
end
end
%w(Comment Like PollParticipation).each do |target_type|
context "#{target_type} target" do
let(:relayable_target) {
FactoryGirl.build(
:related_entity,
author: bob.diaspora_id,
parent: FactoryGirl.build(:related_entity, author: alice.diaspora_id)
)
}
let(:relayable_data) { data.merge(target_type: target_type, target: relayable_target) }
it "allows target author" do
entity = Entities::Retraction.new(relayable_data)
expect(entity.sender_valid?(bob.diaspora_id)).to be_truthy
end
it "allows target parent author" do
entity = Entities::Retraction.new(relayable_data)
expect(entity.sender_valid?(alice.diaspora_id)).to be_truthy
end
it "does not allow any random author" do
entity = Entities::Retraction.new(relayable_data)
invalid_author = FactoryGirl.generate(:diaspora_id)
expect(entity.sender_valid?(invalid_author)).to be_falsey
end
end
end
end
end
end