2013-12-04 06:20:16 -05:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2016-11-23 15:14:08 -05:00
|
|
|
describe API::ProjectHooks, 'ProjectHooks', api: true do
|
2013-12-04 06:20:16 -05:00
|
|
|
include ApiHelpers
|
|
|
|
let(:user) { create(:user) }
|
|
|
|
let(:user3) { create(:user) }
|
2017-01-17 01:15:35 -05:00
|
|
|
let!(:project) { create(:empty_project, creator_id: user.id, namespace: user.namespace) }
|
2015-12-07 07:23:23 -05:00
|
|
|
let!(:hook) do
|
|
|
|
create(:project_hook,
|
2016-08-12 04:48:47 -04:00
|
|
|
:all_events_enabled,
|
|
|
|
project: project,
|
|
|
|
url: 'http://example.com',
|
2015-12-07 07:23:23 -05:00
|
|
|
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
|
2016-08-01 11:00:44 -04:00
|
|
|
it "returns project hooks" do
|
2013-12-04 06:20:16 -05:00
|
|
|
get api("/projects/#{project.id}/hooks", user)
|
2016-06-27 14:10:42 -04:00
|
|
|
expect(response).to have_http_status(200)
|
2013-12-04 06:20:16 -05:00
|
|
|
|
2015-02-12 13:17:35 -05:00
|
|
|
expect(json_response).to be_an Array
|
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.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)
|
2017-03-03 10:39:29 -05:00
|
|
|
expect(json_response.first['job_events']).to eq(true)
|
2016-08-02 06:06:31 -04:00
|
|
|
expect(json_response.first['pipeline_events']).to eq(true)
|
2016-08-23 14:54:44 -04:00
|
|
|
expect(json_response.first['wiki_page_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
|
2016-08-01 11:00:44 -04:00
|
|
|
it "does not access project hooks" do
|
2013-12-04 06:20:16 -05:00
|
|
|
get api("/projects/#{project.id}/hooks", user3)
|
2016-06-27 14:10:42 -04:00
|
|
|
expect(response).to have_http_status(403)
|
2013-12-04 06:20:16 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "GET /projects/:id/hooks/:hook_id" do
|
|
|
|
context "authorized user" do
|
2016-08-01 11:00:44 -04:00
|
|
|
it "returns a project hook" do
|
2013-12-04 06:20:16 -05:00
|
|
|
get api("/projects/#{project.id}/hooks/#{hook.id}", user)
|
2016-06-27 14:10:42 -04:00
|
|
|
expect(response).to have_http_status(200)
|
2015-02-12 13:17:35 -05:00
|
|
|
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)
|
2017-03-03 10:39:29 -05:00
|
|
|
expect(json_response['job_events']).to eq(hook.build_events)
|
2016-08-23 14:51:05 -04:00
|
|
|
expect(json_response['pipeline_events']).to eq(hook.pipeline_events)
|
2016-08-23 14:54:44 -04:00
|
|
|
expect(json_response['wiki_page_events']).to eq(hook.wiki_page_events)
|
2015-09-24 14:16:36 -04:00
|
|
|
expect(json_response['enable_ssl_verification']).to eq(hook.enable_ssl_verification)
|
2013-12-04 06:20:16 -05:00
|
|
|
end
|
|
|
|
|
2016-08-01 11:00:44 -04:00
|
|
|
it "returns a 404 error if hook id is not available" do
|
2013-12-04 06:20:16 -05:00
|
|
|
get api("/projects/#{project.id}/hooks/1234", user)
|
2016-06-27 14:10:42 -04:00
|
|
|
expect(response).to have_http_status(404)
|
2013-12-04 06:20:16 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "unauthorized user" do
|
2016-08-01 11:00:44 -04:00
|
|
|
it "does not access an existing hook" do
|
2013-12-04 06:20:16 -05:00
|
|
|
get api("/projects/#{project.id}/hooks/#{hook.id}", user3)
|
2016-06-27 14:10:42 -04:00
|
|
|
expect(response).to have_http_status(403)
|
2013-12-04 06:20:16 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-08-01 11:00:44 -04:00
|
|
|
it "returns a 404 error if hook id is not available" do
|
2013-12-04 06:20:16 -05:00
|
|
|
get api("/projects/#{project.id}/hooks/1234", user)
|
2016-06-27 14:10:42 -04:00
|
|
|
expect(response).to have_http_status(404)
|
2013-12-04 06:20:16 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "POST /projects/:id/hooks" do
|
2016-08-01 11:00:44 -04:00
|
|
|
it "adds hook to project" do
|
2015-06-22 16:00:54 -04:00
|
|
|
expect do
|
2017-01-03 15:07:12 -05:00
|
|
|
post api("/projects/#{project.id}/hooks", user),
|
|
|
|
url: "http://example.com", issues_events: true, wiki_page_events: true
|
2015-06-22 16:00:54 -04:00
|
|
|
end.to change {project.hooks.count}.by(1)
|
2016-11-01 07:40:06 -04:00
|
|
|
|
2016-06-27 14:10:42 -04:00
|
|
|
expect(response).to have_http_status(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)
|
2017-03-03 10:39:29 -05:00
|
|
|
expect(json_response['job_events']).to eq(false)
|
2016-08-02 06:06:31 -04:00
|
|
|
expect(json_response['pipeline_events']).to eq(false)
|
2017-01-03 15:07:12 -05:00
|
|
|
expect(json_response['wiki_page_events']).to eq(true)
|
2015-09-24 14:16:36 -04:00
|
|
|
expect(json_response['enable_ssl_verification']).to eq(true)
|
2016-11-01 07:40:06 -04:00
|
|
|
expect(json_response).not_to include('token')
|
|
|
|
end
|
|
|
|
|
|
|
|
it "adds the token without including it in the response" do
|
|
|
|
token = "secret token"
|
|
|
|
|
|
|
|
expect do
|
|
|
|
post api("/projects/#{project.id}/hooks", user), url: "http://example.com", token: token
|
|
|
|
end.to change {project.hooks.count}.by(1)
|
|
|
|
|
|
|
|
expect(response).to have_http_status(201)
|
|
|
|
expect(json_response["url"]).to eq("http://example.com")
|
|
|
|
expect(json_response).not_to include("token")
|
|
|
|
|
|
|
|
hook = project.hooks.find(json_response["id"])
|
|
|
|
|
|
|
|
expect(hook.url).to eq("http://example.com")
|
|
|
|
expect(hook.token).to eq(token)
|
2013-12-04 06:20:16 -05:00
|
|
|
end
|
|
|
|
|
2016-08-01 11:00:44 -04:00
|
|
|
it "returns a 400 error if url not given" do
|
2013-12-04 06:20:16 -05:00
|
|
|
post api("/projects/#{project.id}/hooks", user)
|
2016-06-27 14:10:42 -04:00
|
|
|
expect(response).to have_http_status(400)
|
2013-12-04 06:20:16 -05:00
|
|
|
end
|
|
|
|
|
2016-08-01 11:00:44 -04:00
|
|
|
it "returns a 422 error if url not valid" do
|
2013-12-04 06:20:16 -05:00
|
|
|
post api("/projects/#{project.id}/hooks", user), "url" => "ftp://example.com"
|
2016-06-27 14:10:42 -04:00
|
|
|
expect(response).to have_http_status(422)
|
2013-12-04 06:20:16 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "PUT /projects/:id/hooks/:hook_id" do
|
2016-08-01 11:00:44 -04:00
|
|
|
it "updates an existing project hook" do
|
2013-12-04 06:20:16 -05:00
|
|
|
put api("/projects/#{project.id}/hooks/#{hook.id}", user),
|
2013-12-04 06:35:38 -05:00
|
|
|
url: 'http://example.org', push_events: false
|
2016-06-27 14:10:42 -04:00
|
|
|
expect(response).to have_http_status(200)
|
2015-02-12 13:17:35 -05:00
|
|
|
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)
|
2017-03-03 10:39:29 -05:00
|
|
|
expect(json_response['job_events']).to eq(hook.build_events)
|
2016-08-23 14:51:05 -04:00
|
|
|
expect(json_response['pipeline_events']).to eq(hook.pipeline_events)
|
2016-08-23 14:54:44 -04:00
|
|
|
expect(json_response['wiki_page_events']).to eq(hook.wiki_page_events)
|
2015-09-24 14:16:36 -04:00
|
|
|
expect(json_response['enable_ssl_verification']).to eq(hook.enable_ssl_verification)
|
2016-11-01 07:40:06 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "adds the token without including it in the response" do
|
|
|
|
token = "secret token"
|
|
|
|
|
|
|
|
put api("/projects/#{project.id}/hooks/#{hook.id}", user), url: "http://example.org", token: token
|
|
|
|
|
|
|
|
expect(response).to have_http_status(200)
|
|
|
|
expect(json_response["url"]).to eq("http://example.org")
|
|
|
|
expect(json_response).not_to include("token")
|
|
|
|
|
|
|
|
expect(hook.reload.url).to eq("http://example.org")
|
|
|
|
expect(hook.reload.token).to eq(token)
|
2013-12-04 06:20:16 -05:00
|
|
|
end
|
|
|
|
|
2016-08-01 11:00:44 -04:00
|
|
|
it "returns 404 error if hook id not found" do
|
2013-12-04 06:20:16 -05:00
|
|
|
put api("/projects/#{project.id}/hooks/1234", user), url: 'http://example.org'
|
2016-06-27 14:10:42 -04:00
|
|
|
expect(response).to have_http_status(404)
|
2013-12-04 06:20:16 -05:00
|
|
|
end
|
|
|
|
|
2016-08-01 11:00:44 -04:00
|
|
|
it "returns 400 error if url is not given" do
|
2013-12-04 06:20:16 -05:00
|
|
|
put api("/projects/#{project.id}/hooks/#{hook.id}", user)
|
2016-06-27 14:10:42 -04:00
|
|
|
expect(response).to have_http_status(400)
|
2013-12-04 06:20:16 -05:00
|
|
|
end
|
|
|
|
|
2016-08-01 11:00:44 -04:00
|
|
|
it "returns a 422 error if url is not valid" do
|
2013-12-04 06:20:16 -05:00
|
|
|
put api("/projects/#{project.id}/hooks/#{hook.id}", user), url: 'ftp://example.com'
|
2016-06-27 14:10:42 -04:00
|
|
|
expect(response).to have_http_status(422)
|
2013-12-04 06:20:16 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "DELETE /projects/:id/hooks/:hook_id" do
|
2016-08-01 11:00:44 -04:00
|
|
|
it "deletes 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)
|
|
|
|
|
2017-02-20 13:18:12 -05:00
|
|
|
expect(response).to have_http_status(204)
|
|
|
|
end.to change {project.hooks.count}.by(-1)
|
2013-12-04 06:20:16 -05:00
|
|
|
end
|
|
|
|
|
2016-08-01 11:00:44 -04:00
|
|
|
it "returns a 404 error when deleting non existent hook" do
|
2013-12-04 06:20:16 -05:00
|
|
|
delete api("/projects/#{project.id}/hooks/42", user)
|
2016-06-27 14:10:42 -04:00
|
|
|
expect(response).to have_http_status(404)
|
2013-12-04 06:20:16 -05:00
|
|
|
end
|
|
|
|
|
2016-10-07 13:18:02 -04:00
|
|
|
it "returns a 404 error if hook id not given" do
|
2013-12-04 06:20:16 -05:00
|
|
|
delete api("/projects/#{project.id}/hooks", user)
|
2016-10-07 11:39:04 -04:00
|
|
|
|
2016-10-07 13:18:02 -04:00
|
|
|
expect(response).to have_http_status(404)
|
2013-12-04 06:20:16 -05:00
|
|
|
end
|
2016-04-25 01:39:18 -04:00
|
|
|
|
2016-08-01 11:00:44 -04:00
|
|
|
it "returns a 404 if a user attempts to delete project hooks he/she does not own" do
|
2016-04-25 01:39:18 -04:00
|
|
|
test_user = create(:user)
|
2017-01-17 01:15:35 -05:00
|
|
|
other_project = create(:empty_project)
|
2016-04-25 01:39:18 -04:00
|
|
|
other_project.team << [test_user, :master]
|
|
|
|
|
|
|
|
delete api("/projects/#{other_project.id}/hooks/#{hook.id}", test_user)
|
2016-06-27 14:10:42 -04:00
|
|
|
expect(response).to have_http_status(404)
|
2016-04-25 01:39:18 -04:00
|
|
|
expect(WebHook.exists?(hook.id)).to be_truthy
|
|
|
|
end
|
2013-12-04 06:20:16 -05:00
|
|
|
end
|
|
|
|
end
|