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

83 lines
2.1 KiB
Ruby
Raw Normal View History

2014-09-02 18:07:02 +05:30
require 'spec_helper'
describe API::API, api: true do
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-09-13 17:45:13 +05:30
it "returns authentication error" do
2014-09-02 18:07:02 +05:30
get api("/hooks")
2016-08-24 12:49:21 +05:30
expect(response).to have_http_status(401)
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)
2016-08-24 12:49:21 +05:30
expect(response).to have_http_status(403)
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)
2016-08-24 12:49:21 +05:30
expect(response).to have_http_status(200)
2015-04-26 12:48:37 +05:30
expect(json_response).to be_an Array
expect(json_response.first['url']).to eq(hook.url)
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
2014-09-02 18:07:02 +05:30
post api("/hooks", admin), 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)
2016-08-24 12:49:21 +05:30
expect(response).to have_http_status(400)
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
end
describe "GET /hooks/:id" do
2016-09-13 17:45:13 +05:30
it "returns hook by id" do
2014-09-02 18:07:02 +05:30
get api("/hooks/#{hook.id}", admin)
2016-08-24 12:49:21 +05:30
expect(response).to have_http_status(200)
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
2014-09-02 18:07:02 +05:30
get api("/hooks/404", admin)
2016-08-24 12:49:21 +05:30
expect(response).to have_http_status(404)
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)
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
delete api('/hooks/12345', admin)
expect(response).to have_http_status(404)
2014-09-02 18:07:02 +05:30
end
end
end