2018-10-15 14:42:47 +05:30
|
|
|
require 'spec_helper'
|
|
|
|
|
2018-11-18 11:00:15 +05:30
|
|
|
describe Gitlab::Git::CommitterWithHooks, :seed_helper do
|
2018-11-08 19:23:39 +05:30
|
|
|
# TODO https://gitlab.com/gitlab-org/gitaly/issues/1234
|
|
|
|
skip 'needs to be moved to gitaly-ruby test suite' do
|
|
|
|
shared_examples 'calling wiki hooks' do
|
|
|
|
let(:project) { create(:project) }
|
|
|
|
let(:user) { project.owner }
|
|
|
|
let(:project_wiki) { ProjectWiki.new(project, user) }
|
|
|
|
let(:wiki) { project_wiki.wiki }
|
|
|
|
let(:options) do
|
|
|
|
{
|
|
|
|
id: user.id,
|
|
|
|
username: user.username,
|
|
|
|
name: user.name,
|
|
|
|
email: user.email,
|
|
|
|
message: 'commit message'
|
|
|
|
}
|
|
|
|
end
|
2018-10-15 14:42:47 +05:30
|
|
|
|
2018-11-08 19:23:39 +05:30
|
|
|
subject { described_class.new(wiki, options) }
|
2018-10-15 14:42:47 +05:30
|
|
|
|
|
|
|
before do
|
2018-11-08 19:23:39 +05:30
|
|
|
project_wiki.create_page('home', 'test content')
|
2018-10-15 14:42:47 +05:30
|
|
|
end
|
|
|
|
|
2018-11-08 19:23:39 +05:30
|
|
|
shared_examples 'failing pre-receive hook' do
|
|
|
|
before do
|
|
|
|
expect_any_instance_of(Gitlab::Git::HooksService).to receive(:run_hook).with('pre-receive').and_return([false, ''])
|
|
|
|
expect_any_instance_of(Gitlab::Git::HooksService).not_to receive(:run_hook).with('update')
|
|
|
|
expect_any_instance_of(Gitlab::Git::HooksService).not_to receive(:run_hook).with('post-receive')
|
|
|
|
end
|
2018-10-15 14:42:47 +05:30
|
|
|
|
2018-11-08 19:23:39 +05:30
|
|
|
it 'raises exception' do
|
|
|
|
expect { subject.commit }.to raise_error(Gitlab::Git::Wiki::OperationError)
|
|
|
|
end
|
2018-10-15 14:42:47 +05:30
|
|
|
|
2018-11-08 19:23:39 +05:30
|
|
|
it 'does not create a new commit inside the repository' do
|
|
|
|
current_rev = find_current_rev
|
2018-10-15 14:42:47 +05:30
|
|
|
|
2018-11-08 19:23:39 +05:30
|
|
|
expect { subject.commit }.to raise_error(Gitlab::Git::Wiki::OperationError)
|
2018-10-15 14:42:47 +05:30
|
|
|
|
2018-11-08 19:23:39 +05:30
|
|
|
expect(current_rev).to eq find_current_rev
|
|
|
|
end
|
2018-10-15 14:42:47 +05:30
|
|
|
end
|
|
|
|
|
2018-11-08 19:23:39 +05:30
|
|
|
shared_examples 'failing update hook' do
|
|
|
|
before do
|
|
|
|
expect_any_instance_of(Gitlab::Git::HooksService).to receive(:run_hook).with('pre-receive').and_return([true, ''])
|
|
|
|
expect_any_instance_of(Gitlab::Git::HooksService).to receive(:run_hook).with('update').and_return([false, ''])
|
|
|
|
expect_any_instance_of(Gitlab::Git::HooksService).not_to receive(:run_hook).with('post-receive')
|
|
|
|
end
|
2018-10-15 14:42:47 +05:30
|
|
|
|
2018-11-08 19:23:39 +05:30
|
|
|
it 'raises exception' do
|
|
|
|
expect { subject.commit }.to raise_error(Gitlab::Git::Wiki::OperationError)
|
|
|
|
end
|
2018-10-15 14:42:47 +05:30
|
|
|
|
2018-11-08 19:23:39 +05:30
|
|
|
it 'does not create a new commit inside the repository' do
|
|
|
|
current_rev = find_current_rev
|
2018-10-15 14:42:47 +05:30
|
|
|
|
2018-11-08 19:23:39 +05:30
|
|
|
expect { subject.commit }.to raise_error(Gitlab::Git::Wiki::OperationError)
|
2018-10-15 14:42:47 +05:30
|
|
|
|
2018-11-08 19:23:39 +05:30
|
|
|
expect(current_rev).to eq find_current_rev
|
|
|
|
end
|
2018-10-15 14:42:47 +05:30
|
|
|
end
|
|
|
|
|
2018-11-08 19:23:39 +05:30
|
|
|
shared_examples 'failing post-receive hook' do
|
|
|
|
before do
|
|
|
|
expect_any_instance_of(Gitlab::Git::HooksService).to receive(:run_hook).with('pre-receive').and_return([true, ''])
|
|
|
|
expect_any_instance_of(Gitlab::Git::HooksService).to receive(:run_hook).with('update').and_return([true, ''])
|
|
|
|
expect_any_instance_of(Gitlab::Git::HooksService).to receive(:run_hook).with('post-receive').and_return([false, ''])
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not raise exception' do
|
|
|
|
expect { subject.commit }.not_to raise_error
|
|
|
|
end
|
2018-10-15 14:42:47 +05:30
|
|
|
|
2018-11-08 19:23:39 +05:30
|
|
|
it 'creates the commit' do
|
|
|
|
current_rev = find_current_rev
|
2018-10-15 14:42:47 +05:30
|
|
|
|
2018-11-08 19:23:39 +05:30
|
|
|
subject.commit
|
2018-10-15 14:42:47 +05:30
|
|
|
|
2018-11-08 19:23:39 +05:30
|
|
|
expect(current_rev).not_to eq find_current_rev
|
|
|
|
end
|
2018-10-15 14:42:47 +05:30
|
|
|
end
|
|
|
|
|
2018-11-08 19:23:39 +05:30
|
|
|
shared_examples 'when hooks call succceeds' do
|
|
|
|
let(:hook) { double(:hook) }
|
2018-10-15 14:42:47 +05:30
|
|
|
|
2018-11-08 19:23:39 +05:30
|
|
|
it 'calls the three hooks' do
|
|
|
|
expect(Gitlab::Git::Hook).to receive(:new).exactly(3).times.and_return(hook)
|
|
|
|
expect(hook).to receive(:trigger).exactly(3).times.and_return([true, nil])
|
2018-10-15 14:42:47 +05:30
|
|
|
|
2018-11-08 19:23:39 +05:30
|
|
|
subject.commit
|
|
|
|
end
|
2018-10-15 14:42:47 +05:30
|
|
|
|
2018-11-08 19:23:39 +05:30
|
|
|
it 'creates the commit' do
|
|
|
|
current_rev = find_current_rev
|
2018-10-15 14:42:47 +05:30
|
|
|
|
2018-11-08 19:23:39 +05:30
|
|
|
subject.commit
|
2018-10-15 14:42:47 +05:30
|
|
|
|
2018-11-08 19:23:39 +05:30
|
|
|
expect(current_rev).not_to eq find_current_rev
|
|
|
|
end
|
2018-10-15 14:42:47 +05:30
|
|
|
end
|
|
|
|
|
2018-11-08 19:23:39 +05:30
|
|
|
context 'when creating a page' do
|
|
|
|
before do
|
|
|
|
project_wiki.create_page('index', 'test content')
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'failing pre-receive hook'
|
|
|
|
it_behaves_like 'failing update hook'
|
|
|
|
it_behaves_like 'failing post-receive hook'
|
|
|
|
it_behaves_like 'when hooks call succceeds'
|
2018-10-15 14:42:47 +05:30
|
|
|
end
|
|
|
|
|
2018-11-08 19:23:39 +05:30
|
|
|
context 'when updating a page' do
|
|
|
|
before do
|
|
|
|
project_wiki.update_page(find_page('home'), content: 'some other content', format: :markdown)
|
|
|
|
end
|
2018-10-15 14:42:47 +05:30
|
|
|
|
2018-11-08 19:23:39 +05:30
|
|
|
it_behaves_like 'failing pre-receive hook'
|
|
|
|
it_behaves_like 'failing update hook'
|
|
|
|
it_behaves_like 'failing post-receive hook'
|
|
|
|
it_behaves_like 'when hooks call succceeds'
|
2018-10-15 14:42:47 +05:30
|
|
|
end
|
|
|
|
|
2018-11-08 19:23:39 +05:30
|
|
|
context 'when deleting a page' do
|
|
|
|
before do
|
|
|
|
project_wiki.delete_page(find_page('home'))
|
|
|
|
end
|
2018-10-15 14:42:47 +05:30
|
|
|
|
2018-11-08 19:23:39 +05:30
|
|
|
it_behaves_like 'failing pre-receive hook'
|
|
|
|
it_behaves_like 'failing update hook'
|
|
|
|
it_behaves_like 'failing post-receive hook'
|
|
|
|
it_behaves_like 'when hooks call succceeds'
|
2018-10-15 14:42:47 +05:30
|
|
|
end
|
|
|
|
|
2018-11-08 19:23:39 +05:30
|
|
|
def find_current_rev
|
|
|
|
wiki.gollum_wiki.repo.commits.first&.sha
|
|
|
|
end
|
2018-10-15 14:42:47 +05:30
|
|
|
|
2018-11-08 19:23:39 +05:30
|
|
|
def find_page(name)
|
|
|
|
wiki.page(title: name)
|
|
|
|
end
|
2018-10-15 14:42:47 +05:30
|
|
|
end
|
|
|
|
|
2018-11-08 19:23:39 +05:30
|
|
|
context 'when Gitaly is enabled' do
|
|
|
|
it_behaves_like 'calling wiki hooks'
|
2018-10-15 14:42:47 +05:30
|
|
|
end
|
|
|
|
|
2018-11-08 19:23:39 +05:30
|
|
|
context 'when Gitaly is disabled', :disable_gitaly do
|
|
|
|
it_behaves_like 'calling wiki hooks'
|
|
|
|
end
|
2018-10-15 14:42:47 +05:30
|
|
|
end
|
|
|
|
end
|