refactoring for federation tests

This commit is contained in:
Benjamin Neff 2015-12-28 19:46:45 +01:00 committed by Dennis Schubert
parent 9f8e018422
commit 5392c6e6a9
6 changed files with 278 additions and 327 deletions

View file

@ -0,0 +1,26 @@
def remote_user_on_pod_b
@remote_on_b ||= FactoryGirl.build(:user).tap do |user|
user.person = FactoryGirl.create(:person,
profile: FactoryGirl.build(:profile),
serialized_public_key: user.encryption_key.public_key.export,
diaspora_handle: "#{user.username}@remote-b.net")
end
end
def remote_user_on_pod_c
@remote_on_c ||= FactoryGirl.build(:user).tap do |user|
user.person = FactoryGirl.create(:person,
profile: FactoryGirl.build(:profile),
serialized_public_key: user.encryption_key.public_key.export,
diaspora_handle: "#{user.username}@remote-c.net")
end
end
def generate_xml(entity, remote_user, user)
DiasporaFederation::Salmon::EncryptedSlap.generate_xml(
remote_user.diaspora_handle,
OpenSSL::PKey::RSA.new(remote_user.encryption_key),
entity,
OpenSSL::PKey::RSA.new(user.encryption_key)
)
end

View file

@ -1,132 +0,0 @@
def generate_xml(entity, remote_user, user)
DiasporaFederation::Salmon::EncryptedSlap.generate_xml(
remote_user.diaspora_handle,
OpenSSL::PKey::RSA.new(remote_user.encryption_key),
entity,
OpenSSL::PKey::RSA.new(user.encryption_key)
)
end
def generate_status_message
@entity = FactoryGirl.build(
:status_message_entity,
diaspora_id: @remote_user.diaspora_handle,
public: false
)
generate_xml(@entity, @remote_user, @user)
end
def generate_forged_status_message
substitute_wrong_key(@remote_user, 1)
generate_status_message
end
def mock_private_key_for_user(user)
expect(DiasporaFederation.callbacks).to receive(:trigger)
.with(:fetch_private_key_by_diaspora_id, user.person.diaspora_handle)
.once
.and_return(user.encryption_key)
end
def retraction_mock_callbacks(entity, sender)
return unless [
DiasporaFederation::Entities::SignedRetraction,
DiasporaFederation::Entities::RelayableRetraction
].include?(entity.class)
mock_private_key_for_user(sender)
allow(DiasporaFederation.callbacks).to receive(:trigger)
.with(
:fetch_entity_author_id_by_guid,
entity.target_type,
entity.target_guid
)
.once
.and_return(sender.encryption_key)
end
def generate_retraction(entity_name, target_object, sender=@remote_user)
@entity = FactoryGirl.build(
entity_name,
diaspora_id: sender.diaspora_handle,
target_guid: target_object.guid,
target_type: target_object.class.to_s
)
retraction_mock_callbacks(@entity, sender)
generate_xml(@entity, sender, @user)
end
def generate_forged_retraction(entity_name, target_object, sender=@remote_user)
times = 1
if %i(signed_retraction_entity relayable_retraction_entity).include?(entity_name)
times += 2
end
substitute_wrong_key(sender, times)
generate_retraction(entity_name, target_object, sender)
end
def generate_relayable_local_parent(entity_name)
@entity = FactoryGirl.build(
entity_name,
parent_guid: @local_message.guid,
diaspora_id: @remote_user.person.diaspora_handle
)
mock_private_key_for_user(@remote_user)
expect(DiasporaFederation.callbacks).to receive(:trigger)
.with(:fetch_author_private_key_by_entity_guid, "Post", kind_of(String))
.and_return(nil)
generate_xml(@entity, @remote_user, @user)
end
def generate_relayable_remote_parent(entity_name)
@entity = FactoryGirl.build(
entity_name,
parent_guid: @remote_message.guid,
diaspora_id: @remote_user2.person.diaspora_handle
)
mock_private_key_for_user(@remote_user2)
expect(DiasporaFederation.callbacks).to receive(:trigger)
.with(
:fetch_author_private_key_by_entity_guid,
"Post",
@remote_message.guid
)
.once
.and_return(@remote_user.encryption_key)
generate_xml(@entity, @remote_user, @user)
end
def substitute_wrong_key(user, times_number)
expect(user).to receive(:encryption_key).exactly(times_number).times.and_return(
OpenSSL::PKey::RSA.new(1024)
)
end
# Checks when a remote pod wants to send us a relayable without having a key for declared diaspora ID
def generate_relayable_local_parent_wrong_author_key(entity_name)
substitute_wrong_key(@remote_user, 2)
generate_relayable_local_parent(entity_name)
end
# Checks when a remote pod B wants to send us a relayable with authorship from a remote pod C user
# without having correct signature from him.
def generate_relayable_remote_parent_wrong_author_key(entity_name)
substitute_wrong_key(@remote_user2, 1)
generate_relayable_remote_parent(entity_name)
end
# Checks when a remote pod C wants to send us a relayable from its user, but bypassing the pod B where
# remote status came from.
def generate_relayable_remote_parent_wrong_parent_key(entity_name)
substitute_wrong_key(@remote_user, 2)
generate_relayable_remote_parent(entity_name)
end

