File: triggered_hooks_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 (70 lines) | stat: -rw-r--r-- 2,247 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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Projects::TriggeredHooks, feature_category: :webhooks do
  let_it_be(:project) { create(:project) }

  let_it_be(:universal_push_hook) { create(:project_hook, project: project, push_events: true) }
  let_it_be(:selective_push_hook) { create(:project_hook, :with_push_branch_filter, project: project, push_events: true) }
  let_it_be(:issues_hook) { create(:project_hook, project: project, issues_events: true, push_events: false) }

  let(:wh_service) { instance_double(::WebHookService, async_execute: true) }
  let(:data) { { some: 'data', as: 'json' } }

  def run_hooks(scope, data)
    hooks = described_class.new(scope, data)
    hooks.add_hooks(ProjectHook.all)
    hooks.execute
  end

  it 'executes hooks by scope' do
    expect_hook_execution(issues_hook, data, 'issue_hooks')

    run_hooks(:issue_hooks, data)
  end

  it 'applies branch filters, when they match' do
    data = { some: 'data', as: 'json', ref: "refs/heads/#{generate(:branch)}" }

    expect_hook_execution(universal_push_hook, data, 'push_hooks')
    expect_hook_execution(selective_push_hook, data, 'push_hooks')

    run_hooks(:push_hooks, data)
  end

  it 'applies branch filters, when they do not match' do
    data = { some: 'data', as: 'json', ref: "refs/heads/master}" }

    expect_hook_execution(universal_push_hook, data, 'push_hooks')

    run_hooks(:push_hooks, data)
  end

  context 'with access token hooks' do
    let_it_be(:resource_access_token_hook) { create(:project_hook, project: project, resource_access_token_events: true) }

    it 'executes hook' do
      expect_hook_execution(resource_access_token_hook, data, 'resource_access_token_hooks')

      run_hooks(:resource_access_token_hooks, data)
    end
  end

  context 'with emoji hooks' do
    let_it_be(:emoji_hook) { create(:project_hook, project: project, emoji_events: true) }

    it 'executes hook' do
      expect_hook_execution(emoji_hook, data, 'emoji_hooks')

      run_hooks(:emoji_hooks, data)
    end
  end

  def expect_hook_execution(hook, data, scope)
    expect(WebHookService)
      .to receive(:new)
      .with(hook, data, scope, idempotency_key: anything)
      .and_return(wh_service)
  end
end