write tests for Sender and HydraWrapper

This commit is contained in:
Benjamin Neff 2016-01-17 21:14:49 +01:00
parent 2d4f9a60d5
commit b39582f754
3 changed files with 164 additions and 0 deletions

View file

@ -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) { "<xml>post</xml>" }
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

View file

@ -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) { "<xml>post</xml>" }
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" => "<xml>post</xml>",
"https://example.com/receive/user/guid" => "<xml>post2</xml>"
}
}
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" => "<xml>post2</xml>")
end
end
end
end

View file

@ -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 "