gitlab-org--gitlab-foss/spec/models/hooks/system_hook_spec.rb

152 lines
4.5 KiB
Ruby
Raw Normal View History

2012-07-18 17:24:37 -04:00
require "spec_helper"
describe SystemHook do
context 'default attributes' do
let(:system_hook) { build(:system_hook) }
it 'sets defined default parameters' do
attrs = {
push_events: false,
2017-09-13 11:17:34 -04:00
repository_update_events: true,
merge_requests_events: false
}
expect(system_hook).to have_attributes(attrs)
end
end
2012-07-18 17:24:37 -04:00
describe "execute" do
2016-03-30 14:00:57 -04:00
let(:system_hook) { create(:system_hook) }
let(:user) { create(:user) }
let(:project) { create(:project, namespace: user.namespace) }
2016-03-30 14:00:57 -04:00
let(:group) { create(:group) }
let(:params) do
{ name: 'John Doe', username: 'jduser', email: 'jg@example.com', password: 'mydummypass' }
end
2016-03-30 14:00:57 -04:00
before do
WebMock.stub_request(:post, system_hook.url)
2012-07-18 17:24:37 -04:00
end
it "project_create hook" do
2016-03-30 14:00:57 -04:00
Projects::CreateService.new(user, name: 'empty').execute
expect(WebMock).to have_requested(:post, system_hook.url).with(
body: /project_create/,
headers: { 'Content-Type' => 'application/json', 'X-Gitlab-Event' => 'System Hook' }
).once
2012-07-18 17:24:37 -04:00
end
2012-07-18 17:24:37 -04:00
it "project_destroy hook" do
Projects::DestroyService.new(project, user, {}).async_execute
2016-03-30 14:00:57 -04:00
expect(WebMock).to have_requested(:post, system_hook.url).with(
body: /project_destroy/,
headers: { 'Content-Type' => 'application/json', 'X-Gitlab-Event' => 'System Hook' }
).once
2012-07-18 17:24:37 -04:00
end
it "user_create hook" do
Users::CreateService.new(nil, params).execute
2016-09-16 07:37:48 -04:00
2016-03-30 14:00:57 -04:00
expect(WebMock).to have_requested(:post, system_hook.url).with(
body: /user_create/,
headers: { 'Content-Type' => 'application/json', 'X-Gitlab-Event' => 'System Hook' }
).once
2012-07-18 17:24:37 -04:00
end
2012-07-18 17:24:37 -04:00
it "user_destroy hook" do
2013-01-09 01:14:05 -05:00
user.destroy
2016-03-30 14:00:57 -04:00
expect(WebMock).to have_requested(:post, system_hook.url).with(
body: /user_destroy/,
headers: { 'Content-Type' => 'application/json', 'X-Gitlab-Event' => 'System Hook' }
).once
2012-07-18 17:24:37 -04:00
end
2012-07-18 17:24:37 -04:00
it "project_create hook" do
project.add_master(user)
2016-03-30 14:00:57 -04:00
expect(WebMock).to have_requested(:post, system_hook.url).with(
body: /user_add_to_team/,
headers: { 'Content-Type' => 'application/json', 'X-Gitlab-Event' => 'System Hook' }
).once
2012-07-18 17:24:37 -04:00
end
2012-07-18 17:24:37 -04:00
it "project_destroy hook" do
project.add_master(user)
project.project_members.destroy_all
2016-03-30 14:00:57 -04:00
expect(WebMock).to have_requested(:post, system_hook.url).with(
body: /user_remove_from_team/,
headers: { 'Content-Type' => 'application/json', 'X-Gitlab-Event' => 'System Hook' }
).once
2012-07-18 17:24:37 -04:00
end
it 'group create hook' do
create(:group)
2016-03-30 14:00:57 -04:00
expect(WebMock).to have_requested(:post, system_hook.url).with(
body: /group_create/,
headers: { 'Content-Type' => 'application/json', 'X-Gitlab-Event' => 'System Hook' }
).once
end
it 'group destroy hook' do
group.destroy
2016-03-30 14:00:57 -04:00
expect(WebMock).to have_requested(:post, system_hook.url).with(
body: /group_destroy/,
headers: { 'Content-Type' => 'application/json', 'X-Gitlab-Event' => 'System Hook' }
).once
end
it 'group member create hook' do
group.add_master(user)
2016-03-30 14:00:57 -04:00
expect(WebMock).to have_requested(:post, system_hook.url).with(
body: /user_add_to_group/,
headers: { 'Content-Type' => 'application/json', 'X-Gitlab-Event' => 'System Hook' }
).once
end
it 'group member destroy hook' do
group.add_master(user)
group.group_members.destroy_all
2016-03-30 14:00:57 -04:00
expect(WebMock).to have_requested(:post, system_hook.url).with(
body: /user_remove_from_group/,
headers: { 'Content-Type' => 'application/json', 'X-Gitlab-Event' => 'System Hook' }
).once
end
2012-07-18 17:24:37 -04:00
end
2017-05-02 22:36:13 -04: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)
expect(described_class.repository_update_hooks).to eq([hook])
2017-05-02 22:36:13 -04:00
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
2012-07-18 17:24:37 -04:00
end