summaryrefslogtreecommitdiff
path: root/spec/models/design_management/design_spec.rb
blob: bc1f54f057e310280c39ae51d4ef21619cd84fc4 (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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
# frozen_string_literal: true

require 'spec_helper'

describe DesignManagement::Design do
  include DesignManagementTestHelpers

  let_it_be(:issue) { create(:issue) }
  let_it_be(:design1) { create(:design, :with_versions, issue: issue, versions_count: 1) }
  let_it_be(:design2) { create(:design, :with_versions, issue: issue, versions_count: 1) }
  let_it_be(:design3) { create(:design, :with_versions, issue: issue, versions_count: 1) }
  let_it_be(:deleted_design) { create(:design, :with_versions, deleted: true) }

  describe 'relations' do
    it { is_expected.to belong_to(:project) }
    it { is_expected.to belong_to(:issue) }
    it { is_expected.to have_many(:actions) }
    it { is_expected.to have_many(:versions) }
    it { is_expected.to have_many(:notes).dependent(:delete_all) }
    it { is_expected.to have_many(:user_mentions) }
  end

  describe 'validations' do
    subject(:design) { build(:design) }

    it { is_expected.to be_valid }
    it { is_expected.to validate_presence_of(:project) }
    it { is_expected.to validate_presence_of(:issue) }
    it { is_expected.to validate_presence_of(:filename) }
    it { is_expected.to validate_length_of(:filename).is_at_most(255) }
    it { is_expected.to validate_uniqueness_of(:filename).scoped_to(:issue_id) }

    it "validates that the extension is an image" do
      design.filename = "thing.txt"
      extensions = described_class::SAFE_IMAGE_EXT + described_class::DANGEROUS_IMAGE_EXT

      expect(design).not_to be_valid
      expect(design.errors[:filename].first).to eq(
        "does not have a supported extension. Only #{extensions.to_sentence} are supported"
      )
    end

    describe 'validating files with .svg extension' do
      before do
        design.filename = "thing.svg"
      end

      it "allows .svg files when feature flag is enabled" do
        stub_feature_flags(design_management_allow_dangerous_images: true)

        expect(design).to be_valid
      end

      it "does not allow .svg files when feature flag is disabled" do
        stub_feature_flags(design_management_allow_dangerous_images: false)

        expect(design).not_to be_valid
        expect(design.errors[:filename].first).to eq(
          "does not have a supported extension. Only #{described_class::SAFE_IMAGE_EXT.to_sentence} are supported"
        )
      end
    end
  end

  describe 'scopes' do
    describe '.visible_at_version' do
      let(:versions) { DesignManagement::Version.where(issue: issue).ordered }
      let(:found) { described_class.visible_at_version(version) }

      context 'at oldest version' do
        let(:version) { versions.last }

        it 'finds the first design only' do
          expect(found).to contain_exactly(design1)
        end
      end

      context 'at version 2' do
        let(:version) { versions.second }

        it 'finds the first and second designs' do
          expect(found).to contain_exactly(design1, design2)
        end
      end

      context 'at latest version' do
        let(:version) { versions.first }

        it 'finds designs' do
          expect(found).to contain_exactly(design1, design2, design3)
        end
      end

      context 'when the argument is nil' do
        let(:version) { nil }

        it 'finds all undeleted designs' do
          expect(found).to contain_exactly(design1, design2, design3)
        end
      end

      describe 'one of the designs was deleted before the given version' do
        before do
          delete_designs(design2)
        end

        it 'is not returned' do
          current_version = versions.first

          expect(described_class.visible_at_version(current_version)).to contain_exactly(design1, design3)
        end
      end

      context 'a re-created history' do
        before do
          delete_designs(design1, design2)
          restore_designs(design1)
        end

        it 'is returned, though other deleted events are not' do
          expect(described_class.visible_at_version(nil)).to contain_exactly(design1, design3)
        end
      end

      # test that a design that has been modified at various points
      # can be queried for correctly at different points in its history
      describe 'dead or alive' do
        let(:versions) { DesignManagement::Version.where(issue: issue).map { |v| [v, :alive] } }

        before do
          versions << [delete_designs(design1),          :dead]
          versions << [modify_designs(design2),          :dead]
          versions << [restore_designs(design1),         :alive]
          versions << [modify_designs(design3),          :alive]
          versions << [delete_designs(design1),          :dead]
          versions << [modify_designs(design2, design3), :dead]
          versions << [restore_designs(design1),         :alive]
        end

        it 'can establish the history at any point' do
          history = versions.map(&:first).map do |v|
            described_class.visible_at_version(v).include?(design1) ? :alive : :dead
          end

          expect(history).to eq(versions.map(&:second))
        end
      end
    end

    describe '.with_filename' do
      it 'returns correct design when passed a single filename' do
        expect(described_class.with_filename(design1.filename)).to eq([design1])
      end

      it 'returns correct designs when passed an Array of filenames' do
        expect(
          described_class.with_filename([design1, design2].map(&:filename))
        ).to contain_exactly(design1, design2)
      end
    end

    describe '.on_issue' do
      it 'returns correct designs when passed a single issue' do
        expect(described_class.on_issue(issue)).to match_array(issue.designs)
      end

      it 'returns correct designs when passed an Array of issues' do
        expect(
          described_class.on_issue([issue, deleted_design.issue])
        ).to contain_exactly(design1, design2, design3, deleted_design)
      end
    end

    describe '.current' do
      it 'returns just the undeleted designs' do
        delete_designs(design3)

        expect(described_class.current).to contain_exactly(design1, design2)
      end
    end
  end

  describe '#visible_in?' do
    let_it_be(:issue) { create(:issue) }

    # It is expensive to re-create complex histories, so we do it once, and then
    # assert that we can establish visibility at any given version.
    it 'tells us when a design is visible' do
      expected = []

      first_design = create(:design, :with_versions, issue: issue, versions_count: 1)
      prior_to_creation = first_design.versions.first
      expected << [prior_to_creation, :not_created_yet, false]

      v = modify_designs(first_design)
      expected << [v, :not_created_yet, false]

      design = create(:design, :with_versions, issue: issue, versions_count: 1)
      created_in = design.versions.first
      expected << [created_in, :created, true]

      # The future state should not affect the result for any state, so we
      # ensure that most states have a long future as well as a rich past
      2.times do
        v = modify_designs(first_design)
        expected << [v, :unaffected_visible, true]

        v = modify_designs(design)
        expected << [v, :modified, true]

        v = modify_designs(first_design)
        expected << [v, :unaffected_visible, true]

        v = delete_designs(design)
        expected << [v, :deleted, false]

        v = modify_designs(first_design)
        expected << [v, :unaffected_nv, false]

        v = restore_designs(design)
        expected << [v, :restored, true]
      end

      delete_designs(design) # ensure visibility is not corelated with current state

      got = expected.map do |(v, sym, _)|
        [v, sym, design.visible_in?(v)]
      end

      expect(got).to eq(expected)
    end
  end

  describe '#to_ability_name' do
    it { expect(described_class.new.to_ability_name).to eq('design') }
  end

  describe '#status' do
    context 'the design is new' do
      subject { build(:design) }

      it { is_expected.to have_attributes(status: :new) }
    end

    context 'the design is current' do
      subject { design1 }

      it { is_expected.to have_attributes(status: :current) }
    end

    context 'the design has been deleted' do
      subject { deleted_design }

      it { is_expected.to have_attributes(status: :deleted) }
    end
  end

  describe '#deleted?' do
    context 'the design is new' do
      let(:design) { build(:design) }

      it 'is falsy' do
        expect(design).not_to be_deleted
      end
    end

    context 'the design is current' do
      let(:design) { design1 }

      it 'is falsy' do
        expect(design).not_to be_deleted
      end
    end

    context 'the design has been deleted' do
      let(:design) { deleted_design }

      it 'is truthy' do
        expect(design).to be_deleted
      end
    end

    context 'the design has been deleted, but was then re-created' do
      let(:design) { create(:design, :with_versions, versions_count: 1, deleted: true) }

      it 'is falsy' do
        restore_designs(design)

        expect(design).not_to be_deleted
      end
    end
  end

  describe "#new_design?" do
    let(:design) { design1 }

    it "is false when there are versions" do
      expect(design1).not_to be_new_design
    end

    it "is true when there are no versions" do
      expect(build(:design)).to be_new_design
    end

    it 'is false for deleted designs' do
      expect(deleted_design).not_to be_new_design
    end

    it "does not cause extra queries when actions are loaded" do
      design.actions.map(&:id)

      expect { design.new_design? }.not_to exceed_query_limit(0)
    end

    it "implicitly caches values" do
      expect do
        design.new_design?
        design.new_design?
      end.not_to exceed_query_limit(1)
    end

    it "queries again when the clear_version_cache trigger has been called" do
      expect do
        design.new_design?
        design.clear_version_cache
        design.new_design?
      end.not_to exceed_query_limit(2)
    end

    it "causes a single query when there versions are not loaded" do
      design.reload

      expect { design.new_design? }.not_to exceed_query_limit(1)
    end
  end

  describe "#full_path" do
    it "builds the full path for a design" do
      design = build(:design, filename: "hello.jpg")
      expected_path = "#{DesignManagement.designs_directory}/issue-#{design.issue.iid}/hello.jpg"

      expect(design.full_path).to eq(expected_path)
    end
  end

  describe '#diff_refs' do
    let(:design) { create(:design, :with_file, versions_count: versions_count) }

    context 'there are several versions' do
      let(:versions_count) { 3 }

      it "builds diff refs based on the first commit and it's for the design" do
        expect(design.diff_refs.base_sha).to eq(design.versions.ordered.second.sha)
        expect(design.diff_refs.head_sha).to eq(design.versions.ordered.first.sha)
      end
    end

    context 'there is just one version' do
      let(:versions_count) { 1 }

      it 'builds diff refs based on the empty tree if there was only one version' do
        design = create(:design, :with_file, versions_count: 1)

        expect(design.diff_refs.base_sha).to eq(Gitlab::Git::BLANK_SHA)
        expect(design.diff_refs.head_sha).to eq(design.diff_refs.head_sha)
      end
    end

    it 'has no diff ref if new' do
      design = build(:design)

      expect(design.diff_refs).to be_nil
    end
  end

  describe '#repository' do
    it 'is a design repository' do
      design = build(:design)

      expect(design.repository).to be_a(DesignManagement::Repository)
    end
  end

  describe '#note_etag_key' do
    it 'returns a correct etag key' do
      design = create(:design)

      expect(design.note_etag_key).to eq(
        ::Gitlab::Routing.url_helpers.designs_project_issue_path(design.project, design.issue, { vueroute: design.filename })
      )
    end
  end

  describe '#user_notes_count', :use_clean_rails_memory_store_caching do
    let_it_be(:design) { create(:design, :with_file) }

    subject { design.user_notes_count }

    # Note: Cache invalidation tests are in `design_user_notes_count_service_spec.rb`

    it 'returns a count of user-generated notes' do
      create(:diff_note_on_design, noteable: design)

      is_expected.to eq(1)
    end

    it 'does not count notes on other designs' do
      second_design = create(:design, :with_file)
      create(:diff_note_on_design, noteable: second_design)

      is_expected.to eq(0)
    end

    it 'does not count system notes' do
      create(:diff_note_on_design, system: true, noteable: design)

      is_expected.to eq(0)
    end
  end

  describe '#after_note_changed' do
    subject { build(:design) }

    it 'calls #delete_cache on DesignUserNotesCountService' do
      expect_next_instance_of(DesignManagement::DesignUserNotesCountService) do |service|
        expect(service).to receive(:delete_cache)
      end

      subject.after_note_changed(build(:note))
    end

    it 'does not call #delete_cache on DesignUserNotesCountService when passed a system note' do
      expect(DesignManagement::DesignUserNotesCountService).not_to receive(:new)

      subject.after_note_changed(build(:note, :system))
    end
  end

  describe '.for_reference' do
    let_it_be(:design_a) { create(:design) }
    let_it_be(:design_b) { create(:design) }

    it 'avoids extra queries when calling to_reference' do
      designs = described_class.for_reference.where(id: [design_a.id, design_b.id]).to_a

      expect { designs.map(&:to_reference) }.not_to exceed_query_limit(0)
    end
  end

  describe '#to_reference' do
    let(:namespace) { build(:namespace, path: 'sample-namespace') }
    let(:project)   { build(:project, name: 'sample-project', namespace: namespace) }
    let(:group)     { create(:group, name: 'Group', path: 'sample-group') }
    let(:issue)     { build(:issue, iid: 1, project: project) }
    let(:filename)  { 'homescreen.jpg' }
    let(:design)    { build(:design, filename: filename, issue: issue, project: project) }

    context 'when nil argument' do
      let(:reference) { design.to_reference }

      it 'uses the simple format' do
        expect(reference).to eq "#1[homescreen.jpg]"
      end
    end

    context 'when full is true' do
      it 'returns complete path to the issue' do
        refs = [
          design.to_reference(full: true),
          design.to_reference(project, full: true),
          design.to_reference(group, full: true)
        ]

        expect(refs).to all(eq 'sample-namespace/sample-project#1/designs[homescreen.jpg]')
      end
    end

    context 'when full is false' do
      it 'returns complete path to the issue' do
        refs = [
          design.to_reference(build(:project), full: false),
          design.to_reference(group, full: false)
        ]

        expect(refs).to all(eq 'sample-namespace/sample-project#1[homescreen.jpg]')
      end
    end

    context 'when same project argument' do
      it 'returns bare reference' do
        expect(design.to_reference(project)).to eq("#1[homescreen.jpg]")
      end
    end
  end

  describe 'reference_pattern' do
    it 'is nil' do
      expect(described_class.reference_pattern).to be_nil
    end
  end

  describe 'link_reference_pattern' do
    it 'is not nil' do
      expect(described_class.link_reference_pattern).not_to be_nil
    end

    it 'does not match the designs tab' do
      expect(described_class.link_reference_pattern).not_to match(url_for_designs(issue))
    end

    where(:ext) do
      (described_class::SAFE_IMAGE_EXT + described_class::DANGEROUS_IMAGE_EXT).flat_map do |ext|
        [[ext], [ext.upcase]]
      end
    end

    with_them do
      let(:filename) { "my-file.#{ext}" }
      let(:design) { build(:design, filename: filename) }
      let(:url) { url_for_design(design) }
      let(:captures) { described_class.link_reference_pattern.match(url)&.named_captures }

      it 'matches the URL' do
        expect(captures).to include(
          'url_filename' => filename,
          'issue' => design.issue.iid.to_s,
          'namespace' => design.project.namespace.to_param,
          'project' => design.project.name
        )
      end

      context 'the file needs to be encoded' do
        let(:filename) { "my file.#{ext}" }

        it 'extracts the encoded filename' do
          expect(captures).to include('url_filename' => 'my%20file.' + ext)
        end
      end

      context 'the file is all upper case' do
        let(:filename) { "file.#{ext}".upcase }

        it 'extracts the encoded filename' do
          expect(captures).to include('url_filename' => filename)
        end
      end
    end
  end

  describe '.by_issue_id_and_filename' do
    let_it_be(:issue_a) { create(:issue) }
    let_it_be(:issue_b) { create(:issue) }

    let_it_be(:design_a) { create(:design, issue: issue_a) }
    let_it_be(:design_b) { create(:design, issue: issue_a) }
    let_it_be(:design_c) { create(:design, issue: issue_b, filename: design_a.filename) }
    let_it_be(:design_d) { create(:design, issue: issue_b, filename: design_b.filename) }

    it_behaves_like 'a where_composite scope', :by_issue_id_and_filename do
      let(:all_results) { [design_a, design_b, design_c, design_d] }
      let(:first_result) { design_a }

      let(:composite_ids) do
        all_results.map { |design| { issue_id: design.issue_id, filename: design.filename } }
      end
    end
  end
end