From a6d7dbf1ddc2d569fe9240e8dfed523d493c5206 Mon Sep 17 00:00:00 2001 From: Benjamin Neff Date: Sun, 30 Apr 2017 16:41:01 +0200 Subject: [PATCH] Send MagicEnvelope as body with correct Content-Type in specs Related to diaspora/diaspora_federation#30 --- .../federation/attack_vectors_spec.rb | 20 ++-- .../federation/federation_helper.rb | 27 +++--- .../receive_federation_messages_spec.rb | 14 +-- .../federation/shared_receive_relayable.rb | 10 +- .../federation/shared_receive_retraction.rb | 24 ++--- .../federation/shared_receive_stream_items.rb | 96 ++++++++----------- .../federation/dispatcher/private_spec.rb | 51 +++++++--- .../federation/dispatcher/public_spec.rb | 29 ++++-- 8 files changed, 149 insertions(+), 122 deletions(-) diff --git a/spec/integration/federation/attack_vectors_spec.rb b/spec/integration/federation/attack_vectors_spec.rb index c5a802647..25fdaecb1 100644 --- a/spec/integration/federation/attack_vectors_spec.rb +++ b/spec/integration/federation/attack_vectors_spec.rb @@ -23,7 +23,7 @@ describe "attack vectors", type: :request do alice.share_with(eve.person, alices_aspect) - post_message(generate_xml(Diaspora::Federation::Entities.post(original_message), bob, alice), alice) + post_message(generate_payload(Diaspora::Federation::Entities.post(original_message), bob, alice), alice) # alice still should not see eves original post, even though bob sent it to her expect(alice.reload.visible_shareables(Post).where(guid: original_message.guid)).to be_blank @@ -34,7 +34,7 @@ describe "attack vectors", type: :request do profile = eve.profile.clone profile.first_name = "Not BOB" - post_message(generate_xml(Diaspora::Federation::Entities.profile(profile), alice, bob), bob) + post_message(generate_payload(Diaspora::Federation::Entities.profile(profile), alice, bob), bob) expect(eve.profile(true).first_name).not_to eq("Not BOB") end @@ -42,7 +42,7 @@ describe "attack vectors", type: :request do it "public post should not be spoofed from another author" do post = FactoryGirl.build(:status_message, public: true, author: eve.person) - post_message(generate_xml(Diaspora::Federation::Entities.post(post), alice)) + post_message(generate_payload(Diaspora::Federation::Entities.post(post), alice)) expect(StatusMessage.exists?(guid: post.guid)).to be_falsey end @@ -52,7 +52,7 @@ describe "attack vectors", type: :request do retraction = Retraction.for(original_message) expect { - post_message(generate_xml(Diaspora::Federation::Entities.retraction(retraction), alice, bob), bob) + post_message(generate_payload(Diaspora::Federation::Entities.retraction(retraction), alice, bob), bob) }.to_not change { bob.visible_shareables(Post).count(:all) } end @@ -62,7 +62,9 @@ describe "attack vectors", type: :request do contact = bob.contacts(true).find_by(person_id: eve.person.id) expect(contact).to be_sharing - post_message(generate_xml(Diaspora::Federation::Entities.retraction(Retraction.for(contact)), alice, bob), bob) + post_message( + generate_payload(Diaspora::Federation::Entities.retraction(Retraction.for(contact)), alice, bob), bob + ) expect(bob.contacts(true).find_by(person_id: eve.person.id)).to be_sharing end @@ -80,7 +82,7 @@ describe "attack vectors", type: :request do author: alice.person ) - post_message(generate_xml(Diaspora::Federation::Entities.post(malicious_message), alice, bob), bob) + post_message(generate_payload(Diaspora::Federation::Entities.post(malicious_message), alice, bob), bob) expect(original_message.reload.author_id).to eq(eve.person.id) end @@ -93,7 +95,7 @@ describe "attack vectors", type: :request do # eve tries to send me another message with the same ID malicious_message = FactoryGirl.build(:status_message, id: original_message.id, text: "BAD!!!", author: eve.person) - post_message(generate_xml(Diaspora::Federation::Entities.post(malicious_message), eve, bob), bob) + post_message(generate_payload(Diaspora::Federation::Entities.post(malicious_message), eve, bob), bob) expect(original_message.reload.text).to eq("store this!") end @@ -109,7 +111,7 @@ describe "attack vectors", type: :request do ) expect { - post_message(generate_xml(retraction, alice, bob), bob) + post_message(generate_payload(retraction, alice, bob), bob) }.to_not change(StatusMessage, :count) end @@ -122,7 +124,7 @@ describe "attack vectors", type: :request do new_message.height = 23 new_message.width = 42 - post_message(generate_xml(Diaspora::Federation::Entities.photo(new_message), alice, bob), bob) + post_message(generate_payload(Diaspora::Federation::Entities.photo(new_message), alice, bob), bob) expect(original_message.reload.text).to eq("store this!") end diff --git a/spec/integration/federation/federation_helper.rb b/spec/integration/federation/federation_helper.rb index f16cd604c..3525c95fe 100644 --- a/spec/integration/federation/federation_helper.rb +++ b/spec/integration/federation/federation_helper.rb @@ -44,30 +44,29 @@ def create_relayable_entity(entity_name, parent, diaspora_id) ) end -def generate_xml(entity, remote_user, recipient=nil) +def generate_payload(entity, remote_user, recipient=nil) + magic_env = DiasporaFederation::Salmon::MagicEnvelope.new( + entity, + remote_user.diaspora_handle + ).envelop(remote_user.encryption_key) + if recipient - DiasporaFederation::Salmon::EncryptedSlap.prepare( - remote_user.diaspora_handle, - remote_user.encryption_key, - entity - ).generate_xml(recipient.encryption_key) + DiasporaFederation::Salmon::EncryptedMagicEnvelope.encrypt(magic_env, recipient.encryption_key) else - DiasporaFederation::Salmon::Slap.generate_xml( - remote_user.diaspora_handle, - remote_user.encryption_key, - entity - ) + magic_env.to_xml end end -def post_message(xml, recipient=nil) +def post_message(payload, recipient=nil) if recipient inlined_jobs do - post "/receive/users/#{recipient.guid}", guid: recipient.guid, xml: xml + headers = {"CONTENT_TYPE" => "application/json"} + post "/receive/users/#{recipient.guid}", payload, headers end else inlined_jobs do - post "/receive/public", xml: xml + headers = {"CONTENT_TYPE" => "application/magic-envelope+xml"} + post "/receive/public", payload, headers end end end diff --git a/spec/integration/federation/receive_federation_messages_spec.rb b/spec/integration/federation/receive_federation_messages_spec.rb index d532b3c71..651c65150 100644 --- a/spec/integration/federation/receive_federation_messages_spec.rb +++ b/spec/integration/federation/receive_federation_messages_spec.rb @@ -15,14 +15,14 @@ describe "Receive federation messages feature" do let(:recipient) { nil } it "receives account deletion correctly" do - post_message(generate_xml(DiasporaFederation::Entities::AccountDeletion.new(diaspora_id: sender_id), sender)) + post_message(generate_payload(DiasporaFederation::Entities::AccountDeletion.new(diaspora_id: sender_id), sender)) expect(AccountDeletion.exists?(diaspora_handle: sender_id)).to be_truthy end it "rejects account deletion with wrong diaspora_id" do delete_id = Fabricate.sequence(:diaspora_id) - post_message(generate_xml(DiasporaFederation::Entities::AccountDeletion.new(diaspora_id: delete_id), sender)) + post_message(generate_payload(DiasporaFederation::Entities::AccountDeletion.new(diaspora_id: delete_id), sender)) expect(AccountDeletion.exists?(diaspora_handle: delete_id)).to be_falsey expect(AccountDeletion.exists?(diaspora_handle: sender_id)).to be_falsey @@ -38,7 +38,7 @@ describe "Receive federation messages feature" do alice, instance_of(Reshare) ).and_return(double(create!: true)) - post_message(generate_xml(reshare, sender)) + post_message(generate_payload(reshare, sender)) expect(Reshare.exists?(root_guid: post.guid)).to be_truthy expect(Reshare.where(root_guid: post.guid).last.diaspora_handle).to eq(sender_id) @@ -49,7 +49,7 @@ describe "Receive federation messages feature" do reshare = Fabricate( :reshare_entity, root_author: alice.diaspora_handle, root_guid: post.guid, author: sender_id) expect { - post_message(generate_xml(reshare, sender)) + post_message(generate_payload(reshare, sender)) }.to raise_error ActiveRecord::RecordInvalid, "Validation failed: Only posts which are public may be reshared." expect(Reshare.exists?(root_guid: post.guid)).to be_falsey @@ -78,7 +78,7 @@ describe "Receive federation messages feature" do expect(Workers::ReceiveLocal).to receive(:perform_async).and_call_original - post_message(generate_xml(entity, sender, alice), alice) + post_message(generate_payload(entity, sender, alice), alice) expect(alice.contacts.count).to eq(2) new_contact = alice.contacts.find {|c| c.person.diaspora_handle == sender_id } @@ -106,7 +106,7 @@ describe "Receive federation messages feature" do it "treats profile receive correctly" do entity = Fabricate(:profile_entity, author: sender_id) - post_message(generate_xml(entity, sender, alice), alice) + post_message(generate_payload(entity, sender, alice), alice) received_profile = sender.profile.reload @@ -120,7 +120,7 @@ describe "Receive federation messages feature" do author: sender_id, participants: "#{sender_id};#{alice.diaspora_handle}" ) - post_message(generate_xml(entity, sender, alice), alice) + post_message(generate_payload(entity, sender, alice), alice) expect(Conversation.exists?(guid: entity.guid)).to be_truthy end diff --git a/spec/integration/federation/shared_receive_relayable.rb b/spec/integration/federation/shared_receive_relayable.rb index 06e67c247..2d0e150ba 100644 --- a/spec/integration/federation/shared_receive_relayable.rb +++ b/spec/integration/federation/shared_receive_relayable.rb @@ -4,7 +4,7 @@ shared_examples_for "it deals correctly with a relayable" do it "treats upstream receive correctly" do expect(Workers::ReceiveLocal).to receive(:perform_async) - post_message(generate_xml(entity, sender, recipient), recipient) + post_message(generate_payload(entity, sender, recipient), recipient) received_entity = klass.find_by(guid: entity.guid) expect(received_entity).not_to be_nil @@ -15,7 +15,7 @@ shared_examples_for "it deals correctly with a relayable" do it "rejects an upstream entity with a malformed author signature" do expect(Workers::ReceiveLocal).not_to receive(:perform_async) allow(remote_user_on_pod_b).to receive(:encryption_key).and_return(OpenSSL::PKey::RSA.new(1024)) - post_message(generate_xml(entity, sender, recipient), recipient) + post_message(generate_payload(entity, sender, recipient), recipient) expect(klass.exists?(guid: entity.guid)).to be_falsey end @@ -28,7 +28,7 @@ shared_examples_for "it deals correctly with a relayable" do it "treats downstream receive correctly" do expect(Workers::ReceiveLocal).to receive(:perform_async) - post_message(generate_xml(entity, sender, recipient), recipient) + post_message(generate_payload(entity, sender, recipient), recipient) received_entity = klass.find_by(guid: entity.guid) expect(received_entity).not_to be_nil @@ -40,7 +40,7 @@ shared_examples_for "it deals correctly with a relayable" do it "rejects a downstream entity with a malformed author signature" do expect(Workers::ReceiveLocal).not_to receive(:perform_async) allow(remote_user_on_pod_c).to receive(:encryption_key).and_return(OpenSSL::PKey::RSA.new(1024)) - post_message(generate_xml(entity, sender, recipient), recipient) + post_message(generate_payload(entity, sender, recipient), recipient) expect(klass.exists?(guid: entity.guid)).to be_falsey end @@ -50,7 +50,7 @@ shared_examples_for "it deals correctly with a relayable" do it "declines downstream receive when sender signed with a wrong key" do expect(Workers::ReceiveLocal).not_to receive(:perform_async) allow(sender).to receive(:encryption_key).and_return(OpenSSL::PKey::RSA.new(1024)) - post_message(generate_xml(entity, sender, recipient), recipient) + post_message(generate_payload(entity, sender, recipient), recipient) expect(klass.exists?(guid: entity.guid)).to be_falsey end diff --git a/spec/integration/federation/shared_receive_retraction.rb b/spec/integration/federation/shared_receive_retraction.rb index b54588524..adc8112c0 100644 --- a/spec/integration/federation/shared_receive_retraction.rb +++ b/spec/integration/federation/shared_receive_retraction.rb @@ -1,6 +1,6 @@ -def retraction_entity(entity_name, target_object, sender) +def retraction_entity(target_object, sender) Fabricate( - entity_name, + :retraction_entity, author: sender.diaspora_handle, target_guid: target_object.guid, target_type: target_object.class.to_s, @@ -10,23 +10,23 @@ end shared_examples_for "it retracts non-relayable object" do it "retracts object by a correct retraction message" do - entity = retraction_entity(entity_name, target_object, sender) - post_message(generate_xml(entity, sender, recipient), recipient) + entity = retraction_entity(target_object, sender) + post_message(generate_payload(entity, sender, recipient), recipient) expect(target_object.class.exists?(guid: target_object.guid)).to be_falsey end it "doesn't retract object when retraction has wrong signatures" do allow(sender).to receive(:encryption_key).and_return(OpenSSL::PKey::RSA.new(1024)) - entity = retraction_entity(entity_name, target_object, sender) - post_message(generate_xml(entity, sender, recipient), recipient) + entity = retraction_entity(target_object, sender) + post_message(generate_payload(entity, sender, recipient), recipient) expect(target_object.class.exists?(guid: target_object.guid)).to be_truthy end it "doesn't retract object when sender is different from target object" do - entity = retraction_entity(entity_name, target_object, remote_user_on_pod_c) - post_message(generate_xml(entity, remote_user_on_pod_c, recipient), recipient) + entity = retraction_entity(target_object, remote_user_on_pod_c) + post_message(generate_payload(entity, remote_user_on_pod_c, recipient), recipient) expect(target_object.class.exists?(guid: target_object.guid)).to be_truthy end @@ -34,16 +34,16 @@ end shared_examples_for "it retracts relayable object" do it "retracts object by a correct message" do - entity = retraction_entity(entity_name, target_object, sender) - post_message(generate_xml(entity, sender, recipient), recipient) + entity = retraction_entity(target_object, sender) + post_message(generate_payload(entity, sender, recipient), recipient) expect(target_object.class.exists?(guid: target_object.guid)).to be_falsey end it "doesn't retract object when retraction has wrong signatures" do allow(sender).to receive(:encryption_key).and_return(OpenSSL::PKey::RSA.new(1024)) - entity = retraction_entity(entity_name, target_object, sender) - post_message(generate_xml(entity, sender, recipient), recipient) + entity = retraction_entity(target_object, sender) + post_message(generate_payload(entity, sender, recipient), recipient) expect(target_object.class.exists?(guid: target_object.guid)).to be_truthy end diff --git a/spec/integration/federation/shared_receive_stream_items.rb b/spec/integration/federation/shared_receive_stream_items.rb index 67d5766b0..e025a138d 100644 --- a/spec/integration/federation/shared_receive_stream_items.rb +++ b/spec/integration/federation/shared_receive_stream_items.rb @@ -6,7 +6,7 @@ shared_examples_for "messages which are indifferent about sharing fact" do it "treats status message receive correctly" do entity = Fabricate(:status_message_entity, author: sender_id, public: public) - post_message(generate_xml(entity, sender, recipient), recipient) + post_message(generate_payload(entity, sender, recipient), recipient) expect(StatusMessage.exists?(guid: entity.guid)).to be_truthy end @@ -15,7 +15,7 @@ shared_examples_for "messages which are indifferent about sharing fact" do allow(sender).to receive(:encryption_key).and_return(OpenSSL::PKey::RSA.new(1024)) entity = Fabricate(:status_message_entity, author: sender_id, public: public) - post_message(generate_xml(entity, sender, recipient), recipient) + post_message(generate_payload(entity, sender, recipient), recipient) expect(StatusMessage.exists?(guid: entity.guid)).to be_falsey end @@ -27,7 +27,7 @@ shared_examples_for "messages which are indifferent about sharing fact" do describe "notifications are sent where required" do it "for comment on local post" do entity = create_relayable_entity(:comment_entity, local_parent, remote_user_on_pod_b.diaspora_handle) - post_message(generate_xml(entity, sender, recipient), recipient) + post_message(generate_payload(entity, sender, recipient), recipient) expect( Notifications::CommentOnPost.exists?( @@ -40,7 +40,7 @@ shared_examples_for "messages which are indifferent about sharing fact" do it "for like on local post" do entity = create_relayable_entity(:like_entity, local_parent, remote_user_on_pod_b.diaspora_handle) - post_message(generate_xml(entity, sender, recipient), recipient) + post_message(generate_payload(entity, sender, recipient), recipient) expect( Notifications::Liked.exists?( @@ -66,7 +66,7 @@ shared_examples_for "messages which are indifferent about sharing fact" do it "treats participation receive correctly" do expect(Workers::ReceiveLocal).to receive(:perform_async) - post_message(generate_xml(entity, sender, recipient), recipient) + post_message(generate_payload(entity, sender, recipient), recipient) received_entity = Participation.find_by(guid: entity.guid) expect(received_entity).not_to be_nil @@ -77,7 +77,7 @@ shared_examples_for "messages which are indifferent about sharing fact" do expect(Workers::ReceiveLocal).not_to receive(:perform_async) entity = create_relayable_entity(:participation_entity, remote_parent, sender_id) - post_message(generate_xml(entity, sender, recipient), recipient) + post_message(generate_payload(entity, sender, recipient), recipient) expect(Participation.exists?(guid: entity.guid)).to be_falsey end @@ -107,17 +107,11 @@ end shared_examples_for "messages which can't be send without sharing" do # retractions shouldn't depend on sharing fact describe "retractions for non-relayable objects" do - %w(retraction signed_retraction).each do |retraction_entity_name| - context "with #{retraction_entity_name}" do - let(:entity_name) { "#{retraction_entity_name}_entity".to_sym } + %w[status_message photo].each do |target| + context "with #{target}" do + let(:target_object) { FactoryGirl.create(target.to_sym, author: remote_user_on_pod_b.person) } - %w(status_message photo).each do |target| - context "with #{target}" do - let(:target_object) { FactoryGirl.create(target.to_sym, author: remote_user_on_pod_b.person) } - - it_behaves_like "it retracts non-relayable object" - end - end + it_behaves_like "it retracts non-relayable object" end end end @@ -133,7 +127,7 @@ shared_examples_for "messages which can't be send without sharing" do alice.participate!(remote_parent) author_id = remote_user_on_pod_c.diaspora_handle entity = create_relayable_entity(:comment_entity, remote_parent, author_id) - post_message(generate_xml(entity, sender, recipient), recipient) + post_message(generate_payload(entity, sender, recipient), recipient) expect( Notifications::AlsoCommented.exists?( @@ -146,47 +140,41 @@ shared_examples_for "messages which can't be send without sharing" do end describe "retractions for relayable objects" do - %w(retraction signed_retraction relayable_retraction).each do |retraction_entity_name| - context "with #{retraction_entity_name}" do - let(:entity_name) { "#{retraction_entity_name}_entity".to_sym } + before do + allow(DiasporaFederation.callbacks).to receive(:trigger).with( + :fetch_private_key, alice.diaspora_handle + ) { alice.encryption_key } + end - before do - allow(DiasporaFederation.callbacks).to receive(:trigger).with( - :fetch_private_key, alice.diaspora_handle - ) { alice.encryption_key } - end + context "with comment" do + it_behaves_like "it retracts relayable object" do + # case for to-upstream federation + let(:target_object) { + FactoryGirl.create(:comment, author: remote_user_on_pod_b.person, post: local_parent) + } + end - context "with comment" do - it_behaves_like "it retracts relayable object" do - # case for to-upstream federation - let(:target_object) { - FactoryGirl.create(:comment, author: remote_user_on_pod_b.person, post: local_parent) - } - end + it_behaves_like "it retracts relayable object" do + # case for to-downsteam federation + let(:target_object) { + FactoryGirl.create(:comment, author: remote_user_on_pod_c.person, post: remote_parent) + } + end + end - it_behaves_like "it retracts relayable object" do - # case for to-downsteam federation - let(:target_object) { - FactoryGirl.create(:comment, author: remote_user_on_pod_c.person, post: remote_parent) - } - end - end + context "with like" do + it_behaves_like "it retracts relayable object" do + # case for to-upstream federation + let(:target_object) { + FactoryGirl.create(:like, author: remote_user_on_pod_b.person, target: local_parent) + } + end - context "with like" do - it_behaves_like "it retracts relayable object" do - # case for to-upstream federation - let(:target_object) { - FactoryGirl.create(:like, author: remote_user_on_pod_b.person, target: local_parent) - } - end - - it_behaves_like "it retracts relayable object" do - # case for to-downsteam federation - let(:target_object) { - FactoryGirl.create(:like, author: remote_user_on_pod_c.person, target: remote_parent) - } - end - end + it_behaves_like "it retracts relayable object" do + # case for to-downsteam federation + let(:target_object) { + FactoryGirl.create(:like, author: remote_user_on_pod_c.person, target: remote_parent) + } end end end diff --git a/spec/lib/diaspora/federation/dispatcher/private_spec.rb b/spec/lib/diaspora/federation/dispatcher/private_spec.rb index 9f20ece51..b1fd867a6 100644 --- a/spec/lib/diaspora/federation/dispatcher/private_spec.rb +++ b/spec/lib/diaspora/federation/dispatcher/private_spec.rb @@ -28,18 +28,29 @@ describe Diaspora::Federation::Dispatcher::Private do end context "deliver to remote user" do - let(:xml) { "" } + let(:encryption_key) { double } + let(:magic_env) { double } + let(:magic_env_xml) { double } + let(:json) { "{\"aes_key\": \"...\", \"encrypted_magic_envelope\": \"...\"}" } + it "queues a private send job" do expect(Workers::SendPrivate).to receive(:perform_async) do |user_id, _entity_string, targets| expect(user_id).to eq(alice.id) expect(targets.size).to eq(1) expect(targets).to have_key(remote_raphael.receive_url) - expect(targets[remote_raphael.receive_url]).to eq(xml) + expect(targets[remote_raphael.receive_url]).to eq(json) end - salmon = double - expect(DiasporaFederation::Salmon::EncryptedSlap).to receive(:prepare).and_return(salmon) - expect(salmon).to receive(:generate_xml).and_return(xml) + expect(alice).to receive(:encryption_key).and_return(encryption_key) + expect(DiasporaFederation::Salmon::MagicEnvelope).to receive(:new).with( + instance_of(DiasporaFederation::Entities::StatusMessage), alice.diaspora_handle + ).and_return(magic_env) + expect(magic_env).to receive(:envelop).with(encryption_key).and_return(magic_env_xml) + expect(DiasporaFederation::Salmon::EncryptedMagicEnvelope).to receive(:encrypt) do |magic_env, public_key| + expect(magic_env).to eq(magic_env_xml) + expect(public_key.to_s).to eq(remote_raphael.public_key.to_s) + json + end Diaspora::Federation::Dispatcher.build(alice, post).dispatch end @@ -60,12 +71,19 @@ describe Diaspora::Federation::Dispatcher::Private do expect(user_id).to eq(alice.id) expect(targets.size).to eq(1) expect(targets).to have_key(remote_person.receive_url) - expect(targets[remote_person.receive_url]).to eq(xml) + expect(targets[remote_person.receive_url]).to eq(json) end - salmon = double - expect(DiasporaFederation::Salmon::EncryptedSlap).to receive(:prepare).and_return(salmon) - expect(salmon).to receive(:generate_xml).and_return(xml) + expect(alice).to receive(:encryption_key).and_return(encryption_key) + expect(DiasporaFederation::Salmon::MagicEnvelope).to receive(:new).with( + instance_of(DiasporaFederation::Entities::StatusMessage), alice.diaspora_handle + ).and_return(magic_env) + expect(magic_env).to receive(:envelop).with(encryption_key).and_return(magic_env_xml) + expect(DiasporaFederation::Salmon::EncryptedMagicEnvelope).to receive(:encrypt) do |magic_env, public_key| + expect(magic_env).to eq(magic_env_xml) + expect(public_key.to_s).to eq(remote_raphael.public_key.to_s) + json + end Diaspora::Federation::Dispatcher.build(alice, post, subscribers: [remote_person]).dispatch end @@ -79,12 +97,19 @@ describe Diaspora::Federation::Dispatcher::Private do expect(user_id).to eq(alice.id) expect(targets.size).to eq(1) expect(targets).to have_key(remote_person.receive_url) - expect(targets[remote_person.receive_url]).to eq(xml) + expect(targets[remote_person.receive_url]).to eq(json) end - salmon = double - expect(DiasporaFederation::Salmon::EncryptedSlap).to receive(:prepare).and_return(salmon) - expect(salmon).to receive(:generate_xml).and_return(xml) + expect(alice).to receive(:encryption_key).and_return(encryption_key) + expect(DiasporaFederation::Salmon::MagicEnvelope).to receive(:new).with( + instance_of(DiasporaFederation::Entities::StatusMessage), alice.diaspora_handle + ).and_return(magic_env) + expect(magic_env).to receive(:envelop).with(encryption_key).and_return(magic_env_xml) + expect(DiasporaFederation::Salmon::EncryptedMagicEnvelope).to receive(:encrypt) do |magic_env, public_key| + expect(magic_env).to eq(magic_env_xml) + expect(public_key.to_s).to eq(remote_raphael.public_key.to_s) + json + end Diaspora::Federation::Dispatcher.build(alice, post, subscribers: [remote_person, offline_person]).dispatch end diff --git a/spec/lib/diaspora/federation/dispatcher/public_spec.rb b/spec/lib/diaspora/federation/dispatcher/public_spec.rb index bb5bac652..f893c7a57 100644 --- a/spec/lib/diaspora/federation/dispatcher/public_spec.rb +++ b/spec/lib/diaspora/federation/dispatcher/public_spec.rb @@ -48,7 +48,9 @@ describe Diaspora::Federation::Dispatcher::Public do end context "deliver to remote user" do - let(:salmon_xml) { "" } + let(:encryption_key) { double } + let(:magic_env) { double } + let(:magic_env_xml) { double(to_xml: "") } it "queues a public send job" do alice.share_with(remote_raphael, alice.aspects.first) @@ -57,10 +59,14 @@ describe Diaspora::Federation::Dispatcher::Public do expect(user_id).to eq(alice.id) expect(urls.size).to eq(1) expect(urls[0]).to eq(remote_raphael.pod.url_to("/receive/public")) - expect(xml).to eq(salmon_xml) + expect(xml).to eq(magic_env_xml.to_xml) end - expect(DiasporaFederation::Salmon::Slap).to receive(:generate_xml).and_return(salmon_xml) + expect(alice).to receive(:encryption_key).and_return(encryption_key) + expect(DiasporaFederation::Salmon::MagicEnvelope).to receive(:new).with( + instance_of(DiasporaFederation::Entities::StatusMessage), alice.diaspora_handle + ).and_return(magic_env) + expect(magic_env).to receive(:envelop).with(encryption_key).and_return(magic_env_xml) Diaspora::Federation::Dispatcher.build(alice, post).dispatch end @@ -76,11 +82,14 @@ describe Diaspora::Federation::Dispatcher::Public do expect(user_id).to eq(alice.id) expect(urls.size).to eq(1) expect(urls[0]).to eq(remote_raphael.pod.url_to("/receive/public")) - expect(xml).to eq(salmon_xml) + expect(xml).to eq(magic_env_xml.to_xml) end - expect(DiasporaFederation::Salmon::Slap).to receive(:generate_xml).and_return(salmon_xml) - + expect(alice).to receive(:encryption_key).and_return(encryption_key) + expect(DiasporaFederation::Salmon::MagicEnvelope).to receive(:new).with( + instance_of(DiasporaFederation::Entities::StatusMessage), alice.diaspora_handle + ).and_return(magic_env) + expect(magic_env).to receive(:envelop).with(encryption_key).and_return(magic_env_xml) Diaspora::Federation::Dispatcher.build(alice, post, subscribers: [remote_raphael]).dispatch end @@ -92,10 +101,14 @@ describe Diaspora::Federation::Dispatcher::Public do expect(user_id).to eq(alice.id) expect(urls.size).to eq(1) expect(urls[0]).to eq(remote_raphael.pod.url_to("/receive/public")) - expect(xml).to eq(salmon_xml) + expect(xml).to eq(magic_env_xml.to_xml) end - expect(DiasporaFederation::Salmon::Slap).to receive(:generate_xml).and_return(salmon_xml) + expect(alice).to receive(:encryption_key).and_return(encryption_key) + expect(DiasporaFederation::Salmon::MagicEnvelope).to receive(:new).with( + instance_of(DiasporaFederation::Entities::StatusMessage), alice.diaspora_handle + ).and_return(magic_env) + expect(magic_env).to receive(:envelop).with(encryption_key).and_return(magic_env_xml) Diaspora::Federation::Dispatcher.build(alice, post, subscribers: [remote_raphael, offline_person]).dispatch end