summaryrefslogtreecommitdiff
path: root/spec/workers/repository_import_worker_spec.rb
blob: 6b30dabc80e0a33ff0b3868f1b95d4d656ef7f91 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
require 'spec_helper'

describe RepositoryImportWorker do
  let(:project) { create(:empty_project, :import_scheduled) }

  subject { described_class.new }

  describe '#perform' do
    context 'when the import was successful' do
      it 'imports a project' do
        expect_any_instance_of(Projects::ImportService).to receive(:execute)
          .and_return({ status: :ok })

        expect_any_instance_of(Repository).to receive(:expire_emptiness_caches)
        expect_any_instance_of(Project).to receive(:import_finish)

        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
        error = %q{remote: Not Found fatal: repository 'https://user:pass@test.com/root/repoC.git/' not found }

        expect_any_instance_of(Projects::ImportService).to receive(:execute).and_return({ status: :error, message: error })
        allow(subject).to receive(:jid).and_return('123')

        expect do
          subject.perform(project.id)
        end.to raise_error(RepositoryImportWorker::ImportError, error)
        expect(project.reload.import_jid).not_to be_nil
      end
    end

    context 'with unexpected error' do
      it 'marks import as failed' do
        allow_any_instance_of(Projects::ImportService).to receive(:execute).and_raise(RuntimeError)

        expect do
          subject.perform(project.id)
        end.to raise_error(RepositoryImportWorker::ImportError)
        expect(project.reload.import_status).to eq('failed')
      end
    end
  end
end