View file

@ -1,146 +0,0 @@
require "spec_helper"
require "diaspora_federation/test"
require "integration/federation/federation_messages_generation"
require "integration/federation/shared_receive_relayable"
require "integration/federation/shared_receive_retraction"
describe Workers::ReceiveEncryptedSalmon do
before do
@user = alice
allow(User).to receive(:find) { |id|
@user if id == @user.id
}
@remote_user = FactoryGirl.build(:user) # user on pod B
@remote_user2 = FactoryGirl.build(:user) # user on pod C
allow_any_instance_of(DiasporaFederation::Discovery::Discovery)
.to receive(:webfinger) {|instance|
[@remote_user, @remote_user2].find {|user| user.diaspora_handle == instance.diaspora_id }.person.webfinger
}
allow_any_instance_of(DiasporaFederation::Discovery::Discovery)
.to receive(:hcard) {|instance|
[@remote_user, @remote_user2].find {|user| user.diaspora_handle == instance.diaspora_id }.person.hcard
}
@remote_person = Person.find_or_fetch_by_identifier(@remote_user.diaspora_handle)
@remote_person2 = Person.find_or_fetch_by_identifier(@remote_user2.diaspora_handle)
end
it "treats sharing request recive correctly" do
entity = FactoryGirl.build(:request_entity, recipient_id: @user.diaspora_handle)
expect(Diaspora::Fetcher::Public).to receive(:queue_for).exactly(1).times
Workers::ReceiveEncryptedSalmon.new.perform(@user.id, generate_xml(entity, @remote_user, @user))
expect(@user.contacts.count).to eq(2)
new_contact = @user.contacts.order(created_at: :asc).last
expect(new_contact).not_to be_nil
expect(new_contact.sharing).to eq(true)
expect(new_contact.person.diaspora_handle).to eq(@remote_user.diaspora_handle)
end
it "doesn't save the status message if there is no sharing" do
Workers::ReceiveEncryptedSalmon.new.perform(@user.id, generate_status_message)
expect(StatusMessage.exists?(guid: @entity.guid)).to be(false)
end
describe "with messages which require sharing" do
before do
@remote_person = Person.find_or_fetch_by_identifier(@remote_user.diaspora_handle)
contact = @user.contacts.find_or_initialize_by(person_id: @remote_person.id)
contact.sharing = true
contact.save
end
it "treats status message receive correctly" do
Workers::ReceiveEncryptedSalmon.new.perform(@user.id, generate_status_message)
expect(StatusMessage.exists?(guid: @entity.guid)).to be(true)
end
it "doesn't accept status message with wrong signature" do
Workers::ReceiveEncryptedSalmon.new.perform(@user.id, generate_forged_status_message)
expect(StatusMessage.exists?(guid: @entity.guid)).to be(false)
end
describe "retractions for non-relayable objects" do
%w(
retraction
signed_retraction
).each do |retraction_entity_name|
context "with #{retraction_entity_name}" do
%w(status_message photo).each do |target|
context "with #{target}" do
it_behaves_like "it retracts non-relayable object" do
let(:target_object) { FactoryGirl.create(target.to_sym, author: @remote_person) }
let(:entity_name) { "#{retraction_entity_name}_entity".to_sym }
end
end
end
end
end
end
describe "with messages which require a status to operate on" do
before do
@local_message = FactoryGirl.create(:status_message, author: @user.person)
@remote_message = FactoryGirl.create(:status_message, author: @remote_person)
end
%w(comment like participation).each do |entity|
context "with #{entity}" do
it_behaves_like "it deals correctly with a relayable" do
let(:entity_name) { "#{entity}_entity".to_sym }
let(:klass) { entity.camelize.constantize }
end
end
end
describe "retractions for relayable objects" do
%w(
retraction
signed_retraction
relayable_retraction
).each do |retraction_entity_name|
context "with #{retraction_entity_name}" do
context "with comment" do
it_behaves_like "it retracts relayable object" do
# case for to-upstream federation
let(:entity_name) { "#{retraction_entity_name}_entity".to_sym }
let(:target_object) { FactoryGirl.create(:comment, author: @remote_person, post: @local_message) }
let(:sender) { @remote_user }
end
it_behaves_like "it retracts relayable object" do
# case for to-downsteam federation
let(:target_object) { FactoryGirl.create(:comment, author: @remote_person2, post: @remote_message) }
let(:entity_name) { "#{retraction_entity_name}_entity".to_sym }
let(:sender) { @remote_user }
end
end
context "with like" do
it_behaves_like "it retracts relayable object" do
# case for to-upstream federation
let(:entity_name) { "#{retraction_entity_name}_entity".to_sym }
let(:target_object) { FactoryGirl.create(:like, author: @remote_person, target: @local_message) }
let(:sender) { @remote_user }
end
it_behaves_like "it retracts relayable object" do
# case for to-downsteam federation
let(:target_object) { FactoryGirl.create(:like, author: @remote_person2, target: @remote_message) }
let(:entity_name) { "#{retraction_entity_name}_entity".to_sym }
let(:sender) { @remote_user }
end
end
end
end
end
end
end
end

