debian-mirror-gitlab/spec/requests/api/system_hooks_spec.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

224 lines
7.1 KiB
Ruby
Raw Normal View History

2019-12-26 22:10:19 +05:30
# frozen_string_literal: true
2014-09-02 18:07:02 +05:30
require 'spec_helper'
2020-07-28 23:09:34 +05:30
RSpec.describe API::SystemHooks do
2019-06-05 12:25:43 +05:30
include StubRequests
2014-09-02 18:07:02 +05:30
let(:user) { create(:user) }
let(:admin) { create(:admin) }
let!(:hook) { create(:system_hook, url: "http://example.com") }
2017-09-10 17:25:29 +05:30
before do
2019-06-05 12:25:43 +05:30
stub_full_request(hook.url, method: :post)
2017-09-10 17:25:29 +05:30
end
2014-09-02 18:07:02 +05:30
describe "GET /hooks" do
context "when no user" do
2016-09-13 17:45:13 +05:30
it "returns authentication error" do
2014-09-02 18:07:02 +05:30
get api("/hooks")
2017-08-17 22:00:37 +05:30
2020-04-08 14:13:33 +05:30
expect(response).to have_gitlab_http_status(:unauthorized)
2014-09-02 18:07:02 +05:30
end
end
context "when not an admin" do
2016-09-13 17:45:13 +05:30
it "returns forbidden error" do
2014-09-02 18:07:02 +05:30
get api("/hooks", user)
2017-08-17 22:00:37 +05:30
2020-04-08 14:13:33 +05:30
expect(response).to have_gitlab_http_status(:forbidden)
2014-09-02 18:07:02 +05:30
end
end
context "when authenticated as admin" do
2016-09-13 17:45:13 +05:30
it "returns an array of hooks" do
2014-09-02 18:07:02 +05:30
get api("/hooks", admin)
2017-08-17 22:00:37 +05:30
2020-04-08 14:13:33 +05:30
expect(response).to have_gitlab_http_status(:ok)
2017-08-17 22:00:37 +05:30
expect(response).to include_pagination_headers
2022-05-07 20:08:51 +05:30
expect(response).to match_response_schema('public_api/v4/system_hooks')
expect(json_response.first).not_to have_key("token")
2015-04-26 12:48:37 +05:30
expect(json_response.first['url']).to eq(hook.url)
2017-09-10 17:25:29 +05:30
expect(json_response.first['push_events']).to be false
2017-08-17 22:00:37 +05:30
expect(json_response.first['tag_push_events']).to be false
2018-03-17 18:26:18 +05:30
expect(json_response.first['merge_requests_events']).to be false
2017-09-10 17:25:29 +05:30
expect(json_response.first['repository_update_events']).to be true
2022-05-07 20:08:51 +05:30
expect(json_response.first['enable_ssl_verification']).to be true
2022-07-23 23:45:48 +05:30
expect(json_response.first['disabled_until']).to be nil
expect(json_response.first['alert_status']).to eq 'executable'
2022-05-07 20:08:51 +05:30
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),
2022-07-23 23:45:48 +05:30
'enable_ssl_verification' => be(hook.enable_ssl_verification),
'alert_status' => eq(hook.alert_status.to_s),
'disabled_until' => eq(hook.disabled_until&.iso8601(3))
2022-05-07 20:08:51 +05:30
)
end
2022-07-23 23:45:48 +05:30
context 'the hook is disabled' do
before do
hook.disable!
end
it "has the correct alert status", :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 include('alert_status' => 'disabled')
end
end
context 'the hook is backed-off' do
before do
hook.backoff!
end
it "has the correct alert status", :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 include(
'alert_status' => 'temporarily_disabled',
'disabled_until' => hook.disabled_until.iso8601(3)
)
end
end
2022-05-07 20:08:51 +05:30
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)
2014-09-02 18:07:02 +05:30
end
end
end
describe "POST /hooks" do
2016-09-13 17:45:13 +05:30
it "creates new hook" do
2015-09-11 14:41:01 +05:30
expect do
2019-02-15 15:39:39 +05:30
post api("/hooks", admin), params: { url: 'http://example.com' }
2015-09-11 14:41:01 +05:30
end.to change { SystemHook.count }.by(1)
2014-09-02 18:07:02 +05:30
end
2016-09-13 17:45:13 +05:30
it "responds with 400 if url not given" do
2014-09-02 18:07:02 +05:30
post api("/hooks", admin)
2017-08-17 22:00:37 +05:30
2020-04-08 14:13:33 +05:30
expect(response).to have_gitlab_http_status(:bad_request)
2017-08-17 22:00:37 +05:30
end
it "responds with 400 if url is invalid" do
2019-02-15 15:39:39 +05:30
post api("/hooks", admin), params: { url: 'hp://mep.mep' }
2017-08-17 22:00:37 +05:30
2020-04-08 14:13:33 +05:30
expect(response).to have_gitlab_http_status(:bad_request)
2014-09-02 18:07:02 +05:30
end
2016-09-13 17:45:13 +05:30
it "does not create new hook without url" do
2015-09-11 14:41:01 +05:30
expect do
2014-09-02 18:07:02 +05:30
post api("/hooks", admin)
end.not_to change { SystemHook.count }
2014-09-02 18:07:02 +05:30
end
2017-08-17 22:00:37 +05:30
it 'sets default values for events' do
2019-06-05 12:25:43 +05:30
stub_full_request('http://mep.mep', method: :post)
2019-02-15 15:39:39 +05:30
post api('/hooks', admin), params: { url: 'http://mep.mep' }
2017-08-17 22:00:37 +05:30
2020-04-08 14:13:33 +05:30
expect(response).to have_gitlab_http_status(:created)
2022-05-07 20:08:51 +05:30
expect(response).to match_response_schema('public_api/v4/system_hook')
2017-08-17 22:00:37 +05:30
expect(json_response['enable_ssl_verification']).to be true
2018-03-17 18:26:18 +05:30
expect(json_response['push_events']).to be false
2017-08-17 22:00:37 +05:30
expect(json_response['tag_push_events']).to be false
2018-03-17 18:26:18 +05:30
expect(json_response['merge_requests_events']).to be false
2021-09-30 23:02:18 +05:30
expect(json_response['repository_update_events']).to be true
2018-03-17 18:26:18 +05:30
end
it 'sets explicit values for events' do
2019-06-05 12:25:43 +05:30
stub_full_request('http://mep.mep', method: :post)
2018-03-17 18:26:18 +05:30
post api('/hooks', admin),
2019-02-15 15:39:39 +05:30
params: {
url: 'http://mep.mep',
enable_ssl_verification: false,
push_events: true,
tag_push_events: true,
2021-09-30 23:02:18 +05:30
merge_requests_events: true,
repository_update_events: false
2019-02-15 15:39:39 +05:30
}
2018-03-17 18:26:18 +05:30
2020-04-08 14:13:33 +05:30
expect(response).to have_gitlab_http_status(:created)
2022-05-07 20:08:51 +05:30
expect(response).to match_response_schema('public_api/v4/system_hook')
2018-03-17 18:26:18 +05:30
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
2021-09-30 23:02:18 +05:30
expect(json_response['repository_update_events']).to be false
2017-08-17 22:00:37 +05:30
end
2014-09-02 18:07:02 +05:30
end
2021-04-01 16:36:13 +05:30
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)
2015-04-26 12:48:37 +05:30
expect(json_response['event_name']).to eq('project_create')
2014-09-02 18:07:02 +05:30
end
2016-09-13 17:45:13 +05:30
it "returns 404 on failure" do
2021-04-01 16:36:13 +05:30
post api("/hooks/404", admin)
2020-04-08 14:13:33 +05:30
expect(response).to have_gitlab_http_status(:not_found)
2014-09-02 18:07:02 +05:30
end
end
describe "DELETE /hooks/:id" do
2016-09-13 17:45:13 +05:30
it "deletes a hook" do
2015-09-11 14:41:01 +05:30
expect do
2014-09-02 18:07:02 +05:30
delete api("/hooks/#{hook.id}", admin)
2017-08-17 22:00:37 +05:30
2020-04-08 14:13:33 +05:30
expect(response).to have_gitlab_http_status(:no_content)
2015-09-11 14:41:01 +05:30
end.to change { SystemHook.count }.by(-1)
2014-09-02 18:07:02 +05:30
end
2016-11-03 12:29:30 +05:30
it 'returns 404 if the system hook does not exist' do
2020-04-22 19:07:51 +05:30
delete api("/hooks/#{non_existing_record_id}", admin)
2016-11-03 12:29:30 +05:30
2020-04-08 14:13:33 +05:30
expect(response).to have_gitlab_http_status(:not_found)
2018-03-17 18:26:18 +05:30
end
it_behaves_like '412 response' do
let(:request) { api("/hooks/#{hook.id}", admin) }
2014-09-02 18:07:02 +05:30
end
end
end