2019-07-31 22:56:46 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
require 'spec_helper'
|
|
|
|
|
2023-05-27 22:25:52 +05:30
|
|
|
RSpec.describe Notes::BuildService, feature_category: :team_planning do
|
2021-04-17 20:07:23 +05:30
|
|
|
include AdminModeHelper
|
|
|
|
|
2022-06-21 17:19:12 +05:30
|
|
|
let_it_be(:project) { create(:project, :repository) }
|
|
|
|
let_it_be(:note) { create(:discussion_note_on_issue, project: project) }
|
|
|
|
let_it_be(:author) { note.author }
|
|
|
|
let_it_be(:user) { author }
|
|
|
|
let_it_be(:noteable_author) { create(:user) }
|
|
|
|
let_it_be(:other_user) { create(:user) }
|
|
|
|
let_it_be(:external) { create(:user, :external) }
|
|
|
|
|
2021-04-17 20:07:23 +05:30
|
|
|
let(:base_params) { { note: 'Test' } }
|
|
|
|
let(:params) { {} }
|
|
|
|
|
|
|
|
subject(:new_note) { described_class.new(project, user, base_params.merge(params)).execute }
|
2017-08-17 22:00:37 +05:30
|
|
|
|
|
|
|
describe '#execute' do
|
|
|
|
context 'when in_reply_to_discussion_id is specified' do
|
2021-04-17 20:07:23 +05:30
|
|
|
let(:params) { { in_reply_to_discussion_id: note.discussion_id } }
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
context 'when a note with that original discussion ID exists' do
|
|
|
|
it 'sets the note up to be in reply to that note' do
|
|
|
|
expect(new_note).to be_valid
|
|
|
|
expect(new_note.in_reply_to?(note)).to be_truthy
|
2018-12-05 23:21:45 +05:30
|
|
|
expect(new_note.resolved?).to be_falsey
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when discussion is resolved' do
|
2022-06-21 17:19:12 +05:30
|
|
|
let_it_be(:merge_request) { create(:merge_request, source_project: project) }
|
|
|
|
let_it_be(:mr_note) { create(:discussion_note_on_merge_request, :resolved, noteable: merge_request, project: project, author: author) }
|
2021-04-17 20:07:23 +05:30
|
|
|
|
2022-06-21 17:19:12 +05:30
|
|
|
let(:params) { { in_reply_to_discussion_id: mr_note.discussion_id } }
|
2018-12-05 23:21:45 +05:30
|
|
|
|
|
|
|
it 'resolves the note' do
|
|
|
|
expect(new_note).to be_valid
|
|
|
|
expect(new_note.resolved?).to be_truthy
|
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when a note with that discussion ID exists' do
|
|
|
|
it 'sets the note up to be in reply to that note' do
|
|
|
|
expect(new_note).to be_valid
|
|
|
|
expect(new_note.in_reply_to?(note)).to be_truthy
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when no note with that discussion ID exists' do
|
2021-04-17 20:07:23 +05:30
|
|
|
let(:params) { { in_reply_to_discussion_id: 'foo' } }
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
it 'sets an error' do
|
|
|
|
expect(new_note.errors[:base]).to include('Discussion to reply to cannot be found')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-02-02 18:00:53 +05:30
|
|
|
context 'when user has no access to discussion' do
|
2022-06-21 17:19:12 +05:30
|
|
|
let(:user) { other_user }
|
2019-02-02 18:00:53 +05:30
|
|
|
|
2021-04-17 20:07:23 +05:30
|
|
|
it 'sets an error' do
|
2019-02-02 18:00:53 +05:30
|
|
|
expect(new_note.errors[:base]).to include('Discussion to reply to cannot be found')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
context 'personal snippet note' do
|
2022-06-21 17:19:12 +05:30
|
|
|
def reply(note, user = other_user)
|
2017-08-17 22:00:37 +05:30
|
|
|
described_class.new(nil,
|
|
|
|
user,
|
|
|
|
note: 'Test',
|
|
|
|
in_reply_to_discussion_id: note.discussion_id).execute
|
|
|
|
end
|
|
|
|
|
2022-06-21 17:19:12 +05:30
|
|
|
let_it_be(:snippet_author) { noteable_author }
|
2017-08-17 22:00:37 +05:30
|
|
|
|
|
|
|
context 'when a snippet is public' do
|
|
|
|
it 'creates a reply note' do
|
|
|
|
snippet = create(:personal_snippet, :public)
|
|
|
|
note = create(:discussion_note_on_personal_snippet, noteable: snippet)
|
|
|
|
|
|
|
|
new_note = reply(note)
|
|
|
|
|
|
|
|
expect(new_note).to be_valid
|
|
|
|
expect(new_note.in_reply_to?(note)).to be_truthy
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when a snippet is private' do
|
2022-06-21 17:19:12 +05:30
|
|
|
let_it_be(:snippet) { create(:personal_snippet, :private, author: snippet_author) }
|
|
|
|
let_it_be(:note) { create(:discussion_note_on_personal_snippet, noteable: snippet) }
|
2017-08-17 22:00:37 +05:30
|
|
|
|
|
|
|
it 'creates a reply note when the author replies' do
|
|
|
|
new_note = reply(note, snippet_author)
|
|
|
|
|
|
|
|
expect(new_note).to be_valid
|
|
|
|
expect(new_note.in_reply_to?(note)).to be_truthy
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'sets an error when another user replies' do
|
|
|
|
new_note = reply(note)
|
|
|
|
|
|
|
|
expect(new_note.errors[:base]).to include('Discussion to reply to cannot be found')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when a snippet is internal' do
|
2022-06-21 17:19:12 +05:30
|
|
|
let_it_be(:snippet) { create(:personal_snippet, :internal, author: snippet_author) }
|
|
|
|
let_it_be(:note) { create(:discussion_note_on_personal_snippet, noteable: snippet) }
|
2017-08-17 22:00:37 +05:30
|
|
|
|
|
|
|
it 'creates a reply note when the author replies' do
|
|
|
|
new_note = reply(note, snippet_author)
|
|
|
|
|
|
|
|
expect(new_note).to be_valid
|
|
|
|
expect(new_note.in_reply_to?(note)).to be_truthy
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'creates a reply note when a regular user replies' do
|
|
|
|
new_note = reply(note)
|
|
|
|
|
|
|
|
expect(new_note).to be_valid
|
|
|
|
expect(new_note.in_reply_to?(note)).to be_truthy
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'sets an error when an external user replies' do
|
2022-06-21 17:19:12 +05:30
|
|
|
new_note = reply(note, external)
|
2017-08-17 22:00:37 +05:30
|
|
|
|
|
|
|
expect(new_note.errors[:base]).to include('Discussion to reply to cannot be found')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-03-02 22:35:43 +05:30
|
|
|
context 'when replying to individual note' do
|
2022-06-21 17:19:12 +05:30
|
|
|
let_it_be(:note) { create(:note_on_issue, project: project) }
|
|
|
|
|
2021-04-17 20:07:23 +05:30
|
|
|
let(:params) { { in_reply_to_discussion_id: note.discussion_id } }
|
2019-03-02 22:35:43 +05:30
|
|
|
|
2019-07-07 11:18:12 +05:30
|
|
|
it 'sets the note up to be in reply to that note' do
|
2021-04-17 20:07:23 +05:30
|
|
|
expect(new_note).to be_valid
|
|
|
|
expect(new_note).to be_a(DiscussionNote)
|
|
|
|
expect(new_note.discussion_id).to eq(note.discussion_id)
|
2019-03-02 22:35:43 +05:30
|
|
|
end
|
2019-05-30 16:15:17 +05:30
|
|
|
|
2019-07-07 11:18:12 +05:30
|
|
|
context 'when noteable does not support replies' do
|
2022-06-21 17:19:12 +05:30
|
|
|
let_it_be(:note) { create(:note_on_commit, project: project) }
|
2019-05-30 16:15:17 +05:30
|
|
|
|
2019-07-07 11:18:12 +05:30
|
|
|
it 'builds another individual note' do
|
2021-04-17 20:07:23 +05:30
|
|
|
expect(new_note).to be_valid
|
|
|
|
expect(new_note).to be_a(Note)
|
|
|
|
expect(new_note.discussion_id).not_to eq(note.discussion_id)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'confidential comments' do
|
2022-06-21 17:19:12 +05:30
|
|
|
let_it_be(:project) { create(:project, :public) }
|
|
|
|
let_it_be(:guest) { create(:user) }
|
|
|
|
let_it_be(:reporter) { create(:user) }
|
|
|
|
let_it_be(:admin) { create(:admin) }
|
|
|
|
let_it_be(:issuable_assignee) { other_user }
|
|
|
|
let_it_be(:issue) do
|
|
|
|
create(:issue, project: project, author: noteable_author, assignees: [issuable_assignee])
|
|
|
|
end
|
|
|
|
|
2021-04-17 20:07:23 +05:30
|
|
|
before do
|
2022-06-21 17:19:12 +05:30
|
|
|
project.add_guest(guest)
|
|
|
|
project.add_reporter(reporter)
|
2021-04-17 20:07:23 +05:30
|
|
|
end
|
|
|
|
|
2022-06-21 17:19:12 +05:30
|
|
|
context 'when creating a new confidential comment' do
|
2022-08-27 11:52:29 +05:30
|
|
|
let(:params) { { internal: true, noteable: issue } }
|
2021-04-17 20:07:23 +05:30
|
|
|
|
2022-06-21 17:19:12 +05:30
|
|
|
shared_examples 'user allowed to set comment as confidential' do
|
|
|
|
it { expect(new_note.confidential).to be_truthy }
|
2021-04-17 20:07:23 +05:30
|
|
|
end
|
|
|
|
|
2022-06-21 17:19:12 +05:30
|
|
|
shared_examples 'user not allowed to set comment as confidential' do
|
|
|
|
it { expect(new_note.confidential).to be_falsey }
|
|
|
|
end
|
2021-04-17 20:07:23 +05:30
|
|
|
|
2022-06-21 17:19:12 +05:30
|
|
|
context 'reporter' do
|
|
|
|
let(:user) { reporter }
|
|
|
|
|
|
|
|
it_behaves_like 'user allowed to set comment as confidential'
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'issuable author' do
|
|
|
|
let(:user) { noteable_author }
|
|
|
|
|
2023-01-13 00:05:48 +05:30
|
|
|
it_behaves_like 'user not allowed to set comment as confidential'
|
2022-06-21 17:19:12 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
context 'issuable assignee' do
|
|
|
|
let(:user) { issuable_assignee }
|
|
|
|
|
2023-01-13 00:05:48 +05:30
|
|
|
it_behaves_like 'user not allowed to set comment as confidential'
|
2022-06-21 17:19:12 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
context 'admin' do
|
|
|
|
before do
|
|
|
|
enable_admin_mode!(admin)
|
2021-04-17 20:07:23 +05:30
|
|
|
end
|
2022-06-21 17:19:12 +05:30
|
|
|
|
|
|
|
let(:user) { admin }
|
|
|
|
|
|
|
|
it_behaves_like 'user allowed to set comment as confidential'
|
2021-04-17 20:07:23 +05:30
|
|
|
end
|
|
|
|
|
2022-06-21 17:19:12 +05:30
|
|
|
context 'external' do
|
|
|
|
let(:user) { external }
|
2021-04-17 20:07:23 +05:30
|
|
|
|
2022-06-21 17:19:12 +05:30
|
|
|
it_behaves_like 'user not allowed to set comment as confidential'
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'guest' do
|
|
|
|
let(:user) { guest }
|
|
|
|
|
|
|
|
it_behaves_like 'user not allowed to set comment as confidential'
|
2021-04-17 20:07:23 +05:30
|
|
|
end
|
2022-08-27 11:52:29 +05:30
|
|
|
|
|
|
|
context 'when using the deprecated `confidential` parameter' do
|
|
|
|
let(:params) { { internal: true, noteable: issue } }
|
|
|
|
|
|
|
|
shared_examples 'user allowed to set comment as confidential' do
|
|
|
|
it { expect(new_note.confidential).to be_truthy }
|
|
|
|
end
|
|
|
|
end
|
2021-04-17 20:07:23 +05:30
|
|
|
end
|
|
|
|
|
2022-06-21 17:19:12 +05:30
|
|
|
context 'when replying to a confidential comment' do
|
|
|
|
let_it_be(:note) { create(:note_on_issue, confidential: true, noteable: issue, project: project) }
|
2021-04-17 20:07:23 +05:30
|
|
|
|
2022-06-21 17:19:12 +05:30
|
|
|
let(:params) { { in_reply_to_discussion_id: note.discussion_id, confidential: false } }
|
2021-04-17 20:07:23 +05:30
|
|
|
|
2022-06-21 17:19:12 +05:30
|
|
|
shared_examples 'returns `Discussion to reply to cannot be found` error' do
|
|
|
|
it do
|
|
|
|
expect(new_note.errors.added?(:base, "Discussion to reply to cannot be found")).to be true
|
|
|
|
end
|
|
|
|
end
|
2021-04-17 20:07:23 +05:30
|
|
|
|
2022-06-21 17:19:12 +05:30
|
|
|
shared_examples 'confidential set to `true`' do
|
|
|
|
it '`confidential` param is ignored to match the parent note confidentiality' do
|
|
|
|
expect(new_note.confidential).to be_truthy
|
2021-04-17 20:07:23 +05:30
|
|
|
end
|
2022-06-21 17:19:12 +05:30
|
|
|
end
|
2021-04-17 20:07:23 +05:30
|
|
|
|
2022-06-21 17:19:12 +05:30
|
|
|
context 'with reporter access' do
|
|
|
|
let(:user) { reporter }
|
|
|
|
|
|
|
|
it_behaves_like 'confidential set to `true`'
|
|
|
|
end
|
2021-04-17 20:07:23 +05:30
|
|
|
|
2022-06-21 17:19:12 +05:30
|
|
|
context 'with admin access' do
|
|
|
|
let(:user) { admin }
|
2021-04-17 20:07:23 +05:30
|
|
|
|
2022-06-21 17:19:12 +05:30
|
|
|
before do
|
|
|
|
enable_admin_mode!(admin)
|
2021-04-17 20:07:23 +05:30
|
|
|
end
|
|
|
|
|
2022-06-21 17:19:12 +05:30
|
|
|
it_behaves_like 'confidential set to `true`'
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with noteable author' do
|
|
|
|
let(:user) { note.noteable.author }
|
2021-04-17 20:07:23 +05:30
|
|
|
|
2023-01-13 00:05:48 +05:30
|
|
|
it_behaves_like 'returns `Discussion to reply to cannot be found` error'
|
2021-04-17 20:07:23 +05:30
|
|
|
end
|
|
|
|
|
2022-06-21 17:19:12 +05:30
|
|
|
context 'with noteable assignee' do
|
|
|
|
let(:user) { issuable_assignee }
|
2021-04-17 20:07:23 +05:30
|
|
|
|
2023-01-13 00:05:48 +05:30
|
|
|
it_behaves_like 'returns `Discussion to reply to cannot be found` error'
|
2022-06-21 17:19:12 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
context 'with guest access' do
|
|
|
|
let(:user) { guest }
|
|
|
|
|
|
|
|
it_behaves_like 'returns `Discussion to reply to cannot be found` error'
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with external user' do
|
|
|
|
let(:user) { external }
|
|
|
|
|
|
|
|
it_behaves_like 'returns `Discussion to reply to cannot be found` error'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when replying to a public comment' do
|
|
|
|
let_it_be(:note) { create(:note_on_issue, confidential: false, noteable: issue, project: project) }
|
|
|
|
|
|
|
|
let(:params) { { in_reply_to_discussion_id: note.discussion_id, confidential: true } }
|
|
|
|
|
|
|
|
it '`confidential` param is ignored and set to `false`' do
|
|
|
|
expect(new_note.confidential).to be_falsey
|
2019-05-30 16:15:17 +05:30
|
|
|
end
|
|
|
|
end
|
2019-03-02 22:35:43 +05:30
|
|
|
end
|
|
|
|
|
2021-04-17 20:07:23 +05:30
|
|
|
context 'when noteable is not set' do
|
|
|
|
let(:params) { { noteable_type: note.noteable_type, noteable_id: note.noteable_id } }
|
|
|
|
|
|
|
|
it 'builds a note without saving it' do
|
|
|
|
expect(new_note).to be_valid
|
|
|
|
expect(new_note).not_to be_persisted
|
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|