git_hooks_service_spec.rb 1.6 KB
Newer Older
1 2
require 'spec_helper'

Douwe Maan's avatar
Douwe Maan committed
3
describe GitHooksService, services: true do
4 5
  include RepoHelpers

6 7
  let(:user)    { create(:user) }
  let(:project) { create(:project, :repository) }
8 9 10 11 12 13 14 15 16 17
  let(:service) { GitHooksService.new }

  before do
    @blankrev = Gitlab::Git::BLANK_SHA
    @oldrev = sample_commit.parent_id
    @newrev = sample_commit.id
    @ref = 'refs/heads/feature'
  end

  describe '#execute' do
18
    context 'when receive hooks were successful' do
19
      it 'calls post-receive hook' do
20
        hook = double(trigger: [true, nil])
21 22
        expect(Gitlab::Git::Hook).to receive(:new).exactly(3).times.and_return(hook)

23
        service.execute(user, project, @blankrev, @newrev, @ref) { }
24 25 26
      end
    end

27
    context 'when pre-receive hook failed' do
28
      it 'does not call post-receive hook' do
29
        expect(service).to receive(:run_hook).with('pre-receive').and_return([false, ''])
30 31
        expect(service).not_to receive(:run_hook).with('post-receive')

32
        expect do
33
          service.execute(user, project, @blankrev, @newrev, @ref)
34
        end.to raise_error(GitHooksService::PreReceiveError)
35 36 37
      end
    end

38
    context 'when update hook failed' do
39
      it 'does not call post-receive hook' do
40 41
        expect(service).to receive(:run_hook).with('pre-receive').and_return([true, nil])
        expect(service).to receive(:run_hook).with('update').and_return([false, ''])
42 43
        expect(service).not_to receive(:run_hook).with('post-receive')

44
        expect do
45
          service.execute(user, project, @blankrev, @newrev, @ref)
46
        end.to raise_error(GitHooksService::PreReceiveError)
47 48
      end
    end
49 50
  end
end