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

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

365 lines
11 KiB
Ruby
Raw Normal View History

2020-03-13 15:44:24 +05:30
# frozen_string_literal: true
require 'spec_helper'
2023-05-27 22:25:52 +05:30
RSpec.describe PostReceiveService, feature_category: :team_planning do
2022-10-11 01:57:18 +05:30
include GitlabShellHelpers
2020-03-13 15:44:24 +05:30
include Gitlab::Routing
let_it_be(:user) { create(:user) }
2020-04-08 14:13:33 +05:30
let_it_be(:project) { create(:project, :repository, :wiki_repo, namespace: user.namespace) }
let_it_be(:project_snippet) { create(:project_snippet, :repository, project: project, author: user) }
let_it_be(:personal_snippet) { create(:personal_snippet, :repository, author: user) }
2020-03-13 15:44:24 +05:30
let(:identifier) { 'key-123' }
let(:gl_repository) { "project-#{project.id}" }
let(:branch_name) { 'feature' }
let(:reference_counter) { double('ReferenceCounter') }
let(:push_options) { ['ci.skip', 'another push option'] }
2020-04-08 14:13:33 +05:30
let(:repository) { project.repository }
2020-03-13 15:44:24 +05:30
let(:changes) do
"#{Gitlab::Git::BLANK_SHA} 570e7b2abdd848b95f2f578043fc23bd6f6fd24d refs/heads/#{branch_name}"
end
let(:params) do
{
gl_repository: gl_repository,
identifier: identifier,
changes: changes,
push_options: push_options
}
end
2020-04-08 14:13:33 +05:30
let(:service) { described_class.new(user, repository, project, params) }
let(:response) { service.execute }
2020-03-13 15:44:24 +05:30
subject { response.messages.as_json }
2020-04-08 14:13:33 +05:30
context 'when project is nil' do
let(:gl_repository) { "snippet-#{personal_snippet.id}" }
let(:project) { nil }
let(:repository) { personal_snippet.repository }
2020-03-13 15:44:24 +05:30
2020-04-08 14:13:33 +05:30
it 'does not return error' do
expect(subject).to be_empty
end
2021-02-22 17:27:13 +05:30
2021-03-11 19:13:27 +05:30
it_behaves_like 'does not record an onboarding progress action'
2020-03-13 15:44:24 +05:30
end
2020-04-08 14:13:33 +05:30
context 'when repository is nil' do
let(:repository) { nil }
2020-03-13 15:44:24 +05:30
2020-04-08 14:13:33 +05:30
it 'does not return error' do
expect(subject).to be_empty
end
2020-03-13 15:44:24 +05:30
end
2020-04-08 14:13:33 +05:30
context 'when both repository and project are nil' do
let(:gl_repository) { "snippet-#{personal_snippet.id}" }
let(:project) { nil }
let(:repository) { nil }
2020-03-13 15:44:24 +05:30
2020-04-08 14:13:33 +05:30
it 'does not return error' do
expect(subject).to be_empty
end
2020-03-13 15:44:24 +05:30
end
2020-04-08 14:13:33 +05:30
shared_examples 'post_receive_service actions' do
it 'enqueues a PostReceive worker job' do
expect(PostReceive).to receive(:perform_async)
.with(gl_repository, identifier, changes, { ci: { skip: true } })
2020-03-13 15:44:24 +05:30
2020-04-08 14:13:33 +05:30
subject
end
2020-03-13 15:44:24 +05:30
2020-04-08 14:13:33 +05:30
it 'decreases the reference counter and returns the result' do
expect(Gitlab::ReferenceCounter).to receive(:new).with(gl_repository)
.and_return(reference_counter)
expect(reference_counter).to receive(:decrease).and_return(true)
2020-03-13 15:44:24 +05:30
2020-04-08 14:13:33 +05:30
expect(response.reference_counter_decreased).to be(true)
2020-03-13 15:44:24 +05:30
end
2021-02-22 17:27:13 +05:30
2021-03-11 19:13:27 +05:30
it_behaves_like 'records an onboarding progress action', :git_write do
let(:namespace) { project.namespace }
2021-02-22 17:27:13 +05:30
end
2020-03-13 15:44:24 +05:30
end
2020-04-08 14:13:33 +05:30
context 'with Project' do
it_behaves_like 'post_receive_service actions'
2020-03-13 15:44:24 +05:30
2020-04-08 14:13:33 +05:30
it 'returns link to create new merge request' do
message = <<~MESSAGE.strip
To create a merge request for #{branch_name}, visit:
http://#{Gitlab.config.gitlab.host}/#{project.full_path}/-/merge_requests/new?merge_request%5Bsource_branch%5D=#{branch_name}
MESSAGE
expect(subject).to include(build_basic_message(message))
end
it 'returns the link to an existing merge request when it exists' do
merge_request = create(:merge_request, source_project: project, source_branch: branch_name, target_branch: 'master')
message = <<~MESSAGE.strip
View merge request for feature:
#{project_merge_request_url(project, merge_request)}
MESSAGE
2020-03-13 15:44:24 +05:30
2020-04-08 14:13:33 +05:30
expect(subject).to include(build_basic_message(message))
end
2020-03-13 15:44:24 +05:30
2020-04-08 14:13:33 +05:30
context 'when printing_merge_request_link_enabled is false' do
let(:project) { create(:project, printing_merge_request_link_enabled: false) }
it 'returns no merge request messages' do
expect(subject).to be_blank
end
2020-03-13 15:44:24 +05:30
end
2020-04-08 14:13:33 +05:30
it 'does not invoke MergeRequests::PushOptionsHandlerService' do
expect(MergeRequests::PushOptionsHandlerService).not_to receive(:new)
2020-03-13 15:44:24 +05:30
subject
end
2020-04-08 14:13:33 +05:30
context 'when there are merge_request push options' do
let(:params) { super().merge(push_options: ['merge_request.create']) }
2020-03-13 15:44:24 +05:30
2020-04-08 14:13:33 +05:30
before do
project.add_developer(user)
end
2020-03-13 15:44:24 +05:30
2021-10-27 15:23:28 +05:30
it 'invalidates the branch name cache' do
expect(service.repository).to receive(:expire_branches_cache).and_call_original
subject
end
2020-04-08 14:13:33 +05:30
it 'invokes MergeRequests::PushOptionsHandlerService' do
expect(MergeRequests::PushOptionsHandlerService).to receive(:new).and_call_original
subject
end
it 'creates a new merge request' do
expect { Sidekiq::Testing.fake! { subject } }.to change(MergeRequest, :count).by(1)
end
it 'links to the newly created merge request' do
message = <<~MESSAGE.strip
View merge request for #{branch_name}:
http://#{Gitlab.config.gitlab.host}/#{project.full_path}/-/merge_requests/1
MESSAGE
expect(subject).to include(build_basic_message(message))
end
it 'adds errors on the service instance to warnings' do
expect_any_instance_of(
MergeRequests::PushOptionsHandlerService
).to receive(:errors).at_least(:once).and_return(['my error'])
message = "WARNINGS:\nError encountered with push options 'merge_request.create': my error"
expect(subject).to include(build_alert_message(message))
end
it 'adds ActiveRecord errors on invalid MergeRequest records to warnings' do
invalid_merge_request = MergeRequest.new
invalid_merge_request.errors.add(:base, 'my error')
message = "WARNINGS:\nError encountered with push options 'merge_request.create': my error"
expect_any_instance_of(
MergeRequests::CreateService
).to receive(:execute).and_return(invalid_merge_request)
expect(subject).to include(build_alert_message(message))
end
2020-03-13 15:44:24 +05:30
end
2020-04-08 14:13:33 +05:30
end
context 'with PersonalSnippet' do
let(:gl_repository) { "snippet-#{personal_snippet.id}" }
let(:repository) { personal_snippet.repository }
2020-03-13 15:44:24 +05:30
2020-04-08 14:13:33 +05:30
it_behaves_like 'post_receive_service actions'
2020-03-13 15:44:24 +05:30
2020-04-08 14:13:33 +05:30
it 'does not return link to create new merge request' do
expect(subject).to be_empty
end
it 'does not return the link to an existing merge request when it exists' do
create(:merge_request, source_project: project, source_branch: branch_name, target_branch: 'master')
2020-03-13 15:44:24 +05:30
2020-04-08 14:13:33 +05:30
expect(subject).to be_empty
2020-03-13 15:44:24 +05:30
end
2020-04-08 14:13:33 +05:30
end
2020-03-13 15:44:24 +05:30
2020-04-08 14:13:33 +05:30
context 'with ProjectSnippet' do
let(:gl_repository) { "snippet-#{project_snippet.id}" }
let(:repository) { project_snippet.repository }
2020-03-13 15:44:24 +05:30
2020-04-08 14:13:33 +05:30
it_behaves_like 'post_receive_service actions'
2020-03-13 15:44:24 +05:30
2020-04-08 14:13:33 +05:30
it 'does not return link to create new merge request' do
expect(subject).to be_empty
end
it 'does not return the link to an existing merge request when it exists' do
create(:merge_request, source_project: project, source_branch: branch_name, target_branch: 'master')
expect(subject).to be_empty
2020-03-13 15:44:24 +05:30
end
end
2020-04-08 14:13:33 +05:30
context 'broadcast message banner exists' do
2020-03-13 15:44:24 +05:30
it 'outputs a broadcast message' do
2020-04-08 14:13:33 +05:30
broadcast_message = create(:broadcast_message)
2020-03-13 15:44:24 +05:30
expect(subject).to include(build_alert_message(broadcast_message.message))
end
end
2020-04-08 14:13:33 +05:30
context 'broadcast message notification exists' do
it 'does not output a broadcast message' do
create(:broadcast_message, :notification)
expect(has_alert_messages?(subject)).to be_falsey
end
end
2020-03-13 15:44:24 +05:30
context 'broadcast message does not exist' do
it 'does not output a broadcast message' do
expect(has_alert_messages?(subject)).to be_falsey
end
end
context 'nil broadcast message' do
it 'does not output a broadcast message' do
allow(BroadcastMessage).to receive(:current).and_return(nil)
expect(has_alert_messages?(subject)).to be_falsey
end
end
2021-01-29 00:20:46 +05:30
context "broadcast message has a target_path" do
let!(:older_scoped_message) do
create(:broadcast_message, message: "Old top secret", target_path: "/company/sekrit-project")
end
let!(:latest_scoped_message) do
create(:broadcast_message, message: "Top secret", target_path: "/company/sekrit-project")
end
let!(:unscoped_message) do
create(:broadcast_message, message: "Hi")
end
context "no project path matches" do
it "does not output the scoped broadcast messages" do
expect(subject).not_to include(build_alert_message(older_scoped_message.message))
expect(subject).not_to include(build_alert_message(latest_scoped_message.message))
end
it "does output another message that doesn't have a target_path" do
expect(subject).to include(build_alert_message(unscoped_message.message))
end
end
context "project path matches" do
before do
2021-06-08 01:23:25 +05:30
allow(project).to receive(:full_path).and_return("company/sekrit-project")
2021-01-29 00:20:46 +05:30
end
it "does output the latest scoped broadcast message" do
expect(subject).to include(build_alert_message(latest_scoped_message.message))
end
it "does not output the older scoped broadcast message" do
expect(subject).not_to include(build_alert_message(older_scoped_message.message))
end
it "does not output another message that doesn't have a target_path" do
expect(subject).not_to include(build_alert_message(unscoped_message.message))
end
end
end
2020-03-13 15:44:24 +05:30
context 'with a redirected data' do
it 'returns redirected message on the response' do
2021-09-30 23:02:18 +05:30
project_moved = Gitlab::Checks::ContainerMoved.new(project.repository, user, 'http', 'foo/baz')
2020-03-13 15:44:24 +05:30
project_moved.add_message
expect(subject).to include(build_basic_message(project_moved.message))
end
end
context 'with new project data' do
it 'returns new project message on the response' do
2020-04-08 14:13:33 +05:30
project_created = Gitlab::Checks::ProjectCreated.new(project.repository, user, 'http')
2020-03-13 15:44:24 +05:30
project_created.add_message
expect(subject).to include(build_basic_message(project_created.message))
end
end
2020-04-08 14:13:33 +05:30
describe '#process_mr_push_options' do
context 'when repository belongs to a snippet' do
context 'with PersonalSnippet' do
let(:repository) { personal_snippet.repository }
it 'returns an error message' do
result = service.process_mr_push_options(push_options, changes)
expect(result).to match('Push options are only supported for projects')
end
end
context 'with ProjectSnippet' do
let(:repository) { project_snippet.repository }
it 'returns an error message' do
result = service.process_mr_push_options(push_options, changes)
expect(result).to match('Push options are only supported for projects')
end
end
end
end
describe '#merge_request_urls' do
context 'when repository belongs to a snippet' do
context 'with PersonalSnippet' do
let(:repository) { personal_snippet.repository }
it 'returns an empty array' do
expect(service.merge_request_urls).to be_empty
end
end
context 'with ProjectSnippet' do
let(:repository) { project_snippet.repository }
it 'returns an empty array' do
expect(service.merge_request_urls).to be_empty
end
end
end
end
2020-03-13 15:44:24 +05:30
def build_alert_message(message)
{ 'type' => 'alert', 'message' => message }
end
def build_basic_message(message)
{ 'type' => 'basic', 'message' => message }
end
def has_alert_messages?(messages)
messages.any? do |message|
message['type'] == 'alert'
end
end
end