Welcome to mirror list, hosted at ThFree Co, Russian Federation.

http_multi_spec.rb « workers « spec - github.com/diaspora/diaspora.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 5e4202e475587aee8a8a63659c457de090f79aa8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
require 'spec_helper'

describe Workers::HttpMulti do
  before :all do
    WebMock.disable_net_connect! allow_localhost: true
    WebMock::HttpLibAdapters::TyphoeusAdapter.disable!
    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
    allow(Typhoeus::Hydra).to receive(:new).and_return(@hydra)
    @salmon = Salmon::EncryptedSlap.create_by_user_and_activity bob, Base64.decode64(@post_xml)
    allow(Salmon::EncryptedSlap).to receive(:create_by_user_and_activity).and_return @salmon
    @body = "encrypted things"
    allow(@salmon).to receive(:xml_for).and_return @body

    @response = Typhoeus::Response.new(
      code: 200,
      body: "",
      time: 0.2,
      effective_url: 'http://foobar.com',
      return_code: :ok
    )
    @failed_response = Typhoeus::Response.new(
      code: 504,
      body: "",
      time: 0.2,
      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
    )
  end

  it 'POSTs to more than one person' do
    @people.each do |person|
      Typhoeus.stub(person.receive_url).and_return @response
    end

    expect(@hydra).to receive(:queue).twice
    expect(@hydra).to receive(:run).once

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

  it 'retries' do
    person = @people.first

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

    expect(Workers::HttpMulti).to 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 'retries if it could not resolve the server' do
    person = @people.first

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

    expect(Workers::HttpMulti).to 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 'does not retry on an SSL error' do
    person = @people.first

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

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

  it 'max retries' do
    person = @people.first

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

    expect(Workers::HttpMulti).not_to 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.first

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

    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

    response = Typhoeus::Response.new(
      code:             301,
      effective_url:    "https://example.net",
      response_headers: "Location: #{person.receive_url.sub('http://', 'https://')}",
      body:             "",
      time:             0.2
    )
    Typhoeus.stub(person.receive_url).and_return response

    Workers::HttpMulti.new.perform bob.id, @post_xml, [person.id], "Postzord::Dispatcher::Private"
    expect(Person.find(person.id).url).to eq("https://example.net/")
  end

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

    allow(@salmon).to receive(:xml_for).and_call_original

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

    expect(@hydra).to receive(:queue).once
    Workers::HttpMulti.new.perform bob.id, @post_xml, @people.map(&:id), "Postzord::Dispatcher::Private"
  end
end