# Copyright (c) 2010-2011, Diaspora Inc. This file is # licensed under the Affero General Public License version 3 or later. See # the COPYRIGHT file. require 'hydra_wrapper' describe HydraWrapper do before do @wrapper = HydraWrapper.new(stub, [stub, stub, stub], "", stub) end describe 'initialize' do it 'it sets the proper instance variables' do user = "user" people = ["person"] encoded_object_xml = "encoded xml" dispatcher_class = "Postzord::Dispatcher::Private" wrapper = HydraWrapper.new(user, people, encoded_object_xml, dispatcher_class) wrapper.user.should == user wrapper.people.should == people wrapper.encoded_object_xml.should == encoded_object_xml end end describe '#run' do it 'delegates #run to the @hydra' do @wrapper.hydra = stub.as_null_object @wrapper.hydra.should_receive(:run) @wrapper.run end end describe '#salmon' do it 'calls the salmon method on the dispatcher class (and memoizes)' do Base64.stub(:decode64).and_return(@wrapper.encoded_object_xml + 'decoded') decoded = Base64.decode64(@wrapper.encoded_object_xml) @wrapper.dispatcher_class.should_receive(:salmon).with(@wrapper.user, decoded).once.and_return(true) @wrapper.salmon @wrapper.salmon end end describe '#grouped_people' do it 'groups people given their receive_urls' do @wrapper.people.each do |person| @wrapper.dispatcher_class.should_receive(:receive_url_for).with(person).and_return("foo.com") end @wrapper.grouped_people.should == {"foo.com" => @wrapper.people} end end describe '#enqueue_batch' do it 'calls #grouped_people' do @wrapper.should_receive(:grouped_people).and_return([]) @wrapper.enqueue_batch end it 'inserts a job for every group of people' do Base64.stub(:decode64) @wrapper.dispatcher_class = stub(:salmon => stub(:xml_for => "")) @wrapper.stub(:grouped_people).and_return({'https://foo.com' => @wrapper.people}) @wrapper.people.should_receive(:first).once @wrapper.should_receive(:insert_job).with('https://foo.com', "", @wrapper.people).once @wrapper.enqueue_batch end it 'does not insert a job for a person whos xml returns false' do Base64.stub(:decode64) @wrapper.stub(:grouped_people).and_return({'https://foo.com' => [stub]}) @wrapper.dispatcher_class = stub(:salmon => stub(:xml_for => false)) @wrapper.should_not_receive(:insert_job) @wrapper.enqueue_batch end end describe '#insert_job' do it 'creates a new request object' it 'calls #prepare_request! on a new request object' it 'adds request to the hydra queue' end describe '#prepare_request!' do it 'calls Pod.find_or_create_by_url' it 'calls Person.url_batch_update' end describe '#redirecting_to_https?!' do it 'does not execute unless response has a 3xx code' do resp = stub(:code => 200) @wrapper.redirecting_to_https?(resp).should be_false end it "returns true if just the protocol is different" do host = "the-same.com/" resp = stub(:request => stub(:url => "http://#{host}"), :code => 302, :headers_hash => {'Location' => "https://#{host}"}) @wrapper.redirecting_to_https?(resp).should be_true end it "returns false if not just the protocol is different" do host = "the-same.com/" resp = stub(:request => stub(:url => "http://#{host}"), :code => 302, :headers_hash => {'Location' => "https://not-the-same/"}) @wrapper.redirecting_to_https?(resp).should be_false end end end