debian-mirror-gitlab/spec/services/system_hooks_service_spec.rb

71 lines
2.2 KiB
Ruby
Raw Normal View History

2019-07-31 22:56:46 +05:30
# frozen_string_literal: true
2014-09-02 18:07:02 +05:30
require 'spec_helper'
2023-05-27 22:25:52 +05:30
RSpec.describe SystemHooksService, feature_category: :webhooks do
2021-06-08 01:23:25 +05:30
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
end
2018-03-27 19:54:05 +05:30
2021-06-08 01:23:25 +05:30
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-27 19:54:05 +05:30
2021-06-08 01:23:25 +05:30
expect_next_instance_of(builder_class, model) do |builder|
expect(builder).to receive(:build).with(event).and_return(data)
end
2018-03-27 19:54:05 +05:30
2021-06-08 01:23:25 +05:30
service = described_class.new
2018-03-27 19:54:05 +05:30
2021-06-08 01:23:25 +05:30
expect_next_instance_of(SystemHooksService) do |system_hook_service|
expect(system_hook_service).to receive(:execute_hooks).with(data)
end
2018-03-17 18:26:18 +05:30
2021-06-08 01:23:25 +05:30
service.execute_hooks_for(model, event)
end
2018-03-27 19:54:05 +05:30
end
2021-06-08 01:23:25 +05:30
end
2018-03-27 19:54:05 +05:30
2021-06-08 01:23:25 +05:30
describe '#execute_hooks' do
let(:data) { { key: :value } }
2018-03-17 18:26:18 +05:30
2021-06-08 01:23:25 +05:30
subject { described_class.new.execute_hooks(data) }
2018-03-17 18:26:18 +05:30
2021-06-08 01:23:25 +05:30
it 'executes system hooks with the given data' do
hook = create(:system_hook)
2018-03-17 18:26:18 +05:30
2021-06-08 01:23:25 +05:30
allow(SystemHook).to receive_message_chain(:hooks_for, :find_each).and_yield(hook)
2018-03-17 18:26:18 +05:30
2021-06-08 01:23:25 +05:30
expect(hook).to receive(:async_execute).with(data, 'system_hooks')
2018-03-17 18:26:18 +05:30
2021-06-08 01:23:25 +05:30
subject
2018-03-17 18:26:18 +05:30
end
2014-09-02 18:07:02 +05:30
2021-06-08 01:23:25 +05:30
it 'executes FileHook with the given data' do
expect(Gitlab::FileHook).to receive(:execute_all_async).with(data)
2014-09-02 18:07:02 +05:30
2021-06-08 01:23:25 +05:30
subject
end
2014-09-02 18:07:02 +05:30
end
end