validate sender for retraction and relayable

This commit is contained in:
Benjamin Neff 2016-03-31 19:28:49 +02:00
parent c7c2957b07
commit 05cd90f073
3 changed files with 195 additions and 9 deletions

View file

@ -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

View file

@ -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

View file

@ -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