summaryrefslogtreecommitdiff
path: root/spec/requests/api/graphql/mutations/ci/pipeline_schedule_play_spec.rb
blob: e34066d0ee463ef573edb5cb92b66aaeadf5b84c (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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe 'PipelineSchedulePlay', feature_category: :continuious_integration do
  include GraphqlHelpers

  let_it_be(:user) { create(:user) }
  let_it_be(:project) { create(:project) }
  let_it_be(:pipeline_schedule) do
    create(
      :ci_pipeline_schedule,
      :every_minute,
      project: project,
      owner: user
    )
  end

  let(:mutation) do
    graphql_mutation(
      :pipeline_schedule_play,
      { id: pipeline_schedule.to_global_id.to_s },
      <<-QL
        pipelineSchedule { id, nextRunAt }
        errors
      QL
    )
  end

  let(:mutation_response) { graphql_mutation_response(:pipeline_schedule_play) }

  context 'when unauthorized' do
    it 'returns an error' do
      post_graphql_mutation(mutation, current_user: create(:user))

      expect(graphql_errors).not_to be_empty
      expect(graphql_errors[0]['message'])
        .to eq(
          "The resource that you are attempting to access does not exist " \
            "or you don't have permission to perform this action"
        )
    end
  end

  context 'when authorized', :sidekiq_inline do
    before do
      project.add_maintainer(user)
      pipeline_schedule.update_columns(next_run_at: 2.hours.ago)
    end

    context 'when mutation succeeds' do
      let(:service_response) { instance_double('ServiceResponse', payload: new_pipeline) }
      let(:new_pipeline) { instance_double('Ci::Pipeline', persisted?: true) }

      it do
        expect(Ci::CreatePipelineService).to receive_message_chain(:new, :execute).and_return(service_response)
        post_graphql_mutation(mutation, current_user: user)

        expect(mutation_response['pipelineSchedule']['id']).to include(pipeline_schedule.id.to_s)
        new_next_run_at = DateTime.parse(mutation_response['pipelineSchedule']['nextRunAt'])
        expect(new_next_run_at).not_to eq(pipeline_schedule.next_run_at)
        expect(new_next_run_at).to eq(pipeline_schedule.reset.next_run_at)
        expect(mutation_response['errors']).to eq([])
      end
    end

    context 'when mutation fails' do
      it do
        expect(RunPipelineScheduleWorker)
          .to receive(:perform_async)
          .with(pipeline_schedule.id, user.id, next_run_scheduled: true).and_return(nil)

        post_graphql_mutation(mutation, current_user: user)

        expect(mutation_response['pipelineSchedule']).to be_nil
        expect(mutation_response['errors']).to match_array(['Unable to schedule a pipeline to run immediately.'])
      end
    end

    context 'when feature flag ci_use_run_pipeline_schedule_worker is disabled' do
      before do
        stub_feature_flags(ci_use_run_pipeline_schedule_worker: false)
      end

      context 'when mutation succeeds' do
        let(:service_response) { instance_double('ServiceResponse', payload: new_pipeline) }
        let(:new_pipeline) { instance_double('Ci::Pipeline', persisted?: true) }

        it do
          expect(Ci::CreatePipelineService).to receive_message_chain(:new, :execute).and_return(service_response)
          post_graphql_mutation(mutation, current_user: user)

          expect(mutation_response['pipelineSchedule']['id']).to include(pipeline_schedule.id.to_s)
          new_next_run_at = DateTime.parse(mutation_response['pipelineSchedule']['nextRunAt'])
          expect(new_next_run_at).not_to eq(pipeline_schedule.next_run_at)
          expect(new_next_run_at).to eq(pipeline_schedule.reset.next_run_at)
          expect(mutation_response['errors']).to eq([])
        end
      end

      context 'when mutation fails' do
        it do
          expect(RunPipelineScheduleWorker)
            .to receive(:perform_async)
            .with(pipeline_schedule.id, user.id, next_run_scheduled: true).and_return(nil)

          post_graphql_mutation(mutation, current_user: user)

          expect(mutation_response['pipelineSchedule']).to be_nil
          expect(mutation_response['errors']).to match_array(['Unable to schedule a pipeline to run immediately.'])
        end
      end
    end
  end
end