2019-07-07 11:18:12 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-04-26 12:48:37 +05:30
|
|
|
require "spec_helper"
|
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
RSpec.describe SystemHook do
|
2017-09-10 17:25:29 +05:30
|
|
|
context 'default attributes' do
|
|
|
|
let(:system_hook) { build(:system_hook) }
|
|
|
|
|
|
|
|
it 'sets defined default parameters' do
|
|
|
|
attrs = {
|
|
|
|
push_events: false,
|
2018-03-17 18:26:18 +05:30
|
|
|
repository_update_events: true,
|
|
|
|
merge_requests_events: false
|
2017-09-10 17:25:29 +05:30
|
|
|
}
|
|
|
|
expect(system_hook).to have_attributes(attrs)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-10-27 15:23:28 +05:30
|
|
|
describe 'validations' do
|
|
|
|
describe 'url' do
|
|
|
|
let(:url) { 'http://localhost:9000' }
|
|
|
|
|
|
|
|
it { is_expected.not_to allow_value(url).for(:url) }
|
|
|
|
|
|
|
|
it 'is valid if application settings allow local requests from system hooks' do
|
|
|
|
settings = ApplicationSetting.new(allow_local_requests_from_system_hooks: true)
|
|
|
|
allow(ApplicationSetting).to receive(:current).and_return(settings)
|
|
|
|
|
|
|
|
is_expected.to allow_value(url).for(:url)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-12-26 22:10:19 +05:30
|
|
|
describe "execute", :sidekiq_might_not_need_inline do
|
2016-06-02 11:05:42 +05:30
|
|
|
let(:system_hook) { create(:system_hook) }
|
|
|
|
let(:user) { create(:user) }
|
2017-09-10 17:25:29 +05:30
|
|
|
let(:project) { create(:project, namespace: user.namespace) }
|
2016-06-02 11:05:42 +05:30
|
|
|
let(:group) { create(:group) }
|
2017-08-17 22:00:37 +05:30
|
|
|
let(:params) do
|
2022-03-02 08:16:31 +05:30
|
|
|
{ name: 'John Doe', username: 'jduser', email: 'jg@example.com', password: Gitlab::Password.test_default }
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
2016-06-02 11:05:42 +05:30
|
|
|
|
|
|
|
before do
|
|
|
|
WebMock.stub_request(:post, system_hook.url)
|
2015-04-26 12:48:37 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it "project_create hook" do
|
2016-06-02 11:05:42 +05:30
|
|
|
Projects::CreateService.new(user, name: 'empty').execute
|
|
|
|
expect(WebMock).to have_requested(:post, system_hook.url).with(
|
2015-09-11 14:41:01 +05:30
|
|
|
body: /project_create/,
|
2016-06-02 11:05:42 +05:30
|
|
|
headers: { 'Content-Type' => 'application/json', 'X-Gitlab-Event' => 'System Hook' }
|
2015-09-11 14:41:01 +05:30
|
|
|
).once
|
2015-04-26 12:48:37 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it "project_destroy hook" do
|
2016-09-13 17:45:13 +05:30
|
|
|
Projects::DestroyService.new(project, user, {}).async_execute
|
2016-06-02 11:05:42 +05:30
|
|
|
|
|
|
|
expect(WebMock).to have_requested(:post, system_hook.url).with(
|
2015-09-11 14:41:01 +05:30
|
|
|
body: /project_destroy/,
|
2016-06-02 11:05:42 +05:30
|
|
|
headers: { 'Content-Type' => 'application/json', 'X-Gitlab-Event' => 'System Hook' }
|
2015-09-11 14:41:01 +05:30
|
|
|
).once
|
2015-04-26 12:48:37 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it "user_create hook" do
|
2017-08-17 22:00:37 +05:30
|
|
|
Users::CreateService.new(nil, params).execute
|
2016-09-29 09:46:39 +05:30
|
|
|
|
2016-06-02 11:05:42 +05:30
|
|
|
expect(WebMock).to have_requested(:post, system_hook.url).with(
|
2015-09-11 14:41:01 +05:30
|
|
|
body: /user_create/,
|
2016-06-02 11:05:42 +05:30
|
|
|
headers: { 'Content-Type' => 'application/json', 'X-Gitlab-Event' => 'System Hook' }
|
2015-09-11 14:41:01 +05:30
|
|
|
).once
|
2015-04-26 12:48:37 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it "user_destroy hook" do
|
2021-04-29 21:17:54 +05:30
|
|
|
user.destroy!
|
2016-06-02 11:05:42 +05:30
|
|
|
|
|
|
|
expect(WebMock).to have_requested(:post, system_hook.url).with(
|
2015-09-11 14:41:01 +05:30
|
|
|
body: /user_destroy/,
|
2016-06-02 11:05:42 +05:30
|
|
|
headers: { 'Content-Type' => 'application/json', 'X-Gitlab-Event' => 'System Hook' }
|
2015-09-11 14:41:01 +05:30
|
|
|
).once
|
2015-04-26 12:48:37 +05:30
|
|
|
end
|
|
|
|
|
2019-12-04 20:38:33 +05:30
|
|
|
it "project member create hook" do
|
2018-11-18 11:00:15 +05:30
|
|
|
project.add_maintainer(user)
|
2016-06-02 11:05:42 +05:30
|
|
|
|
|
|
|
expect(WebMock).to have_requested(:post, system_hook.url).with(
|
2016-04-02 18:10:28 +05:30
|
|
|
body: /user_add_to_team/,
|
2016-06-02 11:05:42 +05:30
|
|
|
headers: { 'Content-Type' => 'application/json', 'X-Gitlab-Event' => 'System Hook' }
|
2015-09-11 14:41:01 +05:30
|
|
|
).once
|
2015-04-26 12:48:37 +05:30
|
|
|
end
|
|
|
|
|
2019-12-04 20:38:33 +05:30
|
|
|
it "project member destroy hook" do
|
2018-11-18 11:00:15 +05:30
|
|
|
project.add_maintainer(user)
|
2020-06-23 00:09:42 +05:30
|
|
|
project.project_members.destroy_all # rubocop: disable Cop/DestroyAll
|
2016-06-02 11:05:42 +05:30
|
|
|
|
|
|
|
expect(WebMock).to have_requested(:post, system_hook.url).with(
|
2016-04-02 18:10:28 +05:30
|
|
|
body: /user_remove_from_team/,
|
2016-06-02 11:05:42 +05:30
|
|
|
headers: { 'Content-Type' => 'application/json', 'X-Gitlab-Event' => 'System Hook' }
|
2015-09-11 14:41:01 +05:30
|
|
|
).once
|
2015-04-26 12:48:37 +05:30
|
|
|
end
|
|
|
|
|
2019-12-04 20:38:33 +05:30
|
|
|
it "project member update hook" do
|
|
|
|
project.add_guest(user)
|
|
|
|
|
|
|
|
expect(WebMock).to have_requested(:post, system_hook.url).with(
|
|
|
|
body: /user_update_for_team/,
|
|
|
|
headers: { 'Content-Type' => 'application/json', 'X-Gitlab-Event' => 'System Hook' }
|
|
|
|
).once
|
|
|
|
end
|
|
|
|
|
2015-04-26 12:48:37 +05:30
|
|
|
it 'group create hook' do
|
|
|
|
create(:group)
|
2016-06-02 11:05:42 +05:30
|
|
|
|
|
|
|
expect(WebMock).to have_requested(:post, system_hook.url).with(
|
2015-09-11 14:41:01 +05:30
|
|
|
body: /group_create/,
|
2016-06-02 11:05:42 +05:30
|
|
|
headers: { 'Content-Type' => 'application/json', 'X-Gitlab-Event' => 'System Hook' }
|
2015-04-26 12:48:37 +05:30
|
|
|
).once
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'group destroy hook' do
|
2021-04-29 21:17:54 +05:30
|
|
|
group.destroy!
|
2016-06-02 11:05:42 +05:30
|
|
|
|
|
|
|
expect(WebMock).to have_requested(:post, system_hook.url).with(
|
2015-09-11 14:41:01 +05:30
|
|
|
body: /group_destroy/,
|
2016-06-02 11:05:42 +05:30
|
|
|
headers: { 'Content-Type' => 'application/json', 'X-Gitlab-Event' => 'System Hook' }
|
2015-04-26 12:48:37 +05:30
|
|
|
).once
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'group member create hook' do
|
2018-11-18 11:00:15 +05:30
|
|
|
group.add_maintainer(user)
|
2016-06-02 11:05:42 +05:30
|
|
|
|
|
|
|
expect(WebMock).to have_requested(:post, system_hook.url).with(
|
2015-09-11 14:41:01 +05:30
|
|
|
body: /user_add_to_group/,
|
2016-06-02 11:05:42 +05:30
|
|
|
headers: { 'Content-Type' => 'application/json', 'X-Gitlab-Event' => 'System Hook' }
|
2015-04-26 12:48:37 +05:30
|
|
|
).once
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'group member destroy hook' do
|
2018-11-18 11:00:15 +05:30
|
|
|
group.add_maintainer(user)
|
2020-06-23 00:09:42 +05:30
|
|
|
group.group_members.destroy_all # rubocop: disable Cop/DestroyAll
|
2016-06-02 11:05:42 +05:30
|
|
|
|
|
|
|
expect(WebMock).to have_requested(:post, system_hook.url).with(
|
2015-09-11 14:41:01 +05:30
|
|
|
body: /user_remove_from_group/,
|
2016-06-02 11:05:42 +05:30
|
|
|
headers: { 'Content-Type' => 'application/json', 'X-Gitlab-Event' => 'System Hook' }
|
2015-04-26 12:48:37 +05:30
|
|
|
).once
|
|
|
|
end
|
2019-12-04 20:38:33 +05:30
|
|
|
|
|
|
|
it 'group member update hook' do
|
|
|
|
group.add_guest(user)
|
|
|
|
group.add_maintainer(user)
|
|
|
|
|
|
|
|
expect(WebMock).to have_requested(:post, system_hook.url).with(
|
|
|
|
body: /user_update_for_group/,
|
|
|
|
headers: { 'Content-Type' => 'application/json', 'X-Gitlab-Event' => 'System Hook' }
|
|
|
|
).once
|
|
|
|
end
|
2015-04-26 12:48:37 +05:30
|
|
|
end
|
2017-09-10 17:25:29 +05:30
|
|
|
|
|
|
|
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)
|
|
|
|
expect(described_class.repository_update_hooks).to eq([hook])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
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
|
2021-06-08 01:23:25 +05:30
|
|
|
|
|
|
|
describe '#rate_limit' do
|
|
|
|
let(:hook) { build(:system_hook) }
|
|
|
|
|
|
|
|
it 'returns nil' do
|
|
|
|
expect(hook.rate_limit).to be_nil
|
|
|
|
end
|
|
|
|
end
|
2021-09-04 01:27:46 +05:30
|
|
|
|
|
|
|
describe '#application_context' do
|
|
|
|
let(:hook) { build(:system_hook) }
|
|
|
|
|
|
|
|
it 'includes the type' do
|
|
|
|
expect(hook.application_context).to eq(
|
|
|
|
related_class: 'SystemHook'
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
2015-04-26 12:48:37 +05:30
|
|
|
end
|