diaspora/spec/workers/http_multi_spec.rb
Jonne Haß 79a79d65d6 Bye Resque. Ohai Sidekiq.
* Dropped all references to Resque
* Moved all jobs under app/workers since that's the Sidekiq convention
* Renamed Jobs module to Worker to match new location
* Adapted all jobs to Sidekiq
* Replaced all enqueue calls with perform_async
* Dropped Resque hacks from specs and features, replaced with
  sidekig/testing in RSpec and sidekig/testing/inline in Cucumber
* Updated scripts to start a Sidekiq server
* Inline Sidekiq sinatra app
* Let Sidekiq create the actual Redis instance
* Workaround already initialized constant warnings in service models
* Resolved ToDo in one job definition by creating proper exception clases
  for some errors in receiving posts
* Added sidekiq section to configuration to make it completly
  configurable to the user
* Add Sidekiq middleware for clean backtraces
* Delay HttpMulti retry to give offline pods a chance to come back up
* Do not retry on GUID already taken and alike errors
* Be graceful about deleted posts in GatherOEmbedData
2013-03-21 23:39:07 +01:00

99 lines
3.6 KiB
Ruby

require 'spec_helper'
describe Workers::HttpMulti do
before :all do
WebMock.disable_net_connect!(:allow_localhost => true)
enable_typhoeus
end
after :all do
disable_typhoeus
WebMock.disable_net_connect!
end
before do
@people = [FactoryGirl.create(:person), FactoryGirl.create(:person)]
@post_xml = Base64.encode64("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAH")
@hydra = Typhoeus::Hydra.new
@response = Typhoeus::Response.new(:code => 200, :headers => "", :body => "", :time => 0.2, :effective_url => 'http://foobar.com')
@failed_response = Typhoeus::Response.new(:code => 504, :headers => "", :body => "", :time => 0.2, :effective_url => 'http://foobar.com')
end
it 'POSTs to more than one person' do
@people.each do |person|
@hydra.stub(:post, person.receive_url).and_return(@response)
end
@hydra.should_receive(:queue).twice
@hydra.should_receive(:run).once
Typhoeus::Hydra.stub!(:new).and_return(@hydra)
people_ids = @people.map{ |p| p.id }
Workers::HttpMulti.new.perform(bob.id, @post_xml, people_ids, "Postzord::Dispatcher::Private")
end
it 'retries' do
person = @people[0]
@hydra.stub(:post, person.receive_url).and_return(@failed_response)
Typhoeus::Hydra.stub!(:new).and_return(@hydra)
Workers::HttpMulti.should_receive(:perform_in).with(1.hour, bob.id, @post_xml, [person.id], anything, 1).once
Workers::HttpMulti.new.perform(bob.id, @post_xml, [person.id], "Postzord::Dispatcher::Private")
end
it 'max retries' do
person = @people[0]
@hydra.stub(:post, person.receive_url).and_return(@failed_response)
Typhoeus::Hydra.stub!(:new).and_return(@hydra)
Workers::HttpMulti.should_not_receive(:perform_in)
Workers::HttpMulti.new.perform(bob.id, @post_xml, [person.id], "Postzord::Dispatcher::Private", 3)
end
it 'generates encrypted xml for people' do
person = @people[0]
@hydra.stub(:post, person.receive_url).and_return(@response)
Typhoeus::Hydra.stub!(:new).and_return(@hydra)
salmon = Salmon::EncryptedSlap.create_by_user_and_activity(bob, Base64.decode64(@post_xml))
Salmon::EncryptedSlap.stub(:create_by_user_and_activity).and_return(salmon)
salmon.should_receive(:xml_for).and_return("encrypted things")
Workers::HttpMulti.new.perform(bob.id, @post_xml, [person.id], "Postzord::Dispatcher::Private")
end
it 'updates http users who have moved to https' do
person = @people.first
person.url = 'http://remote.net/'
person.save
stub_request(:post, person.receive_url).to_return(:status=>200, :body=>"", :headers=>{})
response = Typhoeus::Response.new(:code => 301,:effective_url => 'https://foobar.com', :headers_hash => {"Location" => person.receive_url.sub('http://', 'https://')}, :body => "", :time => 0.2)
@hydra.stub(:post, person.receive_url).and_return(response)
Typhoeus::Hydra.stub!(:new).and_return(@hydra)
Workers::HttpMulti.new.perform(bob.id, @post_xml, [person.id], "Postzord::Dispatcher::Private")
person.reload
person.url.should == "https://remote.net/"
end
it 'only sends to users with valid RSA keys' do
person = @people[0]
person.serialized_public_key = "-----BEGIN RSA PUBLIC KEY-----\nPsych!\n-----END RSA PUBLIC KEY-----"
person.save
@hydra.stub(:post, @people[0].receive_url).and_return(@response)
@hydra.stub(:post, @people[1].receive_url).and_return(@response)
Typhoeus::Hydra.stub!(:new).and_return(@hydra)
@hydra.should_receive(:queue).once
Workers::HttpMulti.new.perform(bob.id, @post_xml, [@people[0].id, @people[1].id], "Postzord::Dispatcher::Private")
end
end