summaryrefslogtreecommitdiff
path: root/spec/support/shared_examples/models/integrations/has_web_hook_shared_examples.rb
blob: 31ec25249d73a9df00b8ede29a418f9cb9cfd97a (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
# frozen_string_literal: true

RSpec.shared_examples Integrations::HasWebHook do
  include AfterNextHelpers

  describe 'associations' do
    it { is_expected.to have_one(:service_hook).inverse_of(:integration).with_foreign_key(:integration_id) }
  end

  describe 'callbacks' do
    it 'calls #update_web_hook! when enabled' do
      expect(integration).to receive(:update_web_hook!)

      integration.active = true
      integration.save!
    end

    it 'does not call #update_web_hook! when disabled' do
      expect(integration).not_to receive(:update_web_hook!)

      integration.active = false
      integration.save!
    end

    it 'does not call #update_web_hook! when validation fails' do
      expect(integration).not_to receive(:update_web_hook!)

      integration.active = true
      integration.project = nil
      expect(integration.save).to be(false)
    end
  end

  describe '#hook_url' do
    it 'returns a string' do
      expect(integration.hook_url).to be_a(String)
    end
  end

  describe '#url_variables' do
    it 'returns a string' do
      expect(integration.url_variables).to be_a(Hash)
    end
  end

  describe '#hook_ssl_verification' do
    it 'returns a boolean' do
      expect(integration.hook_ssl_verification).to be_in([true, false])
    end

    it 'delegates to #enable_ssl_verification if the concern is included' do
      next unless integration.is_a?(Integrations::EnableSslVerification)

      [true, false].each do |value|
        integration.enable_ssl_verification = value

        expect(integration.hook_ssl_verification).to be(value)
      end
    end
  end

  describe '#update_web_hook!' do
    def call
      integration.update_web_hook!
    end

    it 'creates or updates a service hook' do
      expect { call }.to change(ServiceHook, :count).by(1)
      expect(integration.service_hook.url).to eq(hook_url)

      integration.service_hook.update!(url: 'http://other.com')

      expect { call }.to change { integration.service_hook.reload.url }.from('http://other.com').to(hook_url)
    end

    it 'raises an error if the service hook could not be saved' do
      call
      integration.service_hook.integration = nil

      expect { call }.to raise_error(ActiveRecord::RecordInvalid)
    end

    it 'does not attempt to save the service hook if there are no changes' do
      call

      expect(integration.service_hook).not_to receive(:save!)

      call
    end
  end

  describe '#execute_web_hook!' do
    let(:args) { ['foo', [1, 2, 3]] }

    def call
      integration.execute_web_hook!(*args)
    end

    it 'creates the webhook if necessary and executes it' do
      expect_next(ServiceHook).to receive(:execute).with(*args)
      expect { call }.to change(ServiceHook, :count).by(1)

      expect(integration.service_hook).to receive(:execute).with(*args)
      expect { call }.not_to change(ServiceHook, :count)
    end

    it 'raises an error if the service hook could not be saved' do
      expect_next(ServiceHook).to receive(:execute).with(*args)

      call
      integration.service_hook.integration = nil

      expect(integration.service_hook).not_to receive(:execute)
      expect { call }.to raise_error(ActiveRecord::RecordInvalid)
    end
  end
end