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

update_spec.rb « pipeline_schedule « ci « mutations « graphql « api « requests « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: ec1595f393f9c5445c0c6edbe815d82ecd67566b (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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe 'PipelineScheduleUpdate', feature_category: :continuous_integration do
  include GraphqlHelpers

  let_it_be(:user) { create(:user) }
  let_it_be(:project) { create(:project, :public, :repository) }
  let_it_be(:pipeline_schedule) { create(:ci_pipeline_schedule, project: project, owner: user) }

  let_it_be(:variable_one) do
    create(:ci_pipeline_schedule_variable, key: 'foo', value: 'foovalue', pipeline_schedule: pipeline_schedule)
  end

  let_it_be(:variable_two) do
    create(:ci_pipeline_schedule_variable, key: 'bar', value: 'barvalue', pipeline_schedule: pipeline_schedule)
  end

  let(:mutation) do
    variables = {
      id: pipeline_schedule.to_global_id.to_s,
      **pipeline_schedule_parameters
    }

    graphql_mutation(
      :pipeline_schedule_update,
      variables,
      <<-QL
        pipelineSchedule {
          id
          description
          cron
          refForDisplay
          active
          cronTimezone
          variables {
            nodes {
              key
              value
              variableType
            }
          }
        }
        errors
      QL
    )
  end

  let(:pipeline_schedule_parameters) { {} }
  let(:mutation_response) { graphql_mutation_response(:pipeline_schedule_update) }

  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' do
    before_all do
      project.add_developer(user)
    end

    context 'when success' do
      let(:pipeline_schedule_parameters) do
        {
          description: 'updated_desc',
          cron: '0 1 * * *',
          cronTimezone: 'UTC',
          ref: 'patch-x',
          active: true,
          variables: [
            { key: 'AAA', value: "AAA123", variableType: 'ENV_VAR' }
          ]
        }
      end

      it do
        post_graphql_mutation(mutation, current_user: user)

        expect(response).to have_gitlab_http_status(:success)

        expect_graphql_errors_to_be_empty

        expect(mutation_response['pipelineSchedule']['id']).to eq(pipeline_schedule.to_global_id.to_s)

        %w[description cron cronTimezone active].each do |key|
          expect(mutation_response['pipelineSchedule'][key]).to eq(pipeline_schedule_parameters[key.to_sym])
        end

        expect(mutation_response['pipelineSchedule']['refForDisplay']).to eq(pipeline_schedule_parameters[:ref])

        expect(mutation_response['pipelineSchedule']['variables']['nodes'][2]['key']).to eq('AAA')
        expect(mutation_response['pipelineSchedule']['variables']['nodes'][2]['value']).to eq('AAA123')
      end
    end

    context 'when updating and removing variables' do
      let(:pipeline_schedule_parameters) do
        {
          variables: [
            { key: 'ABC', value: "ABC123", variableType: 'ENV_VAR', destroy: false },
            { id: variable_one.to_global_id.to_s,
              key: 'foo', value: "foovalue",
              variableType: 'ENV_VAR',
              destroy: true },
            { id: variable_two.to_global_id.to_s, key: 'newbar', value: "newbarvalue", variableType: 'ENV_VAR' }
          ]
        }
      end

      it 'processes variables correctly' do
        post_graphql_mutation(mutation, current_user: user)

        expect(response).to have_gitlab_http_status(:success)

        expect(mutation_response['pipelineSchedule']['variables']['nodes'])
          .to match_array(
            [
              { "key" => 'newbar', "value" => 'newbarvalue', "variableType" => 'ENV_VAR' },
              { "key" => 'ABC', "value" => "ABC123", "variableType" => 'ENV_VAR' }
            ]
          )
      end
    end

    context 'when failure' do
      context 'when params are invalid' do
        let(:pipeline_schedule_parameters) do
          {
            description: '',
            cron: 'abc',
            cronTimezone: 'cCc',
            ref: '',
            active: true,
            variables: []
          }
        end

        it do
          post_graphql_mutation(mutation, current_user: user)

          expect(response).to have_gitlab_http_status(:success)

          expect(mutation_response['errors'])
            .to match_array(
              [
                "Cron  is invalid syntax",
                "Cron timezone  is invalid syntax",
                "Ref can't be blank",
                "Description can't be blank"
              ]
            )
        end
      end

      context 'when params have duplicate variables' do
        let(:pipeline_schedule_parameters) do
          {
            variables: [
              { key: 'AAA', value: "AAA123", variableType: 'ENV_VAR' },
              { key: 'AAA', value: "AAA123", variableType: 'ENV_VAR' }
            ]
          }
        end

        it 'returns error' do
          post_graphql_mutation(mutation, current_user: user)

          expect(response).to have_gitlab_http_status(:success)

          expect(mutation_response['errors'])
            .to match_array(
              [
                "Variables have duplicate values (AAA)"
              ]
            )
        end
      end
    end
  end
end