2014-08-18 06:54:51 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2017-07-10 10:24:02 -04:00
|
|
|
describe BuildkiteService, :use_clean_rails_memory_store_caching do
|
2017-01-12 17:31:02 -05:00
|
|
|
include ReactiveCachingHelpers
|
|
|
|
|
2017-08-02 15:55:11 -04:00
|
|
|
let(:project) { create(:project) }
|
2017-01-12 17:31:02 -05:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2014-08-18 06:54:51 -04:00
|
|
|
describe 'Associations' do
|
2015-02-12 13:17:35 -05:00
|
|
|
it { is_expected.to belong_to :project }
|
|
|
|
it { is_expected.to have_one :service_hook }
|
2014-08-18 06:54:51 -04:00
|
|
|
end
|
|
|
|
|
2016-04-21 11:13:14 -04:00
|
|
|
describe 'Validations' do
|
|
|
|
context 'when service is active' do
|
2017-06-14 14:18:56 -04:00
|
|
|
before do
|
|
|
|
subject.active = true
|
|
|
|
end
|
2016-04-21 11:13:14 -04:00
|
|
|
|
|
|
|
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
|
2017-06-14 14:18:56 -04:00
|
|
|
before do
|
|
|
|
subject.active = false
|
|
|
|
end
|
2016-04-21 11:13:14 -04:00
|
|
|
|
|
|
|
it { is_expected.not_to validate_presence_of(:project_url) }
|
|
|
|
it { is_expected.not_to validate_presence_of(:token) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-08-18 06:54:51 -04:00
|
|
|
describe 'commits methods' do
|
|
|
|
before do
|
2017-01-12 17:31:02 -05:00
|
|
|
allow(project).to receive(:default_branch).and_return('default-brancho')
|
2014-08-18 06:54:51 -04:00
|
|
|
end
|
|
|
|
|
2016-07-11 18:12:31 -04:00
|
|
|
describe '#webhook_url' do
|
2014-08-18 06:54:51 -04:00
|
|
|
it 'returns the webhook url' do
|
2017-01-12 17:31:02 -05:00
|
|
|
expect(service.webhook_url).to eq(
|
2015-03-26 07:40:43 -04:00
|
|
|
'https://webhook.buildkite.com/deliver/secret-sauce-webhook-token'
|
2015-02-12 13:17:35 -05:00
|
|
|
)
|
2014-08-18 06:54:51 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-07-11 18:12:31 -04:00
|
|
|
describe '#commit_status_path' do
|
2014-08-18 06:54:51 -04:00
|
|
|
it 'returns the correct status page' do
|
2017-01-12 17:31:02 -05:00
|
|
|
expect(service.commit_status_path('2ab7834c')).to eq(
|
2015-03-26 07:40:43 -04:00
|
|
|
'https://gitlab.buildkite.com/status/secret-sauce-status-token.json?commit=2ab7834c'
|
2015-02-12 13:17:35 -05:00
|
|
|
)
|
2014-08-18 06:54:51 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-07-11 18:12:31 -04:00
|
|
|
describe '#build_page' do
|
2014-08-18 06:54:51 -04:00
|
|
|
it 'returns the correct build page' do
|
2017-01-12 17:31:02 -05:00
|
|
|
expect(service.build_page('2ab7834c', nil)).to eq(
|
2015-03-26 07:40:43 -04:00
|
|
|
'https://buildkite.com/account-name/example-project/builds?commit=2ab7834c'
|
2015-02-12 13:17:35 -05:00
|
|
|
)
|
2014-08-18 06:54:51 -04:00
|
|
|
end
|
|
|
|
end
|
2017-01-12 17:31:02 -05:00
|
|
|
|
|
|
|
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
|
2017-02-22 17:39:43 -05:00
|
|
|
stub_request(body: %q({"status":"Great Success"}))
|
2017-01-12 17:31:02 -05:00
|
|
|
|
|
|
|
is_expected.to eq('Great Success')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def stub_request(status: 200, body: nil)
|
2017-02-22 17:39:43 -05:00
|
|
|
body ||= %q({"status":"success"})
|
2017-01-12 17:31:02 -05:00
|
|
|
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
|
|
|
|
)
|
2014-08-18 06:54:51 -04:00
|
|
|
end
|
|
|
|
end
|