summaryrefslogtreecommitdiff
path: root/spec/models/bulk_imports/export_status_spec.rb
blob: f945ad122440033104205fa813381f71393341d3 (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe BulkImports::ExportStatus do
  let_it_be(:relation) { 'labels' }
  let_it_be(:import) { create(:bulk_import) }
  let_it_be(:config) { create(:bulk_import_configuration, bulk_import: import) }
  let_it_be(:entity) { create(:bulk_import_entity, bulk_import: import, source_full_path: 'foo') }
  let_it_be(:tracker) { create(:bulk_import_tracker, entity: entity) }

  let(:response_double) do
    double(parsed_response: [{ 'relation' => 'labels', 'status' => status, 'error' => 'error!' }])
  end

  subject { described_class.new(tracker, relation) }

  before do
    allow_next_instance_of(BulkImports::Clients::HTTP) do |client|
      allow(client).to receive(:get).and_return(response_double)
    end
  end

  describe '#started?' do
    context 'when export status is started' do
      let(:status) { BulkImports::Export::STARTED }

      it 'returns true' do
        expect(subject.started?).to eq(true)
      end
    end

    context 'when export status is not started' do
      let(:status) { BulkImports::Export::FAILED }

      it 'returns false' do
        expect(subject.started?).to eq(false)
      end
    end
  end

  describe '#failed' do
    context 'when export status is failed' do
      let(:status) { BulkImports::Export::FAILED }

      it 'returns true' do
        expect(subject.failed?).to eq(true)
      end
    end

    context 'when export status is not failed' do
      let(:status) { BulkImports::Export::STARTED }

      it 'returns false' do
        expect(subject.failed?).to eq(false)
      end
    end

    context 'when export status is not present' do
      let(:response_double) do
        double(parsed_response: [])
      end

      it 'returns true' do
        expect(subject.failed?).to eq(true)
        expect(subject.error).to eq('Empty export status response')
      end
    end
  end

  describe '#error' do
    let(:status) { BulkImports::Export::FAILED }

    it 'returns error message' do
      expect(subject.error).to eq('error!')
    end

    context 'when something goes wrong during export status fetch' do
      it 'returns exception class as error' do
        allow_next_instance_of(BulkImports::Clients::HTTP) do |client|
          allow(client).to receive(:get).and_raise(StandardError, 'Error!')
        end

        expect(subject.error).to eq('Error!')
      end
    end
  end
end