2013-03-20 17:46:30 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2017-04-21 16:32:02 -04:00
|
|
|
describe API::SystemHooks do
|
2019-04-21 06:03:26 -04:00
|
|
|
include StubRequests
|
|
|
|
|
2013-03-20 17:46:30 -04:00
|
|
|
let(:user) { create(:user) }
|
|
|
|
let(:admin) { create(:admin) }
|
|
|
|
let!(:hook) { create(:system_hook, url: "http://example.com") }
|
|
|
|
|
2017-06-14 14:18:56 -04:00
|
|
|
before do
|
2019-04-21 06:03:26 -04:00
|
|
|
stub_full_request(hook.url, method: :post)
|
2017-06-14 14:18:56 -04:00
|
|
|
end
|
2013-03-20 17:46:30 -04:00
|
|
|
|
|
|
|
describe "GET /hooks" do
|
|
|
|
context "when no user" do
|
2016-08-01 11:00:44 -04:00
|
|
|
it "returns authentication error" do
|
2013-03-20 17:46:30 -04:00
|
|
|
get api("/hooks")
|
2016-10-18 14:10:08 -04:00
|
|
|
|
2017-10-19 14:28:19 -04:00
|
|
|
expect(response).to have_gitlab_http_status(401)
|
2013-03-20 17:46:30 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when not an admin" do
|
2016-08-01 11:00:44 -04:00
|
|
|
it "returns forbidden error" do
|
2013-03-20 17:46:30 -04:00
|
|
|
get api("/hooks", user)
|
2016-10-18 14:10:08 -04:00
|
|
|
|
2017-10-19 14:28:19 -04:00
|
|
|
expect(response).to have_gitlab_http_status(403)
|
2013-03-20 17:46:30 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when authenticated as admin" do
|
2016-08-01 11:00:44 -04:00
|
|
|
it "returns an array of hooks" do
|
2013-03-20 17:46:30 -04:00
|
|
|
get api("/hooks", admin)
|
2016-10-18 14:10:08 -04:00
|
|
|
|
2017-10-19 14:28:19 -04:00
|
|
|
expect(response).to have_gitlab_http_status(200)
|
2017-01-24 15:49:10 -05:00
|
|
|
expect(response).to include_pagination_headers
|
2015-02-12 13:17:35 -05:00
|
|
|
expect(json_response).to be_an Array
|
|
|
|
expect(json_response.first['url']).to eq(hook.url)
|
2017-05-05 12:00:32 -04:00
|
|
|
expect(json_response.first['push_events']).to be false
|
2016-10-18 14:10:08 -04:00
|
|
|
expect(json_response.first['tag_push_events']).to be false
|
2017-11-23 10:45:00 -05:00
|
|
|
expect(json_response.first['merge_requests_events']).to be false
|
2017-05-05 12:00:32 -04:00
|
|
|
expect(json_response.first['repository_update_events']).to be true
|
2013-03-20 17:46:30 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "POST /hooks" do
|
2016-08-01 11:00:44 -04:00
|
|
|
it "creates new hook" do
|
2015-06-22 09:05:19 -04:00
|
|
|
expect do
|
2018-12-17 17:52:17 -05:00
|
|
|
post api("/hooks", admin), params: { url: 'http://example.com' }
|
2015-06-22 09:05:19 -04:00
|
|
|
end.to change { SystemHook.count }.by(1)
|
2013-03-20 17:46:30 -04:00
|
|
|
end
|
|
|
|
|
2016-08-01 11:00:44 -04:00
|
|
|
it "responds with 400 if url not given" do
|
2013-03-20 17:46:30 -04:00
|
|
|
post api("/hooks", admin)
|
2016-11-02 08:24:24 -04:00
|
|
|
|
2017-10-19 14:28:19 -04:00
|
|
|
expect(response).to have_gitlab_http_status(400)
|
2013-03-20 17:46:30 -04:00
|
|
|
end
|
|
|
|
|
2016-11-08 04:05:19 -05:00
|
|
|
it "responds with 400 if url is invalid" do
|
2018-12-17 17:52:17 -05:00
|
|
|
post api("/hooks", admin), params: { url: 'hp://mep.mep' }
|
2016-11-08 04:05:19 -05:00
|
|
|
|
2017-10-19 14:28:19 -04:00
|
|
|
expect(response).to have_gitlab_http_status(400)
|
2016-11-08 04:05:19 -05:00
|
|
|
end
|
|
|
|
|
2016-08-01 11:00:44 -04:00
|
|
|
it "does not create new hook without url" do
|
2015-06-22 09:05:19 -04:00
|
|
|
expect do
|
2013-03-20 17:46:30 -04:00
|
|
|
post api("/hooks", admin)
|
2016-05-23 19:37:59 -04:00
|
|
|
end.not_to change { SystemHook.count }
|
2013-03-20 17:46:30 -04:00
|
|
|
end
|
2016-10-18 14:10:08 -04:00
|
|
|
|
2016-11-02 08:24:24 -04:00
|
|
|
it 'sets default values for events' do
|
2019-04-21 06:03:26 -04:00
|
|
|
stub_full_request('http://mep.mep', method: :post)
|
|
|
|
|
2018-12-17 17:52:17 -05:00
|
|
|
post api('/hooks', admin), params: { url: 'http://mep.mep' }
|
2016-10-18 14:10:08 -04:00
|
|
|
|
2017-10-19 14:28:19 -04:00
|
|
|
expect(response).to have_gitlab_http_status(201)
|
2016-10-18 14:10:08 -04:00
|
|
|
expect(json_response['enable_ssl_verification']).to be true
|
2017-11-23 10:45:00 -05:00
|
|
|
expect(json_response['push_events']).to be false
|
2016-10-18 14:10:08 -04:00
|
|
|
expect(json_response['tag_push_events']).to be false
|
2017-11-23 10:45:00 -05:00
|
|
|
expect(json_response['merge_requests_events']).to be false
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'sets explicit values for events' do
|
2019-04-21 06:03:26 -04:00
|
|
|
stub_full_request('http://mep.mep', method: :post)
|
|
|
|
|
2017-11-23 10:45:00 -05:00
|
|
|
post api('/hooks', admin),
|
2018-12-17 17:52:17 -05:00
|
|
|
params: {
|
|
|
|
url: 'http://mep.mep',
|
|
|
|
enable_ssl_verification: false,
|
|
|
|
push_events: true,
|
|
|
|
tag_push_events: true,
|
|
|
|
merge_requests_events: true
|
|
|
|
}
|
2017-11-23 10:45:00 -05:00
|
|
|
|
|
|
|
expect(response).to have_http_status(201)
|
|
|
|
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
|
2016-10-18 14:10:08 -04:00
|
|
|
end
|
2013-03-20 17:46:30 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
describe "GET /hooks/:id" do
|
2016-08-01 11:00:44 -04:00
|
|
|
it "returns hook by id" do
|
2013-03-20 17:46:30 -04:00
|
|
|
get api("/hooks/#{hook.id}", admin)
|
2017-10-19 14:28:19 -04:00
|
|
|
expect(response).to have_gitlab_http_status(200)
|
2015-02-12 13:17:35 -05:00
|
|
|
expect(json_response['event_name']).to eq('project_create')
|
2013-03-20 17:46:30 -04:00
|
|
|
end
|
|
|
|
|
2016-08-01 11:00:44 -04:00
|
|
|
it "returns 404 on failure" do
|
2013-03-20 17:46:30 -04:00
|
|
|
get api("/hooks/404", admin)
|
2017-10-19 14:28:19 -04:00
|
|
|
expect(response).to have_gitlab_http_status(404)
|
2013-03-20 17:46:30 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "DELETE /hooks/:id" do
|
2016-08-01 11:00:44 -04:00
|
|
|
it "deletes a hook" do
|
2015-06-22 09:05:19 -04:00
|
|
|
expect do
|
2013-03-20 17:46:30 -04:00
|
|
|
delete api("/hooks/#{hook.id}", admin)
|
2017-02-20 13:18:12 -05:00
|
|
|
|
2017-10-19 14:28:19 -04:00
|
|
|
expect(response).to have_gitlab_http_status(204)
|
2015-06-22 09:05:19 -04:00
|
|
|
end.to change { SystemHook.count }.by(-1)
|
2013-03-20 17:46:30 -04:00
|
|
|
end
|
|
|
|
|
2016-10-13 13:32:10 -04:00
|
|
|
it 'returns 404 if the system hook does not exist' do
|
|
|
|
delete api('/hooks/12345', admin)
|
|
|
|
|
2017-10-19 14:28:19 -04:00
|
|
|
expect(response).to have_gitlab_http_status(404)
|
2013-03-20 17:46:30 -04:00
|
|
|
end
|
2017-08-24 12:03:39 -04:00
|
|
|
|
|
|
|
it_behaves_like '412 response' do
|
|
|
|
let(:request) { api("/hooks/#{hook.id}", admin) }
|
|
|
|
end
|
2013-03-20 17:46:30 -04:00
|
|
|
end
|
|
|
|
end
|