namespaceless_project_destroy_worker_spec.rb 2.61 KB
Newer Older
1 2
# frozen_string_literal: true

3 4 5
require 'spec_helper'

describe NamespacelessProjectDestroyWorker do
6 7
  include ProjectForksHelper

8 9 10 11
  subject { described_class.new }

  before do
    # Stub after_save callbacks that will fail when Project has no namespace
12
    allow_any_instance_of(Project).to receive(:ensure_storage_path_exists).and_return(nil)
13 14 15 16 17 18
    allow_any_instance_of(Project).to receive(:update_project_statistics).and_return(nil)
  end

  describe '#perform' do
    context 'project has namespace' do
      it 'does not do anything' do
19
        project = create(:project)
20

21
        subject.perform(project.id)
22 23 24 25 26

        expect(Project.unscoped.all).to include(project)
      end
    end

27 28 29 30 31
    context 'project has no namespace' do
      let!(:project) { create(:project) }

      before do
        allow_any_instance_of(Project).to receive(:namespace).and_return(nil)
32 33 34 35 36 37
      end

      context 'project not a fork of another project' do
        it "truncates the project's team" do
          expect_any_instance_of(ProjectTeam).to receive(:truncate)

38
          subject.perform(project.id)
39 40 41
        end

        it 'deletes the project' do
42
          subject.perform(project.id)
43 44 45 46 47 48 49

          expect(Project.unscoped.all).not_to include(project)
        end

        it 'does not call unlink_fork' do
          is_expected.not_to receive(:unlink_fork)

50
          subject.perform(project.id)
51 52 53 54 55
        end

        it 'does not do anything in Project#remove_pages method' do
          expect(Gitlab::PagesTransfer).not_to receive(:new)

56
          subject.perform(project.id)
57 58 59 60
        end
      end

      context 'project forked from another' do
61
        let!(:parent_project) { create(:project) }
62 63
        let(:project) do
          namespaceless_project = fork_project(parent_project)
64
          namespaceless_project.save
65
          namespaceless_project
66 67 68 69 70
        end

        it 'closes open merge requests' do
          merge_request = create(:merge_request, source_project: project, target_project: parent_project)

71
          subject.perform(project.id)
72 73 74 75

          expect(merge_request.reload).to be_closed
        end

76
        it 'destroys fork network members' do
77
          subject.perform(project.id)
78

79
          expect(parent_project.forked_to_members).to be_empty
80 81 82
        end
      end
    end
83 84 85 86 87 88 89 90 91 92 93 94 95 96

    context 'project has non-existing namespace' do
      let!(:project) do
        project = build(:project, namespace_id: Namespace.maximum(:id).to_i.succ)
        project.save(validate: false)
        project
      end

      it 'deletes the project' do
        subject.perform(project.id)

        expect(Project.unscoped.all).not_to include(project)
      end
    end
97 98
  end
end