diff --git a/lib/diaspora_federation/federation/receiver/abstract_receiver.rb b/lib/diaspora_federation/federation/receiver/abstract_receiver.rb index 8dc6207..b62317d 100644 --- a/lib/diaspora_federation/federation/receiver/abstract_receiver.rb +++ b/lib/diaspora_federation/federation/receiver/abstract_receiver.rb @@ -27,7 +27,19 @@ module DiasporaFederation end def sender_valid? - sender == entity.author # TODO: handle sender of relayables + 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 + else + sender == entity.author + end end end end diff --git a/spec/lib/diaspora_federation/federation/receiver/private_spec.rb b/spec/lib/diaspora_federation/federation/receiver/private_spec.rb index e043df7..bee4ce1 100644 --- a/spec/lib/diaspora_federation/federation/receiver/private_spec.rb +++ b/spec/lib/diaspora_federation/federation/receiver/private_spec.rb @@ -1,19 +1,19 @@ module DiasporaFederation describe Federation::Receiver::Private do let(:recipient) { 42 } - let(:entity) { FactoryGirl.build(:status_message_entity, public: false) } - let(:magic_env) { Salmon::MagicEnvelope.new(entity, entity.author) } + let(:post) { FactoryGirl.build(:status_message_entity, public: false) } + let(:magic_env) { Salmon::MagicEnvelope.new(post, post.author) } describe "#receive" do it "receives a private post" do - expect_callback(:receive_entity, entity, recipient) + expect_callback(:receive_entity, post, recipient) described_class.new(magic_env, recipient).receive end it "validates the sender" do sender = FactoryGirl.generate(:diaspora_id) - bad_env = Salmon::MagicEnvelope.new(entity, sender) + bad_env = Salmon::MagicEnvelope.new(post, sender) expect { described_class.new(bad_env, recipient).receive @@ -25,6 +25,93 @@ module DiasporaFederation described_class.new(magic_env).receive }.to raise_error Federation::Receiver::RecipientRequired end + + context "with relayable" do + let(:comment) { FactoryGirl.build(:comment_entity, parent: FactoryGirl.build(:related_entity, public: false)) } + + it "receives a comment from the author" do + magic_env = Salmon::MagicEnvelope.new(comment, comment.author) + + expect_callback(:receive_entity, comment, recipient) + + described_class.new(magic_env, recipient).receive + end + + it "receives a comment from the parent author" do + magic_env = Salmon::MagicEnvelope.new(comment, comment.parent.author) + + expect_callback(:receive_entity, comment, recipient) + + described_class.new(magic_env, recipient).receive + end + + it "validates the sender" do + sender = FactoryGirl.generate(:diaspora_id) + bad_env = Salmon::MagicEnvelope.new(comment, sender) + + expect { + described_class.new(bad_env, recipient).receive + }.to raise_error Federation::Receiver::InvalidSender + end + end + + context "with retraction" do + context "for a post" do + let(:retraction) { FactoryGirl.build(:retraction_entity, target_type: "Post") } + + it "retracts a post from the author" do + magic_env = Salmon::MagicEnvelope.new(retraction, retraction.target.author) + + expect_callback(:receive_entity, retraction, recipient) + + described_class.new(magic_env, recipient).receive + end + + it "validates the sender" do + sender = FactoryGirl.generate(:diaspora_id) + bad_env = Salmon::MagicEnvelope.new(retraction, sender) + + expect { + described_class.new(bad_env, recipient).receive + }.to raise_error Federation::Receiver::InvalidSender + end + end + + context "for a comment" do + let(:retraction) { + FactoryGirl.build( + :retraction_entity, + target_type: "Comment", + target: FactoryGirl.build(:related_entity, parent: FactoryGirl.build(:related_entity)) + ) + } + + it "retracts a comment from the author" do + magic_env = Salmon::MagicEnvelope.new(retraction, retraction.target.author) + + expect_callback(:receive_entity, retraction, recipient) + + described_class.new(magic_env, recipient).receive + end + + it "retracts a comment from the parent author" do + magic_env = Salmon::MagicEnvelope.new(retraction, retraction.target.parent.author) + + expect_callback(:receive_entity, retraction, recipient) + + described_class.new(magic_env, recipient).receive + end + + it "validates the sender" do + sender = FactoryGirl.generate(:diaspora_id) + bad_env = Salmon::MagicEnvelope.new(retraction, sender) + + expect { + described_class.new(bad_env, recipient).receive + }.to raise_error Federation::Receiver::InvalidSender + end + end + end end end end diff --git a/spec/lib/diaspora_federation/federation/receiver/public_spec.rb b/spec/lib/diaspora_federation/federation/receiver/public_spec.rb index 1318e9c..b1b1635 100644 --- a/spec/lib/diaspora_federation/federation/receiver/public_spec.rb +++ b/spec/lib/diaspora_federation/federation/receiver/public_spec.rb @@ -1,23 +1,110 @@ module DiasporaFederation describe Federation::Receiver::Public do - let(:entity) { FactoryGirl.build(:status_message_entity) } - let(:magic_env) { Salmon::MagicEnvelope.new(entity, entity.author) } + let(:post) { FactoryGirl.build(:status_message_entity) } + let(:magic_env) { Salmon::MagicEnvelope.new(post, post.author) } describe "#receive" do it "receives a public post" do - expect_callback(:receive_entity, entity, nil) + expect_callback(:receive_entity, post, nil) described_class.new(magic_env).receive end it "validates the sender" do sender = FactoryGirl.generate(:diaspora_id) - bad_env = Salmon::MagicEnvelope.new(entity, sender) + bad_env = Salmon::MagicEnvelope.new(post, sender) expect { described_class.new(bad_env).receive }.to raise_error Federation::Receiver::InvalidSender end + + context "with relayable" do + let(:comment) { FactoryGirl.build(:comment_entity) } + + it "receives a comment from the author" do + magic_env = Salmon::MagicEnvelope.new(comment, comment.author) + + expect_callback(:receive_entity, comment, nil) + + described_class.new(magic_env).receive + end + + it "receives a comment from the author parent" do + magic_env = Salmon::MagicEnvelope.new(comment, comment.parent.author) + + expect_callback(:receive_entity, comment, nil) + + described_class.new(magic_env).receive + end + + it "validates the sender" do + sender = FactoryGirl.generate(:diaspora_id) + bad_env = Salmon::MagicEnvelope.new(comment, sender) + + expect { + described_class.new(bad_env).receive + }.to raise_error Federation::Receiver::InvalidSender + end + end + + context "with retraction" do + context "for a post" do + let(:retraction) { FactoryGirl.build(:retraction_entity, target_type: "Post") } + + it "retracts a post from the author" do + magic_env = Salmon::MagicEnvelope.new(retraction, retraction.target.author) + + expect_callback(:receive_entity, retraction, nil) + + described_class.new(magic_env).receive + end + + it "validates the sender" do + sender = FactoryGirl.generate(:diaspora_id) + bad_env = Salmon::MagicEnvelope.new(retraction, sender) + + expect { + described_class.new(bad_env).receive + }.to raise_error Federation::Receiver::InvalidSender + end + end + + context "for a comment" do + let(:retraction) { + FactoryGirl.build( + :retraction_entity, + target_type: "Comment", + target: FactoryGirl.build(:related_entity, parent: FactoryGirl.build(:related_entity)) + ) + } + + it "retracts a comment from the author" do + magic_env = Salmon::MagicEnvelope.new(retraction, retraction.target.author) + + expect_callback(:receive_entity, retraction, nil) + + described_class.new(magic_env).receive + end + + it "retracts a comment from the parent author" do + magic_env = Salmon::MagicEnvelope.new(retraction, retraction.target.parent.author) + + expect_callback(:receive_entity, retraction, nil) + + described_class.new(magic_env).receive + end + + it "validates the sender" do + sender = FactoryGirl.generate(:diaspora_id) + bad_env = Salmon::MagicEnvelope.new(retraction, sender) + + expect { + described_class.new(bad_env).receive + }.to raise_error Federation::Receiver::InvalidSender + end + end + end end end end