Welcome to mirror list, hosted at ThFree Co, Russian Federation.

create_service_spec.rb « pipeline_schedules « ci « services « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: a01c71432c3820872f72cd33ba8cc50a706155d5 (plain)
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Ci::PipelineSchedules::CreateService, feature_category: :continuous_integration do
  let_it_be(:user) { create(:user) }
  let_it_be(:reporter) { create(:user) }
  let_it_be(:project) { create(:project, :public, :repository) }

  before_all do
    project.add_maintainer(user)
    project.add_reporter(reporter)
  end

  describe "execute" do
    context 'when user does not have permission' do
      subject(:service) { described_class.new(project, reporter, {}) }

      it 'returns ServiceResponse.error' do
        result = service.execute

        expect(result).to be_a(ServiceResponse)
        expect(result.error?).to be(true)

        error_message = _('The current user is not authorized to create the pipeline schedule')
        expect(result.message).to match_array([error_message])
        expect(result.payload.errors).to match_array([error_message])
      end
    end

    context 'when user has permission' do
      let(:params) do
        {
          description: 'desc',
          ref: 'patch-x',
          active: false,
          cron: '*/1 * * * *',
          cron_timezone: 'UTC'
        }
      end

      subject(:service) { described_class.new(project, user, params) }

      it 'saves values with passed params' do
        result = service.execute

        expect(result.payload).to have_attributes(
          description: 'desc',
          ref: 'patch-x',
          active: false,
          cron: '*/1 * * * *',
          cron_timezone: 'UTC'
        )
      end

      it 'returns ServiceResponse.success' do
        result = service.execute

        expect(result).to be_a(ServiceResponse)
        expect(result.success?).to be(true)
      end

      context 'when schedule save fails' do
        subject(:service) { described_class.new(project, user, {}) }

        before do
          errors = ActiveModel::Errors.new(project)
          errors.add(:base, 'An error occurred')

          allow_next_instance_of(Ci::PipelineSchedule) do |instance|
            allow(instance).to receive(:save).and_return(false)
            allow(instance).to receive(:errors).and_return(errors)
          end
        end

        it 'returns ServiceResponse.error' do
          result = service.execute

          expect(result).to be_a(ServiceResponse)
          expect(result.error?).to be(true)
          expect(result.message).to match_array(['An error occurred'])
        end
      end
    end
  end
end