2013-12-04 06:20:16 -05:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2015-12-07 07:23:23 -05:00
|
|
|
describe API::API, 'ProjectHooks', api: true do
|
2013-12-04 06:20:16 -05:00
|
|
|
include ApiHelpers
|
|
|
|
let(:user) { create(:user) }
|
|
|
|
let(:user3) { create(:user) }
|
2014-01-22 14:03:52 -05:00
|
|
|
let!(:project) { create(:project, creator_id: user.id, namespace: user.namespace) }
|
2015-12-07 07:23:23 -05:00
|
|
|
let!(:hook) do
|
|
|
|
create(:project_hook,
|
|
|
|
project: project, url: "http://example.com",
|
|
|
|
push_events: true, merge_requests_events: true, tag_push_events: true,
|
|
|
|
issues_events: true, note_events: true, build_events: true,
|
|
|
|
enable_ssl_verification: true)
|
|
|
|
end
|
2013-12-04 06:20:16 -05:00
|
|
|
|
|
|
|
before do
|
|
|
|
project.team << [user, :master]
|
|
|
|
project.team << [user3, :developer]
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "GET /projects/:id/hooks" do
|
|
|
|
context "authorized user" do
|
|
|
|
it "should return project hooks" do
|
|
|
|
get api("/projects/#{project.id}/hooks", user)
|
2015-02-12 13:17:35 -05:00
|
|
|
expect(response.status).to eq(200)
|
2013-12-04 06:20:16 -05:00
|
|
|
|
2015-02-12 13:17:35 -05:00
|
|
|
expect(json_response).to be_an Array
|
|
|
|
expect(json_response.count).to eq(1)
|
|
|
|
expect(json_response.first['url']).to eq("http://example.com")
|
2015-09-24 14:16:36 -04:00
|
|
|
expect(json_response.first['issues_events']).to eq(true)
|
|
|
|
expect(json_response.first['push_events']).to eq(true)
|
|
|
|
expect(json_response.first['merge_requests_events']).to eq(true)
|
|
|
|
expect(json_response.first['tag_push_events']).to eq(true)
|
|
|
|
expect(json_response.first['note_events']).to eq(true)
|
2015-12-07 07:23:23 -05:00
|
|
|
expect(json_response.first['build_events']).to eq(true)
|
2015-09-24 14:16:36 -04:00
|
|
|
expect(json_response.first['enable_ssl_verification']).to eq(true)
|
2013-12-04 06:20:16 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "unauthorized user" do
|
|
|
|
it "should not access project hooks" do
|
|
|
|
get api("/projects/#{project.id}/hooks", user3)
|
2015-02-12 13:17:35 -05:00
|
|
|
expect(response.status).to eq(403)
|
2013-12-04 06:20:16 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "GET /projects/:id/hooks/:hook_id" do
|
|
|
|
context "authorized user" do
|
|
|
|
it "should return a project hook" do
|
|
|
|
get api("/projects/#{project.id}/hooks/#{hook.id}", user)
|
2015-02-12 13:17:35 -05:00
|
|
|
expect(response.status).to eq(200)
|
|
|
|
expect(json_response['url']).to eq(hook.url)
|
2015-09-24 14:16:36 -04:00
|
|
|
expect(json_response['issues_events']).to eq(hook.issues_events)
|
|
|
|
expect(json_response['push_events']).to eq(hook.push_events)
|
|
|
|
expect(json_response['merge_requests_events']).to eq(hook.merge_requests_events)
|
|
|
|
expect(json_response['tag_push_events']).to eq(hook.tag_push_events)
|
|
|
|
expect(json_response['note_events']).to eq(hook.note_events)
|
|
|
|
expect(json_response['enable_ssl_verification']).to eq(hook.enable_ssl_verification)
|
2013-12-04 06:20:16 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should return a 404 error if hook id is not available" do
|
|
|
|
get api("/projects/#{project.id}/hooks/1234", user)
|
2015-02-12 13:17:35 -05:00
|
|
|
expect(response.status).to eq(404)
|
2013-12-04 06:20:16 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "unauthorized user" do
|
|
|
|
it "should not access an existing hook" do
|
|
|
|
get api("/projects/#{project.id}/hooks/#{hook.id}", user3)
|
2015-02-12 13:17:35 -05:00
|
|
|
expect(response.status).to eq(403)
|
2013-12-04 06:20:16 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should return a 404 error if hook id is not available" do
|
|
|
|
get api("/projects/#{project.id}/hooks/1234", user)
|
2015-02-12 13:17:35 -05:00
|
|
|
expect(response.status).to eq(404)
|
2013-12-04 06:20:16 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "POST /projects/:id/hooks" do
|
|
|
|
it "should add hook to project" do
|
2015-06-22 16:00:54 -04:00
|
|
|
expect do
|
|
|
|
post api("/projects/#{project.id}/hooks", user), url: "http://example.com", issues_events: true
|
|
|
|
end.to change {project.hooks.count}.by(1)
|
2015-02-12 13:17:35 -05:00
|
|
|
expect(response.status).to eq(201)
|
2015-09-24 14:16:36 -04:00
|
|
|
expect(json_response['url']).to eq('http://example.com')
|
|
|
|
expect(json_response['issues_events']).to eq(true)
|
|
|
|
expect(json_response['push_events']).to eq(true)
|
|
|
|
expect(json_response['merge_requests_events']).to eq(false)
|
|
|
|
expect(json_response['tag_push_events']).to eq(false)
|
|
|
|
expect(json_response['note_events']).to eq(false)
|
2015-12-07 07:23:23 -05:00
|
|
|
expect(json_response['build_events']).to eq(false)
|
2015-09-24 14:16:36 -04:00
|
|
|
expect(json_response['enable_ssl_verification']).to eq(true)
|
2013-12-04 06:20:16 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should return a 400 error if url not given" do
|
|
|
|
post api("/projects/#{project.id}/hooks", user)
|
2015-02-12 13:17:35 -05:00
|
|
|
expect(response.status).to eq(400)
|
2013-12-04 06:20:16 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should return a 422 error if url not valid" do
|
|
|
|
post api("/projects/#{project.id}/hooks", user), "url" => "ftp://example.com"
|
2015-02-12 13:17:35 -05:00
|
|
|
expect(response.status).to eq(422)
|
2013-12-04 06:20:16 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "PUT /projects/:id/hooks/:hook_id" do
|
|
|
|
it "should update an existing project hook" do
|
|
|
|
put api("/projects/#{project.id}/hooks/#{hook.id}", user),
|
2013-12-04 06:35:38 -05:00
|
|
|
url: 'http://example.org', push_events: false
|
2015-02-12 13:17:35 -05:00
|
|
|
expect(response.status).to eq(200)
|
|
|
|
expect(json_response['url']).to eq('http://example.org')
|
2015-09-24 14:16:36 -04:00
|
|
|
expect(json_response['issues_events']).to eq(hook.issues_events)
|
|
|
|
expect(json_response['push_events']).to eq(false)
|
|
|
|
expect(json_response['merge_requests_events']).to eq(hook.merge_requests_events)
|
|
|
|
expect(json_response['tag_push_events']).to eq(hook.tag_push_events)
|
|
|
|
expect(json_response['note_events']).to eq(hook.note_events)
|
|
|
|
expect(json_response['enable_ssl_verification']).to eq(hook.enable_ssl_verification)
|
2013-12-04 06:20:16 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should return 404 error if hook id not found" do
|
|
|
|
put api("/projects/#{project.id}/hooks/1234", user), url: 'http://example.org'
|
2015-02-12 13:17:35 -05:00
|
|
|
expect(response.status).to eq(404)
|
2013-12-04 06:20:16 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should return 400 error if url is not given" do
|
|
|
|
put api("/projects/#{project.id}/hooks/#{hook.id}", user)
|
2015-02-12 13:17:35 -05:00
|
|
|
expect(response.status).to eq(400)
|
2013-12-04 06:20:16 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should return a 422 error if url is not valid" do
|
|
|
|
put api("/projects/#{project.id}/hooks/#{hook.id}", user), url: 'ftp://example.com'
|
2015-02-12 13:17:35 -05:00
|
|
|
expect(response.status).to eq(422)
|
2013-12-04 06:20:16 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "DELETE /projects/:id/hooks/:hook_id" do
|
|
|
|
it "should delete hook from project" do
|
2015-06-22 16:00:54 -04:00
|
|
|
expect do
|
2013-12-04 06:20:16 -05:00
|
|
|
delete api("/projects/#{project.id}/hooks/#{hook.id}", user)
|
2015-06-22 16:00:54 -04:00
|
|
|
end.to change {project.hooks.count}.by(-1)
|
2015-02-12 13:17:35 -05:00
|
|
|
expect(response.status).to eq(200)
|
2013-12-04 06:20:16 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should return success when deleting hook" do
|
|
|
|
delete api("/projects/#{project.id}/hooks/#{hook.id}", user)
|
2015-02-12 13:17:35 -05:00
|
|
|
expect(response.status).to eq(200)
|
2013-12-04 06:20:16 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should return success when deleting non existent hook" do
|
|
|
|
delete api("/projects/#{project.id}/hooks/42", user)
|
2015-02-12 13:17:35 -05:00
|
|
|
expect(response.status).to eq(200)
|
2013-12-04 06:20:16 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should return a 405 error if hook id not given" do
|
|
|
|
delete api("/projects/#{project.id}/hooks", user)
|
2015-02-12 13:17:35 -05:00
|
|
|
expect(response.status).to eq(405)
|
2013-12-04 06:20:16 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|