File: hooks_controller_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 (134 lines) | stat: -rw-r--r-- 3,548 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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Admin::HooksController, feature_category: :webhooks do
  let_it_be(:admin) { create(:admin) }

  before do
    sign_in(admin)
  end

  shared_examples 'disabled on GitLab.com' do
    let(:gitlab_com?) { false }

    before do
      allow(::Gitlab).to receive(:com?) { gitlab_com? }
    end

    context 'when on GitLab.com' do
      let(:gitlab_com?) { true }

      it 'responds with a not_found status' do
        subject
        expect(response).to have_gitlab_http_status(:not_found)
      end
    end

    context 'when not on GitLab.com' do
      it 'does not respond with a not_found status' do
        subject
        expect(response).not_to have_gitlab_http_status(:not_found)
      end
    end
  end

  describe 'GET #index' do
    subject(:get_index) { get :index }

    it_behaves_like 'disabled on GitLab.com'
  end

  describe 'POST #create' do
    let_it_be(:hook_params) do
      {
        enable_ssl_verification: true,
        token: 'TEST TOKEN',
        url: 'http://example.com',

        push_events: true,
        tag_push_events: false,
        repository_update_events: true,
        merge_requests_events: false,
        url_variables: [{ key: 'token', value: 'some secret value' }]
      }
    end

    subject(:post_create) { post :create, params: { hook: hook_params } }

    it 'sets all parameters' do
      post_create

      expect(response).to have_gitlab_http_status(:found)
      expect(SystemHook.all.size).to eq(1)
      expect(SystemHook.first).to have_attributes(hook_params.except(:url_variables))
      expect(SystemHook.first).to have_attributes(url_variables: { 'token' => 'some secret value' })
    end

    it_behaves_like 'disabled on GitLab.com'
  end

  describe 'POST #update' do
    let_it_be_with_reload(:hook) { create(:system_hook) }

    let_it_be(:hook_params) do
      {
        url: 'http://example.com/{bar}?token={token}',
        enable_ssl_verification: false,
        url_variables: [
          { key: 'token', value: 'some secret value' },
          { key: 'baz', value: nil },
          { key: 'foo', value: nil },
          { key: 'bar', value: 'qux' }
        ]
      }
    end

    subject(:put_update) { put :update, params: { id: hook.id, hook: hook_params } }

    context 'with an existing token' do
      let_it_be(:hook_params) do
        {
          token: WebHook::SECRET_MASK,
          url: 'http://example.com'
        }
      end

      it 'does not change a token' do
        expect { put_update }.not_to change { hook.reload.token }

        expect(response).to have_gitlab_http_status(:found)
        expect(flash[:alert]).to be_blank
      end
    end

    it 'sets all parameters' do
      hook.update!(url_variables: { 'foo' => 'bar', 'baz' => 'woo' })

      put_update

      hook.reload

      expect(response).to have_gitlab_http_status(:found)
      expect(flash[:notice]).to include('was updated')
      expect(hook).to have_attributes(hook_params.except(:url_variables))
      expect(hook).to have_attributes(
        url_variables: { 'token' => 'some secret value', 'bar' => 'qux' }
      )
    end

    it_behaves_like 'disabled on GitLab.com'
  end

  describe 'DELETE #destroy' do
    let_it_be(:hook) { create(:system_hook) }
    let_it_be(:log) { create(:web_hook_log, web_hook: hook) }
    let(:params) { { id: hook } }

    it_behaves_like 'Web hook destroyer'

    it_behaves_like 'disabled on GitLab.com' do
      subject { delete :destroy, params: params }
    end
  end
end