debian-mirror-gitlab/spec/services/notes/create_service_spec.rb

651 lines
23 KiB
Ruby
Raw Normal View History

2019-07-31 22:56:46 +05:30
# frozen_string_literal: true
2014-09-02 18:07:02 +05:30
require 'spec_helper'
2020-07-28 23:09:34 +05:30
RSpec.describe Notes::CreateService do
2020-03-13 15:44:24 +05:30
let_it_be(:project) { create(:project, :repository) }
let_it_be(:issue) { create(:issue, project: project) }
let_it_be(:user) { create(:user) }
2021-09-30 23:02:18 +05:30
2022-08-27 11:52:29 +05:30
let(:base_opts) { { note: 'Awesome comment', noteable_type: 'Issue', noteable_id: issue.id } }
let(:opts) { base_opts.merge(confidential: true) }
2014-09-02 18:07:02 +05:30
2016-08-24 12:49:21 +05:30
describe '#execute' do
2022-08-27 11:52:29 +05:30
subject(:note) { described_class.new(project, user, opts).execute }
2016-09-13 17:45:13 +05:30
before do
2018-11-18 11:00:15 +05:30
project.add_maintainer(user)
2016-09-13 17:45:13 +05:30
end
2014-09-02 18:07:02 +05:30
context "valid params" do
2017-08-17 22:00:37 +05:30
it 'returns a valid note' do
expect(note).to be_valid
2014-09-02 18:07:02 +05:30
end
2017-08-17 22:00:37 +05:30
it 'returns a persisted note' do
expect(note).to be_persisted
end
2022-08-27 11:52:29 +05:30
context 'with internal parameter' do
context 'when confidential' do
let(:opts) { base_opts.merge(internal: true) }
it 'returns a confidential note' do
expect(note).to be_confidential
end
end
context 'when not confidential' do
let(:opts) { base_opts.merge(internal: false) }
it 'returns a confidential note' do
expect(note).not_to be_confidential
end
end
end
context 'with confidential parameter' do
context 'when confidential' do
let(:opts) { base_opts.merge(confidential: true) }
it 'returns a confidential note' do
expect(note).to be_confidential
end
end
context 'when not confidential' do
let(:opts) { base_opts.merge(confidential: false) }
2017-08-17 22:00:37 +05:30
2022-08-27 11:52:29 +05:30
it 'returns a confidential note' do
expect(note).not_to be_confidential
end
end
end
context 'with confidential and internal parameter set' do
let(:opts) { base_opts.merge(internal: true, confidential: false) }
it 'prefers the internal parameter' do
expect(note).to be_confidential
end
end
it 'note has valid content' do
2017-08-17 22:00:37 +05:30
expect(note.note).to eq(opts[:note])
end
it 'note belongs to the correct project' do
expect(note.project).to eq(project)
end
it 'TodoService#new_note is called' do
2020-11-24 15:15:51 +05:30
note = build(:note, project: project, noteable: issue)
2017-08-17 22:00:37 +05:30
allow(Note).to receive(:new).with(opts) { note }
expect_any_instance_of(TodoService).to receive(:new_note).with(note, user)
described_class.new(project, user, opts).execute
end
it 'enqueues NewNoteWorker' do
2020-11-24 15:15:51 +05:30
note = build(:note, id: non_existing_record_id, project: project, noteable: issue)
2017-08-17 22:00:37 +05:30
allow(Note).to receive(:new).with(opts) { note }
expect(NewNoteWorker).to receive(:perform_async).with(note.id)
described_class.new(project, user, opts).execute
end
2020-11-24 15:15:51 +05:30
context 'issue is an incident' do
let(:issue) { create(:incident, project: project) }
it_behaves_like 'an incident management tracked event', :incident_management_incident_comment do
let(:current_user) { user }
end
end
2016-09-13 17:45:13 +05:30
2022-08-27 11:52:29 +05:30
describe 'event tracking', :snowplow do
let(:event) { Gitlab::UsageDataCounters::IssueActivityUniqueCounter::ISSUE_COMMENT_ADDED }
let(:execute_create_service) { described_class.new(project, user, opts).execute }
2018-12-13 13:39:08 +05:30
2022-08-27 11:52:29 +05:30
it 'tracks issue comment usage data', :clean_gitlab_redis_shared_state do
counter = Gitlab::UsageDataCounters::HLLRedisCounter
expect(Gitlab::UsageDataCounters::IssueActivityUniqueCounter).to receive(:track_issue_comment_added_action)
.with(author: user, project: project)
.and_call_original
expect do
execute_create_service
end.to change { counter.unique_events(event_names: event, start_date: 1.day.ago, end_date: 1.day.from_now) }.by(1)
end
2018-12-13 13:39:08 +05:30
2022-08-27 11:52:29 +05:30
it 'does not track merge request usage data' do
expect(Gitlab::UsageDataCounters::MergeRequestActivityUniqueCounter).not_to receive(:track_create_comment_action)
2021-03-08 18:12:59 +05:30
2022-08-27 11:52:29 +05:30
execute_create_service
end
it_behaves_like 'issue_edit snowplow tracking' do
let(:property) { Gitlab::UsageDataCounters::IssueActivityUniqueCounter::ISSUE_COMMENT_ADDED }
subject(:service_action) { execute_create_service }
end
2021-03-08 18:12:59 +05:30
end
2021-01-29 00:20:46 +05:30
context 'in a merge request' do
let_it_be(:project_with_repo) { create(:project, :repository) }
let_it_be(:merge_request) do
2022-10-11 01:57:18 +05:30
create(:merge_request, source_project: project_with_repo, target_project: project_with_repo)
2021-01-29 00:20:46 +05:30
end
2018-12-13 13:39:08 +05:30
2021-01-29 00:20:46 +05:30
context 'noteable highlight cache clearing' do
let(:position) do
Gitlab::Diff::Position.new(old_path: "files/ruby/popen.rb",
new_path: "files/ruby/popen.rb",
old_line: nil,
new_line: 14,
diff_refs: merge_request.diff_refs)
end
2018-12-13 13:39:08 +05:30
2021-01-29 00:20:46 +05:30
let(:new_opts) do
opts.merge(in_reply_to_discussion_id: nil,
type: 'DiffNote',
noteable_type: 'MergeRequest',
noteable_id: merge_request.id,
2022-06-21 17:19:12 +05:30
position: position.to_h,
confidential: false)
2021-01-29 00:20:46 +05:30
end
2018-12-13 13:39:08 +05:30
2021-01-29 00:20:46 +05:30
before do
allow_any_instance_of(Gitlab::Diff::Position)
.to receive(:unfolded_diff?) { true }
end
2018-12-13 13:39:08 +05:30
2021-03-08 18:12:59 +05:30
it 'does not track issue comment usage data' do
expect(Gitlab::UsageDataCounters::IssueActivityUniqueCounter).not_to receive(:track_issue_comment_added_action)
described_class.new(project_with_repo, user, new_opts).execute
end
it 'tracks merge request usage data' do
expect(Gitlab::UsageDataCounters::MergeRequestActivityUniqueCounter).to receive(:track_create_comment_action).with(note: kind_of(Note))
described_class.new(project_with_repo, user, new_opts).execute
end
2021-01-29 00:20:46 +05:30
it 'clears noteable diff cache when it was unfolded for the note position' do
expect_any_instance_of(Gitlab::Diff::HighlightCache).to receive(:clear)
2020-10-24 23:57:45 +05:30
2021-01-29 00:20:46 +05:30
described_class.new(project_with_repo, user, new_opts).execute
end
2020-10-24 23:57:45 +05:30
2021-01-29 00:20:46 +05:30
it 'does not clear cache when note is not the first of the discussion' do
prev_note =
2022-10-11 01:57:18 +05:30
create(:diff_note_on_merge_request, noteable: merge_request, project: project_with_repo)
2021-01-29 00:20:46 +05:30
reply_opts =
opts.merge(in_reply_to_discussion_id: prev_note.discussion_id,
type: 'DiffNote',
noteable_type: 'MergeRequest',
noteable_id: merge_request.id,
2022-06-21 17:19:12 +05:30
position: position.to_h,
confidential: false)
2018-11-08 19:23:39 +05:30
2021-01-29 00:20:46 +05:30
expect(merge_request).not_to receive(:diffs)
2019-02-02 18:00:53 +05:30
2021-01-29 00:20:46 +05:30
described_class.new(project_with_repo, user, reply_opts).execute
end
2018-11-08 19:23:39 +05:30
end
2021-01-29 00:20:46 +05:30
context 'note diff file' do
let(:line_number) { 14 }
let(:position) do
Gitlab::Diff::Position.new(old_path: "files/ruby/popen.rb",
new_path: "files/ruby/popen.rb",
old_line: nil,
new_line: line_number,
diff_refs: merge_request.diff_refs)
end
2018-11-08 19:23:39 +05:30
2021-01-29 00:20:46 +05:30
let(:previous_note) do
create(:diff_note_on_merge_request, noteable: merge_request, project: project_with_repo)
end
2018-11-08 19:23:39 +05:30
2021-01-29 00:20:46 +05:30
before do
project_with_repo.add_maintainer(user)
end
context 'when eligible to have a note diff file' do
let(:new_opts) do
opts.merge(in_reply_to_discussion_id: nil,
type: 'DiffNote',
noteable_type: 'MergeRequest',
noteable_id: merge_request.id,
2022-06-21 17:19:12 +05:30
position: position.to_h,
confidential: false)
2021-01-29 00:20:46 +05:30
end
2018-11-08 19:23:39 +05:30
2021-01-29 00:20:46 +05:30
it 'note is associated with a note diff file' do
2021-06-08 01:23:25 +05:30
MergeRequests::MergeToRefService.new(project: merge_request.project, current_user: merge_request.author).execute(merge_request)
2018-11-08 19:23:39 +05:30
2021-01-29 00:20:46 +05:30
note = described_class.new(project_with_repo, user, new_opts).execute
2018-11-08 19:23:39 +05:30
2021-01-29 00:20:46 +05:30
expect(note).to be_persisted
expect(note.note_diff_file).to be_present
expect(note.diff_note_positions).to be_present
end
2021-10-27 15:23:28 +05:30
context 'when skip_capture_diff_note_position execute option is set to true' do
it 'does not execute Discussions::CaptureDiffNotePositionService' do
expect(Discussions::CaptureDiffNotePositionService).not_to receive(:new)
described_class.new(project_with_repo, user, new_opts).execute(skip_capture_diff_note_position: true)
end
end
2022-07-16 23:28:13 +05:30
it 'does not track ipynb note usage data' do
expect(::Gitlab::UsageDataCounters::IpynbDiffActivityCounter).not_to receive(:note_created)
described_class.new(project_with_repo, user, new_opts).execute
end
context 'is ipynb file' do
before do
allow_any_instance_of(::Gitlab::Diff::File).to receive(:ipynb?).and_return(true)
stub_feature_flags(ipynbdiff_notes_tracker: false)
end
context ':ipynbdiff_notes_tracker is off' do
it 'does not track ipynb note usage data' do
expect(::Gitlab::UsageDataCounters::IpynbDiffActivityCounter).not_to receive(:note_created)
described_class.new(project_with_repo, user, new_opts).execute
end
end
context ':ipynbdiff_notes_tracker is on' do
before do
stub_feature_flags(ipynbdiff_notes_tracker: true)
end
it 'tracks ipynb diff note creation' do
expect(::Gitlab::UsageDataCounters::IpynbDiffActivityCounter).to receive(:note_created)
described_class.new(project_with_repo, user, new_opts).execute
end
end
end
2018-11-08 19:23:39 +05:30
end
2021-01-29 00:20:46 +05:30
context 'when DiffNote is a reply' do
let(:new_opts) do
opts.merge(in_reply_to_discussion_id: previous_note.discussion_id,
type: 'DiffNote',
noteable_type: 'MergeRequest',
noteable_id: merge_request.id,
2022-06-21 17:19:12 +05:30
position: position.to_h,
confidential: false)
2021-01-29 00:20:46 +05:30
end
it 'note is not associated with a note diff file' do
expect(Discussions::CaptureDiffNotePositionService).not_to receive(:new)
note = described_class.new(project_with_repo, user, new_opts).execute
expect(note).to be_persisted
expect(note.note_diff_file).to be_nil
end
context 'when DiffNote from an image' do
let(:image_position) do
Gitlab::Diff::Position.new(old_path: "files/images/6049019_460s.jpg",
new_path: "files/images/6049019_460s.jpg",
width: 100,
height: 100,
x: 1,
y: 100,
diff_refs: merge_request.diff_refs,
position_type: 'image')
end
let(:new_opts) do
opts.merge(in_reply_to_discussion_id: nil,
type: 'DiffNote',
noteable_type: 'MergeRequest',
noteable_id: merge_request.id,
2022-06-21 17:19:12 +05:30
position: image_position.to_h,
confidential: false)
2021-01-29 00:20:46 +05:30
end
it 'note is not associated with a note diff file' do
note = described_class.new(project_with_repo, user, new_opts).execute
expect(note).to be_persisted
expect(note.note_diff_file).to be_nil
end
end
2018-11-08 19:23:39 +05:30
end
end
end
end
2018-03-27 19:54:05 +05:30
context 'note with commands' do
2019-09-30 21:07:59 +05:30
context 'all quick actions' do
2020-03-13 15:44:24 +05:30
let_it_be(:milestone) { create(:milestone, project: project, title: "sprint") }
let_it_be(:bug_label) { create(:label, project: project, title: 'bug') }
let_it_be(:to_be_copied_label) { create(:label, project: project, title: 'to be copied') }
let_it_be(:feature_label) { create(:label, project: project, title: 'feature') }
let_it_be(:issue, reload: true) { create(:issue, project: project, labels: [bug_label], due_date: '2019-01-01') }
let_it_be(:issue_2) { create(:issue, project: project, labels: [bug_label, to_be_copied_label]) }
2019-09-30 21:07:59 +05:30
context 'for issues' do
let(:issuable) { issue }
let(:note_params) { opts }
let(:issue_quick_actions) do
[
QuickAction.new(
action_text: '/confidential',
expectation: ->(noteable, can_use_quick_action) {
if can_use_quick_action
expect(noteable).to be_confidential
else
expect(noteable).not_to be_confidential
end
}
),
QuickAction.new(
action_text: '/due 2016-08-28',
expectation: ->(noteable, can_use_quick_action) {
expect(noteable.due_date == Date.new(2016, 8, 28)).to eq(can_use_quick_action)
}
),
QuickAction.new(
action_text: '/remove_due_date',
expectation: ->(noteable, can_use_quick_action) {
if can_use_quick_action
expect(noteable.due_date).to be_nil
else
expect(noteable.due_date).not_to be_nil
end
}
),
QuickAction.new(
action_text: "/duplicate #{issue_2.to_reference}",
before_action: -> {
issuable.reopen
},
expectation: ->(noteable, can_use_quick_action) {
expect(noteable.closed?).to eq(can_use_quick_action)
}
)
]
2018-03-27 19:54:05 +05:30
end
2019-09-30 21:07:59 +05:30
it_behaves_like 'issuable quick actions' do
let(:quick_actions) { issuable_quick_actions + issue_quick_actions }
2018-03-27 19:54:05 +05:30
end
2016-09-13 17:45:13 +05:30
end
2019-07-07 11:18:12 +05:30
2019-09-30 21:07:59 +05:30
context 'for merge requests' do
2020-03-13 15:44:24 +05:30
let_it_be(:merge_request) { create(:merge_request, source_project: project, labels: [bug_label]) }
2021-09-30 23:02:18 +05:30
2019-09-30 21:07:59 +05:30
let(:issuable) { merge_request }
2022-06-21 17:19:12 +05:30
let(:note_params) { opts.merge(noteable_type: 'MergeRequest', noteable_id: merge_request.id, confidential: false) }
2019-09-30 21:07:59 +05:30
let(:merge_request_quick_actions) do
[
QuickAction.new(
action_text: "/target_branch fix",
expectation: ->(noteable, can_use_quick_action) {
expect(noteable.target_branch == "fix").to eq(can_use_quick_action)
}
),
# Set WIP status
QuickAction.new(
2021-09-04 01:27:46 +05:30
action_text: "/draft",
2019-09-30 21:07:59 +05:30
before_action: -> {
2021-01-29 00:20:46 +05:30
issuable.reload.update!(title: "title")
2019-09-30 21:07:59 +05:30
},
expectation: ->(issuable, can_use_quick_action) {
2022-07-23 23:45:48 +05:30
expect(issuable.draft?).to eq(can_use_quick_action)
2019-09-30 21:07:59 +05:30
}
),
2022-10-11 01:57:18 +05:30
# Remove draft (set ready) status
2019-09-30 21:07:59 +05:30
QuickAction.new(
2022-10-11 01:57:18 +05:30
action_text: "/ready",
2019-09-30 21:07:59 +05:30
before_action: -> {
2022-04-04 11:22:00 +05:30
issuable.reload.update!(title: "Draft: title")
2019-09-30 21:07:59 +05:30
},
expectation: ->(noteable, can_use_quick_action) {
2022-07-23 23:45:48 +05:30
expect(noteable.draft?).not_to eq(can_use_quick_action)
2019-09-30 21:07:59 +05:30
}
)
]
end
2019-07-07 11:18:12 +05:30
2019-09-30 21:07:59 +05:30
it_behaves_like 'issuable quick actions' do
let(:quick_actions) { issuable_quick_actions + merge_request_quick_actions }
2019-07-07 11:18:12 +05:30
end
end
2016-09-13 17:45:13 +05:30
end
2015-11-26 14:37:03 +05:30
2019-09-30 21:07:59 +05:30
context 'when note only have commands' do
it 'adds commands applied message to note errors' do
note_text = %(/close)
service = double(:service)
allow(Issues::UpdateService).to receive(:new).and_return(service)
expect(service).to receive(:execute)
2018-03-27 19:54:05 +05:30
2019-09-30 21:07:59 +05:30
note = described_class.new(project, user, opts.merge(note: note_text)).execute
2015-11-26 14:37:03 +05:30
2019-09-30 21:07:59 +05:30
expect(note.errors[:commands_only]).to be_present
2017-08-17 22:00:37 +05:30
end
2021-04-29 21:17:54 +05:30
it 'adds commands failed message to note errors' do
note_text = %(/reopen)
note = described_class.new(project, user, opts.merge(note: note_text)).execute
expect(note.errors[:commands_only]).to contain_exactly('Could not apply reopen command.')
end
it 'generates success and failed error messages' do
note_text = %(/close\n/reopen)
service = double(:service)
allow(Issues::UpdateService).to receive(:new).and_return(service)
expect(service).to receive(:execute)
note = described_class.new(project, user, opts.merge(note: note_text)).execute
expect(note.errors[:commands_only]).to contain_exactly('Closed this issue. Could not apply reopen command.')
end
2017-08-17 22:00:37 +05:30
end
2015-11-26 14:37:03 +05:30
end
2018-03-27 19:54:05 +05:30
context 'personal snippet note' do
2017-08-17 22:00:37 +05:30
subject { described_class.new(nil, user, params).execute }
2017-08-17 22:00:37 +05:30
let(:snippet) { create(:personal_snippet) }
let(:params) do
{ note: 'comment', noteable_type: 'Snippet', noteable_id: snippet.id }
end
it 'returns a valid note' do
expect(subject).to be_valid
end
2017-08-17 22:00:37 +05:30
it 'returns a persisted note' do
expect(subject).to be_persisted
end
2015-11-26 14:37:03 +05:30
2017-08-17 22:00:37 +05:30
it 'note has valid content' do
expect(subject.note).to eq(params[:note])
end
end
2015-11-26 14:37:03 +05:30
2020-05-24 23:13:21 +05:30
context 'design note' do
subject(:service) { described_class.new(project, user, params) }
let_it_be(:design) { create(:design, :with_file) }
let_it_be(:project) { design.project }
2022-03-02 08:16:31 +05:30
let_it_be(:user) { project.first_owner }
2020-05-24 23:13:21 +05:30
let_it_be(:params) do
{
type: 'DiffNote',
noteable: design,
note: "A message",
position: {
old_path: design.full_path,
new_path: design.full_path,
position_type: 'image',
width: '100',
height: '100',
x: '50',
y: '50',
base_sha: design.diff_refs.base_sha,
start_sha: design.diff_refs.base_sha,
head_sha: design.diff_refs.head_sha
}
}
end
it 'can create diff notes for designs' do
note = service.execute
expect(note).to be_a(DiffNote)
expect(note).to be_persisted
expect(note.noteable).to eq(design)
end
it 'sends a notification about this note', :sidekiq_might_not_need_inline do
notifier = double
allow(::NotificationService).to receive(:new).and_return(notifier)
expect(notifier)
.to receive(:new_note)
.with have_attributes(noteable: design)
service.execute
end
it 'correctly builds the position of the note' do
note = service.execute
expect(note.position.new_path).to eq(design.full_path)
expect(note.position.old_path).to eq(design.full_path)
expect(note.position.diff_refs).to eq(design.diff_refs)
end
end
2018-03-27 19:54:05 +05:30
context 'note with emoji only' do
2017-08-17 22:00:37 +05:30
it 'creates regular note' do
opts = {
note: ':smile: ',
noteable_type: 'Issue',
noteable_id: issue.id
}
note = described_class.new(project, user, opts).execute
expect(note).to be_valid
expect(note.note).to eq(':smile:')
end
2015-11-26 14:37:03 +05:30
end
2019-03-02 22:35:43 +05:30
context 'reply to individual note' do
let(:existing_note) { create(:note_on_issue, noteable: issue, project: project) }
let(:reply_opts) { opts.merge(in_reply_to_discussion_id: existing_note.discussion_id) }
subject { described_class.new(project, user, reply_opts).execute }
2019-07-07 11:18:12 +05:30
it 'creates a DiscussionNote in reply to existing note' do
expect(subject).to be_a(DiscussionNote)
expect(subject.discussion_id).to eq(existing_note.discussion_id)
2019-03-02 22:35:43 +05:30
end
2019-07-07 11:18:12 +05:30
it 'converts existing note to DiscussionNote' do
expect do
existing_note
2019-03-02 22:35:43 +05:30
2021-01-03 14:25:43 +05:30
travel_to(Time.current + 1.minute) { subject }
2019-03-02 22:35:43 +05:30
2019-07-07 11:18:12 +05:30
existing_note.reload
end.to change { existing_note.type }.from(nil).to('DiscussionNote')
.and change { existing_note.updated_at }
2019-03-02 22:35:43 +05:30
end
2020-01-01 13:55:28 +05:30
2021-03-11 19:13:27 +05:30
context 'failure in when_saved' do
let(:service) { described_class.new(project, user, reply_opts) }
it 'converts existing note to DiscussionNote' do
expect do
existing_note
allow(service).to receive(:when_saved).and_raise(ActiveRecord::StatementInvalid)
travel_to(Time.current + 1.minute) do
service.execute
rescue ActiveRecord::StatementInvalid
end
existing_note.reload
end.to change { existing_note.type }.from(nil).to('DiscussionNote')
.and change { existing_note.updated_at }
end
end
2020-10-24 23:57:45 +05:30
it 'returns a DiscussionNote with its parent discussion refreshed correctly' do
discussion_notes = subject.discussion.notes
expect(discussion_notes.size).to eq(2)
expect(discussion_notes.first).to be_a(DiscussionNote)
end
2020-01-01 13:55:28 +05:30
context 'discussion to reply cannot be found' do
before do
existing_note.delete
end
it 'returns an note with errors' do
note = subject
expect(note.errors).not_to be_empty
expect(note.errors[:base]).to eq(['Discussion to reply to cannot be found'])
end
end
2019-03-02 22:35:43 +05:30
end
2019-10-12 21:52:04 +05:30
describe "usage counter" do
let(:counter) { Gitlab::UsageDataCounters::NoteCounter }
context 'snippet note' do
let(:snippet) { create(:project_snippet, project: project) }
let(:opts) { { note: 'reply', noteable_type: 'Snippet', noteable_id: snippet.id, project: project } }
it 'increments usage counter' do
expect do
note = described_class.new(project, user, opts).execute
expect(note).to be_valid
end.to change { counter.read(:create, opts[:noteable_type]) }.by 1
end
it 'does not increment usage counter when creation fails' do
expect do
note = described_class.new(project, user, { note: '' }).execute
expect(note).to be_invalid
end.not_to change { counter.read(:create, opts[:noteable_type]) }
end
end
context 'issue note' do
let(:issue) { create(:issue, project: project) }
let(:opts) { { note: 'reply', noteable_type: 'Issue', noteable_id: issue.id, project: project } }
it 'does not increment usage counter' do
expect do
note = described_class.new(project, user, opts).execute
expect(note).to be_valid
end.not_to change { counter.read(:create, opts[:noteable_type]) }
end
end
end
2015-11-26 14:37:03 +05:30
end
2014-09-02 18:07:02 +05:30
end