diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2020-02-18 18:08:51 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2020-02-18 18:08:51 +0300 |
commit | 163a7046ac76eb4109184e82ce0af911633e6626 (patch) | |
tree | 9f22bb438db435d518e8f5520b309c6319ae0bd8 /spec/requests/api | |
parent | 0637ba1e6e9024f35b2cbf561d9002ec17350bb3 (diff) |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'spec/requests/api')
-rw-r--r-- | spec/requests/api/internal/base_spec.rb | 193 |
1 files changed, 11 insertions, 182 deletions
diff --git a/spec/requests/api/internal/base_spec.rb b/spec/requests/api/internal/base_spec.rb index 02dc2229769..c3b5f9ded21 100644 --- a/spec/requests/api/internal/base_spec.rb +++ b/spec/requests/api/internal/base_spec.rb @@ -811,6 +811,8 @@ describe API::Internal::Base do describe 'POST /internal/post_receive', :clean_gitlab_redis_shared_state do let(:identifier) { 'key-123' } + let(:branch_name) { 'feature' } + let(:push_options) { ['ci.skip', 'another push option'] } let(:valid_params) do { @@ -822,192 +824,33 @@ describe API::Internal::Base do } end - let(:branch_name) { 'feature' } - let(:changes) do "#{Gitlab::Git::BLANK_SHA} 570e7b2abdd848b95f2f578043fc23bd6f6fd24d refs/heads/#{branch_name}" end - let(:push_options) do - ['ci.skip', - 'another push option'] - end + subject { post api('/internal/post_receive'), params: valid_params } before do project.add_developer(user) allow_any_instance_of(Gitlab::Identifier).to receive(:identify).and_return(user) end - it 'enqueues a PostReceive worker job' do - expect(PostReceive).to receive(:perform_async) - .with(gl_repository, identifier, changes, { ci: { skip: true } }) - - post api('/internal/post_receive'), params: valid_params - end - - 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) - - post api('/internal/post_receive'), params: valid_params - - expect(json_response['reference_counter_decreased']).to be(true) - end - - it 'returns link to create new merge request' do - post api('/internal/post_receive'), params: valid_params - + it 'executes PostReceiveService' 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(json_response['messages']).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') - - post api('/internal/post_receive'), params: valid_params - - message = <<~MESSAGE.strip - View merge request for feature: - #{project_merge_request_url(project, merge_request)} - MESSAGE - - expect(json_response['messages']).to include(build_basic_message(message)) - end - - it 'returns no merge request messages if printing_merge_request_link_enabled is false' do - project.update!(printing_merge_request_link_enabled: false) - - post api('/internal/post_receive'), params: valid_params - - expect(json_response['messages']).to be_blank - end - - it 'does not invoke MergeRequests::PushOptionsHandlerService' do - expect(MergeRequests::PushOptionsHandlerService).not_to receive(:new) + subject - post api('/internal/post_receive'), params: valid_params + expect(json_response).to eq({ + 'messages' => [{ 'message' => message, 'type' => 'basic' }], + 'reference_counter_decreased' => true + }) end it_behaves_like 'storing arguments in the application context' do let(:expected_params) { { user: user.username, project: project.full_path } } - - subject { post api('/internal/post_receive'), params: valid_params } - end - - context 'when there are merge_request push options' do - before do - valid_params[:push_options] = ['merge_request.create'] - end - - it 'invokes MergeRequests::PushOptionsHandlerService' do - expect(MergeRequests::PushOptionsHandlerService).to receive(:new) - - post api('/internal/post_receive'), params: valid_params - end - - it 'creates a new merge request' do - expect do - Sidekiq::Testing.fake! do - post api('/internal/post_receive'), params: valid_params - end - end.to change { MergeRequest.count }.by(1) - end - - it 'links to the newly created merge request' do - post api('/internal/post_receive'), params: valid_params - - message = <<~MESSAGE.strip - View merge request for #{branch_name}: - http://#{Gitlab.config.gitlab.host}/#{project.full_path}/-/merge_requests/1 - MESSAGE - - expect(json_response['messages']).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']) - - post api('/internal/post_receive'), params: valid_params - - message = "WARNINGS:\nError encountered with push options 'merge_request.create': my error" - expect(json_response['messages']).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') - - expect_any_instance_of( - MergeRequests::CreateService - ).to receive(:execute).and_return(invalid_merge_request) - - post api('/internal/post_receive'), params: valid_params - - message = "WARNINGS:\nError encountered with push options 'merge_request.create': my error" - expect(json_response['messages']).to include(build_alert_message(message)) - end - end - - context 'broadcast message exists' do - let!(:broadcast_message) { create(:broadcast_message, starts_at: 1.day.ago, ends_at: 1.day.from_now ) } - - it 'outputs a broadcast message' do - post api('/internal/post_receive'), params: valid_params - - expect(response).to have_gitlab_http_status(:ok) - expect(json_response['messages']).to include(build_alert_message(broadcast_message.message)) - end - end - - context 'broadcast message does not exist' do - it 'does not output a broadcast message' do - post api('/internal/post_receive'), params: valid_params - - expect(response).to have_gitlab_http_status(:ok) - expect(has_alert_messages?(json_response['messages'])).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) - - post api('/internal/post_receive'), params: valid_params - - expect(response).to have_gitlab_http_status(:ok) - expect(has_alert_messages?(json_response['messages'])).to be_falsey - end - end - - context 'with a redirected data' do - it 'returns redirected message on the response' do - project_moved = Gitlab::Checks::ProjectMoved.new(project, user, 'http', 'foo/baz') - project_moved.add_message - - post api('/internal/post_receive'), params: valid_params - - expect(response).to have_gitlab_http_status(:ok) - expect(json_response['messages']).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 - project_created = Gitlab::Checks::ProjectCreated.new(project, user, 'http') - project_created.add_message - - post api('/internal/post_receive'), params: valid_params - - expect(response).to have_gitlab_http_status(:ok) - expect(json_response['messages']).to include(build_basic_message(project_created.message)) - end end context 'with an orphaned write deploy key' do @@ -1016,7 +859,7 @@ describe API::Internal::Base do expect(Gitlab::Checks::ProjectMoved).not_to receive(:fetch_message) - post api('/internal/post_receive'), params: valid_params + subject expect(response).to have_gitlab_http_status(:ok) end @@ -1030,7 +873,7 @@ describe API::Internal::Base do expect(Gitlab::Checks::ProjectMoved).not_to receive(:fetch_message) - post api('/internal/post_receive'), params: valid_params + subject expect(response).to have_gitlab_http_status(:ok) end @@ -1142,18 +985,4 @@ describe API::Internal::Base do } ) end - - 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 |