diaspora/spec/shared_behaviors/receiving.rb
Benjamin Neff b39f3ccc74 relay relayables after receive
also refactored relayable specs to use `let`
2016-06-26 06:21:02 +02:00

66 lines
2.3 KiB
Ruby

require "spec_helper"
shared_examples_for "it ignores existing object received twice" do |klass, method|
it "return nil if the #{klass} already exists" do
expect(Diaspora::Federation::Receive.public_send(method, entity)).not_to be_nil
expect(Diaspora::Federation::Receive.public_send(method, entity)).to be_nil
end
it "does not change anything if the #{klass} already exists" do
Diaspora::Federation::Receive.public_send(method, entity)
expect_any_instance_of(klass).not_to receive(:create_or_update)
Diaspora::Federation::Receive.public_send(method, entity)
end
end
shared_examples_for "it rejects if the parent author ignores the author" do |klass, method|
it "saves the relayable if the author is not ignored" do
Diaspora::Federation::Receive.public_send(method, entity)
expect(klass.find_by!(guid: entity.guid)).to be_instance_of(klass)
end
context "if the author is ignored" do
before do
alice.blocks.create(person: sender)
end
it "raises an error and does not save the relayable" do
expect {
Diaspora::Federation::Receive.public_send(method, entity)
}.to raise_error Diaspora::Federation::AuthorIgnored
expect(klass.find_by(guid: entity.guid)).to be_nil
end
it "it sends a retraction back to the author" do
dispatcher = double
expect(Diaspora::Federation::Dispatcher).to receive(:build) do |retraction_sender, retraction, opts|
expect(retraction_sender).to eq(alice)
expect(retraction.data[:target_guid]).to eq(entity.guid)
expect(retraction.data[:target_type]).to eq(klass.to_s)
expect(opts).to eq(subscribers: [sender])
dispatcher
end
expect(dispatcher).to receive(:dispatch)
expect {
Diaspora::Federation::Receive.public_send(method, entity)
}.to raise_error Diaspora::Federation::AuthorIgnored
end
end
end
shared_examples_for "it relays relayables" do |klass, method|
it "dispatches the received relayable" do
expect(Diaspora::Federation::Dispatcher).to receive(:defer_dispatch) do |parent_author, relayable|
expect(parent_author).to eq(alice)
expect(relayable).to be_instance_of(klass)
expect(relayable.guid).to eq(entity.guid)
end
Diaspora::Federation::Receive.public_send(method, entity)
end
end