debian-mirror-gitlab/spec/models/hooks/project_hook_spec.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

219 lines
6.2 KiB
Ruby
Raw Permalink Normal View History

2019-07-07 11:18:12 +05:30
# frozen_string_literal: true
2014-09-02 18:07:02 +05:30
require 'spec_helper'
2023-04-23 21:23:45 +05:30
RSpec.describe ProjectHook, feature_category: :integrations do
include_examples 'a hook that gets automatically disabled on failure' do
let_it_be(:project) { create(:project) }
let(:hook) { build(:project_hook, project: project) }
let(:hook_factory) { :project_hook }
let(:default_factory_arguments) { { project: project } }
def find_hooks
project.hooks
end
end
2017-09-10 17:25:29 +05:30
describe 'associations' do
2016-06-02 11:05:42 +05:30
it { is_expected.to belong_to :project }
end
2017-09-10 17:25:29 +05:30
describe 'validations' do
it { is_expected.to validate_presence_of(:project) }
end
2020-05-24 23:13:21 +05:30
it_behaves_like 'includes Limitable concern' do
2023-01-13 00:05:48 +05:30
subject { build(:project_hook) }
2020-05-24 23:13:21 +05:30
end
2022-08-13 15:12:31 +05:30
describe '.for_projects' do
it 'finds related project hooks' do
2023-01-13 00:05:48 +05:30
hook_a = create(:project_hook, project: build(:project))
hook_b = create(:project_hook, project: build(:project))
hook_c = create(:project_hook, project: build(:project))
2022-08-13 15:12:31 +05:30
expect(described_class.for_projects([hook_a.project, hook_b.project]))
.to contain_exactly(hook_a, hook_b)
expect(described_class.for_projects(hook_c.project))
.to contain_exactly(hook_c)
end
end
2014-09-02 18:07:02 +05:30
describe '.push_hooks' do
2016-09-13 17:45:13 +05:30
it 'returns hooks for push events only' do
2023-01-13 00:05:48 +05:30
project = build(:project)
hook = create(:project_hook, project: project, push_events: true)
create(:project_hook, project: project, push_events: false)
2017-09-10 17:25:29 +05:30
expect(described_class.push_hooks).to eq([hook])
2014-09-02 18:07:02 +05:30
end
end
describe '.tag_push_hooks' do
2016-09-13 17:45:13 +05:30
it 'returns hooks for tag push events only' do
2023-01-13 00:05:48 +05:30
project = build(:project)
hook = create(:project_hook, project: project, tag_push_events: true)
create(:project_hook, project: project, tag_push_events: false)
2017-09-10 17:25:29 +05:30
expect(described_class.tag_push_hooks).to eq([hook])
2014-09-02 18:07:02 +05:30
end
end
2021-06-08 01:23:25 +05:30
2022-03-02 08:16:31 +05:30
describe '#parent' do
it 'returns the associated project' do
project = build(:project)
hook = build(:project_hook, project: project)
expect(hook.parent).to eq(project)
end
end
2021-09-04 01:27:46 +05:30
describe '#application_context' do
let_it_be(:hook) { build(:project_hook) }
it 'includes the type and project' do
expect(hook.application_context).to include(
related_class: 'ProjectHook',
project: hook.project
)
end
end
2022-08-13 15:12:31 +05:30
describe '#update_last_failure', :clean_gitlab_redis_shared_state do
2023-04-23 21:23:45 +05:30
let_it_be(:hook) { create(:project_hook) }
def last_failure
Gitlab::Redis::SharedState.with do |redis|
redis.get(hook.project.last_failure_redis_key)
end
end
def any_failed?
Gitlab::Redis::SharedState.with do |redis|
Gitlab::Utils.to_boolean(redis.get(hook.project.web_hook_failure_redis_key))
end
end
2022-08-13 15:12:31 +05:30
it 'is a method of this class' do
expect { hook.update_last_failure }.not_to raise_error
end
context 'when the hook is executable' do
2023-04-23 21:23:45 +05:30
let(:redis_key) { hook.project.web_hook_failure_redis_key }
def redis_value
any_failed?
end
context 'when the state was previously failing' do
before do
Gitlab::Redis::SharedState.with do |redis|
redis.set(redis_key, true)
end
end
it 'does update the state' do
expect { hook.update_last_failure }.to change { redis_value }.to(false)
end
context 'when there is another failing sibling hook' do
before do
create(:project_hook, :permanently_disabled, project: hook.project)
end
it 'does not update the state' do
expect { hook.update_last_failure }.not_to change { redis_value }.from(true)
end
it 'caches the current value' do
Gitlab::Redis::SharedState.with do |redis|
expect(redis).to receive(:set).with(redis_key, 'true', ex: 1.hour).and_call_original
end
hook.update_last_failure
end
end
end
context 'when the state was previously unknown' do
before do
Gitlab::Redis::SharedState.with do |redis|
redis.del(redis_key)
end
end
it 'does not update the state' do
expect { hook.update_last_failure }.not_to change { redis_value }.from(nil)
end
end
context 'when the state was previously not failing' do
before do
Gitlab::Redis::SharedState.with do |redis|
redis.set(redis_key, false)
end
end
2022-08-13 15:12:31 +05:30
2023-04-23 21:23:45 +05:30
it 'does not update the state' do
expect { hook.update_last_failure }.not_to change { redis_value }.from(false)
end
it 'does not cache the current value' do
Gitlab::Redis::SharedState.with do |redis|
expect(redis).not_to receive(:set)
end
hook.update_last_failure
end
2022-08-13 15:12:31 +05:30
end
end
context 'when the hook is failed' do
before do
allow(hook).to receive(:executable?).and_return(false)
end
context 'there is no prior value', :freeze_time do
2023-04-23 21:23:45 +05:30
it 'updates last_failure' do
2022-08-13 15:12:31 +05:30
expect { hook.update_last_failure }.to change { last_failure }.to(Time.current)
end
2023-04-23 21:23:45 +05:30
it 'updates any_failed?' do
expect { hook.update_last_failure }.to change { any_failed? }.to(true)
end
2022-08-13 15:12:31 +05:30
end
2023-04-23 21:23:45 +05:30
context 'when there is a prior last_failure, from before now' do
2022-08-13 15:12:31 +05:30
it 'updates the state' do
the_future = 1.minute.from_now
hook.update_last_failure
travel_to(the_future) do
expect { hook.update_last_failure }.to change { last_failure }.to(the_future.iso8601)
end
end
2023-04-23 21:23:45 +05:30
it 'does not change the failing state' do
the_future = 1.minute.from_now
hook.update_last_failure
travel_to(the_future) do
expect { hook.update_last_failure }.not_to change { any_failed? }.from(true)
end
end
2022-08-13 15:12:31 +05:30
end
context 'there is a prior value, from after now' do
it 'does not update the state' do
the_past = 1.minute.ago
hook.update_last_failure
travel_to(the_past) do
expect { hook.update_last_failure }.not_to change { last_failure }
end
end
end
end
end
2014-09-02 18:07:02 +05:30
end