2019-04-11 08:17:24 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2013-02-20 05:53:15 -05:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-24 11:08:50 -04:00
|
|
|
RSpec.describe SystemHooksService do
|
2021-04-23 08:09:52 -04:00
|
|
|
describe '#execute_hooks_for' do
|
|
|
|
let_it_be(:user) { create(:user) }
|
|
|
|
let_it_be(:group) { create(:group) }
|
|
|
|
let_it_be(:project) { create(:project) }
|
|
|
|
let_it_be(:group_member) { create(:group_member, source: group, user: user) }
|
|
|
|
let_it_be(:project_member) { create(:project_member, source: project, user: user) }
|
|
|
|
let_it_be(:key) { create(:key, user: user) }
|
|
|
|
let_it_be(:deploy_key) { create(:key) }
|
|
|
|
|
|
|
|
let(:event) { :create }
|
|
|
|
|
|
|
|
using RSpec::Parameterized::TableSyntax
|
|
|
|
|
|
|
|
where(:model_name, :builder_class) do
|
|
|
|
:group_member | Gitlab::HookData::GroupMemberBuilder
|
|
|
|
:group | Gitlab::HookData::GroupBuilder
|
|
|
|
:project_member | Gitlab::HookData::ProjectMemberBuilder
|
|
|
|
:user | Gitlab::HookData::UserBuilder
|
|
|
|
:project | Gitlab::HookData::ProjectBuilder
|
|
|
|
:key | Gitlab::HookData::KeyBuilder
|
|
|
|
:deploy_key | Gitlab::HookData::KeyBuilder
|
2015-05-05 11:13:11 -04:00
|
|
|
end
|
2018-03-05 08:41:54 -05:00
|
|
|
|
2021-04-23 08:09:52 -04:00
|
|
|
with_them do
|
|
|
|
it 'builds the data with the relevant builder class and then calls #execute_hooks with the obtained data' do
|
|
|
|
data = double
|
|
|
|
model = public_send(model_name)
|
2018-03-05 08:41:54 -05:00
|
|
|
|
2021-04-23 08:09:52 -04:00
|
|
|
expect_next_instance_of(builder_class, model) do |builder|
|
|
|
|
expect(builder).to receive(:build).with(event).and_return(data)
|
|
|
|
end
|
2018-03-05 08:41:54 -05:00
|
|
|
|
2021-04-23 08:09:52 -04:00
|
|
|
service = described_class.new
|
2018-03-05 08:41:54 -05:00
|
|
|
|
2021-04-23 08:09:52 -04:00
|
|
|
expect_next_instance_of(SystemHooksService) do |system_hook_service|
|
|
|
|
expect(system_hook_service).to receive(:execute_hooks).with(data)
|
|
|
|
end
|
2017-11-03 07:26:52 -04:00
|
|
|
|
2021-04-23 08:09:52 -04:00
|
|
|
service.execute_hooks_for(model, event)
|
|
|
|
end
|
2018-03-05 16:37:37 -05:00
|
|
|
end
|
2021-04-23 08:09:52 -04:00
|
|
|
end
|
2018-03-05 16:37:37 -05:00
|
|
|
|
2021-04-23 08:09:52 -04:00
|
|
|
describe '#execute_hooks' do
|
|
|
|
let(:data) { { key: :value } }
|
2017-11-03 07:26:52 -04:00
|
|
|
|
2021-04-23 08:09:52 -04:00
|
|
|
subject { described_class.new.execute_hooks(data) }
|
2017-11-03 07:26:52 -04:00
|
|
|
|
2021-04-23 08:09:52 -04:00
|
|
|
it 'executes system hooks with the given data' do
|
|
|
|
hook = create(:system_hook)
|
2017-11-03 07:26:52 -04:00
|
|
|
|
2021-04-23 08:09:52 -04:00
|
|
|
allow(SystemHook).to receive_message_chain(:hooks_for, :find_each).and_yield(hook)
|
2017-11-03 07:26:52 -04:00
|
|
|
|
2021-04-23 08:09:52 -04:00
|
|
|
expect(hook).to receive(:async_execute).with(data, 'system_hooks')
|
2017-11-03 07:26:52 -04:00
|
|
|
|
2021-04-23 08:09:52 -04:00
|
|
|
subject
|
2017-11-03 07:26:52 -04:00
|
|
|
end
|
2013-02-20 05:53:15 -05:00
|
|
|
|
2021-04-23 08:09:52 -04:00
|
|
|
it 'executes FileHook with the given data' do
|
|
|
|
expect(Gitlab::FileHook).to receive(:execute_all_async).with(data)
|
2013-02-20 05:53:15 -05:00
|
|
|
|
2021-04-23 08:09:52 -04:00
|
|
|
subject
|
|
|
|
end
|
2013-02-20 05:53:15 -05:00
|
|
|
end
|
|
|
|
end
|