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