2019-03-30 03:23:56 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2011-12-14 11:38:52 -05:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2017-07-10 10:24:02 -04:00
|
|
|
describe WebHook do
|
2017-04-27 06:08:57 -04:00
|
|
|
let(:hook) { build(:project_hook) }
|
|
|
|
|
|
|
|
describe 'associations' do
|
2019-10-17 11:06:17 -04:00
|
|
|
it { is_expected.to have_many(:web_hook_logs) }
|
2017-04-27 06:08:57 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
describe 'validations' do
|
2015-02-12 13:17:35 -05:00
|
|
|
it { is_expected.to validate_presence_of(:url) }
|
2011-12-14 11:38:52 -05:00
|
|
|
|
2016-02-22 07:55:36 -05:00
|
|
|
describe 'url' do
|
2017-04-27 06:08:57 -04:00
|
|
|
it { is_expected.to allow_value('http://example.com').for(:url) }
|
|
|
|
it { is_expected.to allow_value('https://example.com').for(:url) }
|
|
|
|
it { is_expected.to allow_value(' https://example.com ').for(:url) }
|
|
|
|
it { is_expected.to allow_value('http://test.com/api').for(:url) }
|
|
|
|
it { is_expected.to allow_value('http://test.com/api?key=abc').for(:url) }
|
|
|
|
it { is_expected.to allow_value('http://test.com/api?key=abc&type=def').for(:url) }
|
2011-12-14 11:38:52 -05:00
|
|
|
|
2017-04-27 06:08:57 -04:00
|
|
|
it { is_expected.not_to allow_value('example.com').for(:url) }
|
|
|
|
it { is_expected.not_to allow_value('ftp://example.com').for(:url) }
|
|
|
|
it { is_expected.not_to allow_value('herp-and-derp').for(:url) }
|
2016-02-22 07:55:36 -05:00
|
|
|
|
|
|
|
it 'strips :url before saving it' do
|
2017-04-27 06:08:57 -04:00
|
|
|
hook.url = ' https://example.com '
|
|
|
|
hook.save
|
2016-02-22 07:55:36 -05:00
|
|
|
|
|
|
|
expect(hook.url).to eq('https://example.com')
|
|
|
|
end
|
2011-12-14 11:38:52 -05:00
|
|
|
end
|
2018-01-05 16:36:18 -05:00
|
|
|
|
|
|
|
describe 'token' do
|
|
|
|
it { is_expected.to allow_value("foobar").for(:token) }
|
|
|
|
|
|
|
|
it { is_expected.not_to allow_values("foo\nbar", "foo\r\nbar").for(:token) }
|
|
|
|
end
|
2018-06-07 03:35:17 -04:00
|
|
|
|
|
|
|
describe 'push_events_branch_filter' do
|
|
|
|
it { is_expected.to allow_values("good_branch_name", "another/good-branch_name").for(:push_events_branch_filter) }
|
|
|
|
it { is_expected.to allow_values("").for(:push_events_branch_filter) }
|
|
|
|
it { is_expected.not_to allow_values("bad branch name", "bad~branchname").for(:push_events_branch_filter) }
|
|
|
|
|
|
|
|
it 'gets rid of whitespace' do
|
|
|
|
hook.push_events_branch_filter = ' branch '
|
|
|
|
hook.save
|
|
|
|
|
|
|
|
expect(hook.push_events_branch_filter).to eq('branch')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'stores whitespace only as empty' do
|
|
|
|
hook.push_events_branch_filter = ' '
|
|
|
|
hook.save
|
|
|
|
|
|
|
|
expect(hook.push_events_branch_filter).to eq('')
|
|
|
|
end
|
|
|
|
end
|
2011-12-14 11:38:52 -05:00
|
|
|
end
|
|
|
|
|
2018-09-14 13:21:28 -04:00
|
|
|
describe 'encrypted attributes' do
|
|
|
|
subject { described_class.encrypted_attributes.keys }
|
|
|
|
|
|
|
|
it { is_expected.to contain_exactly(:token, :url) }
|
|
|
|
end
|
|
|
|
|
2017-04-27 06:08:57 -04:00
|
|
|
describe 'execute' do
|
|
|
|
let(:data) { { key: 'value' } }
|
|
|
|
let(:hook_name) { 'project hook' }
|
2015-12-01 19:15:01 -05:00
|
|
|
|
2017-04-27 06:08:57 -04:00
|
|
|
before do
|
|
|
|
expect(WebHookService).to receive(:new).with(hook, data, hook_name).and_call_original
|
2015-12-01 19:15:01 -05:00
|
|
|
end
|
2016-01-07 09:28:24 -05:00
|
|
|
|
2017-04-27 06:08:57 -04:00
|
|
|
it '#execute' do
|
|
|
|
expect_any_instance_of(WebHookService).to receive(:execute)
|
2016-01-07 09:28:24 -05:00
|
|
|
|
2017-04-27 06:08:57 -04:00
|
|
|
hook.execute(data, hook_name)
|
2016-01-07 09:28:24 -05:00
|
|
|
end
|
|
|
|
|
2017-04-27 06:08:57 -04:00
|
|
|
it '#async_execute' do
|
|
|
|
expect_any_instance_of(WebHookService).to receive(:async_execute)
|
2016-01-07 09:28:24 -05:00
|
|
|
|
2017-04-27 06:08:57 -04:00
|
|
|
hook.async_execute(data, hook_name)
|
2016-01-07 09:28:24 -05:00
|
|
|
end
|
2011-12-14 11:38:52 -05:00
|
|
|
end
|
2019-10-17 11:06:17 -04:00
|
|
|
|
|
|
|
describe '#destroy' do
|
|
|
|
it 'cascades to web_hook_logs' do
|
|
|
|
web_hook = create(:project_hook)
|
|
|
|
create_list(:web_hook_log, 3, web_hook: web_hook)
|
|
|
|
|
|
|
|
expect { web_hook.destroy }.to change(web_hook.web_hook_logs, :count).by(-3)
|
|
|
|
end
|
|
|
|
end
|
2011-12-14 11:38:52 -05:00
|
|
|
end
|