File: create_service_spec.rb

package info (click to toggle)
gitlab 17.6.5-19
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 629,368 kB
  • sloc: ruby: 1,915,304; javascript: 557,307; sql: 60,639; xml: 6,509; sh: 4,567; makefile: 1,239; python: 406
file content (62 lines) | stat: -rw-r--r-- 2,004 bytes parent folder | download
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