http_multi_spec.rb 4.49 KB
Newer Older
maxwell's avatar
maxwell committed
1 2
require 'spec_helper'

Jonne Haß's avatar
Jonne Haß committed
3
describe Workers::HttpMulti do
4
  before :all do
5 6
    WebMock.disable_net_connect! allow_localhost: true
    WebMock::HttpLibAdapters::TyphoeusAdapter.disable!
7 8 9 10
    enable_typhoeus
  end
  after :all do
    disable_typhoeus
11
    WebMock.disable_net_connect!
12
  end
13

maxwell's avatar
maxwell committed
14
  before do
Jonne Haß's avatar
Jonne Haß committed
15
    @people = [FactoryGirl.create(:person), FactoryGirl.create(:person)]
16
    @post_xml = Base64.encode64 "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAH"
17

maxwell's avatar
maxwell committed
18
    @hydra = Typhoeus::Hydra.new
19
    allow(Typhoeus::Hydra).to receive(:new).and_return(@hydra)
20
    @salmon = Salmon::EncryptedSlap.create_by_user_and_activity bob, Base64.decode64(@post_xml)
21
    allow(Salmon::EncryptedSlap).to receive(:create_by_user_and_activity).and_return @salmon
22
    @body = "encrypted things"
23
    allow(@salmon).to receive(:xml_for).and_return @body
24 25 26 27 28

    @response = Typhoeus::Response.new(
      code: 200,
      body: "",
      time: 0.2,
29 30
      effective_url: 'http://foobar.com',
      return_code: :ok
31 32 33 34 35
    )
    @failed_response = Typhoeus::Response.new(
      code: 504,
      body: "",
      time: 0.2,
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
      effective_url: 'http://foobar.com',
      return_code: :ok
    )
    @ssl_error_response = Typhoeus::Response.new(
      code: 0,
      body: "",
      time: 0.2,
      effective_url: 'http://foobar.com',
      return_code: :ssl_connect_error
    )
    @unable_to_resolve_response = Typhoeus::Response.new(
      code: 0,
      body: "",
      time: 0.2,
      effective_url: 'http://foobar.com',
      return_code: :couldnt_resolve_host
52
    )
maxwell's avatar
maxwell committed
53 54
  end

55 56
  it 'POSTs to more than one person' do
    @people.each do |person|
57
      Typhoeus.stub(person.receive_url).and_return @response
maxwell's avatar
maxwell committed
58 59
    end

60 61
    expect(@hydra).to receive(:queue).twice
    expect(@hydra).to receive(:run).once
maxwell's avatar
maxwell committed
62

63
    Workers::HttpMulti.new.perform bob.id, @post_xml, @people.map(&:id), "Postzord::Dispatcher::Private"
64 65 66
  end

  it 'retries' do
67
    person = @people.first
68

69
    Typhoeus.stub(person.receive_url).and_return @failed_response
70

71
    expect(Workers::HttpMulti).to receive(:perform_in).with(1.hour, bob.id, @post_xml, [person.id], anything, 1).once
72
    Workers::HttpMulti.new.perform bob.id, @post_xml, [person.id], "Postzord::Dispatcher::Private"
73 74
  end

75 76 77 78 79
  it 'retries if it could not resolve the server' do
    person = @people.first

    Typhoeus.stub(person.receive_url).and_return @unable_to_resolve_response

80
    expect(Workers::HttpMulti).to receive(:perform_in).with(1.hour, bob.id, @post_xml, [person.id], anything, 1).once
81 82 83 84 85 86 87 88
    Workers::HttpMulti.new.perform bob.id, @post_xml, [person.id], "Postzord::Dispatcher::Private"
  end

  it 'does not retry on an SSL error' do
    person = @people.first

    Typhoeus.stub(person.receive_url).and_return @ssl_error_response

89
    expect(Workers::HttpMulti).not_to receive(:perform_in)
90 91 92
    Workers::HttpMulti.new.perform bob.id, @post_xml, [person.id], "Postzord::Dispatcher::Private"
  end

93
  it 'max retries' do
94
    person = @people.first
95

96
    Typhoeus.stub(person.receive_url).and_return @failed_response
97

98
    expect(Workers::HttpMulti).not_to receive :perform_in
99
    Workers::HttpMulti.new.perform bob.id, @post_xml, [person.id], "Postzord::Dispatcher::Private", 3
maxwell's avatar
maxwell committed
100 101
  end

102
  it 'generates encrypted xml for people' do
103
    person = @people.first
104

105
    Typhoeus.stub(person.receive_url).and_return @response
106
    expect(@salmon).to receive(:xml_for).and_return @body
107

108
    Workers::HttpMulti.new.perform bob.id, @post_xml, [person.id], "Postzord::Dispatcher::Private"
maxwell's avatar
maxwell committed
109
  end
110 111 112

  it 'updates http users who have moved to https' do
    person = @people.first
Benjamin Neff's avatar
Benjamin Neff committed
113
    person.update_url("http://remote.net/")
114

115 116 117 118 119 120 121 122
    response = Typhoeus::Response.new(
      code: 301,
      effective_url: 'https://foobar.com',
      response_headers: "Location: #{person.receive_url.sub('http://', 'https://')}",
      body: "",
      time: 0.2
    )
    Typhoeus.stub(person.receive_url).and_return response
123

124
    Workers::HttpMulti.new.perform bob.id, @post_xml, [person.id], "Postzord::Dispatcher::Private"
Benjamin Neff's avatar
Benjamin Neff committed
125
    expect(Person.find(person.id).url).to eq("https://remote.net/")
126
  end
127 128

  it 'only sends to users with valid RSA keys' do
129
    person = @people.first
130 131 132
    person.serialized_public_key = "-----BEGIN RSA PUBLIC KEY-----\nPsych!\n-----END RSA PUBLIC KEY-----"
    person.save

133
    allow(@salmon).to receive(:xml_for).and_call_original
134 135 136

    Typhoeus.stub(person.receive_url).and_return @response
    Typhoeus.stub(@people[1].receive_url).and_return @response
137

138
    expect(@hydra).to receive(:queue).once
139
    Workers::HttpMulti.new.perform bob.id, @post_xml, @people.map(&:id), "Postzord::Dispatcher::Private"
140
  end
maxwell's avatar
maxwell committed
141
end