summaryrefslogtreecommitdiff
path: root/spec/models/integrations/buildkite_spec.rb
blob: 29c649af6c6aa9f70c7aa4d14f7c0c5e09bcbdbb (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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Integrations::Buildkite, :use_clean_rails_memory_store_caching do
  include ReactiveCachingHelpers
  include StubRequests

  let_it_be(:project) { create(:project) }

  subject(:integration) do
    described_class.create!(
      project: project,
      properties: {
        project_url: 'https://buildkite.com/organization-name/example-pipeline',
        token: 'secret-sauce-webhook-token:secret-sauce-status-token'
      }
    )
  end

  it_behaves_like Integrations::BaseCi

  it_behaves_like Integrations::ResetSecretFields

  it_behaves_like Integrations::HasWebHook do
    let(:hook_url) { 'https://webhook.buildkite.com/deliver/{webhook_token}' }
  end

  describe 'Validations' do
    context 'when integration is active' do
      before do
        subject.active = true
      end

      it { is_expected.to validate_presence_of(:project_url) }
      it { is_expected.to validate_presence_of(:token) }

      it_behaves_like 'issue tracker integration URL attribute', :project_url
    end

    context 'when integration is inactive' do
      before do
        subject.active = false
      end

      it { is_expected.not_to validate_presence_of(:project_url) }
      it { is_expected.not_to validate_presence_of(:token) }
    end
  end

  describe '.supported_events' do
    it 'supports push, merge_request, and tag_push events' do
      expect(integration.supported_events).to eq %w(push merge_request tag_push)
    end
  end

  describe 'commits methods' do
    before do
      allow(project).to receive(:default_branch).and_return('default-brancho')
    end

    it 'always activates SSL verification after saved' do
      integration.create_service_hook(enable_ssl_verification: false)

      integration.enable_ssl_verification = false
      integration.active = true

      expect { integration.save! }
        .to change { integration.service_hook.enable_ssl_verification }.from(false).to(true)
    end

    describe '#hook_url' do
      it 'returns the webhook url' do
        expect(integration.hook_url).to eq(
          'https://webhook.buildkite.com/deliver/{webhook_token}'
        )
      end
    end

    describe '#commit_status_path' do
      it 'returns the correct status page' do
        expect(integration.commit_status_path('2ab7834c')).to eq(
          'https://gitlab.buildkite.com/status/secret-sauce-status-token.json?commit=2ab7834c'
        )
      end
    end

    describe '#build_page' do
      it 'returns the correct build page' do
        expect(integration.build_page('2ab7834c', nil)).to eq(
          'https://buildkite.com/organization-name/example-pipeline/builds?commit=2ab7834c'
        )
      end
    end

    describe '#commit_status' do
      it 'returns the contents of the reactive cache' do
        stub_reactive_cache(integration, { commit_status: 'foo' }, 'sha', 'ref')

        expect(integration.commit_status('sha', 'ref')).to eq('foo')
      end
    end

    describe '#calculate_reactive_cache' do
      describe '#commit_status' do
        let(:buildkite_full_url) do
          'https://gitlab.buildkite.com/status/secret-sauce-status-token.json?commit=123'
        end

        subject { integration.calculate_reactive_cache('123', 'unused')[:commit_status] }

        it 'sets commit status to :error when status is 500' do
          stub_request(status: 500)

          is_expected.to eq(:error)
        end

        it 'sets commit status to :error when status is 404' do
          stub_request(status: 404)

          is_expected.to eq(:error)
        end

        it 'passes through build status untouched when status is 200' do
          stub_request(body: %q({"status":"Great Success"}))

          is_expected.to eq('Great Success')
        end

        Gitlab::HTTP::HTTP_ERRORS.each do |http_error|
          it "sets commit status to :error with a #{http_error.name} error" do
            WebMock.stub_request(:get, buildkite_full_url)
              .to_raise(http_error)

            expect(Gitlab::ErrorTracking)
              .to receive(:log_exception)
              .with(instance_of(http_error), { project_id: project.id })

            is_expected.to eq(:error)
          end
        end
      end
    end
  end

  def stub_request(status: 200, body: nil)
    body ||= %q({"status":"success"})

    stub_full_request(buildkite_full_url).to_return(
      status: status,
      headers: { 'Content-Type' => 'application/json' },
      body: body
    )
  end
end