View file

@ -0,0 +1,130 @@
require "spec_helper"
require "integration/federation/federation_helper"
require "integration/federation/shared_receive_relayable"
require "integration/federation/shared_receive_retraction"
describe Workers::ReceiveEncryptedSalmon do
it "treats sharing request receive correctly" do
entity = FactoryGirl.build(:request_entity, recipient_id: alice.diaspora_handle)
expect(Diaspora::Fetcher::Public).to receive(:queue_for)
Workers::ReceiveEncryptedSalmon.new.perform(alice.id, generate_xml(entity, remote_user_on_pod_c, alice))
new_contact = alice.contacts.find {|c| c.person.diaspora_handle == remote_user_on_pod_c.diaspora_handle }
expect(new_contact).not_to be_nil
expect(new_contact.sharing).to eq(true)
end
it "doesn't save the status message if there is no sharing" do
entity = FactoryGirl.build(:status_message_entity, diaspora_id: remote_user_on_pod_b.diaspora_handle, public: false)
Workers::ReceiveEncryptedSalmon.new.perform(alice.id, generate_xml(entity, remote_user_on_pod_b, alice))
expect(StatusMessage.exists?(guid: entity.guid)).to be(false)
end
describe "with messages which require sharing" do
before do
contact = alice.contacts.find_or_initialize_by(person_id: remote_user_on_pod_b.person.id)
contact.sharing = true
contact.save
end
it "treats status message receive correctly" do
entity = FactoryGirl.build(:status_message_entity,
diaspora_id: remote_user_on_pod_b.diaspora_handle, public: false)
Workers::ReceiveEncryptedSalmon.new.perform(alice.id, generate_xml(entity, remote_user_on_pod_b, alice))
expect(StatusMessage.exists?(guid: entity.guid)).to be(true)
end
it "doesn't accept status message with wrong signature" do
expect(remote_user_on_pod_b).to receive(:encryption_key).and_return(OpenSSL::PKey::RSA.new(1024))
entity = FactoryGirl.build(:status_message_entity,
diaspora_id: remote_user_on_pod_b.diaspora_handle, public: false)
Workers::ReceiveEncryptedSalmon.new.perform(alice.id, generate_xml(entity, remote_user_on_pod_b, alice))
expect(StatusMessage.exists?(guid: entity.guid)).to be(false)
end
describe "retractions for non-relayable objects" do
%w(
retraction
signed_retraction
).each do |retraction_entity_name|
context "with #{retraction_entity_name}" do
%w(status_message photo).each do |target|
context "with #{target}" do
it_behaves_like "it retracts non-relayable object" do
let(:target_object) { FactoryGirl.create(target.to_sym, author: remote_user_on_pod_b.person) }
let(:entity_name) { "#{retraction_entity_name}_entity".to_sym }
end
end
end
end
end
end
describe "with messages which require a status to operate on" do
let(:local_message) { FactoryGirl.create(:status_message, author: alice.person) }
let(:remote_message) { FactoryGirl.create(:status_message, author: remote_user_on_pod_b.person) }
%w(comment like participation).each do |entity|
context "with #{entity}" do
it_behaves_like "it deals correctly with a relayable" do
let(:entity_name) { "#{entity}_entity".to_sym }
let(:klass) { entity.camelize.constantize }
end
end
end
describe "retractions for relayable objects" do
let(:sender) { remote_user_on_pod_b }
%w(
retraction
signed_retraction
relayable_retraction
).each do |retraction_entity_name|
context "with #{retraction_entity_name}" do
context "with comment" do
it_behaves_like "it retracts object" do
# case for to-upstream federation
let(:entity_name) { "#{retraction_entity_name}_entity".to_sym }
let(:target_object) {
FactoryGirl.create(:comment, author: remote_user_on_pod_b.person, post: local_message)
}
end
it_behaves_like "it retracts object" do
# case for to-downsteam federation
let(:entity_name) { "#{retraction_entity_name}_entity".to_sym }
let(:target_object) {
FactoryGirl.create(:comment, author: remote_user_on_pod_c.person, post: remote_message)
}
end
end
context "with like" do
it_behaves_like "it retracts object" do
# case for to-upstream federation
let(:entity_name) { "#{retraction_entity_name}_entity".to_sym }
let(:target_object) {
FactoryGirl.create(:like, author: remote_user_on_pod_b.person, target: local_message)
}
end
it_behaves_like "it retracts object" do
# case for to-downsteam federation
let(:entity_name) { "#{retraction_entity_name}_entity".to_sym }
let(:target_object) {
FactoryGirl.create(:like, author: remote_user_on_pod_c.person, target: remote_message)
}
end
end
end
end
end
end
end
end

