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

system_hooks_spec.rb « api « requests « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: d94b70ec0f90a7c6876941858ba3dc0c55edf57c (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe API::SystemHooks do
  include StubRequests

  let(:user) { create(:user) }
  let(:admin) { create(:admin) }
  let!(:hook) { create(:system_hook, url: "http://example.com") }

  before do
    stub_full_request(hook.url, method: :post)
  end

  describe "GET /hooks" do
    context "when no user" do
      it "returns authentication error" do
        get api("/hooks")

        expect(response).to have_gitlab_http_status(:unauthorized)
      end
    end

    context "when not an admin" do
      it "returns forbidden error" do
        get api("/hooks", user)

        expect(response).to have_gitlab_http_status(:forbidden)
      end
    end

    context "when authenticated as admin" do
      it "returns an array of hooks" do
        get api("/hooks", admin)

        expect(response).to have_gitlab_http_status(:ok)
        expect(response).to include_pagination_headers
        expect(response).to match_response_schema('public_api/v4/system_hooks')
        expect(json_response.first).not_to have_key("token")
        expect(json_response.first['url']).to eq(hook.url)
        expect(json_response.first['push_events']).to be false
        expect(json_response.first['tag_push_events']).to be false
        expect(json_response.first['merge_requests_events']).to be false
        expect(json_response.first['repository_update_events']).to be true
        expect(json_response.first['enable_ssl_verification']).to be true
      end
    end
  end

  describe "GET /hooks/:id" do
    context "when no user" do
      it "returns authentication error" do
        get api("/hooks/#{hook.id}")

        expect(response).to have_gitlab_http_status(:unauthorized)
      end
    end

    context "when not an admin" do
      it "returns forbidden error" do
        get api("/hooks/#{hook.id}", user)

        expect(response).to have_gitlab_http_status(:forbidden)
      end
    end

    context "when authenticated as admin" do
      it "gets a hook", :aggregate_failures do
        get api("/hooks/#{hook.id}", admin)

        expect(response).to have_gitlab_http_status(:ok)
        expect(response).to match_response_schema('public_api/v4/system_hook')
        expect(json_response).to match(
          'id' => be(hook.id),
          'url' => eq(hook.url),
          'created_at' => eq(hook.created_at.iso8601(3)),
          'push_events' => be(hook.push_events),
          'tag_push_events' => be(hook.tag_push_events),
          'merge_requests_events' => be(hook.merge_requests_events),
          'repository_update_events' => be(hook.repository_update_events),
          'enable_ssl_verification' => be(hook.enable_ssl_verification)
        )
      end

      it 'returns 404 if the system hook does not exist' do
        get api("/hooks/#{non_existing_record_id}", admin)

        expect(response).to have_gitlab_http_status(:not_found)
      end
    end
  end

  describe "POST /hooks" do
    it "creates new hook" do
      expect do
        post api("/hooks", admin), params: { url: 'http://example.com' }
      end.to change { SystemHook.count }.by(1)
    end

    it "responds with 400 if url not given" do
      post api("/hooks", admin)

      expect(response).to have_gitlab_http_status(:bad_request)
    end

    it "responds with 400 if url is invalid" do
      post api("/hooks", admin), params: { url: 'hp://mep.mep' }

      expect(response).to have_gitlab_http_status(:bad_request)
    end

    it "does not create new hook without url" do
      expect do
        post api("/hooks", admin)
      end.not_to change { SystemHook.count }
    end

    it 'sets default values for events' do
      stub_full_request('http://mep.mep', method: :post)

      post api('/hooks', admin), params: { url: 'http://mep.mep' }

      expect(response).to have_gitlab_http_status(:created)
      expect(response).to match_response_schema('public_api/v4/system_hook')
      expect(json_response['enable_ssl_verification']).to be true
      expect(json_response['push_events']).to be false
      expect(json_response['tag_push_events']).to be false
      expect(json_response['merge_requests_events']).to be false
      expect(json_response['repository_update_events']).to be true
    end

    it 'sets explicit values for events' do
      stub_full_request('http://mep.mep', method: :post)

      post api('/hooks', admin),
        params: {
          url: 'http://mep.mep',
          enable_ssl_verification: false,
          push_events: true,
          tag_push_events: true,
          merge_requests_events: true,
          repository_update_events: false
        }

      expect(response).to have_gitlab_http_status(:created)
      expect(response).to match_response_schema('public_api/v4/system_hook')
      expect(json_response['enable_ssl_verification']).to be false
      expect(json_response['push_events']).to be true
      expect(json_response['tag_push_events']).to be true
      expect(json_response['merge_requests_events']).to be true
      expect(json_response['repository_update_events']).to be false
    end
  end

  describe 'POST /hooks/:id' do
    it "returns and trigger hook by id" do
      post api("/hooks/#{hook.id}", admin)
      expect(response).to have_gitlab_http_status(:created)
      expect(json_response['event_name']).to eq('project_create')
    end

    it "returns 404 on failure" do
      post api("/hooks/404", admin)
      expect(response).to have_gitlab_http_status(:not_found)
    end
  end

  describe "DELETE /hooks/:id" do
    it "deletes a hook" do
      expect do
        delete api("/hooks/#{hook.id}", admin)

        expect(response).to have_gitlab_http_status(:no_content)
      end.to change { SystemHook.count }.by(-1)
    end

    it 'returns 404 if the system hook does not exist' do
      delete api("/hooks/#{non_existing_record_id}", admin)

      expect(response).to have_gitlab_http_status(:not_found)
    end

    it_behaves_like '412 response' do
      let(:request) { api("/hooks/#{hook.id}", admin) }
    end
  end
end