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

project_hook_spec.rb « hooks « models « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: c3484c4a42c97ce5a4f8cdfb34b5b76cf0af8bb4 (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
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe ProjectHook, feature_category: :integrations do
  include_examples 'a hook that gets automatically disabled on failure' do
    let_it_be(:project) { create(:project) }

    let(:hook) { build(:project_hook, project: project) }
    let(:hook_factory) { :project_hook }
    let(:default_factory_arguments) { { project: project } }

    def find_hooks
      project.hooks
    end
  end

  describe 'associations' do
    it { is_expected.to belong_to :project }
  end

  describe 'validations' do
    it { is_expected.to validate_presence_of(:project) }
  end

  it_behaves_like 'includes Limitable concern' do
    subject { build(:project_hook) }
  end

  describe '.for_projects' do
    it 'finds related project hooks' do
      hook_a = create(:project_hook, project: build(:project))
      hook_b = create(:project_hook, project: build(:project))
      hook_c = create(:project_hook, project: build(:project))

      expect(described_class.for_projects([hook_a.project, hook_b.project]))
        .to contain_exactly(hook_a, hook_b)
      expect(described_class.for_projects(hook_c.project))
        .to contain_exactly(hook_c)
    end
  end

  describe '.push_hooks' do
    it 'returns hooks for push events only' do
      project = build(:project)
      hook = create(:project_hook, project: project, push_events: true)
      create(:project_hook, project: project, push_events: false)
      expect(described_class.push_hooks).to eq([hook])
    end
  end

  describe '.tag_push_hooks' do
    it 'returns hooks for tag push events only' do
      project = build(:project)
      hook = create(:project_hook, project: project, tag_push_events: true)
      create(:project_hook, project: project, tag_push_events: false)
      expect(described_class.tag_push_hooks).to eq([hook])
    end
  end

  describe '#parent' do
    it 'returns the associated project' do
      project = build(:project)
      hook = build(:project_hook, project: project)

      expect(hook.parent).to eq(project)
    end
  end

  describe '#application_context' do
    let_it_be(:hook) { build(:project_hook) }

    it 'includes the type and project' do
      expect(hook.application_context).to include(
        related_class: 'ProjectHook',
        project: hook.project
      )
    end
  end

  describe '#update_last_failure', :clean_gitlab_redis_shared_state do
    let_it_be(:hook) { create(:project_hook) }

    def last_failure
      Gitlab::Redis::SharedState.with do |redis|
        redis.get(hook.project.last_failure_redis_key)
      end
    end

    def any_failed?
      Gitlab::Redis::SharedState.with do |redis|
        Gitlab::Utils.to_boolean(redis.get(hook.project.web_hook_failure_redis_key))
      end
    end

    it 'is a method of this class' do
      expect { hook.update_last_failure }.not_to raise_error
    end

    context 'when the hook is executable' do
      let(:redis_key) { hook.project.web_hook_failure_redis_key }

      def redis_value
        any_failed?
      end

      context 'when the state was previously failing' do
        before do
          Gitlab::Redis::SharedState.with do |redis|
            redis.set(redis_key, true)
          end
        end

        it 'does update the state' do
          expect { hook.update_last_failure }.to change { redis_value }.to(false)
        end

        context 'when there is another failing sibling hook' do
          before do
            create(:project_hook, :permanently_disabled, project: hook.project)
          end

          it 'does not update the state' do
            expect { hook.update_last_failure }.not_to change { redis_value }.from(true)
          end

          it 'caches the current value' do
            Gitlab::Redis::SharedState.with do |redis|
              expect(redis).to receive(:set).with(redis_key, 'true', ex: 1.hour).and_call_original
            end

            hook.update_last_failure
          end
        end
      end

      context 'when the state was previously unknown' do
        before do
          Gitlab::Redis::SharedState.with do |redis|
            redis.del(redis_key)
          end
        end

        it 'does not update the state' do
          expect { hook.update_last_failure }.not_to change { redis_value }.from(nil)
        end
      end

      context 'when the state was previously not failing' do
        before do
          Gitlab::Redis::SharedState.with do |redis|
            redis.set(redis_key, false)
          end
        end

        it 'does not update the state' do
          expect { hook.update_last_failure }.not_to change { redis_value }.from(false)
        end

        it 'does not cache the current value' do
          Gitlab::Redis::SharedState.with do |redis|
            expect(redis).not_to receive(:set)
          end

          hook.update_last_failure
        end
      end
    end

    context 'when the hook is failed' do
      before do
        allow(hook).to receive(:executable?).and_return(false)
      end

      context 'there is no prior value', :freeze_time do
        it 'updates last_failure' do
          expect { hook.update_last_failure }.to change { last_failure }.to(Time.current)
        end

        it 'updates any_failed?' do
          expect { hook.update_last_failure }.to change { any_failed? }.to(true)
        end
      end

      context 'when there is a prior last_failure, from before now' do
        it 'updates the state' do
          the_future = 1.minute.from_now
          hook.update_last_failure

          travel_to(the_future) do
            expect { hook.update_last_failure }.to change { last_failure }.to(the_future.iso8601)
          end
        end

        it 'does not change the failing state' do
          the_future = 1.minute.from_now
          hook.update_last_failure

          travel_to(the_future) do
            expect { hook.update_last_failure }.not_to change { any_failed? }.from(true)
          end
        end
      end

      context 'there is a prior value, from after now' do
        it 'does not update the state' do
          the_past = 1.minute.ago

          hook.update_last_failure

          travel_to(the_past) do
            expect { hook.update_last_failure }.not_to change { last_failure }
          end
        end
      end
    end
  end
end