View file

@ -1,39 +1,93 @@
shared_examples_for "it deals correctly with a relayable" do
it "treats upstream receive correctly" do
Workers::ReceiveEncryptedSalmon.new.perform(@user.id, generate_relayable_local_parent(entity_name))
received_entity = klass.find_by(guid: @entity.guid)
expect(received_entity).not_to be_nil
expect(received_entity.author.diaspora_handle).to eq(@remote_person.diaspora_handle)
context "local" do
let(:entity) {
FactoryGirl.build(
entity_name,
parent_guid: local_message.guid,
diaspora_id: remote_user_on_pod_b.diaspora_handle
)
}
def mock_private_keys
allow(DiasporaFederation.callbacks).to receive(:trigger)
.with(:fetch_private_key_by_diaspora_id,
remote_user_on_pod_b.diaspora_handle)
.and_return(remote_user_on_pod_b.encryption_key)
allow(DiasporaFederation.callbacks).to receive(:trigger)
.with(:fetch_author_private_key_by_entity_guid, "Post", kind_of(String))
.and_return(nil)
end
it "treats upstream receive correctly" do
mock_private_keys
Workers::ReceiveEncryptedSalmon.new.perform(alice.id, generate_xml(entity, remote_user_on_pod_b, alice))
received_entity = klass.find_by(guid: entity.guid)
expect(received_entity).not_to be_nil
expect(received_entity.author.diaspora_handle).to eq(remote_user_on_pod_b.person.diaspora_handle)
end
# Checks when a remote pod wants to send us a relayable without having a key for declared diaspora ID
it "rejects an upstream entity with a malformed author signature" do
allow(remote_user_on_pod_b).to receive(:encryption_key).and_return(OpenSSL::PKey::RSA.new(1024))
mock_private_keys
Workers::ReceiveEncryptedSalmon.new.perform(alice.id, generate_xml(entity, remote_user_on_pod_b, alice))
expect(klass.exists?(guid: entity.guid)).to be(false)
end
end
it "rejects an upstream entity with a malformed author signature" do
Workers::ReceiveEncryptedSalmon.new.perform(
@user.id,
generate_relayable_local_parent_wrong_author_key(entity_name)
)
expect(klass.exists?(guid: @entity.guid)).to be(false)
end
context "remote parent" do
let(:entity) {
FactoryGirl.build(
entity_name,
parent_guid: remote_message.guid,
diaspora_id: remote_user_on_pod_c.diaspora_handle
)
}
it "treats downstream receive correctly" do
Workers::ReceiveEncryptedSalmon.new.perform(@user.id, generate_relayable_remote_parent(entity_name))
received_entity = klass.find_by(guid: @entity.guid)
expect(received_entity).not_to be_nil
expect(received_entity.author.diaspora_handle).to eq(@remote_person2.diaspora_handle)
end
def mock_private_keys
allow(DiasporaFederation.callbacks).to receive(:trigger)
.with(:fetch_private_key_by_diaspora_id,
remote_user_on_pod_c.diaspora_handle)
.and_return(remote_user_on_pod_c.encryption_key)
it "rejects a downstream entity with a malformed author signature" do
Workers::ReceiveEncryptedSalmon.new.perform(
@user.id,
generate_relayable_remote_parent_wrong_author_key(entity_name)
)
expect(klass.exists?(guid: @entity.guid)).to be(false)
end
allow(DiasporaFederation.callbacks).to receive(:trigger)
.with(
:fetch_author_private_key_by_entity_guid,
"Post",
remote_message.guid
)
.and_return(remote_user_on_pod_b.encryption_key)
end
it "declines downstream receive when sender signed with a wrong key" do
Workers::ReceiveEncryptedSalmon.new.perform(
@user.id,
generate_relayable_remote_parent_wrong_parent_key(entity_name)
)
expect(klass.exists?(guid: @entity.guid)).to be(false)
it "treats downstream receive correctly" do
mock_private_keys
Workers::ReceiveEncryptedSalmon.new.perform(alice.id, generate_xml(entity, remote_user_on_pod_b, alice))
received_entity = klass.find_by(guid: entity.guid)
expect(received_entity).not_to be_nil
expect(received_entity.author.diaspora_handle).to eq(remote_user_on_pod_c.diaspora_handle)
end
# Checks when a remote pod B wants to send us a relayable with authorship from a remote pod C user
# without having correct signature from him.
it "rejects a downstream entity with a malformed author signature" do
allow(remote_user_on_pod_c).to receive(:encryption_key).and_return(OpenSSL::PKey::RSA.new(1024))
mock_private_keys
Workers::ReceiveEncryptedSalmon.new.perform(alice.id, generate_xml(entity, remote_user_on_pod_b, alice))
expect(klass.exists?(guid: entity.guid)).to be(false)
end
# Checks when a remote pod C wants to send us a relayable from its user, but bypassing the pod B where
# remote status came from.
it "declines downstream receive when sender signed with a wrong key" do
allow(remote_user_on_pod_b).to receive(:encryption_key).and_return(OpenSSL::PKey::RSA.new(1024))
mock_private_keys
Workers::ReceiveEncryptedSalmon.new.perform(alice.id, generate_xml(entity, remote_user_on_pod_b, alice))
expect(klass.exists?(guid: entity.guid)).to be(false)
end
end
end

