From 5a9ba266b49f8e79415c568ccb168c03d56c0361 Mon Sep 17 00:00:00 2001 From: Benjamin Neff Date: Sat, 7 May 2016 03:12:33 +0200 Subject: [PATCH] add ParentNotFound error, if parent can't be fetched --- lib/diaspora_federation/entities/relayable.rb | 5 +++++ .../entities/relayable_spec.rb | 17 +++++++++++++++-- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/lib/diaspora_federation/entities/relayable.rb b/lib/diaspora_federation/entities/relayable.rb index 4967380..4687ed3 100644 --- a/lib/diaspora_federation/entities/relayable.rb +++ b/lib/diaspora_federation/entities/relayable.rb @@ -216,6 +216,7 @@ module DiasporaFederation # fetch and receive parent from remote, if not available locally Federation::Fetcher.fetch_public(data[:author], type, guid) data[:parent] = DiasporaFederation.callbacks.trigger(:fetch_related_entity, type, guid) + raise ParentNotFound unless data[:parent] end end end @@ -231,6 +232,10 @@ module DiasporaFederation # Raised, if verify_signatures fails to verify signatures (signatures are wrong) class SignatureVerificationFailed < RuntimeError end + + # Raised, if parent was not found and can't be fetched + class ParentNotFound < RuntimeError + end end end end diff --git a/spec/lib/diaspora_federation/entities/relayable_spec.rb b/spec/lib/diaspora_federation/entities/relayable_spec.rb index 730982c..8a3586b 100644 --- a/spec/lib/diaspora_federation/entities/relayable_spec.rb +++ b/spec/lib/diaspora_federation/entities/relayable_spec.rb @@ -297,8 +297,6 @@ XML context "fetch parent" do before do - expect_callback(:fetch_public_key, author).and_return(author_pkey.public_key) - expect_callback(:fetch_public_key, remote_parent.author).and_return(parent_pkey.public_key) expect_callback(:fetch_private_key, author).and_return(author_pkey) expect_callback(:fetch_private_key, remote_parent.author).and_return(parent_pkey) end @@ -306,6 +304,9 @@ XML let(:xml) { SomeRelayable.new(hash).to_xml } it "fetches the parent from the backend" do + expect_callback(:fetch_public_key, author).and_return(author_pkey.public_key) + expect_callback(:fetch_public_key, remote_parent.author).and_return(parent_pkey.public_key) + expect_callback(:fetch_related_entity, "Parent", parent_guid).and_return(remote_parent) expect(Federation::Fetcher).not_to receive(:fetch_public) @@ -315,6 +316,9 @@ XML end it "fetches the parent from remote if not found on backend" do + expect_callback(:fetch_public_key, author).and_return(author_pkey.public_key) + expect_callback(:fetch_public_key, remote_parent.author).and_return(parent_pkey.public_key) + expect_callback(:fetch_related_entity, "Parent", parent_guid).and_return(nil, remote_parent) expect(Federation::Fetcher).to receive(:fetch_public).with(author, "Parent", parent_guid) @@ -322,6 +326,15 @@ XML expect(entity.parent).to eq(remote_parent) end + + it "raises if the parent can't be fetched from remote" do + expect_callback(:fetch_related_entity, "Parent", parent_guid).exactly(2).times.and_return(nil) + expect(Federation::Fetcher).to receive(:fetch_public).with(author, "Parent", parent_guid) + + expect { + SomeRelayable.from_xml(xml) + }.to raise_error DiasporaFederation::Entities::Relayable::ParentNotFound + end end describe "#sender_valid?" do