summaryrefslogtreecommitdiff
path: root/spec/workers/jira_connect/retry_request_worker_spec.rb
blob: 7a93e5fe41d6c2710fa7cc5f414ed7daf50461c5 (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe JiraConnect::RetryRequestWorker do
  describe '#perform' do
    let(:jwt) { 'some-jwt' }
    let(:event_url) { 'https://example.com/somewhere' }
    let(:attempts) { 3 }

    subject(:perform) { described_class.new.perform(event_url, jwt, attempts) }

    it 'sends the request, with the appropriate headers' do
      expect(JiraConnect::RetryRequestWorker).not_to receive(:perform_in)

      stub_request(:post, event_url)

      perform

      expect(WebMock).to have_requested(:post, event_url).with(headers: { 'Authorization' => 'JWT some-jwt' })
    end

    context 'when the proxied request fails' do
      before do
        stub_request(:post, event_url).to_return(status: 500, body: '', headers: {})
      end

      it 'arranges to retry the request' do
        expect(JiraConnect::RetryRequestWorker).to receive(:perform_in).with(1.hour, event_url, jwt, attempts - 1)

        perform
      end

      context 'when there are no more attempts left' do
        let(:attempts) { 0 }

        it 'does not retry' do
          expect(JiraConnect::RetryRequestWorker).not_to receive(:perform_in)

          perform
        end
      end
    end
  end
end