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

environments_spec.rb « project « graphql « api « requests « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: bb1763ee228ae9ef0e68c71183146961f89ef594 (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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe 'Project Environments query', feature_category: :continuous_delivery do
  include GraphqlHelpers

  let_it_be(:project) { create(:project, :private, :repository) }
  let_it_be_with_refind(:production) { create(:environment, :production, project: project) }
  let_it_be_with_refind(:staging) { create(:environment, :staging, project: project) }
  let_it_be(:developer) { create(:user).tap { |u| project.add_developer(u) } }

  subject { post_graphql(query, current_user: user) }

  let(:user) { developer }

  let(:query) do
    %(
      query {
        project(fullPath: "#{project.full_path}") {
          environment(name: "#{production.name}") {
            slug
            createdAt
            updatedAt
            autoStopAt
            autoDeleteAt
            tier
            environmentType
          }
        }
      }
    )
  end

  it 'returns the specified fields of the environment', :aggregate_failures do
    production.update!(auto_stop_at: 1.day.ago, auto_delete_at: 2.days.ago, environment_type: 'review')

    subject

    environment_data = graphql_data.dig('project', 'environment')
    expect(environment_data['slug']).to eq(production.slug)
    expect(environment_data['createdAt']).to eq(production.created_at.iso8601)
    expect(environment_data['updatedAt']).to eq(production.updated_at.iso8601)
    expect(environment_data['autoStopAt']).to eq(production.auto_stop_at.iso8601)
    expect(environment_data['autoDeleteAt']).to eq(production.auto_delete_at.iso8601)
    expect(environment_data['tier']).to eq(production.tier.upcase)
    expect(environment_data['environmentType']).to eq(production.environment_type)
  end

  describe 'user permissions' do
    let(:query) do
      %(
        query {
          project(fullPath: "#{project.full_path}") {
            environment(name: "#{production.name}") {
              userPermissions {
                updateEnvironment
                destroyEnvironment
                stopEnvironment
              }
            }
          }
        }
      )
    end

    it 'returns user permissions of the environment', :aggregate_failures do
      subject

      permission_data = graphql_data.dig('project', 'environment', 'userPermissions')
      expect(permission_data['updateEnvironment']).to eq(true)
      expect(permission_data['destroyEnvironment']).to eq(false)
      expect(permission_data['stopEnvironment']).to eq(true)
    end

    context 'when fetching user permissions for multiple environments' do
      let(:query) do
        %(
          query {
            project(fullPath: "#{project.full_path}") {
              environments {
                nodes {
                  userPermissions {
                    updateEnvironment
                    destroyEnvironment
                    stopEnvironment
                  }
                }
              }
            }
          }
        )
      end

      it 'limits the result', :aggregate_failures do
        subject

        expect_graphql_errors_to_include('"userPermissions" field can be requested only ' \
                                         'for 1 Environment(s) at a time.')
      end
    end
  end

  describe 'last deployments of environments' do
    ::Deployment.statuses.each do |status, _| # rubocop:disable RSpec/UselessDynamicDefinition
      let_it_be(:"production_#{status}_deployment") do
        create(:deployment, status.to_sym, environment: production, project: project)
      end

      let_it_be(:"staging_#{status}_deployment") do
        create(:deployment, status.to_sym, environment: staging, project: project)
      end
    end

    let(:query) do
      %(
        query {
          project(fullPath: "#{project.full_path}") {
            environments {
              nodes {
                name
                lastSuccessDeployment: lastDeployment(status: SUCCESS) {
                  iid
                }
                lastRunningDeployment: lastDeployment(status: RUNNING) {
                  iid
                }
                lastBlockedDeployment: lastDeployment(status: BLOCKED) {
                  iid
                }
              }
            }
          }
        }
      )
    end

    it 'returns all last deployments of the environment' do
      subject

      environments_data = graphql_data_at(:project, :environments, :nodes)

      environments_data.each do |environment_data|
        name = environment_data['name']
        success_deployment = public_send(:"#{name}_success_deployment")
        running_deployment = public_send(:"#{name}_running_deployment")
        blocked_deployment = public_send(:"#{name}_blocked_deployment")

        expect(environment_data['lastSuccessDeployment']['iid']).to eq(success_deployment.iid.to_s)
        expect(environment_data['lastRunningDeployment']['iid']).to eq(running_deployment.iid.to_s)
        expect(environment_data['lastBlockedDeployment']['iid']).to eq(blocked_deployment.iid.to_s)
      end
    end

    it 'executes the same number of queries in single environment and multiple environments' do
      single_environment_query =
        %(
          query {
            project(fullPath: "#{project.full_path}") {
              environment(name: "#{production.name}") {
                name
                lastSuccessDeployment: lastDeployment(status: SUCCESS) {
                  iid
                }
                lastRunningDeployment: lastDeployment(status: RUNNING) {
                  iid
                }
                lastBlockedDeployment: lastDeployment(status: BLOCKED) {
                  iid
                }
              }
            }
          }
        )

      baseline = ActiveRecord::QueryRecorder.new do
        run_with_clean_state(single_environment_query, context: { current_user: user })
      end

      multi = ActiveRecord::QueryRecorder.new do
        run_with_clean_state(query, context: { current_user: user })
      end

      expect(multi).not_to exceed_query_limit(baseline)
    end
  end

  describe 'nested environments' do
    let_it_be(:testing1) { create(:environment, name: 'testing/one', project: project) }
    let_it_be(:testing2) { create(:environment, name: 'testing/two', project: project) }

    context 'with query' do
      let(:query) do
        %(
        query {
          project(fullPath: "#{project.full_path}") {
            nestedEnvironments {
              nodes {
                name
                size
                environment {
                  name
                  path
                }
              }
            }
          }
        }
      )
      end

      it 'can fetch nested environments' do
        subject

        nested_envs = graphql_data.dig('project', 'nestedEnvironments', 'nodes')
        expect(nested_envs.count).to be(3)
        expect(nested_envs.pluck('name')).to match_array(%w[production staging testing])
        expect(nested_envs.pluck('size')).to match_array([1, 1, 2])
        expect(nested_envs[0].dig('environment', 'name')).to eq(production.name)
      end

      context 'when user is guest' do
        let(:user) { create(:user).tap { |u| project.add_guest(u) } }

        it 'returns nothing' do
          subject

          nested_envs = graphql_data.dig('project', 'nestedEnvironments', 'nodes')

          expect(nested_envs).to be_nil
        end
      end
    end

    context 'when using pagination' do
      let(:query) do
        %(
        query {
          project(fullPath: "#{project.full_path}") {
            nestedEnvironments(first: 1) {
              nodes {
                name
              }
              pageInfo {
                hasPreviousPage
                startCursor
                endCursor
                hasNextPage
              }
            }
          }
        }
      )
      end

      it 'supports pagination' do
        subject
        nested_envs = graphql_data.dig('project', 'nestedEnvironments')
        expect(nested_envs['nodes'].count).to eq(1)
        expect(nested_envs.dig('pageInfo', 'hasNextPage')).to be_truthy
      end
    end
  end
end