diff options
Diffstat (limited to 'spec/models/hooks')
-rw-r--r-- | spec/models/hooks/system_hook_spec.rb | 15 | ||||
-rw-r--r-- | spec/models/hooks/web_hook_spec.rb | 59 |
2 files changed, 66 insertions, 8 deletions
diff --git a/spec/models/hooks/system_hook_spec.rb b/spec/models/hooks/system_hook_spec.rb index a99263078b3..17cb5da977a 100644 --- a/spec/models/hooks/system_hook_spec.rb +++ b/spec/models/hooks/system_hook_spec.rb @@ -16,6 +16,21 @@ RSpec.describe SystemHook do end end + describe 'validations' do + describe 'url' do + let(:url) { 'http://localhost:9000' } + + it { is_expected.not_to allow_value(url).for(:url) } + + it 'is valid if application settings allow local requests from system hooks' do + settings = ApplicationSetting.new(allow_local_requests_from_system_hooks: true) + allow(ApplicationSetting).to receive(:current).and_return(settings) + + is_expected.to allow_value(url).for(:url) + end + end + end + describe "execute", :sidekiq_might_not_need_inline do let(:system_hook) { create(:system_hook) } let(:user) { create(:user) } diff --git a/spec/models/hooks/web_hook_spec.rb b/spec/models/hooks/web_hook_spec.rb index 1761b537dc0..c68ad3bf0c4 100644 --- a/spec/models/hooks/web_hook_spec.rb +++ b/spec/models/hooks/web_hook_spec.rb @@ -32,6 +32,19 @@ RSpec.describe WebHook do it { is_expected.not_to allow_value('ftp://example.com').for(:url) } it { is_expected.not_to allow_value('herp-and-derp').for(:url) } + context 'when url is local' do + let(:url) { 'http://localhost:9000' } + + it { is_expected.not_to allow_value(url).for(:url) } + + it 'is valid if application settings allow local requests from web hooks' do + settings = ApplicationSetting.new(allow_local_requests_from_web_hooks_and_services: true) + allow(ApplicationSetting).to receive(:current).and_return(settings) + + is_expected.to allow_value(url).for(:url) + end + end + it 'strips :url before saving it' do hook.url = ' https://example.com ' hook.save! @@ -267,6 +280,15 @@ RSpec.describe WebHook do end end + shared_examples 'is tolerant of invalid records' do + specify do + hook.url = nil + + expect(hook).to be_invalid + run_expectation + end + end + describe '#enable!' do it 'makes a hook executable if it was marked as failed' do hook.recent_failures = 1000 @@ -281,15 +303,17 @@ RSpec.describe WebHook do end it 'does not update hooks unless necessary' do - expect(hook).not_to receive(:update!) + sql_count = ActiveRecord::QueryRecorder.new { hook.enable! }.count - hook.enable! + expect(sql_count).to eq(0) end - it 'is idempotent on executable hooks' do - expect(hook).not_to receive(:update!) + include_examples 'is tolerant of invalid records' do + def run_expectation + hook.recent_failures = 1000 - expect { hook.enable! }.not_to change(hook, :executable?) + expect { hook.enable! }.to change(hook, :executable?).from(false).to(true) + end end end @@ -307,6 +331,12 @@ RSpec.describe WebHook do expect { hook.backoff! }.not_to change(hook, :backoff_count) end + + include_examples 'is tolerant of invalid records' do + def run_expectation + expect { hook.backoff! }.to change(hook, :backoff_count).by(1) + end + end end describe 'failed!' do @@ -314,11 +344,18 @@ RSpec.describe WebHook do expect { hook.failed! }.to change(hook, :recent_failures).by(1) end - it 'does not allow the failure count to exceed the maximum value' do + it 'does not update the hook if the the failure count exceeds the maximum value' do hook.recent_failures = described_class::MAX_FAILURES - expect(hook).not_to receive(:update!) - expect { hook.failed! }.not_to change(hook, :recent_failures) + sql_count = ActiveRecord::QueryRecorder.new { hook.failed! }.count + + expect(sql_count).to eq(0) + end + + include_examples 'is tolerant of invalid records' do + def run_expectation + expect { hook.failed! }.to change(hook, :recent_failures).by(1) + end end end @@ -326,5 +363,11 @@ RSpec.describe WebHook do it 'disables a hook' do expect { hook.disable! }.to change(hook, :executable?).from(true).to(false) end + + include_examples 'is tolerant of invalid records' do + def run_expectation + expect { hook.disable! }.to change(hook, :executable?).from(true).to(false) + end + end end end |