get_urls_service_spec.rb 5.92 KB
Newer Older
1 2 3
require "spec_helper"

describe MergeRequests::GetUrlsService do
4
  let(:project) { create(:project, :public, :repository) }
5
  let(:service) { described_class.new(project) }
6
  let(:source_branch) { "my_branch" }
7 8
  let(:new_merge_request_url) { "http://#{Gitlab.config.gitlab.host}/#{project.namespace.name}/#{project.path}/merge_requests/new?merge_request%5Bsource_branch%5D=#{source_branch}" }
  let(:show_merge_request_url) { "http://#{Gitlab.config.gitlab.host}/#{project.namespace.name}/#{project.path}/merge_requests/#{merge_request.iid}" }
9
  let(:new_branch_changes) { "#{Gitlab::Git::BLANK_SHA} 570e7b2abdd848b95f2f578043fc23bd6f6fd24d refs/heads/#{source_branch}" }
10
  let(:deleted_branch_changes) { "d14d6c0abdd253381df51a723d58691b2ee1ab08 #{Gitlab::Git::BLANK_SHA} refs/heads/#{source_branch}" }
11
  let(:existing_branch_changes) { "d14d6c0abdd253381df51a723d58691b2ee1ab08 570e7b2abdd848b95f2f578043fc23bd6f6fd24d refs/heads/#{source_branch}" }
12
  let(:default_branch_changes) { "d14d6c0abdd253381df51a723d58691b2ee1ab08 570e7b2abdd848b95f2f578043fc23bd6f6fd24d refs/heads/master" }
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36

  describe "#execute" do
    shared_examples 'new_merge_request_link' do
      it 'returns url to create new merge request' do
        result = service.execute(changes)
        expect(result).to match([{
          branch_name: source_branch,
          url: new_merge_request_url,
          new_merge_request: true
        }])
      end
    end

    shared_examples 'show_merge_request_url' do
      it 'returns url to view merge request' do
        result = service.execute(changes)
        expect(result).to match([{
          branch_name: source_branch,
          url: show_merge_request_url,
          new_merge_request: false
        }])
      end
    end

37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
    shared_examples 'no_merge_request_url' do
      it 'returns no URL' do
        result = service.execute(changes)
        expect(result).to be_empty
      end
    end

    context 'pushing to default branch' do
      let(:changes) { default_branch_changes }
      it_behaves_like 'no_merge_request_url'
    end

    context 'pushing to project with MRs disabled' do
      let(:changes) { new_branch_changes }

      before do
53
        project.project_feature.update_attribute(:merge_requests_access_level, ProjectFeature::DISABLED)
54 55 56 57 58
      end

      it_behaves_like 'no_merge_request_url'
    end

59 60 61 62 63 64 65 66 67 68
    context 'pushing one completely new branch' do
      let(:changes) { new_branch_changes }
      it_behaves_like 'new_merge_request_link'
    end

    context 'pushing to existing branch but no merge request' do
      let(:changes) { existing_branch_changes }
      it_behaves_like 'new_merge_request_link'
    end

69 70 71 72 73
    context 'pushing to deleted branch' do
      let(:changes) { deleted_branch_changes }
      it_behaves_like 'no_merge_request_url'
    end

74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
    context 'pushing to existing branch and merge request opened' do
      let!(:merge_request) { create(:merge_request, source_project: project, source_branch: source_branch) }
      let(:changes) { existing_branch_changes }
      it_behaves_like 'show_merge_request_url'
    end

    context 'pushing to existing branch and merge request is reopened' do
      let!(:merge_request) { create(:merge_request, :reopened, source_project: project, source_branch: source_branch) }
      let(:changes) { existing_branch_changes }
      it_behaves_like 'show_merge_request_url'
    end

    context 'pushing to existing branch from forked project' do
      let(:user) { create(:user) }
      let!(:forked_project) { Projects::ForkService.new(project, user).execute }
      let!(:merge_request) { create(:merge_request, source_project: forked_project, target_project: project, source_branch: source_branch) }
      let(:changes) { existing_branch_changes }
      # Source project is now the forked one
92
      let(:service) { described_class.new(forked_project) }
93 94 95 96 97

      before do
        allow(forked_project).to receive(:empty_repo?).and_return(false)
      end

98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
      it_behaves_like 'show_merge_request_url'
    end

    context 'pushing to existing branch and merge request is closed' do
      let!(:merge_request) { create(:merge_request, :closed, source_project: project, source_branch: source_branch) }
      let(:changes) { existing_branch_changes }
      it_behaves_like 'new_merge_request_link'
    end

    context 'pushing to existing branch and merge request is merged' do
      let!(:merge_request) { create(:merge_request, :merged, source_project: project, source_branch: source_branch) }
      let(:changes) { existing_branch_changes }
      it_behaves_like 'new_merge_request_link'
    end

    context 'pushing new branch and existing branch (with merge request created) at once' do
      let!(:merge_request) { create(:merge_request, source_project: project, source_branch: "existing_branch") }
      let(:new_branch_changes) { "#{Gitlab::Git::BLANK_SHA} 570e7b2abdd848b95f2f578043fc23bd6f6fd24d refs/heads/new_branch" }
      let(:existing_branch_changes) { "d14d6c0abdd253381df51a723d58691b2ee1ab08 570e7b2abdd848b95f2f578043fc23bd6f6fd24d refs/heads/existing_branch" }
      let(:changes) { "#{new_branch_changes}\n#{existing_branch_changes}" }
118
      let(:new_merge_request_url) { "http://#{Gitlab.config.gitlab.host}/#{project.namespace.name}/#{project.path}/merge_requests/new?merge_request%5Bsource_branch%5D=new_branch" }
119 120 121 122 123 124 125 126 127 128 129 130 131 132

      it 'returns 2 urls for both creating new and showing merge request' do
        result = service.execute(changes)
        expect(result).to match([{
          branch_name: "new_branch",
          url: new_merge_request_url,
          new_merge_request: true
        }, {
          branch_name: "existing_branch",
          url: show_merge_request_url,
          new_merge_request: false
        }])
      end
    end
133 134 135 136 137 138 139 140 141 142

    context 'when printing_merge_request_link_enabled is false' do
      it 'returns empty array' do
        project.update!(printing_merge_request_link_enabled: false)

        result = service.execute(existing_branch_changes)

        expect(result).to eq([])
      end
    end
143 144
  end
end