Welcome to mirror list, hosted at ThFree Co, Russian Federation.

destroy_service_spec.rb « web_hooks « services « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 5269fe08ac0cebd3c085c0da2a094ab232b49d08 (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe WebHooks::DestroyService do
  let_it_be(:user) { create(:user) }

  subject { described_class.new(user) }

  shared_examples 'batched destroys' do
    it 'destroys all hooks in batches' do
      stub_const("#{described_class}::BATCH_SIZE", 1)
      expect(subject).to receive(:delete_web_hook_logs_in_batches).exactly(4).times.and_call_original

      expect do
        status = subject.execute(hook)
        expect(status[:async]).to be false
      end
        .to change { WebHook.count }.from(1).to(0)
        .and change { WebHookLog.count }.from(3).to(0)
    end

    it 'returns an error if sync destroy fails' do
      expect(hook).to receive(:destroy).and_return(false)

      result = subject.sync_destroy(hook)

      expect(result[:status]).to eq(:error)
      expect(result[:message]).to eq("Unable to destroy #{hook.model_name.human}")
    end

    it 'schedules an async delete' do
      stub_const('WebHooks::DestroyService::LOG_COUNT_THRESHOLD', 1)

      expect(WebHooks::DestroyWorker).to receive(:perform_async).with(user.id, hook.id).and_call_original

      status = subject.execute(hook)

      expect(status[:async]).to be true
    end
  end

  context 'with system hook' do
    let!(:hook) { create(:system_hook, url: "http://example.com") }
    let!(:log) { create_list(:web_hook_log, 3, web_hook: hook) }

    it_behaves_like 'batched destroys'
  end

  context 'with project hook' do
    let!(:hook) { create(:project_hook) }
    let!(:log) { create_list(:web_hook_log, 3, web_hook: hook) }

    it_behaves_like 'batched destroys'
  end
end