delete_merged_branches_service_spec.rb 2.35 KB
Newer Older
1 2
require 'spec_helper'

3
describe DeleteMergedBranchesService do
4 5
  include ProjectForksHelper

6 7
  subject(:service) { described_class.new(project, project.owner) }

8
  let(:project) { create(:project, :repository) }
9 10

  context '#execute' do
11 12 13 14 15 16 17 18 19 20
    it 'deletes a branch that was merged' do
      service.execute

      expect(project.repository.branch_names).not_to include('improve/awesome')
    end

    it 'keeps branch that is unmerged' do
      service.execute

      expect(project.repository.branch_names).to include('feature')
21 22
    end

23 24
    it 'keeps "master"' do
      service.execute
25

26
      expect(project.repository.branch_names).to include('master')
27 28
    end

29 30 31 32 33 34 35 36
    it 'keeps protected branches' do
      create(:protected_branch, project: project, name: 'improve/awesome')

      service.execute

      expect(project.repository.branch_names).to include('improve/awesome')
    end

37 38 39 40 41 42 43 44
    it 'keeps wildcard protected branches' do
      create(:protected_branch, project: project, name: 'improve/*')

      service.execute

      expect(project.repository.branch_names).to include('improve/awesome')
    end

45 46 47 48 49 50 51 52
    it 'ignores protected tags' do
      create(:protected_tag, project: project, name: 'improve/*')

      service.execute

      expect(project.repository.branch_names).not_to include('improve/awesome')
    end

53 54 55 56 57 58 59
    context 'user without rights' do
      let(:user) { create(:user) }

      it 'cannot execute' do
        expect { described_class.new(project, user).execute }.to raise_error(Gitlab::Access::AccessDeniedError)
      end
    end
60 61 62

    context 'open merge requests' do
      it 'does not delete branches from open merge requests' do
63
        forked_project = fork_project(project)
64
        create(:merge_request, :opened, source_project: project, target_project: project, source_branch: 'branch-merged', target_branch: 'master')
65
        create(:merge_request, :opened, source_project: forked_project, target_project: project, target_branch: 'improve/awesome', source_branch: 'master')
66 67 68 69 70 71 72

        service.execute

        expect(project.repository.branch_names).to include('branch-merged')
        expect(project.repository.branch_names).to include('improve/awesome')
      end
    end
73 74 75 76 77 78 79 80 81 82
  end

  context '#async_execute' do
    it 'calls DeleteMergedBranchesWorker async' do
      expect(DeleteMergedBranchesWorker).to receive(:perform_async)

      service.async_execute
    end
  end
end