1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
|
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe WebHooks::CreateService, feature_category: :webhooks do
let_it_be(:current_user) { create(:user) }
describe '#execute' do
let_it_be(:project) { create(:project) }
let_it_be(:relation) { ProjectHook.none }
let(:hook_params) { { url: 'https://example.com/hook', project_id: project.id } }
subject(:webhook_created) { described_class.new(current_user) }
context 'when creating a new hook' do
it 'creates a new hook' do
expect do
response = webhook_created.execute(hook_params, relation)
expect(response).to be_success
expect(response[:async]).to eq(false)
end.to change { ProjectHook.count }.by(1)
end
end
context 'when the URL is invalid' do
it 'returns an error response' do
hook_params[:url] = 'invalid_url'
response = webhook_created.execute(hook_params, relation)
expect(response).not_to be_success
expect(response[:message]).to eq("Invalid url given")
expect(response[:http_status]).to eq(422)
end
end
context 'when the branch filter is invalid' do
let(:invalid_params) { hook_params.merge(push_events_branch_filter: 'bad branch name') }
it 'returns an error response' do
response = webhook_created.execute(invalid_params, relation)
expect(response).not_to be_success
expect(response[:message]).to eq("Invalid branch filter given")
expect(response[:http_status]).to eq(422)
end
end
context 'when the project is not provided' do
let(:invalid_params) { hook_params.merge(project_id: nil) }
it 'returns an error response for missing project' do
response = webhook_created.execute(invalid_params, relation)
expect(response).not_to be_success
expect(response[:message]).to eq("Project can't be blank")
expect(response[:http_status]).to eq(422)
end
end
end
end
|