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

82 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
it "should return authentication error" do
get api("/hooks")
2015-04-26 12:48:37 +05:30
expect(response.status).to eq(401)
2014-09-02 18:07:02 +05:30
end
end
context "when not an admin" do
it "should return forbidden error" do
get api("/hooks", user)
2015-04-26 12:48:37 +05:30
expect(response.status).to eq(403)
2014-09-02 18:07:02 +05:30
end
end
context "when authenticated as admin" do
it "should return an array of hooks" do
get api("/hooks", admin)
2015-04-26 12:48:37 +05:30
expect(response.status).to eq(200)
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
it "should create 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
it "should respond with 400 if url not given" do
post api("/hooks", admin)
2015-04-26 12:48:37 +05:30
expect(response.status).to eq(400)
2014-09-02 18:07:02 +05:30
end
it "should 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)
2015-09-11 14:41:01 +05:30
end.to_not change { SystemHook.count }
2014-09-02 18:07:02 +05:30
end
end
describe "GET /hooks/:id" do
it "should return hook by id" do
get api("/hooks/#{hook.id}", admin)
2015-04-26 12:48:37 +05:30
expect(response.status).to eq(200)
expect(json_response['event_name']).to eq('project_create')
2014-09-02 18:07:02 +05:30
end
it "should return 404 on failure" do
get api("/hooks/404", admin)
2015-04-26 12:48:37 +05:30
expect(response.status).to eq(404)
2014-09-02 18:07:02 +05:30
end
end
describe "DELETE /hooks/:id" do
it "should delete 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
it "should return success if hook id not found" do
delete api("/hooks/12345", admin)
2015-04-26 12:48:37 +05:30
expect(response.status).to eq(200)
2014-09-02 18:07:02 +05:30
end
end
end