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

project_query_spec.rb « graphql « api « requests « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 2d9c6367676c2ce629445bc514b003e55c2d3b00 (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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe 'getting project information', feature_category: :groups_and_projects do
  include GraphqlHelpers

  let_it_be(:group) { create(:group) }
  let_it_be(:project, reload: true) { create(:project, :repository, group: group) }
  let_it_be(:current_user) { create(:user) }
  let_it_be(:other_user) { create(:user) }

  let(:project_fields) { all_graphql_fields_for('project'.to_s.classify, max_depth: 1) }

  let(:query) do
    graphql_query_for(:project, { full_path: project.full_path }, project_fields)
  end

  context 'when the user has full access to the project' do
    before do
      project.add_maintainer(current_user)
    end

    it 'includes the project', :use_clean_rails_memory_store_caching, :request_store do
      post_graphql(query, current_user: current_user)

      expect(graphql_data['project']).to include('id' => global_id_of(project).to_s)
    end

    context 'when querying for pipeline triggers' do
      let(:project_fields) { query_nodes(:pipeline_triggers) }
      let(:pipeline_trigger) { project.triggers.first }

      before do
        create(:ci_trigger, project: project, owner: current_user)
      end

      it 'fetches the pipeline trigger tokens' do
        post_graphql(query, current_user: current_user)

        expect(graphql_data_at(:project, :pipeline_triggers, :nodes).first).to match({
          'id' => pipeline_trigger.to_global_id.to_s,
          'canAccessProject' => true,
          'description' => pipeline_trigger.description,
          'hasTokenExposed' => true,
          'lastUsed' => nil,
          'token' => pipeline_trigger.token
        })
      end

      it 'does not produce N+1 queries' do
        baseline = ActiveRecord::QueryRecorder.new { post_graphql(query, current_user: current_user) }

        build_list(:ci_trigger, 2, owner: current_user, project: project)

        expect { post_graphql(query, current_user: current_user) }.not_to exceed_query_limit(baseline)
      end

      context 'when another project member or owner who is not also the token owner' do
        before do
          project.add_owner(other_user)
          post_graphql(query, current_user: other_user)
        end

        it 'is not authorized and shows truncated token' do
          expect(graphql_data_at(:project, :pipeline_triggers, :nodes).first).to match({
            'id' => pipeline_trigger.to_global_id.to_s,
            'canAccessProject' => true,
            'description' => pipeline_trigger.description,
            'hasTokenExposed' => false,
            'lastUsed' => nil,
            'token' => pipeline_trigger.token[0, 4]
          })
        end
      end

      context 'when user is not a member of a public project' do
        before do
          project.update!(visibility_level: Gitlab::VisibilityLevel::PUBLIC)
          post_graphql(query, current_user: other_user)
        end

        it 'cannot read the token' do
          expect(graphql_data_at(:project, :pipeline_triggers, :nodes)).to eql([])
        end
      end
    end
  end

  context 'when the user has access to the project', :use_clean_rails_memory_store_caching, :request_store do
    before_all do
      project.add_developer(current_user)
    end

    it 'includes the project' do
      post_graphql(query, current_user: current_user)

      expect(graphql_data['project']).to include('id' => global_id_of(project).to_s)
    end

    it_behaves_like 'a working graphql query that returns data' do
      before do
        post_graphql(query, current_user: current_user)
      end
    end

    context 'when there are pipelines present' do
      let(:project_fields) { query_nodes(:pipelines) }

      before do
        create(:ci_pipeline, project: project)
      end

      it 'is included in the pipelines connection' do
        post_graphql(query, current_user: current_user)

        expect(graphql_data_at(:project, :pipelines, :nodes)).to contain_exactly(a_kind_of(Hash))
      end
    end

    context 'topics' do
      it 'includes empty topics array if no topics set' do
        post_graphql(query, current_user: current_user)

        expect(graphql_data_at(:project, :topics)).to match([])
      end

      it 'includes topics array' do
        project.update!(topic_list: 'topic1, topic2, topic3')

        post_graphql(query, current_user: current_user)

        expect(graphql_data_at(:project, :topics)).to match(%w[topic1 topic2 topic3])
      end
    end

    it 'includes inherited members in project_members' do
      group_member = create(:group_member, group: group)
      project_member = create(:project_member, project: project)
      member_query = <<~GQL
        query {
          project(fullPath: "#{project.full_path}") {
            projectMembers {
              nodes {
                id
                user {
                  username
                }
                ... on ProjectMember {
                  project {
                    id
                  }
                }
                ... on GroupMember {
                  group {
                    id
                  }
                }
              }
            }
          }
        }
      GQL

      post_graphql(member_query, current_user: current_user)

      member_ids = graphql_data.dig('project', 'projectMembers', 'nodes')
      expect(member_ids).to include(
        a_hash_including(
          'id' => group_member.to_global_id.to_s,
          'group' => { 'id' => group.to_global_id.to_s }
        )
      )
      expect(member_ids).to include(
        a_hash_including(
          'id' => project_member.to_global_id.to_s,
          'project' => { 'id' => project.to_global_id.to_s }
        )
      )
    end
  end

  describe 'is_catalog_resource' do
    before do
      project.add_owner(current_user)
    end

    let(:catalog_resource_query) do
      <<~GRAPHQL
        {
          project(fullPath: "#{project.full_path}") {
            isCatalogResource
          }
        }
      GRAPHQL
    end

    context 'when the project is not a catalog resource' do
      it 'is false' do
        post_graphql(catalog_resource_query, current_user: current_user)

        expect(graphql_data.dig('project', 'isCatalogResource')).to be(false)
      end
    end

    context 'when the project is a catalog resource' do
      before do
        create(:ci_catalog_resource, project: project)
      end

      it 'is true' do
        post_graphql(catalog_resource_query, current_user: current_user)

        expect(graphql_data.dig('project', 'isCatalogResource')).to be(true)
      end
    end

    context 'for N+1 queries with isCatalogResource' do
      let_it_be(:project1) { create(:project, group: group) }
      let_it_be(:project2) { create(:project, group: group) }

      it 'avoids N+1 database queries' do
        pending('See: https://gitlab.com/gitlab-org/gitlab/-/issues/403634')
        ctx = { current_user: current_user }

        baseline_query = graphql_query_for(:project, { full_path: project1.full_path }, 'isCatalogResource')

        query = <<~GQL
          query {
            a: #{query_graphql_field(:project, { full_path: project1.full_path }, 'isCatalogResource')}
            b: #{query_graphql_field(:project, { full_path: project2.full_path }, 'isCatalogResource')}
          }
        GQL

        control = ActiveRecord::QueryRecorder.new do
          run_with_clean_state(baseline_query, context: ctx)
        end

        expect { run_with_clean_state(query, context: ctx) }.not_to exceed_query_limit(control)
      end
    end
  end

  context 'when the user has reporter access to the project' do
    let(:statistics_query) do
      <<~GRAPHQL
        {
          project(fullPath: "#{project.full_path}") {
            statistics { wikiSize }
          }
        }
      GRAPHQL
    end

    before do
      project.add_reporter(current_user)
      create(:project_statistics, project: project, wiki_size: 100)
    end

    it 'allows fetching project statistics' do
      post_graphql(statistics_query, current_user: current_user)

      expect(graphql_data.dig('project', 'statistics')).to include('wikiSize' => 100.0)
    end
  end

  context 'when the user has guest access' do
    context 'when the project has public pipelines' do
      before do
        pipeline = create(:ci_pipeline, project: project)
        create(:ci_build, project: project, pipeline: pipeline, name: 'a test job')
        project.add_guest(current_user)
      end

      it 'shows all jobs' do
        query = <<~GQL
          query {
            project(fullPath: "#{project.full_path}") {
              jobs {
                nodes {
                  name
                  stage {
                    name
                  }
                }
              }
            }
          }
        GQL

        post_graphql(query, current_user: current_user)

        expect(graphql_data_at(:project, :jobs, :nodes)).to contain_exactly({
          'name' => 'a test job',
          'stage' => { 'name' => 'test' }
        })
      end
    end
  end

  context 'when the user does not have access to the project' do
    it_behaves_like 'a working graphql query that returns no data' do
      before do
        post_graphql(query, current_user: current_user)
      end
    end
  end

  context 'with timelog categories' do
    let_it_be(:timelog_category) do
      create(:timelog_category, namespace: project.project_namespace, name: 'TimelogCategoryTest')
    end

    let(:project_fields) do
      <<~GQL
        timelogCategories {
          nodes {
            #{all_graphql_fields_for('TimeTrackingTimelogCategory')}
          }
        }
      GQL
    end

    context 'when user is guest and the project is public' do
      before do
        project.update!(visibility_level: Gitlab::VisibilityLevel::PUBLIC)
      end

      it 'includes empty timelog categories array' do
        post_graphql(query, current_user: current_user)

        expect(graphql_data_at(:project, :timelogCategories, :nodes)).to match([])
      end
    end

    context 'when user has reporter role' do
      before do
        project.add_reporter(current_user)
      end

      it 'returns the timelog category with all its fields' do
        post_graphql(query, current_user: current_user)

        expect(graphql_data_at(:project, :timelogCategories, :nodes))
          .to contain_exactly(a_graphql_entity_for(timelog_category))
      end

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

        it 'returns no timelog categories' do
          post_graphql(query, current_user: current_user)

          expect(graphql_data_at(:project, :timelogCategories)).to be_nil
        end
      end
    end

    context 'for N+1 queries' do
      let!(:project1) { create(:project) }
      let!(:project2) { create(:project) }

      before do
        project1.add_reporter(current_user)
        project2.add_reporter(current_user)
      end

      it 'avoids N+1 database queries' do
        pending('See: https://gitlab.com/gitlab-org/gitlab/-/issues/369396')

        ctx = { current_user: current_user }

        baseline_query = <<~GQL
          query {
            a: project(fullPath: "#{project1.full_path}") { ... p }
          }

          fragment p on Project {
            timelogCategories { nodes { name } }
          }
        GQL

        query = <<~GQL
          query {
            a: project(fullPath: "#{project1.full_path}") { ... p }
            b: project(fullPath: "#{project2.full_path}") { ... p }
          }

          fragment p on Project {
            timelogCategories { nodes { name } }
          }
        GQL

        control = ActiveRecord::QueryRecorder.new do
          run_with_clean_state(baseline_query, context: ctx)
        end

        expect { run_with_clean_state(query, context: ctx) }.not_to exceed_query_limit(control)
      end
    end
  end
end