File: execute_worker_spec.rb

package info (click to toggle)
gitlab 17.6.5-19
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 629,368 kB
  • sloc: ruby: 1,915,304; javascript: 557,307; sql: 60,639; xml: 6,509; sh: 4,567; makefile: 1,239; python: 406
file content (144 lines) | stat: -rw-r--r-- 4,227 bytes parent folder | download
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Integrations::ExecuteWorker, '#perform', feature_category: :integrations do
  let_it_be(:integration) { create(:jira_integration) }

  let(:worker) { described_class.new }

  it 'executes integration with given data' do
    data = { test: 'test' }

    expect_next_found_instance_of(integration.class) do |integration|
      expect(integration).to receive(:execute).with(data)
    end

    worker.perform(integration.id, data)
  end

  it 'logs error messages' do
    error = StandardError.new('invalid URL')

    expect_next_found_instance_of(integration.class) do |integration|
      expect(integration).to receive(:execute).and_raise(error)
      expect(integration).to receive(:log_exception).with(error)
    end

    worker.perform(integration.id, {})
  end

  context 'when integration cannot be found' do
    it 'completes silently and does not log an error' do
      expect(Gitlab::IntegrationsLogger).not_to receive(:error)

      expect do
        worker.perform(non_existing_record_id, {})
      end.not_to raise_error
    end
  end

  context 'when the Gitlab::SilentMode is enabled' do
    before do
      allow(Gitlab::SilentMode).to receive(:enabled?).and_return(true)
    end

    it 'completes silently and does not log an error' do
      expect(Gitlab::IntegrationsLogger).not_to receive(:error)

      expect do
        worker.perform(non_existing_record_id, {})
      end.not_to raise_error
    end
  end

  context 'when object is wiki_page' do
    let_it_be(:container) { create(:project) }
    let_it_be(:wiki) { container.wiki }
    let_it_be(:content) { 'test content' }
    let_it_be(:wiki_page) { create(:wiki_page, container: container, content: content) }

    let(:object_kind) { 'wiki_page' }
    let(:slug) { wiki_page.slug }
    let(:version_id) { wiki_page.version.id }
    let(:args) do
      {
        object_kind: object_kind,
        project: {
          id: container.id
        },
        object_attributes: {
          slug: slug,
          version_id: version_id
        }
      }
    end

    it 'injects content into wiki_page' do
      expected_data = args.deep_merge(object_attributes: { content: content })

      expect(ProjectWiki).to receive(:find_by_id).with(container.id).and_return(wiki)
      expect(wiki).to receive(:find_page).with(slug, version_id).and_return(wiki_page)
      expect_next_found_instance_of(integration.class) do |integration|
        expect(integration).to receive(:execute).with(expected_data)
      end

      worker.perform(integration.id, args)
    end

    context 'when parameter slug empty' do
      let(:slug) { '' }

      it 'uses existing data' do
        expected_data = args

        expect_next_found_instance_of(integration.class) do |integration|
          expect(integration).to receive(:execute).with(expected_data)
        end

        worker.perform(integration.id, args)
      end
    end

    context 'when parameter version_id empty' do
      let(:version_id) { '' }

      it 'uses existing data' do
        expected_data = args

        expect_next_found_instance_of(integration.class) do |integration|
          expect(integration).to receive(:execute).with(expected_data)
        end

        worker.perform(integration.id, args)
      end
    end

    context 'when wiki empty' do
      it 'uses existing data' do
        expected_data = args

        expect(ProjectWiki).to receive(:find_by_id).with(container.id).and_return(nil)
        expect_next_found_instance_of(integration.class) do |integration|
          expect(integration).to receive(:execute).with(expected_data)
        end

        worker.perform(integration.id, args)
      end
    end

    context 'when wiki page empty' do
      it 'uses existing data' do
        expected_data = args

        expect(ProjectWiki).to receive(:find_by_id).with(container.id).and_return(wiki)
        expect(wiki).to receive(:find_page).with(slug, version_id).and_return(nil)
        expect_next_found_instance_of(integration.class) do |integration|
          expect(integration).to receive(:execute).with(expected_data)
        end

        worker.perform(integration.id, args)
      end
    end
  end
end