2019-03-30 07:23:56 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-03-10 01:29:11 +00:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-24 18:09:03 +00:00
|
|
|
RSpec.describe Noteable do
|
2017-04-04 22:27:23 +00:00
|
|
|
let!(:active_diff_note1) { create(:diff_note_on_merge_request) }
|
|
|
|
let(:project) { active_diff_note1.project }
|
|
|
|
subject { active_diff_note1.noteable }
|
2019-10-18 11:11:44 +00:00
|
|
|
|
2017-04-04 22:27:23 +00:00
|
|
|
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) }
|
2017-03-17 19:25:52 +00:00
|
|
|
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) }
|
2017-03-31 21:52:38 +00:00
|
|
|
let!(:commit_diff_note2) { create(:diff_note_on_commit, project: project, in_reply_to: commit_diff_note1) }
|
2017-03-17 19:25:52 +00:00
|
|
|
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) }
|
2017-04-04 22:27:23 +00:00
|
|
|
let!(:note1) { create(:note, project: project, noteable: subject) }
|
|
|
|
let!(:note2) { create(:note, project: project, noteable: subject) }
|
2017-03-17 19:25:52 +00:00
|
|
|
|
|
|
|
let(:active_position2) do
|
|
|
|
Gitlab::Diff::Position.new(
|
2021-01-05 03:10:19 +00:00
|
|
|
old_path: 'files/ruby/popen.rb',
|
|
|
|
new_path: 'files/ruby/popen.rb',
|
2017-03-17 19:25:52 +00:00
|
|
|
old_line: 16,
|
|
|
|
new_line: 22,
|
2017-04-04 22:27:23 +00:00
|
|
|
diff_refs: subject.diff_refs
|
2017-03-17 19:25:52 +00:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:outdated_position) do
|
|
|
|
Gitlab::Diff::Position.new(
|
2021-01-05 03:10:19 +00:00
|
|
|
old_path: 'files/ruby/popen.rb',
|
|
|
|
new_path: 'files/ruby/popen.rb',
|
2017-03-17 19:25:52 +00:00
|
|
|
old_line: nil,
|
|
|
|
new_line: 9,
|
2021-01-05 03:10:19 +00:00
|
|
|
diff_refs: project.commit('874797c3a73b60d2187ed6e2fcabd289ff75171e').diff_refs
|
2017-03-17 19:25:52 +00:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#discussions' do
|
2017-04-04 22:27:23 +00:00
|
|
|
let(:discussions) { subject.discussions }
|
2017-03-17 19:25:52 +00:00
|
|
|
|
|
|
|
it 'includes discussions for diff notes, commit diff notes, commit notes, and regular notes' do
|
2017-04-04 22:27:23 +00:00
|
|
|
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)
|
2017-03-17 19:25:52 +00:00
|
|
|
])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-03-25 09:08:11 +00:00
|
|
|
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
|
|
|
|
|
2017-03-17 19:25:52 +00:00
|
|
|
describe '#grouped_diff_discussions' do
|
2017-04-04 22:27:23 +00:00
|
|
|
let(:grouped_diff_discussions) { subject.grouped_diff_discussions }
|
2017-03-17 19:25:52 +00:00
|
|
|
|
2021-01-05 03:10:19 +00:00
|
|
|
it 'includes active discussions' do
|
2017-04-04 22:27:23 +00:00
|
|
|
discussions = grouped_diff_discussions.values.flatten
|
2017-03-17 19:25:52 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2021-01-05 03:10:19 +00:00
|
|
|
it 'does not include outdated discussions' do
|
2017-04-04 22:27:23 +00:00
|
|
|
expect(grouped_diff_discussions.values.flatten.map(&:id)).not_to include(outdated_diff_note1.discussion_id)
|
2017-03-17 19:25:52 +00:00
|
|
|
end
|
|
|
|
|
2021-01-05 03:10:19 +00:00
|
|
|
it 'groups the discussions by line code' do
|
2017-04-04 22:27:23 +00:00
|
|
|
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
|
|
|
|
|
2021-01-05 03:10:19 +00:00
|
|
|
context 'discussion status' do
|
2017-04-04 22:27:23 +00:00
|
|
|
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
|
|
|
|
|
2021-01-05 03:10:19 +00:00
|
|
|
describe '#discussions_resolvable?' do
|
|
|
|
context 'when all discussions are unresolvable' do
|
2017-04-04 22:27:23 +00:00
|
|
|
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
|
|
|
|
|
2021-01-05 03:10:19 +00:00
|
|
|
it 'returns false' do
|
2017-04-04 22:27:23 +00:00
|
|
|
expect(subject.discussions_resolvable?).to be false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-01-05 03:10:19 +00:00
|
|
|
context 'when some discussions are unresolvable and some discussions are resolvable' do
|
2017-04-04 22:27:23 +00:00
|
|
|
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
|
|
|
|
|
2021-01-05 03:10:19 +00:00
|
|
|
it 'returns true' do
|
2017-04-04 22:27:23 +00:00
|
|
|
expect(subject.discussions_resolvable?).to be true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-01-05 03:10:19 +00:00
|
|
|
context 'when all discussions are resolvable' do
|
2017-04-04 22:27:23 +00:00
|
|
|
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
|
|
|
|
|
2021-01-05 03:10:19 +00:00
|
|
|
it 'returns true' do
|
2017-04-04 22:27:23 +00:00
|
|
|
expect(subject.discussions_resolvable?).to be true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-01-05 03:10:19 +00:00
|
|
|
describe '#discussions_resolved?' do
|
|
|
|
context 'when discussions are not resolvable' do
|
2017-04-04 22:27:23 +00:00
|
|
|
before do
|
|
|
|
allow(subject).to receive(:discussions_resolvable?).and_return(false)
|
|
|
|
end
|
|
|
|
|
2021-01-05 03:10:19 +00:00
|
|
|
it 'returns false' do
|
2017-04-04 22:27:23 +00:00
|
|
|
expect(subject.discussions_resolved?).to be false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-01-05 03:10:19 +00:00
|
|
|
context 'when discussions are resolvable' do
|
2017-04-04 22:27:23 +00:00
|
|
|
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
|
|
|
|
|
2021-01-05 03:10:19 +00:00
|
|
|
context 'when all resolvable discussions are resolved' do
|
2017-04-04 22:27:23 +00:00
|
|
|
before do
|
|
|
|
allow(first_discussion).to receive(:resolved?).and_return(true)
|
|
|
|
allow(third_discussion).to receive(:resolved?).and_return(true)
|
|
|
|
end
|
|
|
|
|
2021-01-05 03:10:19 +00:00
|
|
|
it 'returns true' do
|
2017-04-04 22:27:23 +00:00
|
|
|
expect(subject.discussions_resolved?).to be true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-01-05 03:10:19 +00:00
|
|
|
context 'when some resolvable discussions are not resolved' do
|
2017-04-04 22:27:23 +00:00
|
|
|
before do
|
|
|
|
allow(first_discussion).to receive(:resolved?).and_return(true)
|
|
|
|
allow(third_discussion).to receive(:resolved?).and_return(false)
|
|
|
|
end
|
|
|
|
|
2021-01-05 03:10:19 +00:00
|
|
|
it 'returns false' do
|
2017-04-04 22:27:23 +00:00
|
|
|
expect(subject.discussions_resolved?).to be false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-01-05 03:10:19 +00:00
|
|
|
describe '#discussions_to_be_resolved' do
|
2017-04-04 22:27:23 +00:00
|
|
|
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
|
2017-03-17 19:25:52 +00:00
|
|
|
end
|
|
|
|
end
|
2019-06-03 14:41:05 +00:00
|
|
|
|
|
|
|
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
|
2020-05-07 12:09:46 +00:00
|
|
|
expect(described_class.resolvable_types).to include('MergeRequest', 'DesignManagement::Design')
|
2019-06-03 14:41:05 +00:00
|
|
|
end
|
|
|
|
end
|
2019-08-25 23:55:00 +00:00
|
|
|
|
2021-01-05 03:10:19 +00:00
|
|
|
describe '.email_creatable_types' do
|
|
|
|
it 'exposes the email creatable types' do
|
|
|
|
expect(described_class.email_creatable_types).to include('Issue')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-08-25 23:55:00 +00:00
|
|
|
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
|
2020-06-18 18:08:58 +00:00
|
|
|
|
2021-01-05 03:10:19 +00:00
|
|
|
describe '#has_any_diff_note_positions?' do
|
|
|
|
let(:source_branch) { 'compare-with-merge-head-source' }
|
|
|
|
let(:target_branch) { 'compare-with-merge-head-target' }
|
2020-06-18 18:08:58 +00:00
|
|
|
let(:merge_request) { create(:merge_request, source_branch: source_branch, target_branch: target_branch) }
|
|
|
|
|
|
|
|
let!(:note) do
|
2021-01-05 03:10:19 +00:00
|
|
|
path = 'files/markdown/ruby-style-guide.md'
|
2020-06-18 18:08:58 +00:00
|
|
|
|
|
|
|
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(merge_request.project, merge_request.author).execute(merge_request)
|
|
|
|
Discussions::CaptureDiffNotePositionsService.new(merge_request).execute
|
|
|
|
end
|
|
|
|
|
2021-01-05 03:10:19 +00:00
|
|
|
it 'returns true when it has diff note positions' do
|
2020-06-18 18:08:58 +00:00
|
|
|
expect(merge_request.has_any_diff_note_positions?).to be(true)
|
|
|
|
end
|
|
|
|
|
2021-01-05 03:10:19 +00:00
|
|
|
it 'returns false when it has notes but no diff note positions' do
|
2020-06-18 18:08:58 +00:00
|
|
|
DiffNotePosition.where(note: note).find_each(&:delete)
|
|
|
|
|
|
|
|
expect(merge_request.has_any_diff_note_positions?).to be(false)
|
|
|
|
end
|
|
|
|
|
2021-01-05 03:10:19 +00:00
|
|
|
it 'returns false when it has no notes' do
|
2020-06-18 18:08:58 +00:00
|
|
|
merge_request.notes.find_each(&:destroy)
|
|
|
|
|
|
|
|
expect(merge_request.has_any_diff_note_positions?).to be(false)
|
|
|
|
end
|
|
|
|
end
|
2021-01-05 03:10:19 +00:00
|
|
|
|
|
|
|
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
|
2017-03-10 01:29:11 +00:00
|
|
|
end
|