File: starred_projects_query_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 (348 lines) | stat: -rw-r--r-- 11,146 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
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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe 'Getting starredProjects of the user', feature_category: :groups_and_projects do
  include GraphqlHelpers

  let(:query) do
    graphql_query_for(:user, user_params, user_fields)
  end

  let(:user_params) { { username: user.username } }

  let_it_be(:project_a) { create(:project, :public, name: 'ProjectA', path: 'Project-A', star_count: 30) }
  let_it_be(:project_b) { create(:project, :private, name: 'ProjectB', path: 'Project-B', star_count: 20) }
  let_it_be(:project_c) { create(:project, :private, name: 'ProjectC', path: 'Project-C', star_count: 10) }
  let_it_be(:user, reload: true) { create(:user) }

  let_it_be(:path) { %i[user starred_projects nodes] }

  let(:user_fields) { 'starredProjects { nodes { id } }' }

  let(:starred_projects) do
    post_graphql(query, current_user: current_user)

    graphql_data_at(*path)
  end

  before do
    project_b.add_reporter(user)
    project_c.add_reporter(user)

    user.toggle_star(project_a)
    user.toggle_star(project_b)
    user.toggle_star(project_c)
  end

  context 'anonymous access' do
    let(:current_user) { nil }

    it 'returns nothing' do
      expect(starred_projects).to be_nil
    end
  end

  context 'the current user is the user' do
    let(:current_user) { user }

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

    it 'found all projects' do
      expect(starred_projects).to contain_exactly(
        a_graphql_entity_for(project_a),
        a_graphql_entity_for(project_b),
        a_graphql_entity_for(project_c)
      )
    end
  end

  context 'the current user is a member of a private project the user starred' do
    let_it_be(:other_user) { create(:user) }

    let(:current_user) { other_user }

    before do
      project_b.add_reporter(other_user)
    end

    it 'finds public and member projects' do
      expect(starred_projects).to contain_exactly(
        a_graphql_entity_for(project_a),
        a_graphql_entity_for(project_b)
      )
    end
  end

  context 'the user has a private profile' do
    before do
      user.update!(private_profile: true)
    end

    context 'the current user does not have access to view the private profile of the user' do
      let(:current_user) { create(:user) }

      it 'finds no projects' do
        expect(starred_projects).to be_empty
      end
    end

    context 'the current user has access to view the private profile of the user' do
      let(:current_user) { create(:admin) }

      it 'finds all projects starred by the user, which the current user has access to' do
        expect(starred_projects).to contain_exactly(
          a_graphql_entity_for(project_a),
          a_graphql_entity_for(project_b),
          a_graphql_entity_for(project_c)
        )
      end
    end

    context 'when sort parameter is provided' do
      let(:user_fields_with_sort) { "starredProjects(sort: #{sort_parameter}) { nodes { id name } }" }
      let(:query_with_sort) { graphql_query_for(:user, user_params, user_fields_with_sort) }
      let(:current_user) { user }

      context 'when sort parameter provided is invalid' do
        let(:sort_parameter) { 'does_not_exist' }

        it 'raises an exception' do
          post_graphql(query_with_sort, current_user: current_user)

          expect(graphql_errors).to include(
            a_hash_including(
              'message' => "Argument 'sort' on Field 'starredProjects' has an invalid value (#{sort_parameter}). " \
                "Expected type 'ProjectSort'."
            )
          )
        end
      end

      context 'when sort parameter for id is provided' do
        context 'when ID_ASC is provided' do
          let(:sort_parameter) { 'ID_ASC' }

          it 'sorts starred projects by id in ascending order' do
            post_graphql(query_with_sort, current_user: current_user)

            expect(graphql_data_at(*path).pluck('id')).to eq([
              project_a.to_global_id.to_s,
              project_b.to_global_id.to_s,
              project_c.to_global_id.to_s
            ])
          end
        end

        context 'when ID_DESC is provided' do
          let(:sort_parameter) { 'ID_DESC' }

          it 'sorts starred projects by id in descending order' do
            post_graphql(query_with_sort, current_user: current_user)

            expect(graphql_data_at(*path).pluck('id')).to eq([
              project_c.to_global_id.to_s,
              project_b.to_global_id.to_s,
              project_a.to_global_id.to_s
            ])
          end
        end
      end

      context 'when sort parameter for latest activity is provided' do
        before do
          project_a.update!(last_activity_at: 2.hours.from_now)
          project_b.update!(last_activity_at: 3.hours.from_now)
          project_c.update!(last_activity_at: 4.hours.from_now)
        end

        context 'when LATEST_ACTIVITY_ASC is provided' do
          let(:sort_parameter) { 'LATEST_ACTIVITY_ASC' }

          it 'sorts starred projects by latest activity in ascending order' do
            post_graphql(query_with_sort, current_user: current_user)

            expect(graphql_data_at(*path).pluck('id')).to eq([
              project_a.to_global_id.to_s,
              project_b.to_global_id.to_s,
              project_c.to_global_id.to_s
            ])
          end
        end

        context 'when LATEST_ACTIVITY_DESC is provided' do
          let(:sort_parameter) { 'LATEST_ACTIVITY_DESC' }

          it 'sorts starred projects by latest activity in descending order' do
            post_graphql(query_with_sort, current_user: current_user)

            expect(graphql_data_at(*path).pluck('id')).to eq([
              project_c.to_global_id.to_s,
              project_b.to_global_id.to_s,
              project_a.to_global_id.to_s
            ])
          end
        end
      end

      context 'when sort parameter for name is provided' do
        context 'when NAME_ASC is provided' do
          let(:sort_parameter) { 'NAME_ASC' }

          it 'sorts starred projects by name in ascending order' do
            post_graphql(query_with_sort, current_user: current_user)

            expect(graphql_data_at(*path).pluck('id')).to eq([
              project_a.to_global_id.to_s,
              project_b.to_global_id.to_s,
              project_c.to_global_id.to_s
            ])
          end
        end

        context 'when NAME_DESC is provided' do
          let(:sort_parameter) { 'NAME_DESC' }

          it 'sorts starred projects by name in descending order' do
            post_graphql(query_with_sort, current_user: current_user)

            expect(graphql_data_at(*path).pluck('id')).to eq([
              project_c.to_global_id.to_s,
              project_b.to_global_id.to_s,
              project_a.to_global_id.to_s
            ])
          end
        end
      end

      context 'when sort parameter for path is provided' do
        context 'when PATH_ASC is provided' do
          let(:sort_parameter) { 'PATH_ASC' }

          it 'sorts starred projects by path in ascending order' do
            post_graphql(query_with_sort, current_user: current_user)

            expect(graphql_data_at(*path).pluck('id')).to eq([
              project_a.to_global_id.to_s,
              project_b.to_global_id.to_s,
              project_c.to_global_id.to_s
            ])
          end
        end

        context 'when PATH_DESC is provided' do
          let(:sort_parameter) { 'PATH_DESC' }

          it 'sorts starred projects by path in descending order' do
            post_graphql(query_with_sort, current_user: current_user)

            expect(graphql_data_at(*path).pluck('id')).to eq([
              project_c.to_global_id.to_s,
              project_b.to_global_id.to_s,
              project_a.to_global_id.to_s
            ])
          end
        end
      end

      context 'when sort parameter for stars is provided' do
        context 'when STARS_ASC is provided' do
          let(:sort_parameter) { 'STARS_ASC' }

          it 'sorts starred projects by stars in ascending order' do
            post_graphql(query_with_sort, current_user: current_user)

            expect(graphql_data_at(*path).pluck('id')).to eq([
              project_c.to_global_id.to_s,
              project_b.to_global_id.to_s,
              project_a.to_global_id.to_s
            ])
          end
        end

        context 'when STARS_DESC is provided' do
          let(:sort_parameter) { 'STARS_DESC' }

          it 'sorts starred projects by stars in descending order' do
            post_graphql(query_with_sort, current_user: current_user)

            expect(graphql_data_at(*path).pluck('id')).to eq([
              project_a.to_global_id.to_s,
              project_b.to_global_id.to_s,
              project_c.to_global_id.to_s
            ])
          end
        end
      end
    end
  end

  describe 'min_access_level' do
    let(:current_user) { user }

    let_it_be(:project_with_owner_access) { create(:project, :private) }

    let(:user_fields_with_min_access_level) do
      "starredProjects(minAccessLevel: #{min_access_level}) { nodes { id name } }"
    end

    let(:query_with_min_access_level) { graphql_query_for(:user, user_params, user_fields_with_min_access_level) }

    before_all do
      project_with_owner_access.add_owner(user)
      user.toggle_star(project_with_owner_access)
    end

    context 'when min_access_level is OWNER' do
      let(:min_access_level) { :OWNER }

      it 'returns only projects user has owner access to' do
        post_graphql(query_with_min_access_level, current_user: current_user)

        expect(graphql_data_at(*path))
          .to contain_exactly(a_graphql_entity_for(project_with_owner_access))
      end
    end

    context 'when min_access_level is REPORTER' do
      let(:min_access_level) { :REPORTER }

      it 'returns only projects user has reporter or higher access to' do
        post_graphql(query_with_min_access_level, current_user: current_user)

        expect(graphql_data_at(*path))
          .to contain_exactly(
            a_graphql_entity_for(project_with_owner_access),
            a_graphql_entity_for(project_b),
            a_graphql_entity_for(project_c)
          )
      end
    end
  end

  describe 'programming_language_name' do
    let(:current_user) { user }

    let_it_be(:ruby) { create(:programming_language, name: 'Ruby') }
    let_it_be(:repository_language) do
      create(:repository_language, project: project_b, programming_language: ruby, share: 1)
    end

    let(:query_with_programming_language_name) do
      graphql_query_for(:user, user_params, 'starredProjects(programmingLanguageName: "ruby") { nodes { id } }')
    end

    it 'returns only projects with ruby programming language' do
      post_graphql(query_with_programming_language_name, current_user: current_user)

      expect(graphql_data_at(*path))
        .to contain_exactly(
          a_graphql_entity_for(project_b)
        )
    end
  end
end