From b39582f754946b87c8144193f6c28b60908d63dd Mon Sep 17 00:00:00 2001 From: Benjamin Neff Date: Sun, 17 Jan 2016 21:14:49 +0100 Subject: [PATCH] write tests for Sender and HydraWrapper --- .../federation/sender/hydra_wrapper_spec.rb | 79 +++++++++++++++++++ .../federation/sender_spec.rb | 62 +++++++++++++++ spec/lib/diaspora_federation_spec.rb | 23 ++++++ 3 files changed, 164 insertions(+) create mode 100644 spec/lib/diaspora_federation/federation/sender/hydra_wrapper_spec.rb create mode 100644 spec/lib/diaspora_federation/federation/sender_spec.rb diff --git a/spec/lib/diaspora_federation/federation/sender/hydra_wrapper_spec.rb b/spec/lib/diaspora_federation/federation/sender/hydra_wrapper_spec.rb new file mode 100644 index 0000000..249d5bb --- /dev/null +++ b/spec/lib/diaspora_federation/federation/sender/hydra_wrapper_spec.rb @@ -0,0 +1,79 @@ +module DiasporaFederation + describe Federation::Sender::HydraWrapper do + let(:sender_id) { FactoryGirl.generate(:diaspora_id) } + let(:obj_str) { "status_message@guid" } + let(:xml) { "post" } + let(:url) { "http://example.org/receive/public" } + let(:url2) { "http://example.com/receive/public" } + + let(:hydra) { Typhoeus::Hydra.new } + let(:hydra_wrapper) { Federation::Sender::HydraWrapper.new(sender_id, obj_str) } + + before do + allow(Typhoeus::Hydra).to receive(:new).and_return(hydra) + end + + describe "#insert_job" do + it "queues a request to hydra" do + expect(hydra).to receive(:queue).with(kind_of(Typhoeus::Request)) + expect(Typhoeus::Request).to receive(:new) + .with(url, Federation::Sender::HydraWrapper.hydra_opts.merge(body: {xml: xml})) + .and_call_original + + hydra_wrapper.insert_job(url, xml) + end + + it "queues multiple requests to hydra" do + expect(hydra).to receive(:queue).twice.with(kind_of(Typhoeus::Request)) + + hydra_wrapper.insert_job(url, xml) + hydra_wrapper.insert_job(url2, xml) + end + end + + describe "#send" do + let(:response) { + Typhoeus::Response.new( + code: 200, + body: "", + time: 0.2, + effective_url: url.sub("http://", "https://"), + return_code: :ok + ) + } + let(:error_response) { + Typhoeus::Response.new( + code: 0, + body: "", + time: 0.2, + effective_url: url2, + return_code: :couldnt_resolve_host + ) + } + + before do + Typhoeus.stub(url).and_return(response) + Typhoeus.stub(url2).and_return(error_response) + hydra_wrapper.insert_job(url, xml) + hydra_wrapper.insert_job(url2, xml) + end + before :all do + WebMock::HttpLibAdapters::TyphoeusAdapter.disable! + end + after :all do + WebMock::HttpLibAdapters::TyphoeusAdapter.enable! + end + + it "returns all failed urls" do + expect(hydra_wrapper.send).to eq([url2]) + end + + it "calls the update_pod callback for all responses with effective_url and success-status" do + expect(DiasporaFederation.callbacks).to receive(:trigger).with(:update_pod, "https://example.org/", true) + expect(DiasporaFederation.callbacks).to receive(:trigger).with(:update_pod, "http://example.com/", false) + + hydra_wrapper.send + end + end + end +end diff --git a/spec/lib/diaspora_federation/federation/sender_spec.rb b/spec/lib/diaspora_federation/federation/sender_spec.rb new file mode 100644 index 0000000..9cb59cb --- /dev/null +++ b/spec/lib/diaspora_federation/federation/sender_spec.rb @@ -0,0 +1,62 @@ +module DiasporaFederation + describe Federation::Sender do + let(:sender_id) { FactoryGirl.generate(:diaspora_id) } + let(:obj_str) { "status_message@guid" } + let(:hydra_wrapper) { double } + + before do + expect(Federation::Sender::HydraWrapper).to receive(:new).with(sender_id, obj_str).and_return(hydra_wrapper) + end + + describe ".public" do + let(:xml) { "post" } + let(:urls) { ["https://example.org/receive/public", "https://example.com/receive/public"] } + + before do + expect(hydra_wrapper).to receive(:insert_job).with(urls.at(0), xml) + expect(hydra_wrapper).to receive(:insert_job).with(urls.at(1), xml) + end + + it "returns empty array if send was successful" do + expect(hydra_wrapper).to receive(:send).and_return([]) + + expect(Federation::Sender.public(sender_id, obj_str, urls, xml)).to eq([]) + end + + it "returns failing urls array if send was not successful" do + failing_urls = ["https://example.com/receive/public"] + expect(hydra_wrapper).to receive(:send).and_return(failing_urls) + + expect(Federation::Sender.public(sender_id, obj_str, urls, xml)).to eq(failing_urls) + end + end + + describe ".private" do + let(:targets) { + { + "https://example.org/receive/user/guid" => "post", + "https://example.com/receive/user/guid" => "post2" + } + } + + before do + targets.each do |url, xml| + expect(hydra_wrapper).to receive(:insert_job).with(url, xml) + end + end + + it "returns empty array if send was successful" do + expect(hydra_wrapper).to receive(:send).and_return([]) + + expect(Federation::Sender.private(sender_id, obj_str, targets)).to eq({}) + end + + it "returns failing urls array if send was not successful" do + expect(hydra_wrapper).to receive(:send).and_return(["https://example.com/receive/user/guid"]) + + expect(Federation::Sender.private(sender_id, obj_str, targets)) + .to eq("https://example.com/receive/user/guid" => "post2") + end + end + end +end diff --git a/spec/lib/diaspora_federation_spec.rb b/spec/lib/diaspora_federation_spec.rb index 9e48636..3a5b4f0 100644 --- a/spec/lib/diaspora_federation_spec.rb +++ b/spec/lib/diaspora_federation_spec.rb @@ -45,6 +45,29 @@ module DiasporaFederation end end + context "http configs" do + it "should fail if the http_concurrency is not a number" do + DiasporaFederation.http_concurrency = nil + expect { DiasporaFederation.validate_config }.to raise_error ConfigurationError, + "http_concurrency: please configure a number" + DiasporaFederation.http_concurrency = 20 + end + + it "should fail if the http_timeout is not a number" do + DiasporaFederation.http_timeout = nil + expect { DiasporaFederation.validate_config }.to raise_error ConfigurationError, + "http_timeout: please configure a number" + DiasporaFederation.http_timeout = 30 + end + + it "should fail if the http_verbose is not a boolean" do + DiasporaFederation.http_verbose = nil + expect { DiasporaFederation.validate_config }.to raise_error ConfigurationError, + "http_verbose: please configure a boolean" + DiasporaFederation.http_verbose = false + end + end + it "should validate the callbacks" do expect(DiasporaFederation.callbacks).to receive(:definition_complete?).and_return(false) expect { DiasporaFederation.validate_config }.to raise_error ConfigurationError, "Missing handlers for "