summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/bulk_import/client_spec.rb
blob: a6f8dd6d194aa85f8e3592883181314f5e8430fb (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Gitlab::BulkImport::Client do
  include ImportSpecHelper

  let(:uri) { 'http://gitlab.example' }
  let(:token) { 'token' }
  let(:resource) { 'resource' }

  subject { described_class.new(uri: uri, token: token) }

  describe '#get' do
    let(:response_double) { double(code: 200, success?: true, parsed_response: {}) }

    shared_examples 'performs network request' do
      it 'performs network request' do
        expect(Gitlab::HTTP).to receive(:get).with(*expected_args).and_return(response_double)

        subject.get(resource)
      end
    end

    describe 'parsed response' do
      it 'returns parsed response' do
        response_double = double(code: 200, success?: true, parsed_response: [{ id: 1 }, { id: 2 }])

        allow(Gitlab::HTTP).to receive(:get).and_return(response_double)

        expect(subject.get(resource)).to eq(response_double.parsed_response)
      end
    end

    describe 'request query' do
      include_examples 'performs network request' do
        let(:expected_args) do
          [
            anything,
            hash_including(
              query: {
                page: described_class::DEFAULT_PAGE,
                per_page: described_class::DEFAULT_PER_PAGE
              }
            )
          ]
        end
      end
    end

    describe 'request headers' do
      include_examples 'performs network request' do
        let(:expected_args) do
          [
            anything,
            hash_including(
              headers: {
                'Content-Type' => 'application/json',
                'Authorization' => "Bearer #{token}"
              }
            )
          ]
        end
      end
    end

    describe 'request uri' do
      include_examples 'performs network request' do
        let(:expected_args) do
          ['http://gitlab.example:80/api/v4/resource', anything]
        end
      end
    end

    context 'error handling' do
      context 'when error occurred' do
        it 'raises ConnectionError' do
          allow(Gitlab::HTTP).to receive(:get).and_raise(Errno::ECONNREFUSED)

          expect { subject.get(resource) }.to raise_exception(described_class::ConnectionError)
        end
      end

      context 'when response is not success' do
        it 'raises ConnectionError' do
          response_double = double(code: 503, success?: false)

          allow(Gitlab::HTTP).to receive(:get).and_return(response_double)

          expect { subject.get(resource) }.to raise_exception(described_class::ConnectionError)
        end
      end
    end
  end
end