BigW Consortium Gitlab

buildkite_service_spec.rb 3.2 KB
Newer Older
Keith Pitt committed
1 2
require 'spec_helper'

3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
describe BuildkiteService, models: true, caching: true do
  include ReactiveCachingHelpers

  let(:project) { create(:empty_project) }

  subject(:service) do
    described_class.create(
      project: project,
      properties: {
        service_hook: true,
        project_url: 'https://buildkite.com/account-name/example-project',
        token: 'secret-sauce-webhook-token:secret-sauce-status-token'
      }
    )
  end

Keith Pitt committed
19
  describe 'Associations' do
20 21
    it { is_expected.to belong_to :project }
    it { is_expected.to have_one :service_hook }
Keith Pitt committed
22 23
  end

24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
  describe 'Validations' do
    context 'when service is active' do
      before { subject.active = true }

      it { is_expected.to validate_presence_of(:project_url) }
      it { is_expected.to validate_presence_of(:token) }
      it_behaves_like 'issue tracker service URL attribute', :project_url
    end

    context 'when service is inactive' do
      before { subject.active = false }

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

Keith Pitt committed
41 42
  describe 'commits methods' do
    before do
43
      allow(project).to receive(:default_branch).and_return('default-brancho')
Keith Pitt committed
44 45
    end

46
    describe '#webhook_url' do
Keith Pitt committed
47
      it 'returns the webhook url' do
48
        expect(service.webhook_url).to eq(
49
          'https://webhook.buildkite.com/deliver/secret-sauce-webhook-token'
50
        )
Keith Pitt committed
51 52 53
      end
    end

54
    describe '#commit_status_path' do
Keith Pitt committed
55
      it 'returns the correct status page' do
56
        expect(service.commit_status_path('2ab7834c')).to eq(
57
          'https://gitlab.buildkite.com/status/secret-sauce-status-token.json?commit=2ab7834c'
58
        )
Keith Pitt committed
59 60 61
      end
    end

62
    describe '#build_page' do
Keith Pitt committed
63
      it 'returns the correct build page' do
64
        expect(service.build_page('2ab7834c', nil)).to eq(
65
          'https://buildkite.com/account-name/example-project/builds?commit=2ab7834c'
66
        )
Keith Pitt committed
67 68
      end
    end
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

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

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

    describe '#calculate_reactive_cache' do
      context '#commit_status' do
        subject { service.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
      end
    end
  end

  def stub_request(status: 200, body: nil)
    body ||= %Q({"status":"success"})
    buildkite_full_url = 'https://gitlab.buildkite.com/status/secret-sauce-status-token.json?commit=123'

    WebMock.stub_request(:get, buildkite_full_url).to_return(
      status: status,
      headers: { 'Content-Type' => 'application/json' },
      body: body
    )
Keith Pitt committed
112 113
  end
end