summaryrefslogtreecommitdiff
path: root/spec/requests/api/graphql/project/issue/designs/designs_spec.rb
blob: 965534654ea947297b1e799e65ad0a9a34aaca02 (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe 'Getting designs related to an issue' do
  include GraphqlHelpers
  include DesignManagementTestHelpers

  let_it_be(:design) { create(:design, :with_smaller_image_versions, versions_count: 1) }
  let_it_be(:current_user) { design.project.first_owner }

  let(:design_query) do
    <<~NODE
    designs {
      edges {
        node {
          id
          filename
          fullPath
          event
          image
          imageV432x230
        }
      }
    }
    NODE
  end

  let(:issue) { design.issue }
  let(:project) { issue.project }
  let(:query) { make_query }
  let(:design_collection) do
    graphql_data_at(:project, :issue, :design_collection)
  end

  let(:design_response) do
    design_collection.dig('designs', 'edges').first['node']
  end

  def make_query(dq = design_query)
    designs_field = query_graphql_field(:design_collection, {}, dq)
    issue_field = query_graphql_field(:issue, { iid: issue.iid.to_s }, designs_field)

    graphql_query_for(:project, { fullPath: project.full_path }, issue_field)
  end

  def design_image_url(design, ref: nil, size: nil)
    Gitlab::UrlBuilder.build(design, ref: ref, size: size)
  end

  context 'when the feature is available' do
    before do
      enable_design_management
    end

    it 'returns the design properties correctly' do
      version_sha = design.versions.first.sha

      post_graphql(query, current_user: current_user)

      expect(design_response).to match a_graphql_entity_for(
        design,
        'event' => 'CREATION',
        'fullPath' => design.full_path,
        'filename' => design.filename,
        'image' => design_image_url(design, ref: version_sha),
        'imageV432x230' => design_image_url(design, ref: version_sha, size: :v432x230)
      )
    end

    context 'when the v432x230-sized design image has not been processed' do
      before do
        allow_next_instance_of(DesignManagement::DesignV432x230Uploader) do |uploader|
          allow(uploader).to receive(:file).and_return(nil)
        end
      end

      it 'returns nil for the v432x230-sized design image' do
        post_graphql(query, current_user: current_user)

        expect(design_response['imageV432x230']).to be_nil
      end
    end

    describe 'pagination' do
      before do
        create_list(:design, 5, :with_file, issue: issue)
        project.add_developer(current_user)
        post_graphql(query, current_user: current_user)
      end

      let(:issue) { create(:issue) }

      let(:end_cursor) { design_collection.dig('designs', 'pageInfo', 'endCursor') }

      let(:expected_designs) { issue.designs.order(:id).map { |d| a_graphql_entity_for(d) } }

      let(:query) { make_query(designs_fragment(first: 2)) }

      let(:design_query_fields) { 'pageInfo { endCursor } edges { node { id } }' }

      let(:cursored_query) do
        make_query(designs_fragment(after: end_cursor))
      end

      def designs_fragment(params)
        query_graphql_field(:designs, params, design_query_fields)
      end

      def response_designs(data = graphql_data)
        path = %w[project issue designCollection designs edges]
        data.dig(*path).map { |e| e['node'] }
      end

      it 'sorts designs for reliable pagination' do
        expect(response_designs).to match_array(expected_designs.take(2))

        post_graphql(cursored_query, current_user: current_user)

        new_data = Gitlab::Json.parse(response.body).fetch('data')

        expect(response_designs(new_data)).to match_array(expected_designs.drop(2))
      end
    end

    context 'with versions' do
      let_it_be(:version) { design.versions.take }

      let(:design_query) do
        <<~NODE
        designs {
          edges {
            node {
              filename
              versions {
                edges {
                  node {
                    id
                    sha
                  }
                }
              }
            }
          }
        }
        NODE
      end

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

        version_id = design_response['versions']['edges'].first['node']['id']

        expect(version_id).to eq(version.to_global_id.to_s)
      end

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

        version_sha = design_response['versions']['edges'].first['node']['sha']

        expect(version_sha).to eq(version.sha)
      end
    end

    describe 'viewing a design board at a particular version' do
      let_it_be(:issue) { design.issue }
      let_it_be(:second_design, reload: true) { create(:design, :with_smaller_image_versions, issue: issue, versions_count: 1) }
      let_it_be(:deleted_design) { create(:design, :with_versions, issue: issue, deleted: true, versions_count: 1) }

      let(:all_versions) { issue.design_versions.ordered.reverse }
      let(:design_query) do
        <<~NODE
        designs(atVersion: "#{version.to_global_id}") {
          edges {
            node {
              id
              image
              imageV432x230
              event
              versions {
                edges {
                  node {
                    id
                  }
                }
              }
            }
          }
        }
        NODE
      end

      let(:design_response) do
        design_collection['designs']['edges']
      end

      def global_id(object)
        object.to_global_id.to_s
      end

      # Filters just design nodes from the larger `design_response`
      def design_nodes
        design_response.map do |response|
          response['node']
        end
      end

      # Filters just version nodes from the larger `design_response`
      def version_nodes
        design_response.map do |response|
          response.dig('node', 'versions', 'edges')
        end
      end

      context 'viewing the original version, when one design was created' do
        let(:version) { all_versions.first }

        before do
          post_graphql(query, current_user: current_user)
        end

        it 'only returns the first design' do
          expect(design_nodes).to contain_exactly(
            a_hash_including('id' => global_id(design))
          )
        end

        it 'returns the correct full-sized design image' do
          expect(design_nodes).to contain_exactly(
            a_hash_including('image' => design_image_url(design, ref: version.sha))
          )
        end

        it 'returns the correct v432x230-sized design image' do
          expect(design_nodes).to contain_exactly(
            a_hash_including('imageV432x230' => design_image_url(design, ref: version.sha, size: :v432x230))
          )
        end

        it 'returns the correct event for the design in this version' do
          expect(design_nodes).to contain_exactly(
            a_hash_including('event' => 'CREATION')
          )
        end

        it 'only returns one version record for the design (the original version)' do
          expect(version_nodes).to eq(
            [
              [{ 'node' => { 'id' => global_id(version) } }]
            ])
        end
      end

      context 'viewing the second version, when one design was created' do
        let(:version) { all_versions.second }

        before do
          post_graphql(query, current_user: current_user)
        end

        it 'only returns the first two designs' do
          expect(design_nodes).to contain_exactly(
            a_hash_including('id' => global_id(design)),
            a_hash_including('id' => global_id(second_design))
          )
        end

        it 'returns the correct full-sized design images' do
          expect(design_nodes).to contain_exactly(
            a_hash_including('image' => design_image_url(design, ref: version.sha)),
            a_hash_including('image' => design_image_url(second_design, ref: version.sha))
          )
        end

        it 'returns the correct v432x230-sized design images' do
          v0 = design.actions.most_recent.first.version

          expect(design_nodes).to contain_exactly(
            a_hash_including('imageV432x230' => design_image_url(design, ref: v0.sha, size: :v432x230)),
            a_hash_including('imageV432x230' => design_image_url(second_design, ref: version.sha, size: :v432x230))
          )
        end

        it 'returns the correct events for the designs in this version' do
          expect(design_nodes).to contain_exactly(
            a_hash_including('event' => 'NONE'),
            a_hash_including('event' => 'CREATION')
          )
        end

        it 'returns the correct versions records for both designs' do
          expect(version_nodes).to eq(
            [
              [{ 'node' => { 'id' => global_id(design.versions.first) } }],
              [{ 'node' => { 'id' => global_id(second_design.versions.first) } }]
            ])
        end
      end

      context 'viewing the last version, when one design was deleted and one was updated' do
        let(:version) { all_versions.last }
        let!(:second_design_update) do
          create(:design_action, :with_image_v432x230, design: second_design, version: version, event: 'modification')
        end

        before do
          post_graphql(query, current_user: current_user)
        end

        it 'does not include the deleted design' do
          # The design does exist in the version
          expect(version.designs).to include(deleted_design)

          # But the GraphQL API does not include it in these results
          expect(design_nodes).to contain_exactly(
            a_hash_including('id' => global_id(design)),
            a_hash_including('id' => global_id(second_design))
          )
        end

        it 'returns the correct full-sized design images' do
          expect(design_nodes).to contain_exactly(
            a_hash_including('image' => design_image_url(design, ref: version.sha)),
            a_hash_including('image' => design_image_url(second_design, ref: version.sha))
          )
        end

        it 'returns the correct v432x230-sized design images' do
          v0 = design.actions.most_recent.first.version

          expect(design_nodes).to contain_exactly(
            a_hash_including('imageV432x230' => design_image_url(design, ref: v0.sha, size: :v432x230)),
            a_hash_including('imageV432x230' => design_image_url(second_design, ref: version.sha, size: :v432x230))
          )
        end

        it 'returns the correct events for the designs in this version' do
          expect(design_nodes).to contain_exactly(
            a_hash_including('event' => 'NONE'),
            a_hash_including('event' => 'MODIFICATION')
          )
        end

        it 'returns all versions records for the designs' do
          expect(version_nodes).to eq(
            [
              [
                { 'node' => { 'id' => global_id(design.versions.first) } }
              ],
              [
                { 'node' => { 'id' => global_id(second_design.versions.second) } },
                { 'node' => { 'id' => global_id(second_design.versions.first) } }
              ]
            ])
        end
      end
    end

    describe 'a design with note annotations' do
      let_it_be(:note) { create(:diff_note_on_design, noteable: design) }

      let(:design_query) do
        <<~NODE
        designs {
          edges {
            node {
              notesCount
              notes {
                edges {
                  node {
                    id
                  }
                }
              }
            }
          }
        }
        NODE
      end

      let(:design_response) do
        design_collection['designs']['edges'].first['node']
      end

      before do
        post_graphql(query, current_user: current_user)
      end

      it 'returns the notes for the design' do
        expect(design_response.dig('notes', 'edges')).to eq(
          ['node' => { 'id' => note.to_global_id.to_s }]
        )
      end

      it 'returns a note_count for the design' do
        expect(design_response['notesCount']).to eq(1)
      end
    end
  end
end