gitlab-org--gitlab-foss/spec/services/test_hooks/system_service_spec.rb
Stan Hu 1e53f40c25 Simplify system hook testing and guarantee test will fire
The change in !11728 would cause an arbitrary project to be chosen to test
system hooks, and it's likely that the project would not have any commits or
relevant commits to test the hook. This would prevent admins from verifying
that the hook fired. Instead of trying to create a representative hook
dynamically, just send static data to guarantee the hook will actually be
tested.

Closes #37067
2017-08-27 15:50:20 -07:00

61 lines
2.1 KiB
Ruby

require 'spec_helper'
describe TestHooks::SystemService do
let(:current_user) { create(:user) }
describe '#execute' do
let(:project) { create(:project, :repository) }
let(:hook) { create(:system_hook) }
let(:service) { described_class.new(hook, current_user, trigger) }
let(:success_result) { { status: :success, http_status: 200, message: 'ok' } }
before do
allow(Project).to receive(:first).and_return(project)
end
context 'hook with not implemented test' do
let(:trigger) { 'not_implemented_events' }
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' }
it 'executes hook' do
allow(project).to receive(:empty_repo?).and_return(false)
expect(Gitlab::DataBuilder::Push).to receive(:sample_data).and_call_original
expect(hook).to receive(:execute).with(Gitlab::DataBuilder::Push::SAMPLE_DATA, trigger).and_return(success_result)
expect(service.execute).to include(success_result)
end
end
context 'tag_push_events' do
let(:trigger) { 'tag_push_events' }
it 'executes hook' do
allow(project.repository).to receive(:tags).and_return(['tag'])
expect(Gitlab::DataBuilder::Push).to receive(:sample_data).and_call_original
expect(hook).to receive(:execute).with(Gitlab::DataBuilder::Push::SAMPLE_DATA, trigger).and_return(success_result)
expect(service.execute).to include(success_result)
end
end
context 'repository_update_events' do
let(:trigger) { 'repository_update_events' }
it 'executes hook' do
allow(project).to receive(:empty_repo?).and_return(false)
expect(Gitlab::DataBuilder::Repository).to receive(:sample_data).and_call_original
expect(hook).to receive(:execute).with(Gitlab::DataBuilder::Repository::SAMPLE_DATA, trigger).and_return(success_result)
expect(service.execute).to include(success_result)
end
end
end
end