repository_import_worker_spec.rb 3.45 KB
Newer Older
1 2 3
require 'spec_helper'

describe RepositoryImportWorker do
4 5 6 7 8
  describe 'modules' do
    it 'includes ProjectImportOptions' do
      expect(described_class).to include_module(ProjectImportOptions)
    end
  end
9 10

  describe '#perform' do
11
    let(:project) { create(:project, :import_scheduled) }
12
    let(:import_state) { project.import_state }
13

14 15
    context 'when worker was reset without cleanup' do
      it 'imports the project successfully' do
16 17
        jid = '12345678'
        started_project = create(:project)
18
        started_import_state = create(:import_state, :started, project: started_project, jid: jid)
19

20 21 22 23 24
        allow(subject).to receive(:jid).and_return(jid)

        expect_any_instance_of(Projects::ImportService).to receive(:execute)
          .and_return({ status: :ok })

25
        # Works around https://github.com/rspec/rspec-mocks/issues/910
26 27 28 29
        expect(Project).to receive(:find).with(started_project.id).and_return(started_project)
        expect(started_project.repository).to receive(:expire_emptiness_caches)
        expect(started_project.wiki.repository).to receive(:expire_emptiness_caches)
        expect(started_import_state).to receive(:finish)
30

31
        subject.perform(started_project.id)
32 33 34
      end
    end

35 36
    context 'when the import was successful' do
      it 'imports a project' do
37 38
        expect_any_instance_of(Projects::ImportService).to receive(:execute)
          .and_return({ status: :ok })
39

40 41 42 43
        # Works around https://github.com/rspec/rspec-mocks/issues/910
        expect(Project).to receive(:find).with(project.id).and_return(project)
        expect(project.repository).to receive(:expire_emptiness_caches)
        expect(project.wiki.repository).to receive(:expire_emptiness_caches)
44
        expect(import_state).to receive(:finish)
45

46 47 48 49 50 51
        subject.perform(project.id)
      end
    end

    context 'when the import has failed' do
      it 'hide the credentials that were used in the import URL' do
52
        error = %q{remote: Not Found fatal: repository 'https://user:pass@test.com/root/repoC.git/' not found }
53

54
        import_state.update(jid: '123')
55
        expect_any_instance_of(Projects::ImportService).to receive(:execute).and_return({ status: :error, message: error })
56

57 58
        expect do
          subject.perform(project.id)
James Lopez's avatar
James Lopez committed
59
        end.to raise_error(RuntimeError, error)
60
        expect(import_state.reload.jid).not_to be_nil
61
      end
James Lopez's avatar
James Lopez committed
62 63 64 65

      it 'updates the error on Import/Export' do
        error = %q{remote: Not Found fatal: repository 'https://user:pass@test.com/root/repoC.git/' not found }

66 67
        project.update(import_type: 'gitlab_project')
        import_state.update(jid: '123')
James Lopez's avatar
James Lopez committed
68 69 70 71
        expect_any_instance_of(Projects::ImportService).to receive(:execute).and_return({ status: :error, message: error })

        expect do
          subject.perform(project.id)
James Lopez's avatar
James Lopez committed
72
        end.to raise_error(RuntimeError, error)
James Lopez's avatar
James Lopez committed
73

74
        expect(import_state.reload.last_error).not_to be_nil
James Lopez's avatar
James Lopez committed
75
      end
76
    end
77

78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
    context 'when using an asynchronous importer' do
      it 'does not mark the import process as finished' do
        service = double(:service)

        allow(Projects::ImportService)
          .to receive(:new)
          .and_return(service)

        allow(service)
          .to receive(:execute)
          .and_return(true)

        allow(service)
          .to receive(:async?)
          .and_return(true)

94 95
        expect_any_instance_of(ProjectImportState)
          .not_to receive(:finish)
96 97 98 99

        subject.perform(project.id)
      end
    end
100 101
  end
end