summaryrefslogtreecommitdiff
path: root/spec/services/repositories/destroy_rollback_service_spec.rb
blob: c3cdae17de7d6e78266f05083a228db7be4cae76 (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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# frozen_string_literal: true

require 'spec_helper'

describe Repositories::DestroyRollbackService do
  let_it_be(:user) { create(:user) }
  let!(:project) { create(:project, :repository, namespace: user.namespace) }
  let(:repository) { project.repository }
  let(:path) { repository.disk_path }
  let(:remove_path) { "#{path}+#{project.id}#{described_class::DELETED_FLAG}" }

  subject { described_class.new(repository).execute }

  before do
    # Dont run sidekiq to check if renamed repository exists
    Sidekiq::Testing.fake! { destroy_project(project, user) }
  end

  it 'moves the repository from the +deleted folder' do
    expect(project.gitlab_shell.repository_exists?(project.repository_storage, remove_path + '.git')).to be_truthy
    expect(project.gitlab_shell.repository_exists?(project.repository_storage, path + '.git')).to be_falsey

    subject

    expect(project.gitlab_shell.repository_exists?(project.repository_storage, remove_path + '.git')).to be_falsey
    expect(project.gitlab_shell.repository_exists?(project.repository_storage, path + '.git')).to be_truthy
  end

  it 'logs the successful action' do
    expect(Gitlab::AppLogger).to receive(:info)

    subject
  end

  it 'flushes the repository cache' do
    expect(repository).to receive(:before_delete)

    subject
  end

  it 'returns success and does not perform any action if repository path does not exist' do
    expect(repository).to receive(:disk_path).and_return('foo')
    expect(repository).not_to receive(:before_delete)

    result = subject

    expect(result[:status]).to eq :success
  end

  context 'when move operation cannot be performed' do
    let(:service) { described_class.new(repository) }

    before do
      allow(service).to receive(:mv_repository).and_return(false)
    end

    it 'returns error' do
      result = service.execute

      expect(result[:status]).to eq :error
    end

    it 'logs the error' do
      expect(Gitlab::AppLogger).to receive(:error)

      service.execute
    end
  end

  def destroy_project(project, user)
    Projects::DestroyService.new(project, user, {}).execute
  end
end