summaryrefslogtreecommitdiff
path: root/spec/workers/repository_cleanup_worker_spec.rb
blob: 3adae0b6cfa4a326d7947359d382b98e42feddca (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
require 'spec_helper'

describe RepositoryCleanupWorker do
  let(:project) { create(:project) }
  let(:user) { create(:user) }

  subject(:worker) { described_class.new }

  describe '#perform' do
    it 'executes the cleanup service and sends a success notification' do
      expect_next_instance_of(Projects::CleanupService) do |service|
        expect(service.project).to eq(project)
        expect(service.current_user).to eq(user)

        expect(service).to receive(:execute)
      end

      expect_next_instance_of(NotificationService) do |service|
        expect(service).to receive(:repository_cleanup_success).with(project, user)
      end

      worker.perform(project.id, user.id)
    end

    it 'raises an error if the project cannot be found' do
      project.destroy

      expect { worker.perform(project.id, user.id) }.to raise_error(ActiveRecord::RecordNotFound)
    end

    it 'raises an error if the user cannot be found' do
      user.destroy

      expect { worker.perform(project.id, user.id) }.to raise_error(ActiveRecord::RecordNotFound)
    end
  end

  describe '#sidekiq_retries_exhausted' do
    let(:job) { { 'args' => [project.id, user.id], 'error_message' => 'Error' } }

    it 'does not send a failure notification for a RecordNotFound error' do
      expect(NotificationService).not_to receive(:new)

      described_class.sidekiq_retries_exhausted_block.call(job, ActiveRecord::RecordNotFound.new)
    end

    it 'sends a failure notification' do
      expect_next_instance_of(NotificationService) do |service|
        expect(service).to receive(:repository_cleanup_failure).with(project, user, 'Error')
      end

      described_class.sidekiq_retries_exhausted_block.call(job, StandardError.new)
    end
  end
end