2019-09-30 08:06:01 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-06-28 18:14:11 -04:00
|
|
|
require "spec_helper"
|
|
|
|
|
2020-06-24 05:08:32 -04:00
|
|
|
RSpec.describe NotesHelper do
|
2017-04-30 16:32:09 -04:00
|
|
|
include RepoHelpers
|
|
|
|
|
2020-10-05 05:08:17 -04:00
|
|
|
let_it_be(:owner) { create(:owner) }
|
|
|
|
let_it_be(:group) { create(:group) }
|
|
|
|
let_it_be(:project) { create(:project, namespace: group) }
|
|
|
|
let_it_be(:maintainer) { create(:user) }
|
|
|
|
let_it_be(:reporter) { create(:user) }
|
|
|
|
let_it_be(:guest) { create(:user) }
|
|
|
|
|
|
|
|
let_it_be(:owner_note) { create(:note, author: owner, project: project) }
|
|
|
|
let_it_be(:maintainer_note) { create(:note, author: maintainer, project: project) }
|
|
|
|
let_it_be(:reporter_note) { create(:note, author: reporter, project: project) }
|
2021-04-19 14:09:09 -04:00
|
|
|
|
2018-07-11 10:36:08 -04:00
|
|
|
let!(:notes) { [owner_note, maintainer_note, reporter_note] }
|
2016-06-29 09:11:42 -04:00
|
|
|
|
2020-10-05 05:08:17 -04:00
|
|
|
before_all do
|
2016-07-20 00:52:31 -04:00
|
|
|
group.add_owner(owner)
|
2018-07-11 10:36:08 -04:00
|
|
|
project.add_maintainer(maintainer)
|
2017-12-22 03:18:28 -05:00
|
|
|
project.add_reporter(reporter)
|
|
|
|
project.add_guest(guest)
|
2016-07-20 00:52:31 -04:00
|
|
|
end
|
|
|
|
|
2020-05-26 08:08:22 -04:00
|
|
|
describe '#note_target_title' do
|
|
|
|
context 'note does not exist' do
|
|
|
|
it 'returns nil' do
|
|
|
|
expect(helper.note_target_title(nil)).to be_blank
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'target does not exist' do
|
|
|
|
it 'returns nil' do
|
|
|
|
note = Note.new
|
|
|
|
expect(helper.note_target_title(note)).to be_blank
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when given a design target' do
|
|
|
|
it 'returns nil' do
|
|
|
|
note = build_stubbed(:note_on_design)
|
|
|
|
expect(helper.note_target_title(note)).to be_blank
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when given a non-design target' do
|
|
|
|
it 'returns the issue title' do
|
|
|
|
issue = build_stubbed(:issue, title: 'Issue 1')
|
|
|
|
note = build_stubbed(:note, noteable: issue)
|
|
|
|
expect(helper.note_target_title(note)).to eq('Issue 1')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-07-20 00:52:31 -04:00
|
|
|
describe "#notes_max_access_for_users" do
|
2017-07-29 11:04:42 -04:00
|
|
|
it 'returns access levels' do
|
|
|
|
expect(helper.note_max_access_for_user(owner_note)).to eq(Gitlab::Access::OWNER)
|
2018-07-11 10:36:08 -04:00
|
|
|
expect(helper.note_max_access_for_user(maintainer_note)).to eq(Gitlab::Access::MAINTAINER)
|
2017-07-29 11:04:42 -04:00
|
|
|
expect(helper.note_max_access_for_user(reporter_note)).to eq(Gitlab::Access::REPORTER)
|
2016-06-28 18:14:11 -04:00
|
|
|
end
|
2016-06-29 09:11:42 -04:00
|
|
|
|
|
|
|
it 'handles access in different projects' do
|
2017-08-02 15:55:11 -04:00
|
|
|
second_project = create(:project)
|
2018-07-11 10:36:08 -04:00
|
|
|
second_project.add_reporter(maintainer)
|
|
|
|
other_note = create(:note, author: maintainer, project: second_project)
|
2016-06-29 09:11:42 -04:00
|
|
|
|
2018-07-11 10:36:08 -04:00
|
|
|
expect(helper.note_max_access_for_user(maintainer_note)).to eq(Gitlab::Access::MAINTAINER)
|
2017-07-29 11:04:42 -04:00
|
|
|
expect(helper.note_max_access_for_user(other_note)).to eq(Gitlab::Access::REPORTER)
|
2017-07-27 16:38:52 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-04-30 16:32:09 -04:00
|
|
|
describe '#discussion_path' do
|
2020-10-05 05:08:17 -04:00
|
|
|
let_it_be(:project) { create(:project, :repository) }
|
2021-04-19 14:09:09 -04:00
|
|
|
|
2017-12-18 10:21:33 -05:00
|
|
|
let(:anchor) { discussion.line_code }
|
2017-04-30 16:32:09 -04:00
|
|
|
|
|
|
|
context 'for a merge request discusion' do
|
2020-10-05 05:08:17 -04:00
|
|
|
let_it_be(:merge_request) { create(:merge_request, source_project: project, target_project: project, importing: true) }
|
|
|
|
let_it_be(:merge_request_diff1) { merge_request.merge_request_diffs.create!(head_commit_sha: '6f6d7e7ed97bb5f0054f2b1df789b39ca89b6ff9') }
|
|
|
|
let_it_be(:merge_request_diff2) { merge_request.merge_request_diffs.create!(head_commit_sha: nil) }
|
|
|
|
let_it_be(:merge_request_diff3) { merge_request.merge_request_diffs.create!(head_commit_sha: '5937ac0a7beb003549fc5fd26fc247adbce4a52e') }
|
2017-04-30 16:32:09 -04:00
|
|
|
|
|
|
|
context 'for a diff discussion' do
|
|
|
|
context 'when the discussion is active' do
|
|
|
|
let(:discussion) { create(:diff_note_on_merge_request, noteable: merge_request, project: project).to_discussion }
|
|
|
|
|
|
|
|
it 'returns the diff path with the line code' do
|
2017-06-29 13:06:35 -04:00
|
|
|
expect(helper.discussion_path(discussion)).to eq(diffs_project_merge_request_path(project, merge_request, anchor: discussion.line_code))
|
2017-04-30 16:32:09 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the discussion is on an older merge request version' do
|
|
|
|
let(:position) do
|
2020-02-17 13:09:00 -05:00
|
|
|
build(:text_diff_position, :added,
|
|
|
|
file: ".gitmodules",
|
2017-04-30 16:32:09 -04:00
|
|
|
new_line: 4,
|
|
|
|
diff_refs: merge_request_diff1.diff_refs
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:diff_note) { create(:diff_note_on_merge_request, noteable: merge_request, project: project, position: position) }
|
|
|
|
let(:discussion) { diff_note.to_discussion }
|
|
|
|
|
|
|
|
before do
|
|
|
|
diff_note.position = diff_note.original_position
|
|
|
|
diff_note.save!
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns the diff version path with the line code' do
|
2017-06-29 13:06:35 -04:00
|
|
|
expect(helper.discussion_path(discussion)).to eq(diffs_project_merge_request_path(project, merge_request, diff_id: merge_request_diff1, anchor: discussion.line_code))
|
2017-04-30 16:32:09 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the discussion is on a comparison between merge request versions' do
|
|
|
|
let(:position) do
|
2020-02-17 13:09:00 -05:00
|
|
|
build(:text_diff_position,
|
|
|
|
file: ".gitmodules",
|
2017-04-30 16:32:09 -04:00
|
|
|
old_line: 4,
|
|
|
|
new_line: 4,
|
|
|
|
diff_refs: merge_request_diff3.compare_with(merge_request_diff1.head_commit_sha).diff_refs
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2017-05-22 16:56:20 -04:00
|
|
|
let(:diff_note) { create(:diff_note_on_merge_request, noteable: merge_request, project: project, position: position) }
|
|
|
|
let(:discussion) { diff_note.to_discussion }
|
|
|
|
|
|
|
|
before do
|
|
|
|
diff_note.position = diff_note.original_position
|
|
|
|
diff_note.save!
|
|
|
|
end
|
2017-04-30 16:32:09 -04:00
|
|
|
|
|
|
|
it 'returns the diff version comparison path with the line code' do
|
2017-06-29 13:06:35 -04:00
|
|
|
expect(helper.discussion_path(discussion)).to eq(diffs_project_merge_request_path(project, merge_request, diff_id: merge_request_diff3, start_sha: merge_request_diff1.head_commit_sha, anchor: discussion.line_code))
|
2017-04-30 16:32:09 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the discussion does not have a merge request version' do
|
|
|
|
let(:outdated_diff_note) { create(:diff_note_on_merge_request, noteable: merge_request, project: project, diff_refs: project.commit(sample_commit.id).diff_refs) }
|
|
|
|
let(:discussion) { outdated_diff_note.to_discussion }
|
|
|
|
|
|
|
|
before do
|
|
|
|
outdated_diff_note.position = outdated_diff_note.original_position
|
|
|
|
outdated_diff_note.save!
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns nil' do
|
|
|
|
expect(helper.discussion_path(discussion)).to be_nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'for a legacy diff discussion' do
|
|
|
|
let(:discussion) { create(:legacy_diff_note_on_merge_request, noteable: merge_request, project: project).to_discussion }
|
|
|
|
|
|
|
|
context 'when the discussion is active' do
|
|
|
|
before do
|
|
|
|
allow(discussion).to receive(:active?).and_return(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns the diff path with the line code' do
|
2017-06-29 13:06:35 -04:00
|
|
|
expect(helper.discussion_path(discussion)).to eq(diffs_project_merge_request_path(project, merge_request, anchor: discussion.line_code))
|
2017-04-30 16:32:09 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the discussion is outdated' do
|
|
|
|
before do
|
|
|
|
allow(discussion).to receive(:active?).and_return(false)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns nil' do
|
|
|
|
expect(helper.discussion_path(discussion)).to be_nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'for a non-diff discussion' do
|
|
|
|
let(:discussion) { create(:discussion_note_on_merge_request, noteable: merge_request, project: project).to_discussion }
|
|
|
|
|
|
|
|
it 'returns nil' do
|
|
|
|
expect(helper.discussion_path(discussion)).to be_nil
|
|
|
|
end
|
|
|
|
end
|
2017-12-18 10:21:33 -05:00
|
|
|
|
|
|
|
context 'for a contextual commit discussion' do
|
|
|
|
let(:commit) { merge_request.commits.last }
|
|
|
|
let(:discussion) { create(:diff_note_on_merge_request, noteable: merge_request, project: project, commit_id: commit.id).to_discussion }
|
|
|
|
|
|
|
|
it 'returns the merge request diff discussion scoped in the commit' do
|
|
|
|
expect(helper.discussion_path(discussion)).to eq(diffs_project_merge_request_path(project, merge_request, commit_id: commit.id, anchor: anchor))
|
|
|
|
end
|
|
|
|
end
|
2017-04-30 16:32:09 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'for a commit discussion' do
|
|
|
|
let(:commit) { discussion.noteable }
|
|
|
|
|
|
|
|
context 'for a diff discussion' do
|
|
|
|
let(:discussion) { create(:diff_note_on_commit, project: project).to_discussion }
|
|
|
|
|
|
|
|
it 'returns the commit path with the line code' do
|
2017-12-18 10:21:33 -05:00
|
|
|
expect(helper.discussion_path(discussion)).to eq(project_commit_path(project, commit, anchor: anchor))
|
2017-04-30 16:32:09 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'for a legacy diff discussion' do
|
|
|
|
let(:discussion) { create(:legacy_diff_note_on_commit, project: project).to_discussion }
|
|
|
|
|
|
|
|
it 'returns the commit path with the line code' do
|
2017-12-18 10:21:33 -05:00
|
|
|
expect(helper.discussion_path(discussion)).to eq(project_commit_path(project, commit, anchor: anchor))
|
2017-04-30 16:32:09 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'for a non-diff discussion' do
|
|
|
|
let(:discussion) { create(:discussion_note_on_commit, project: project).to_discussion }
|
|
|
|
|
2019-01-17 06:15:33 -05:00
|
|
|
it 'returns the commit path with the note anchor' do
|
|
|
|
expect(helper.discussion_path(discussion)).to eq(project_commit_path(project, commit, anchor: "note_#{discussion.first_note.id}"))
|
2017-04-30 16:32:09 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2017-05-03 04:48:01 -04:00
|
|
|
|
|
|
|
describe '#notes_url' do
|
|
|
|
it 'return snippet notes path for personal snippet' do
|
|
|
|
@snippet = create(:personal_snippet)
|
|
|
|
|
2020-07-23 11:09:28 -04:00
|
|
|
expect(helper.notes_url).to eq("/-/snippets/#{@snippet.id}/notes")
|
2017-05-03 04:48:01 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'return project notes path for project snippet' do
|
2020-10-05 05:08:17 -04:00
|
|
|
@project = project
|
2017-05-03 04:48:01 -04:00
|
|
|
@snippet = create(:project_snippet, project: @project)
|
|
|
|
@noteable = @snippet
|
|
|
|
|
2020-10-05 05:08:17 -04:00
|
|
|
expect(helper.notes_url).to eq("/#{project.full_path}/noteable/project_snippet/#{@noteable.id}/notes")
|
2017-05-03 04:48:01 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'return project notes path for other noteables' do
|
2020-10-05 05:08:17 -04:00
|
|
|
@project = project
|
2017-05-03 04:48:01 -04:00
|
|
|
@noteable = create(:issue, project: @project)
|
|
|
|
|
2020-10-05 05:08:17 -04:00
|
|
|
expect(helper.notes_url).to eq("/#{@project.full_path}/noteable/issue/#{@noteable.id}/notes")
|
2017-05-03 04:48:01 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#note_url' do
|
|
|
|
it 'return snippet notes path for personal snippet' do
|
|
|
|
note = create(:note_on_personal_snippet)
|
|
|
|
|
2020-07-23 11:09:28 -04:00
|
|
|
expect(helper.note_url(note)).to eq("/-/snippets/#{note.noteable.id}/notes/#{note.id}")
|
2017-05-03 04:48:01 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'return project notes path for project snippet' do
|
2020-10-05 05:08:17 -04:00
|
|
|
@project = project
|
2017-05-03 04:48:01 -04:00
|
|
|
note = create(:note_on_project_snippet, project: @project)
|
|
|
|
|
2020-10-05 05:08:17 -04:00
|
|
|
expect(helper.note_url(note)).to eq("/#{project.full_path}/notes/#{note.id}")
|
2017-05-03 04:48:01 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'return project notes path for other noteables' do
|
2020-10-05 05:08:17 -04:00
|
|
|
@project = project
|
2017-05-03 04:48:01 -04:00
|
|
|
note = create(:note_on_issue, project: @project)
|
|
|
|
|
2020-10-05 05:08:17 -04:00
|
|
|
expect(helper.note_url(note)).to eq("/#{project.full_path}/notes/#{note.id}")
|
2017-05-03 04:48:01 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-09-05 11:49:05 -04:00
|
|
|
describe '#form_resources' do
|
2017-05-03 04:48:01 -04:00
|
|
|
it 'returns note for personal snippet' do
|
|
|
|
@snippet = create(:personal_snippet)
|
|
|
|
@note = create(:note_on_personal_snippet)
|
|
|
|
|
|
|
|
expect(helper.form_resources).to eq([@note])
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns namespace, project and note for project snippet' do
|
2020-10-05 05:08:17 -04:00
|
|
|
@project = project
|
2017-05-03 04:48:01 -04:00
|
|
|
@snippet = create(:project_snippet, project: @project)
|
|
|
|
@note = create(:note_on_personal_snippet)
|
|
|
|
|
2020-07-27 08:09:50 -04:00
|
|
|
expect(helper.form_resources).to eq([@project, @note])
|
2017-05-03 04:48:01 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns namespace, project and note path for other noteables' do
|
2020-10-05 05:08:17 -04:00
|
|
|
@project = project
|
2017-05-03 04:48:01 -04:00
|
|
|
@note = create(:note_on_issue, project: @project)
|
|
|
|
|
2020-07-27 08:09:50 -04:00
|
|
|
expect(helper.form_resources).to eq([@project, @note])
|
2017-05-03 04:48:01 -04:00
|
|
|
end
|
|
|
|
end
|
2017-06-07 14:33:50 -04:00
|
|
|
|
|
|
|
describe '#noteable_note_url' do
|
|
|
|
let(:issue) { create(:issue, project: project) }
|
|
|
|
let(:note) { create(:note_on_issue, noteable: issue, project: project) }
|
|
|
|
|
|
|
|
it 'returns the noteable url with an anchor to the note' do
|
2020-03-02 10:08:01 -05:00
|
|
|
expect(noteable_note_url(note)).to match("/#{project.namespace.path}/#{project.path}/-/issues/#{issue.iid}##{dom_id(note)}")
|
2017-06-07 14:33:50 -04:00
|
|
|
end
|
|
|
|
end
|
2017-09-05 11:49:05 -04:00
|
|
|
|
|
|
|
describe '#discussion_resolved_intro' do
|
|
|
|
context 'when the discussion was resolved by a push' do
|
|
|
|
let(:discussion) { double(:discussion, resolved_by_push?: true) }
|
|
|
|
|
|
|
|
it 'returns "Automatically resolved"' do
|
|
|
|
expect(discussion_resolved_intro(discussion)).to eq('Automatically resolved')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the discussion was not resolved by a push' do
|
|
|
|
let(:discussion) { double(:discussion, resolved_by_push?: false) }
|
|
|
|
|
|
|
|
it 'returns "Resolved"' do
|
|
|
|
expect(discussion_resolved_intro(discussion)).to eq('Resolved')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2021-02-04 07:09:25 -05:00
|
|
|
|
|
|
|
describe '#notes_data' do
|
|
|
|
let(:issue) { create(:issue, project: project) }
|
|
|
|
|
|
|
|
it 'sets last_fetched_at to 0 when start_at_zero is true' do
|
|
|
|
@project = project
|
|
|
|
@noteable = issue
|
|
|
|
|
|
|
|
expect(helper.notes_data(issue, true)[:lastFetchedAt]).to eq(0)
|
|
|
|
end
|
|
|
|
end
|
2016-06-28 18:14:11 -04:00
|
|
|
end
|