File: resources_spec.rb

package info (click to toggle)
gitlab 17.6.5-19
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 629,368 kB
  • sloc: ruby: 1,915,304; javascript: 557,307; sql: 60,639; xml: 6,509; sh: 4,567; makefile: 1,239; python: 406
file content (198 lines) | stat: -rw-r--r-- 5,250 bytes parent folder | download
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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe 'Query.ciCatalogResources', feature_category: :pipeline_composition do
  include GraphqlHelpers

  let_it_be(:user) { create(:user) }
  let_it_be(:namespace) { create(:group, developers: user) }
  let_it_be(:project2) { create(:project, namespace: namespace) }

  let_it_be(:private_project) do
    create(
      :project, :with_avatar, :custom_repo,
      name: 'Component Repository',
      description: 'A simple component',
      namespace: namespace,
      star_count: 1,
      files: { 'README.md' => '**Test**' }
    )
  end

  let_it_be(:public_project) do
    create(
      :project, :with_avatar, :custom_repo, :public,
      name: 'Public Component',
      description: 'A public component',
      files: { 'README.md' => '**Test**' }
    )
  end

  let_it_be(:private_resource) do
    create(:ci_catalog_resource, :published, project: private_project, latest_released_at: '2023-01-01T00:00:00Z',
      last_30_day_usage_count: 15)
  end

  let_it_be(:public_resource) { create(:ci_catalog_resource, :published, project: public_project) }

  let(:query) do
    <<~GQL
      query {
        ciCatalogResources {
          nodes {
            id
            name
            description
            icon
            fullPath
            webPath
            verificationLevel
            visibilityLevel
            latestReleasedAt
            starCount
            starrersPath
            last30DayUsageCount
          }
        }
      }
    GQL
  end

  subject(:post_query) { post_graphql(query, current_user: user) }

  shared_examples 'avoids N+1 queries' do
    it do
      ctx = { current_user: user }

      control_count = ActiveRecord::QueryRecorder.new(skip_cached: false) do
        run_with_clean_state(query, context: ctx)
      end

      create(:ci_catalog_resource, :published, project: project2)

      expect do
        run_with_clean_state(query, context: ctx)
      end.not_to exceed_query_limit(control_count)
    end
  end

  it_behaves_like 'avoids N+1 queries'

  it 'returns the resources with the expected data' do
    post_query

    expect(graphql_data_at(:ciCatalogResources, :nodes)).to contain_exactly(
      a_graphql_entity_for(
        private_resource, :name, :description,
        icon: private_project.avatar_path,
        latestReleasedAt: private_resource.latest_released_at,
        starCount: private_project.star_count,
        starrersPath: Gitlab::Routing.url_helpers.project_starrers_path(private_project),
        verificationLevel: 'UNVERIFIED',
        visibilityLevel: 'private',
        fullPath: private_project.full_path,
        webPath: "/#{private_project.full_path}",
        last30DayUsageCount: private_resource.last_30_day_usage_count
      ),
      a_graphql_entity_for(public_resource, visibilityLevel: 'public')
    )
  end

  describe 'with an unauthorized user on a private project' do
    let_it_be(:query) do
      <<~GQL
        query {
          ciCatalogResources {
            nodes {
              id
              versions {
                nodes {
                  id
                  name
                }
              }
            }
          }
        }
      GQL
    end

    it 'returns only the public data' do
      post_graphql(query, current_user: create(:user))

      expect(graphql_data_at(:ciCatalogResources, :nodes)).to contain_exactly(
        a_graphql_entity_for(public_resource)
      )
    end
  end

  describe 'versions' do
    let!(:private_resource_v1) do
      create(:ci_catalog_resource_version, semver: '1.0.0', catalog_resource: private_resource)
    end

    let!(:private_resource_v2) do
      create(:ci_catalog_resource_version, semver: '2.0.0', catalog_resource: private_resource)
    end

    let!(:public_resource_v1) do
      create(:ci_catalog_resource_version, semver: '1.0.0', catalog_resource: public_resource)
    end

    let!(:public_resource_v2) do
      create(:ci_catalog_resource_version, semver: '2.0.0', catalog_resource: public_resource)
    end

    let(:query) do
      <<~GQL
        query {
          ciCatalogResources {
            nodes {
              id
              versions {
                nodes {
                  id
                  name
                  releasedAt
                  author {
                    id
                    name
                    webUrl
                  }
                }
              }
            }
          }
        }
      GQL
    end

    it 'returns versions for the catalog resources ordered by semver' do
      post_query

      expect(graphql_data_at(:ciCatalogResources, :nodes)).to contain_exactly(
        a_graphql_entity_for(
          private_resource,
          versions: {
            'nodes' => [
              a_graphql_entity_for(private_resource_v2),
              a_graphql_entity_for(private_resource_v1)
            ]
          }
        ),
        a_graphql_entity_for(
          public_resource,
          versions: {
            'nodes' => [
              a_graphql_entity_for(public_resource_v2),
              a_graphql_entity_for(public_resource_v1)
            ]
          }
        )
      )
    end

    it_behaves_like 'avoids N+1 queries'
  end
end