summaryrefslogtreecommitdiff
path: root/spec/models/hooks/web_hook_spec.rb
blob: 9d4db1bfb526ae0a437928e8f2df47028bffcece (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
require 'spec_helper'

describe WebHook, models: true do
  describe "Validations" do
    it { is_expected.to validate_presence_of(:url) }

    describe 'url' do
      it { is_expected.to allow_value("http://example.com").for(:url) }
      it { is_expected.to allow_value("https://example.com").for(:url) }
      it { is_expected.to allow_value(" https://example.com ").for(:url) }
      it { is_expected.to allow_value("http://test.com/api").for(:url) }
      it { is_expected.to allow_value("http://test.com/api?key=abc").for(:url) }
      it { is_expected.to allow_value("http://test.com/api?key=abc&type=def").for(:url) }

      it { is_expected.not_to allow_value("example.com").for(:url) }
      it { is_expected.not_to allow_value("ftp://example.com").for(:url) }
      it { is_expected.not_to allow_value("herp-and-derp").for(:url) }

      it 'strips :url before saving it' do
        hook = create(:project_hook, url: ' https://example.com ')

        expect(hook.url).to eq('https://example.com')
      end
    end
  end

  describe "execute" do
    let(:project) { create(:empty_project) }
    let(:project_hook) { create(:project_hook) }

    before(:each) do
      project.hooks << [project_hook]
      @data = { before: 'oldrev', after: 'newrev', ref: 'ref' }

      WebMock.stub_request(:post, project_hook.url)
    end

    context 'when token is defined' do
      let(:project_hook) { create(:project_hook, :token) }

      it 'POSTs to the webhook URL' do
        project_hook.execute(@data, 'push_hooks')
        expect(WebMock).to have_requested(:post, project_hook.url).with(
          headers: { 'Content-Type' => 'application/json',
                     'X-Gitlab-Event' => 'Push Hook',
                     'X-Gitlab-Token' => project_hook.token }
        ).once
      end
    end

    it "POSTs to the webhook URL" do
      project_hook.execute(@data, 'push_hooks')
      expect(WebMock).to have_requested(:post, project_hook.url).with(
        headers: { 'Content-Type' => 'application/json', 'X-Gitlab-Event' => 'Push Hook' }
      ).once
    end

    it "POSTs the data as JSON" do
      project_hook.execute(@data, 'push_hooks')
      expect(WebMock).to have_requested(:post, project_hook.url).with(
        headers: { 'Content-Type' => 'application/json', 'X-Gitlab-Event' => 'Push Hook' }
      ).once
    end

    it "catches exceptions" do
      expect(WebHook).to receive(:post).and_raise("Some HTTP Post error")

      expect { project_hook.execute(@data, 'push_hooks') }.to raise_error(RuntimeError)
    end

    it "handles SSL exceptions" do
      expect(WebHook).to receive(:post).and_raise(OpenSSL::SSL::SSLError.new('SSL error'))

      expect(project_hook.execute(@data, 'push_hooks')).to eq([false, 'SSL error'])
    end

    it "handles 200 status code" do
      WebMock.stub_request(:post, project_hook.url).to_return(status: 200, body: "Success")

      expect(project_hook.execute(@data, 'push_hooks')).to eq([200, 'Success'])
    end

    it "handles 2xx status codes" do
      WebMock.stub_request(:post, project_hook.url).to_return(status: 201, body: "Success")

      expect(project_hook.execute(@data, 'push_hooks')).to eq([201, 'Success'])
    end
  end
end