debian-mirror-gitlab/spec/services/test_hooks/project_service_spec.rb

212 lines
7.6 KiB
Ruby
Raw Normal View History

2019-07-31 22:56:46 +05:30
# frozen_string_literal: true
2017-09-10 17:25:29 +05:30
require 'spec_helper'
2020-07-28 23:09:34 +05:30
RSpec.describe TestHooks::ProjectService do
2021-03-11 19:13:27 +05:30
include AfterNextHelpers
2017-09-10 17:25:29 +05:30
let(:current_user) { create(:user) }
describe '#execute' do
2021-03-11 19:13:27 +05:30
let_it_be(:project) { create(:project, :repository) }
2021-09-30 23:02:18 +05:30
2021-03-11 19:13:27 +05:30
let(:hook) { create(:project_hook, project: project) }
2018-11-08 19:23:39 +05:30
let(:trigger) { 'not_implemented_events' }
2017-09-10 17:25:29 +05:30
let(:service) { described_class.new(hook, current_user, trigger) }
let(:sample_data) { { data: 'sample' } }
let(:success_result) { { status: :success, http_status: 200, message: 'ok' } }
2018-11-08 19:23:39 +05:30
it 'allows to set a custom project' do
project = double
service.project = project
expect(service.project).to eq(project)
end
2017-09-10 17:25:29 +05:30
2018-11-08 19:23:39 +05:30
context 'hook with not implemented test' do
2017-09-10 17:25:29 +05:30
it 'returns error message' do
expect(hook).not_to receive(:execute)
expect(service.execute).to include({ status: :error, message: 'Testing not available for this hook' })
end
end
context 'push_events' do
let(:trigger) { 'push_events' }
2018-03-17 18:26:18 +05:30
let(:trigger_key) { :push_hooks }
2017-09-10 17:25:29 +05:30
it 'executes hook' do
allow(Gitlab::DataBuilder::Push).to receive(:build_sample).and_return(sample_data)
2022-04-04 11:22:00 +05:30
expect(hook).to receive(:execute).with(sample_data, trigger_key, force: true).and_return(success_result)
2017-09-10 17:25:29 +05:30
expect(service.execute).to include(success_result)
end
end
context 'tag_push_events' do
let(:trigger) { 'tag_push_events' }
2018-03-17 18:26:18 +05:30
let(:trigger_key) { :tag_push_hooks }
2017-09-10 17:25:29 +05:30
it 'executes hook' do
allow(Gitlab::DataBuilder::Push).to receive(:build_sample).and_return(sample_data)
2022-04-04 11:22:00 +05:30
expect(hook).to receive(:execute).with(sample_data, trigger_key, force: true).and_return(success_result)
2017-09-10 17:25:29 +05:30
expect(service.execute).to include(success_result)
end
end
context 'note_events' do
let(:trigger) { 'note_events' }
2018-03-17 18:26:18 +05:30
let(:trigger_key) { :note_hooks }
2017-09-10 17:25:29 +05:30
it 'returns error message if not enough data' do
expect(hook).not_to receive(:execute)
expect(service.execute).to include({ status: :error, message: 'Ensure the project has notes.' })
end
it 'executes hook' do
2021-03-11 19:13:27 +05:30
create(:note, project: project)
2017-09-10 17:25:29 +05:30
allow(Gitlab::DataBuilder::Note).to receive(:build).and_return(sample_data)
2021-03-11 19:13:27 +05:30
allow_next(NotesFinder).to receive(:execute).and_return(Note.all)
2017-09-10 17:25:29 +05:30
2022-04-04 11:22:00 +05:30
expect(hook).to receive(:execute).with(sample_data, trigger_key, force: true).and_return(success_result)
2017-09-10 17:25:29 +05:30
expect(service.execute).to include(success_result)
end
end
2021-03-11 19:13:27 +05:30
shared_examples_for 'a test webhook that operates on issues' do
2017-09-10 17:25:29 +05:30
let(:issue) { build(:issue) }
it 'returns error message if not enough data' do
expect(hook).not_to receive(:execute)
expect(service.execute).to include({ status: :error, message: 'Ensure the project has issues.' })
end
it 'executes hook' do
allow(issue).to receive(:to_hook_data).and_return(sample_data)
2021-03-11 19:13:27 +05:30
allow_next(IssuesFinder).to receive(:execute).and_return([issue])
2017-09-10 17:25:29 +05:30
2022-04-04 11:22:00 +05:30
expect(hook).to receive(:execute).with(sample_data, trigger_key, force: true).and_return(success_result)
2017-09-10 17:25:29 +05:30
expect(service.execute).to include(success_result)
end
end
2021-03-11 19:13:27 +05:30
context 'issues_events' do
let(:trigger) { 'issues_events' }
let(:trigger_key) { :issue_hooks }
it_behaves_like 'a test webhook that operates on issues'
end
2017-09-10 17:25:29 +05:30
context 'confidential_issues_events' do
let(:trigger) { 'confidential_issues_events' }
2018-03-17 18:26:18 +05:30
let(:trigger_key) { :confidential_issue_hooks }
2017-09-10 17:25:29 +05:30
2021-03-11 19:13:27 +05:30
it_behaves_like 'a test webhook that operates on issues'
2017-09-10 17:25:29 +05:30
end
context 'merge_requests_events' do
let(:trigger) { 'merge_requests_events' }
2018-03-17 18:26:18 +05:30
let(:trigger_key) { :merge_request_hooks }
2021-03-11 19:13:27 +05:30
let(:merge_request) { build(:merge_request) }
2017-09-10 17:25:29 +05:30
it 'returns error message if not enough data' do
expect(hook).not_to receive(:execute)
expect(service.execute).to include({ status: :error, message: 'Ensure the project has merge requests.' })
end
it 'executes hook' do
2021-03-11 19:13:27 +05:30
allow(merge_request).to receive(:to_hook_data).and_return(sample_data)
allow_next(MergeRequestsFinder).to receive(:execute).and_return([merge_request])
2017-09-10 17:25:29 +05:30
2022-04-04 11:22:00 +05:30
expect(hook).to receive(:execute).with(sample_data, trigger_key, force: true).and_return(success_result)
2017-09-10 17:25:29 +05:30
expect(service.execute).to include(success_result)
end
end
context 'job_events' do
let(:trigger) { 'job_events' }
2018-03-17 18:26:18 +05:30
let(:trigger_key) { :job_hooks }
2021-03-11 19:13:27 +05:30
let(:ci_job) { build(:ci_build) }
2017-09-10 17:25:29 +05:30
it 'returns error message if not enough data' do
expect(hook).not_to receive(:execute)
expect(service.execute).to include({ status: :error, message: 'Ensure the project has CI jobs.' })
end
it 'executes hook' do
allow(Gitlab::DataBuilder::Build).to receive(:build).and_return(sample_data)
2021-03-11 19:13:27 +05:30
allow_next(Ci::JobsFinder).to receive(:execute).and_return([ci_job])
2017-09-10 17:25:29 +05:30
2022-04-04 11:22:00 +05:30
expect(hook).to receive(:execute).with(sample_data, trigger_key, force: true).and_return(success_result)
2017-09-10 17:25:29 +05:30
expect(service.execute).to include(success_result)
end
end
context 'pipeline_events' do
let(:trigger) { 'pipeline_events' }
2018-03-17 18:26:18 +05:30
let(:trigger_key) { :pipeline_hooks }
2021-03-11 19:13:27 +05:30
let(:pipeline) { build(:ci_empty_pipeline) }
2017-09-10 17:25:29 +05:30
it 'returns error message if not enough data' do
expect(hook).not_to receive(:execute)
expect(service.execute).to include({ status: :error, message: 'Ensure the project has CI pipelines.' })
end
it 'executes hook' do
allow(Gitlab::DataBuilder::Pipeline).to receive(:build).and_return(sample_data)
2021-03-11 19:13:27 +05:30
allow_next(Ci::PipelinesFinder).to receive(:execute).and_return([pipeline])
2017-09-10 17:25:29 +05:30
2022-04-04 11:22:00 +05:30
expect(hook).to receive(:execute).with(sample_data, trigger_key, force: true).and_return(success_result)
2017-09-10 17:25:29 +05:30
expect(service.execute).to include(success_result)
end
end
context 'wiki_page_events' do
2021-03-11 19:13:27 +05:30
let_it_be(:project) { create(:project, :wiki_repo) }
2021-09-30 23:02:18 +05:30
2017-09-10 17:25:29 +05:30
let(:trigger) { 'wiki_page_events' }
2018-03-17 18:26:18 +05:30
let(:trigger_key) { :wiki_page_hooks }
2017-09-10 17:25:29 +05:30
it 'returns error message if wiki disabled' do
allow(project).to receive(:wiki_enabled?).and_return(false)
expect(hook).not_to receive(:execute)
expect(service.execute).to include({ status: :error, message: 'Ensure the wiki is enabled and has pages.' })
end
it 'returns error message if not enough data' do
expect(hook).not_to receive(:execute)
expect(service.execute).to include({ status: :error, message: 'Ensure the wiki is enabled and has pages.' })
end
it 'executes hook' do
create(:wiki_page, wiki: project.wiki)
allow(Gitlab::DataBuilder::WikiPage).to receive(:build).and_return(sample_data)
2022-04-04 11:22:00 +05:30
expect(hook).to receive(:execute).with(sample_data, trigger_key, force: true).and_return(success_result)
2017-09-10 17:25:29 +05:30
expect(service.execute).to include(success_result)
end
end
2021-01-29 00:20:46 +05:30
context 'releases_events' do
let(:trigger) { 'releases_events' }
let(:trigger_key) { :release_hooks }
2021-03-11 19:13:27 +05:30
let(:release) { build(:release) }
2021-01-29 00:20:46 +05:30
it 'returns error message if not enough data' do
expect(hook).not_to receive(:execute)
expect(service.execute).to include({ status: :error, message: 'Ensure the project has releases.' })
end
it 'executes hook' do
2021-03-11 19:13:27 +05:30
allow(release).to receive(:to_hook_data).and_return(sample_data)
allow_next(ReleasesFinder).to receive(:execute).and_return([release])
2021-01-29 00:20:46 +05:30
2022-04-04 11:22:00 +05:30
expect(hook).to receive(:execute).with(sample_data, trigger_key, force: true).and_return(success_result)
2021-01-29 00:20:46 +05:30
expect(service.execute).to include(success_result)
end
end
2017-09-10 17:25:29 +05:30
end
end