2021-01-03 14:25:43 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
RSpec.describe WebHooks::DestroyService do
|
|
|
|
let_it_be(:user) { create(:user) }
|
|
|
|
|
|
|
|
subject { described_class.new(user) }
|
|
|
|
|
2022-07-23 23:45:48 +05:30
|
|
|
describe '#execute' do
|
2022-08-27 11:52:29 +05:30
|
|
|
# Testing with a project hook only - for permission tests, see policy specs.
|
|
|
|
let!(:hook) { create(:project_hook) }
|
|
|
|
let!(:log) { create_list(:web_hook_log, 3, web_hook: hook) }
|
|
|
|
|
|
|
|
context 'when the user does not have permission' do
|
|
|
|
it 'is an error' do
|
|
|
|
expect(subject.execute(hook))
|
|
|
|
.to be_error
|
|
|
|
.and have_attributes(message: described_class::DENIED)
|
|
|
|
end
|
|
|
|
end
|
2021-01-03 14:25:43 +05:30
|
|
|
|
2022-08-27 11:52:29 +05:30
|
|
|
context 'when the user does have permission' do
|
|
|
|
before do
|
|
|
|
hook.project.add_maintainer(user)
|
|
|
|
end
|
2021-01-03 14:25:43 +05:30
|
|
|
|
2022-08-27 11:52:29 +05:30
|
|
|
it 'is successful' do
|
|
|
|
expect(subject.execute(hook)).to be_success
|
|
|
|
end
|
2021-01-03 14:25:43 +05:30
|
|
|
|
2022-08-27 11:52:29 +05:30
|
|
|
it 'destroys the hook' do
|
|
|
|
expect { subject.execute(hook) }.to change(WebHook, :count).from(1).to(0)
|
|
|
|
end
|
2021-01-03 14:25:43 +05:30
|
|
|
|
2022-08-27 11:52:29 +05:30
|
|
|
it 'does not destroy logs' do
|
|
|
|
expect { subject.execute(hook) }.not_to change(WebHookLog, :count)
|
|
|
|
end
|
2021-01-03 14:25:43 +05:30
|
|
|
|
2022-08-27 11:52:29 +05:30
|
|
|
it 'schedules the destruction of logs' do
|
|
|
|
expect(WebHooks::LogDestroyWorker).to receive(:perform_async).with({ 'hook_id' => hook.id })
|
|
|
|
expect(Gitlab::AppLogger).to receive(:info).with(match(/scheduled a deletion of logs/))
|
2021-01-03 14:25:43 +05:30
|
|
|
|
2022-08-27 11:52:29 +05:30
|
|
|
subject.execute(hook)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the hook fails to destroy' do
|
|
|
|
before do
|
|
|
|
allow(hook).to receive(:destroy).and_return(false)
|
|
|
|
end
|
2021-01-03 14:25:43 +05:30
|
|
|
|
2022-08-27 11:52:29 +05:30
|
|
|
it 'is not a success' do
|
|
|
|
expect(WebHooks::LogDestroyWorker).not_to receive(:perform_async)
|
2021-01-03 14:25:43 +05:30
|
|
|
|
2022-08-27 11:52:29 +05:30
|
|
|
r = subject.execute(hook)
|
2021-01-03 14:25:43 +05:30
|
|
|
|
2022-08-27 11:52:29 +05:30
|
|
|
expect(r).to be_error
|
|
|
|
expect(r[:message]).to match %r{Unable to destroy}
|
2022-07-23 23:45:48 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2021-01-03 14:25:43 +05:30
|
|
|
end
|
|
|
|
end
|