2012-07-18 21:24:37 +00:00
|
|
|
require "spec_helper"
|
|
|
|
|
2017-07-10 14:24:02 +00:00
|
|
|
describe SystemHook do
|
2017-05-05 12:11:28 +00:00
|
|
|
context 'default attributes' do
|
|
|
|
let(:system_hook) { build(:system_hook) }
|
|
|
|
|
|
|
|
it 'sets defined default parameters' do
|
|
|
|
attrs = {
|
|
|
|
push_events: false,
|
2017-07-20 15:12:06 +00:00
|
|
|
repository_update_events: true
|
2017-05-05 12:11:28 +00:00
|
|
|
}
|
|
|
|
expect(system_hook).to have_attributes(attrs)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-07-18 21:24:37 +00:00
|
|
|
describe "execute" do
|
2016-03-30 18:00:57 +00:00
|
|
|
let(:system_hook) { create(:system_hook) }
|
|
|
|
let(:user) { create(:user) }
|
2017-08-02 19:55:11 +00:00
|
|
|
let(:project) { create(:project, namespace: user.namespace) }
|
2016-03-30 18:00:57 +00:00
|
|
|
let(:group) { create(:group) }
|
2017-03-27 09:37:24 +00:00
|
|
|
let(:params) do
|
|
|
|
{ name: 'John Doe', username: 'jduser', email: 'jg@example.com', password: 'mydummypass' }
|
|
|
|
end
|
2016-03-30 18:00:57 +00:00
|
|
|
|
|
|
|
before do
|
|
|
|
WebMock.stub_request(:post, system_hook.url)
|
2012-07-18 21:24:37 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "project_create hook" do
|
2016-03-30 18:00:57 +00:00
|
|
|
Projects::CreateService.new(user, name: 'empty').execute
|
|
|
|
expect(WebMock).to have_requested(:post, system_hook.url).with(
|
2015-01-24 00:10:43 +00:00
|
|
|
body: /project_create/,
|
2016-05-11 02:58:06 +00:00
|
|
|
headers: { 'Content-Type' => 'application/json', 'X-Gitlab-Event' => 'System Hook' }
|
2015-01-24 00:10:43 +00:00
|
|
|
).once
|
2012-07-18 21:24:37 +00:00
|
|
|
end
|
2012-08-28 05:42:28 +00:00
|
|
|
|
2012-07-18 21:24:37 +00:00
|
|
|
it "project_destroy hook" do
|
2016-08-06 14:25:51 +00:00
|
|
|
Projects::DestroyService.new(project, user, {}).async_execute
|
2016-03-30 18:00:57 +00:00
|
|
|
|
|
|
|
expect(WebMock).to have_requested(:post, system_hook.url).with(
|
2015-01-24 00:10:43 +00:00
|
|
|
body: /project_destroy/,
|
2016-05-11 02:58:06 +00:00
|
|
|
headers: { 'Content-Type' => 'application/json', 'X-Gitlab-Event' => 'System Hook' }
|
2015-01-24 00:10:43 +00:00
|
|
|
).once
|
2012-07-18 21:24:37 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "user_create hook" do
|
2017-03-27 09:37:24 +00:00
|
|
|
Users::CreateService.new(nil, params).execute
|
2016-09-16 11:37:48 +00:00
|
|
|
|
2016-03-30 18:00:57 +00:00
|
|
|
expect(WebMock).to have_requested(:post, system_hook.url).with(
|
2015-01-24 00:10:43 +00:00
|
|
|
body: /user_create/,
|
2016-05-11 02:58:06 +00:00
|
|
|
headers: { 'Content-Type' => 'application/json', 'X-Gitlab-Event' => 'System Hook' }
|
2015-01-24 00:10:43 +00:00
|
|
|
).once
|
2012-07-18 21:24:37 +00:00
|
|
|
end
|
2012-08-28 05:42:28 +00:00
|
|
|
|
2012-07-18 21:24:37 +00:00
|
|
|
it "user_destroy hook" do
|
2013-01-09 06:14:05 +00:00
|
|
|
user.destroy
|
2016-03-30 18:00:57 +00:00
|
|
|
|
|
|
|
expect(WebMock).to have_requested(:post, system_hook.url).with(
|
2015-01-24 00:10:43 +00:00
|
|
|
body: /user_destroy/,
|
2016-05-11 02:58:06 +00:00
|
|
|
headers: { 'Content-Type' => 'application/json', 'X-Gitlab-Event' => 'System Hook' }
|
2015-01-24 00:10:43 +00:00
|
|
|
).once
|
2012-07-18 21:24:37 +00:00
|
|
|
end
|
2012-08-28 05:42:28 +00:00
|
|
|
|
2012-07-18 21:24:37 +00:00
|
|
|
it "project_create hook" do
|
2013-01-09 06:14:05 +00:00
|
|
|
project.team << [user, :master]
|
2016-03-30 18:00:57 +00:00
|
|
|
|
|
|
|
expect(WebMock).to have_requested(:post, system_hook.url).with(
|
2016-01-22 19:13:37 +00:00
|
|
|
body: /user_add_to_team/,
|
2016-05-11 02:58:06 +00:00
|
|
|
headers: { 'Content-Type' => 'application/json', 'X-Gitlab-Event' => 'System Hook' }
|
2015-01-24 00:10:43 +00:00
|
|
|
).once
|
2012-07-18 21:24:37 +00:00
|
|
|
end
|
2012-08-28 05:42:28 +00:00
|
|
|
|
2012-07-18 21:24:37 +00:00
|
|
|
it "project_destroy hook" do
|
2013-01-04 06:43:25 +00:00
|
|
|
project.team << [user, :master]
|
2014-09-14 16:32:51 +00:00
|
|
|
project.project_members.destroy_all
|
2016-03-30 18:00:57 +00:00
|
|
|
|
|
|
|
expect(WebMock).to have_requested(:post, system_hook.url).with(
|
2016-01-22 19:13:37 +00:00
|
|
|
body: /user_remove_from_team/,
|
2016-05-11 02:58:06 +00:00
|
|
|
headers: { 'Content-Type' => 'application/json', 'X-Gitlab-Event' => 'System Hook' }
|
2015-01-24 00:10:43 +00:00
|
|
|
).once
|
2012-07-18 21:24:37 +00:00
|
|
|
end
|
2014-08-21 10:53:32 +00:00
|
|
|
|
|
|
|
it 'group create hook' do
|
|
|
|
create(:group)
|
2016-03-30 18:00:57 +00:00
|
|
|
|
|
|
|
expect(WebMock).to have_requested(:post, system_hook.url).with(
|
2015-01-24 00:10:43 +00:00
|
|
|
body: /group_create/,
|
2016-05-11 02:58:06 +00:00
|
|
|
headers: { 'Content-Type' => 'application/json', 'X-Gitlab-Event' => 'System Hook' }
|
2014-08-21 10:53:32 +00:00
|
|
|
).once
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'group destroy hook' do
|
|
|
|
group.destroy
|
2016-03-30 18:00:57 +00:00
|
|
|
|
|
|
|
expect(WebMock).to have_requested(:post, system_hook.url).with(
|
2015-01-24 00:10:43 +00:00
|
|
|
body: /group_destroy/,
|
2016-05-11 02:58:06 +00:00
|
|
|
headers: { 'Content-Type' => 'application/json', 'X-Gitlab-Event' => 'System Hook' }
|
2014-08-21 10:53:32 +00:00
|
|
|
).once
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'group member create hook' do
|
2015-08-07 04:20:02 +00:00
|
|
|
group.add_master(user)
|
2016-03-30 18:00:57 +00:00
|
|
|
|
|
|
|
expect(WebMock).to have_requested(:post, system_hook.url).with(
|
2015-01-24 00:10:43 +00:00
|
|
|
body: /user_add_to_group/,
|
2016-05-11 02:58:06 +00:00
|
|
|
headers: { 'Content-Type' => 'application/json', 'X-Gitlab-Event' => 'System Hook' }
|
2014-08-21 10:53:32 +00:00
|
|
|
).once
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'group member destroy hook' do
|
2015-08-07 04:20:02 +00:00
|
|
|
group.add_master(user)
|
2014-08-21 10:53:32 +00:00
|
|
|
group.group_members.destroy_all
|
2016-03-30 18:00:57 +00:00
|
|
|
|
|
|
|
expect(WebMock).to have_requested(:post, system_hook.url).with(
|
2015-01-24 00:10:43 +00:00
|
|
|
body: /user_remove_from_group/,
|
2016-05-11 02:58:06 +00:00
|
|
|
headers: { 'Content-Type' => 'application/json', 'X-Gitlab-Event' => 'System Hook' }
|
2014-08-21 10:53:32 +00:00
|
|
|
).once
|
|
|
|
end
|
2012-07-18 21:24:37 +00:00
|
|
|
end
|
2017-05-03 02:36:13 +00:00
|
|
|
|
|
|
|
describe '.repository_update_hooks' do
|
|
|
|
it 'returns hooks for repository update events only' do
|
|
|
|
hook = create(:system_hook, repository_update_events: true)
|
|
|
|
create(:system_hook, repository_update_events: false)
|
2017-07-25 17:09:00 +00:00
|
|
|
expect(described_class.repository_update_hooks).to eq([hook])
|
2017-05-03 02:36:13 +00:00
|
|
|
end
|
|
|
|
end
|
2017-04-27 10:08:57 +00:00
|
|
|
|
|
|
|
describe 'execute WebHookService' do
|
|
|
|
let(:hook) { build(:system_hook) }
|
|
|
|
let(:data) { { key: 'value' } }
|
|
|
|
let(:hook_name) { 'system_hook' }
|
|
|
|
|
|
|
|
before do
|
|
|
|
expect(WebHookService).to receive(:new).with(hook, data, hook_name).and_call_original
|
|
|
|
end
|
|
|
|
|
|
|
|
it '#execute' do
|
|
|
|
expect_any_instance_of(WebHookService).to receive(:execute)
|
|
|
|
|
|
|
|
hook.execute(data, hook_name)
|
|
|
|
end
|
|
|
|
|
|
|
|
it '#async_execute' do
|
|
|
|
expect_any_instance_of(WebHookService).to receive(:async_execute)
|
|
|
|
|
|
|
|
hook.async_execute(data, hook_name)
|
|
|
|
end
|
|
|
|
end
|
2012-07-18 21:24:37 +00:00
|
|
|
end
|