summaryrefslogtreecommitdiff
path: root/spec/models/concerns/noteable_spec.rb
blob: 383ed68816e33f6903f26ab4ffad0494da2a3999 (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Noteable do
  let!(:active_diff_note1) { create(:diff_note_on_merge_request) }
  let(:project) { active_diff_note1.project }
  subject { active_diff_note1.noteable }

  let!(:active_diff_note2) { create(:diff_note_on_merge_request, project: project, noteable: subject, in_reply_to: active_diff_note1) }
  let!(:active_diff_note3) { create(:diff_note_on_merge_request, project: project, noteable: subject, position: active_position2) }
  let!(:outdated_diff_note1) { create(:diff_note_on_merge_request, project: project, noteable: subject, position: outdated_position) }
  let!(:outdated_diff_note2) { create(:diff_note_on_merge_request, project: project, noteable: subject, in_reply_to: outdated_diff_note1) }
  let!(:discussion_note1) { create(:discussion_note_on_merge_request, project: project, noteable: subject) }
  let!(:discussion_note2) { create(:discussion_note_on_merge_request, in_reply_to: discussion_note1) }
  let!(:commit_diff_note1) { create(:diff_note_on_commit, project: project) }
  let!(:commit_diff_note2) { create(:diff_note_on_commit, project: project, in_reply_to: commit_diff_note1) }
  let!(:commit_note1) { create(:note_on_commit, project: project) }
  let!(:commit_note2) { create(:note_on_commit, project: project) }
  let!(:commit_discussion_note1) { create(:discussion_note_on_commit, project: project) }
  let!(:commit_discussion_note2) { create(:discussion_note_on_commit, in_reply_to: commit_discussion_note1) }
  let!(:commit_discussion_note3) { create(:discussion_note_on_commit, project: project) }
  let!(:note1) { create(:note, project: project, noteable: subject) }
  let!(:note2) { create(:note, project: project, noteable: subject) }

  let(:active_position2) do
    Gitlab::Diff::Position.new(
      old_path: 'files/ruby/popen.rb',
      new_path: 'files/ruby/popen.rb',
      old_line: 16,
      new_line: 22,
      diff_refs: subject.diff_refs
    )
  end

  let(:outdated_position) do
    Gitlab::Diff::Position.new(
      old_path: 'files/ruby/popen.rb',
      new_path: 'files/ruby/popen.rb',
      old_line: nil,
      new_line: 9,
      diff_refs: project.commit('874797c3a73b60d2187ed6e2fcabd289ff75171e').diff_refs
    )
  end

  describe '#discussions' do
    let(:discussions) { subject.discussions }

    it 'includes discussions for diff notes, commit diff notes, commit notes, and regular notes' do
      expect(discussions).to eq(
        [
          DiffDiscussion.new([active_diff_note1, active_diff_note2], subject),
          DiffDiscussion.new([active_diff_note3], subject),
          DiffDiscussion.new([outdated_diff_note1, outdated_diff_note2], subject),
          Discussion.new([discussion_note1, discussion_note2], subject),
          DiffDiscussion.new([commit_diff_note1, commit_diff_note2], subject),
          OutOfContextDiscussion.new([commit_note1, commit_note2], subject),
          Discussion.new([commit_discussion_note1, commit_discussion_note2], subject),
          Discussion.new([commit_discussion_note3], subject),
          IndividualNoteDiscussion.new([note1], subject),
          IndividualNoteDiscussion.new([note2], subject)
        ])
    end
  end

  # rubocop:disable RSpec/MultipleMemoizedHelpers
  describe '#commenters' do
    shared_examples 'commenters' do
      it 'does not automatically include the noteable author' do
        expect(commenters).not_to include(noteable.author)
      end

      context 'with no user' do
        it 'contains a distinct list of non-internal note authors' do
          expect(commenters).to contain_exactly(commenter, another_commenter)
        end
      end

      context 'with non project member' do
        let(:current_user) { create(:user) }

        it 'contains a distinct list of non-internal note authors' do
          expect(commenters).to contain_exactly(commenter, another_commenter)
        end

        it 'does not include a commenter from another noteable' do
          expect(commenters).not_to include(other_noteable_commenter)
        end
      end
    end

    let_it_be(:commenter) { create(:user) }
    let_it_be(:another_commenter) { create(:user) }
    let_it_be(:internal_commenter) { create(:user) }
    let_it_be(:other_noteable_commenter) { create(:user) }

    let(:current_user) {}
    let(:commenters) { noteable.commenters(user: current_user) }

    let!(:comments) { create_list(:note, 2, author: commenter, noteable: noteable, project: noteable.project) }
    let!(:more_comments) { create_list(:note, 2, author: another_commenter, noteable: noteable, project: noteable.project) }

    context 'when noteable is an issue' do
      let(:noteable) { create(:issue) }

      let!(:internal_comments) { create_list(:note, 2, author: internal_commenter, noteable: noteable, project: noteable.project, internal: true) }
      let!(:other_noteable_comments) { create_list(:note, 2, author: other_noteable_commenter, noteable: create(:issue, project: noteable.project), project: noteable.project) }

      it_behaves_like 'commenters'

      context 'with reporter' do
        let(:current_user) { create(:user) }

        before do
          noteable.project.add_reporter(current_user)
        end

        it 'contains a distinct list of non-internal note authors' do
          expect(commenters).to contain_exactly(commenter, another_commenter, internal_commenter)
        end

        context 'with noteable author' do
          let(:current_user) { noteable.author }

          it 'contains a distinct list of non-internal note authors' do
            expect(commenters).to contain_exactly(commenter, another_commenter, internal_commenter)
          end
        end
      end
    end

    context 'when noteable is a merge request' do
      let(:noteable) { create(:merge_request) }

      let!(:other_noteable_comments) { create_list(:note, 2, author: other_noteable_commenter, noteable: create(:merge_request, source_project: noteable.project, source_branch: 'feat123'), project: noteable.project) }

      it_behaves_like 'commenters'
    end
  end
  # rubocop:enable RSpec/MultipleMemoizedHelpers

  describe '#discussion_ids_relation' do
    it 'returns ordered discussion_ids' do
      discussion_ids = subject.discussion_ids_relation.pluck(:discussion_id)

      expect(discussion_ids).to eq([
        active_diff_note1,
        active_diff_note3,
        outdated_diff_note1,
        discussion_note1,
        note1,
        note2
      ].map(&:discussion_id))
    end
  end

  describe '#discussion_root_note_ids' do
    let!(:label_event) { create(:resource_label_event, merge_request: subject) }
    let!(:system_note) { create(:system_note, project: project, noteable: subject) }
    let!(:milestone_event) { create(:resource_milestone_event, merge_request: subject) }
    let!(:state_event) { create(:resource_state_event, merge_request: subject) }

    it 'returns ordered discussion_ids and synthetic note ids' do
      discussions = subject.discussion_root_note_ids(notes_filter: UserPreference::NOTES_FILTERS[:all_notes]).map do |n|
        { table_name: n.table_name, discussion_id: n.discussion_id, id: n.id }
      end

      expect(discussions).to match(
        [
          a_hash_including(table_name: 'notes', discussion_id: active_diff_note1.discussion_id),
          a_hash_including(table_name: 'notes', discussion_id: active_diff_note3.discussion_id),
          a_hash_including(table_name: 'notes', discussion_id: outdated_diff_note1.discussion_id),
          a_hash_including(table_name: 'notes', discussion_id: discussion_note1.discussion_id),
          a_hash_including(table_name: 'notes', discussion_id: commit_diff_note1.discussion_id),
          a_hash_including(table_name: 'notes', discussion_id: commit_note1.discussion_id),
          a_hash_including(table_name: 'notes', discussion_id: commit_note2.discussion_id),
          a_hash_including(table_name: 'notes', discussion_id: commit_discussion_note1.discussion_id),
          a_hash_including(table_name: 'notes', discussion_id: commit_discussion_note3.discussion_id),
          a_hash_including(table_name: 'notes', discussion_id: note1.discussion_id),
          a_hash_including(table_name: 'notes', discussion_id: note2.discussion_id),
          a_hash_including(table_name: 'resource_label_events', id: label_event.id),
          a_hash_including(table_name: 'notes', discussion_id: system_note.discussion_id),
          a_hash_including(table_name: 'resource_milestone_events', id: milestone_event.id),
          a_hash_including(table_name: 'resource_state_events', id: state_event.id)
        ])
    end

    it 'filters by comments only' do
      discussions = subject.discussion_root_note_ids(notes_filter: UserPreference::NOTES_FILTERS[:only_comments]).map do |n|
        { table_name: n.table_name, discussion_id: n.discussion_id, id: n.id }
      end

      expect(discussions).to match(
        [
          a_hash_including(table_name: 'notes', discussion_id: active_diff_note1.discussion_id),
          a_hash_including(table_name: 'notes', discussion_id: active_diff_note3.discussion_id),
          a_hash_including(table_name: 'notes', discussion_id: outdated_diff_note1.discussion_id),
          a_hash_including(table_name: 'notes', discussion_id: discussion_note1.discussion_id),
          a_hash_including(table_name: 'notes', discussion_id: commit_diff_note1.discussion_id),
          a_hash_including(table_name: 'notes', discussion_id: commit_note1.discussion_id),
          a_hash_including(table_name: 'notes', discussion_id: commit_note2.discussion_id),
          a_hash_including(table_name: 'notes', discussion_id: commit_discussion_note1.discussion_id),
          a_hash_including(table_name: 'notes', discussion_id: commit_discussion_note3.discussion_id),
          a_hash_including(table_name: 'notes', discussion_id: note1.discussion_id),
          a_hash_including(table_name: 'notes', discussion_id: note2.discussion_id)
        ])
    end

    it 'filters by system notes only' do
      discussions = subject.discussion_root_note_ids(notes_filter: UserPreference::NOTES_FILTERS[:only_activity]).map do |n|
        { table_name: n.table_name, discussion_id: n.discussion_id, id: n.id }
      end

      expect(discussions).to match(
        [
          a_hash_including(table_name: 'resource_label_events', id: label_event.id),
          a_hash_including(table_name: 'notes', discussion_id: system_note.discussion_id),
          a_hash_including(table_name: 'resource_milestone_events', id: milestone_event.id),
          a_hash_including(table_name: 'resource_state_events', id: state_event.id)
        ])
    end
  end

  describe '#grouped_diff_discussions' do
    let(:grouped_diff_discussions) { subject.grouped_diff_discussions }

    it 'includes active discussions' do
      discussions = grouped_diff_discussions.values.flatten

      expect(discussions.count).to eq(2)
      expect(discussions.map(&:id)).to eq([active_diff_note1.discussion_id, active_diff_note3.discussion_id])
      expect(discussions.all?(&:active?)).to be true

      expect(discussions.first.notes).to eq([active_diff_note1, active_diff_note2])
      expect(discussions.last.notes).to eq([active_diff_note3])
    end

    it 'does not include outdated discussions' do
      expect(grouped_diff_discussions.values.flatten.map(&:id)).not_to include(outdated_diff_note1.discussion_id)
    end

    it 'groups the discussions by line code' do
      expect(grouped_diff_discussions[active_diff_note1.line_code].first.id).to eq(active_diff_note1.discussion_id)
      expect(grouped_diff_discussions[active_diff_note3.line_code].first.id).to eq(active_diff_note3.discussion_id)
    end
  end

  context 'discussion status' do
    let(:first_discussion) { build_stubbed(:discussion_note_on_merge_request, noteable: subject, project: project).to_discussion }
    let(:second_discussion) { build_stubbed(:discussion_note_on_merge_request, noteable: subject, project: project).to_discussion }
    let(:third_discussion) { build_stubbed(:discussion_note_on_merge_request, noteable: subject, project: project).to_discussion }

    before do
      allow(subject).to receive(:resolvable_discussions).and_return([first_discussion, second_discussion, third_discussion])
    end

    describe '#discussions_resolvable?' do
      context 'when all discussions are unresolvable' do
        before do
          allow(first_discussion).to receive(:resolvable?).and_return(false)
          allow(second_discussion).to receive(:resolvable?).and_return(false)
          allow(third_discussion).to receive(:resolvable?).and_return(false)
        end

        it 'returns false' do
          expect(subject.discussions_resolvable?).to be false
        end
      end

      context 'when some discussions are unresolvable and some discussions are resolvable' do
        before do
          allow(first_discussion).to receive(:resolvable?).and_return(true)
          allow(second_discussion).to receive(:resolvable?).and_return(false)
          allow(third_discussion).to receive(:resolvable?).and_return(true)
        end

        it 'returns true' do
          expect(subject.discussions_resolvable?).to be true
        end
      end

      context 'when all discussions are resolvable' do
        before do
          allow(first_discussion).to receive(:resolvable?).and_return(true)
          allow(second_discussion).to receive(:resolvable?).and_return(true)
          allow(third_discussion).to receive(:resolvable?).and_return(true)
        end

        it 'returns true' do
          expect(subject.discussions_resolvable?).to be true
        end
      end
    end

    describe '#discussions_resolved?' do
      context 'when discussions are not resolvable' do
        before do
          allow(subject).to receive(:discussions_resolvable?).and_return(false)
        end

        it 'returns false' do
          expect(subject.discussions_resolved?).to be false
        end
      end

      context 'when discussions are resolvable' do
        before do
          allow(subject).to receive(:discussions_resolvable?).and_return(true)

          allow(first_discussion).to receive(:resolvable?).and_return(true)
          allow(second_discussion).to receive(:resolvable?).and_return(false)
          allow(third_discussion).to receive(:resolvable?).and_return(true)
        end

        context 'when all resolvable discussions are resolved' do
          before do
            allow(first_discussion).to receive(:resolved?).and_return(true)
            allow(third_discussion).to receive(:resolved?).and_return(true)
          end

          it 'returns true' do
            expect(subject.discussions_resolved?).to be true
          end
        end

        context 'when some resolvable discussions are not resolved' do
          before do
            allow(first_discussion).to receive(:resolved?).and_return(true)
            allow(third_discussion).to receive(:resolved?).and_return(false)
          end

          it 'returns false' do
            expect(subject.discussions_resolved?).to be false
          end
        end
      end
    end

    describe '#discussions_to_be_resolved' do
      before do
        allow(first_discussion).to receive(:to_be_resolved?).and_return(true)
        allow(second_discussion).to receive(:to_be_resolved?).and_return(false)
        allow(third_discussion).to receive(:to_be_resolved?).and_return(false)
      end

      it 'includes only discussions that need to be resolved' do
        expect(subject.discussions_to_be_resolved).to eq([first_discussion])
      end
    end

    describe '#discussions_can_be_resolved_by?' do
      let(:user) { build(:user) }

      context 'all discussions can be resolved by the user' do
        before do
          allow(first_discussion).to receive(:can_resolve?).with(user).and_return(true)
          allow(second_discussion).to receive(:can_resolve?).with(user).and_return(true)
          allow(third_discussion).to receive(:can_resolve?).with(user).and_return(true)
        end

        it 'allows a user to resolve the discussions' do
          expect(subject.discussions_can_be_resolved_by?(user)).to be(true)
        end
      end

      context 'one discussion cannot be resolved by the user' do
        before do
          allow(first_discussion).to receive(:can_resolve?).with(user).and_return(true)
          allow(second_discussion).to receive(:can_resolve?).with(user).and_return(true)
          allow(third_discussion).to receive(:can_resolve?).with(user).and_return(false)
        end

        it 'allows a user to resolve the discussions' do
          expect(subject.discussions_can_be_resolved_by?(user)).to be(false)
        end
      end
    end
  end

  describe '.replyable_types' do
    it 'exposes the replyable types' do
      expect(described_class.replyable_types).to include('Issue', 'MergeRequest')
    end
  end

  describe '.resolvable_types' do
    it 'exposes the replyable types' do
      expect(described_class.resolvable_types).to include('MergeRequest', 'DesignManagement::Design')
    end
  end

  describe '.email_creatable_types' do
    it 'exposes the email creatable types' do
      expect(described_class.email_creatable_types).to include('Issue')
    end
  end

  describe '#capped_notes_count' do
    context 'notes number < 10' do
      it 'the number of notes is returned' do
        expect(subject.capped_notes_count(10)).to eq(9)
      end
    end

    context 'notes number > 10' do
      before do
        create_list(:note, 2, project: project, noteable: subject)
      end

      it '10 is returned' do
        expect(subject.capped_notes_count(10)).to eq(10)
      end
    end
  end

  describe '#has_any_diff_note_positions?' do
    let(:source_branch) { 'compare-with-merge-head-source' }
    let(:target_branch) { 'compare-with-merge-head-target' }
    let(:merge_request) { create(:merge_request, source_branch: source_branch, target_branch: target_branch) }

    let!(:note) do
      path = 'files/markdown/ruby-style-guide.md'

      position = Gitlab::Diff::Position.new(
        old_path: path,
        new_path: path,
        new_line: 508,
        diff_refs: merge_request.diff_refs
      )

      create(:diff_note_on_merge_request, project: merge_request.project, position: position, noteable: merge_request)
    end

    before do
      MergeRequests::MergeToRefService.new(project: merge_request.project, current_user: merge_request.author).execute(merge_request)
      Discussions::CaptureDiffNotePositionsService.new(merge_request).execute
    end

    it 'returns true when it has diff note positions' do
      expect(merge_request.has_any_diff_note_positions?).to be(true)
    end

    it 'returns false when it has notes but no diff note positions' do
      DiffNotePosition.where(note: note).find_each(&:delete)

      expect(merge_request.has_any_diff_note_positions?).to be(false)
    end

    it 'returns false when it has no notes' do
      merge_request.notes.find_each(&:destroy)

      expect(merge_request.has_any_diff_note_positions?).to be(false)
    end
  end

  describe '#creatable_note_email_address' do
    let_it_be(:user) { create(:user) }
    let_it_be(:source_branch) { 'compare-with-merge-head-source' }

    let(:issue) { create(:issue, project: project) }
    let(:snippet) { build(:snippet) }

    context 'incoming email enabled' do
      before do
        stub_incoming_email_setting(enabled: true, address: "p+%{key}@gl.ab")
      end

      it 'returns the address to create a note' do
        address = "p+#{project.full_path_slug}-#{project.project_id}-#{user.incoming_email_token}-issue-#{issue.iid}@gl.ab"

        expect(issue.creatable_note_email_address(user)).to eq(address)
      end

      it 'returns nil for unsupported types' do
        expect(snippet.creatable_note_email_address(user)).to be_nil
      end
    end

    context 'incoming email disabled' do
      before do
        stub_incoming_email_setting(enabled: false)
      end

      it 'returns nil' do
        expect(issue.creatable_note_email_address(user)).to be_nil
      end
    end
  end
end