summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/phabricator_import/conduit/response_spec.rb
blob: a8596968f14a1911001f949d4010805960483d72 (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
# frozen_string_literal: true
require 'spec_helper'

describe Gitlab::PhabricatorImport::Conduit::Response do
  let(:response) { described_class.new(JSON.parse(fixture_file('phabricator_responses/maniphest.search.json')))}
  let(:error_response) { described_class.new(JSON.parse(fixture_file('phabricator_responses/auth_failed.json'))) }

  describe '.parse!' do
    it 'raises a ResponseError if the http response was not successfull' do
      fake_response = double(:http_response, success?: false, status: 401)

      expect { described_class.parse!(fake_response) }
        .to raise_error(Gitlab::PhabricatorImport::Conduit::ResponseError, /responded with 401/)
    end

    it 'raises a ResponseError if the response contained a Phabricator error' do
      fake_response = double(:http_response,
                             success?: true,
                             status: 200,
                             body: fixture_file('phabricator_responses/auth_failed.json'))

      expect { described_class.parse!(fake_response) }
        .to raise_error(Gitlab::PhabricatorImport::Conduit::ResponseError, /ERR-INVALID-AUTH: API token/)
    end

    it 'raises a ResponseError if JSON parsing failed' do
      fake_response = double(:http_response,
                       success?: true,
                       status: 200,
                       body: 'This is no JSON')

      expect { described_class.parse!(fake_response) }
        .to raise_error(Gitlab::PhabricatorImport::Conduit::ResponseError, /unexpected token at/)
    end

    it 'returns a parsed response for valid input' do
      fake_response = double(:http_response,
                       success?: true,
                       status: 200,
                       body: fixture_file('phabricator_responses/maniphest.search.json'))

      expect(described_class.parse!(fake_response)).to be_a(described_class)
    end
  end

  describe '#success?' do
    it { expect(response).to be_success }
    it { expect(error_response).not_to be_success }
  end

  describe '#error_code' do
    it { expect(error_response.error_code).to eq('ERR-INVALID-AUTH') }
    it { expect(response.error_code).to be_nil }
  end

  describe '#error_info' do
    it 'returns the correct error info' do
      expected_message = 'API token "api-token" has the wrong length. API tokens should be 32 characters long.'

      expect(error_response.error_info).to eq(expected_message)
    end

    it { expect(response.error_info).to be_nil }
  end

  describe '#data' do
    it { expect(error_response.data).to be_nil }
    it { expect(response.data).to be_an(Array) }
  end

  describe '#pagination' do
    it { expect(error_response.pagination).to be_nil }

    it 'builds the pagination correctly' do
      expect(response.pagination).to be_a(Gitlab::PhabricatorImport::Conduit::Pagination)
      expect(response.pagination.next_page).to eq('284')
    end
  end
end