View file

@ -1,43 +1,62 @@
shared_examples_for "it retracts non-relayable object" do
it "retracts object by a correct retraction message" do
target_klass = target_object.class.to_s.constantize
Workers::ReceiveEncryptedSalmon.new.perform(@user.id, generate_retraction(entity_name, target_object))
expect(target_klass.exists?(guid: target_object.guid)).to be(false)
def mock_private_keys_for_retraction(entity_name, entity, sender)
if %i(signed_retraction_entity relayable_retraction_entity).include?(entity_name)
allow(DiasporaFederation.callbacks).to receive(:trigger)
.with(:fetch_private_key_by_diaspora_id, sender.diaspora_handle)
.and_return(sender.encryption_key)
end
if entity_name == :relayable_retraction_entity
allow(DiasporaFederation.callbacks).to receive(:trigger)
.with(
:fetch_entity_author_id_by_guid,
entity.target_type,
entity.target_guid
)
.and_return(sender.encryption_key)
end
end
it "doesn't retract object when retraction has wrong signatures" do
target_klass = target_object.class.to_s.constantize
Workers::ReceiveEncryptedSalmon.new.perform(@user.id, generate_forged_retraction(entity_name, target_object))
def generate_retraction(entity_name, target_object, sender)
entity = FactoryGirl.build(
entity_name,
diaspora_id: sender.diaspora_handle,
target_guid: target_object.guid,
target_type: target_object.class.to_s
)
expect(target_klass.exists?(guid: target_object.guid)).to be(true)
mock_private_keys_for_retraction(entity_name, entity, sender)
generate_xml(entity, sender, alice)
end
shared_examples_for "it retracts non-relayable object" do
it_behaves_like "it retracts object" do
let(:sender) { remote_user_on_pod_b }
end
it "doesn't retract object when sender is different from target object" do
target_klass = target_object.class.to_s.constantize
Workers::ReceiveEncryptedSalmon.new.perform(
@user.id,
generate_retraction(entity_name, target_object, @remote_user2)
alice.id,
generate_retraction(entity_name, target_object, remote_user_on_pod_c)
)
expect(target_klass.exists?(guid: target_object.guid)).to be(true)
end
end
shared_examples_for "it retracts relayable object" do
shared_examples_for "it retracts object" do
it "retracts object by a correct message" do
target_klass = target_object.class.to_s.constantize
Workers::ReceiveEncryptedSalmon.new.perform(@user.id, generate_retraction(entity_name, target_object, sender))
Workers::ReceiveEncryptedSalmon.new.perform(alice.id, generate_retraction(entity_name, target_object, sender))
expect(target_klass.exists?(guid: target_object.guid)).to be(false)
end
it "doesn't retract object when retraction has wrong signatures" do
target_klass = target_object.class.to_s.constantize
Workers::ReceiveEncryptedSalmon.new.perform(
@user.id,
generate_forged_retraction(entity_name, target_object, sender)
)
allow(sender).to receive(:encryption_key).and_return(OpenSSL::PKey::RSA.new(1024))
Workers::ReceiveEncryptedSalmon.new.perform(alice.id, generate_retraction(entity_name, target_object, sender))
expect(target_klass.exists?(guid: target_object.guid)).to be(true)
end