2014-09-02 18:07:02 +05:30
|
|
|
require 'spec_helper'
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
describe Notes::CreateService do
|
|
|
|
let(:project) { create(:project) }
|
2014-09-02 18:07:02 +05:30
|
|
|
let(:issue) { create(:issue, project: project) }
|
|
|
|
let(:user) { create(:user) }
|
2016-09-13 17:45:13 +05:30
|
|
|
let(:opts) do
|
|
|
|
{ note: 'Awesome comment', noteable_type: 'Issue', noteable_id: issue.id }
|
|
|
|
end
|
2014-09-02 18:07:02 +05:30
|
|
|
|
2016-08-24 12:49:21 +05:30
|
|
|
describe '#execute' do
|
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
|
|
|
|
note = described_class.new(project, user, opts).execute
|
|
|
|
|
|
|
|
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
|
|
|
|
note = described_class.new(project, user, opts).execute
|
|
|
|
|
|
|
|
expect(note).to be_persisted
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'note has valid content' do
|
|
|
|
note = described_class.new(project, user, opts).execute
|
|
|
|
|
|
|
|
expect(note.note).to eq(opts[:note])
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'note belongs to the correct project' do
|
|
|
|
note = described_class.new(project, user, opts).execute
|
|
|
|
|
|
|
|
expect(note.project).to eq(project)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'TodoService#new_note is called' do
|
|
|
|
note = build(:note, project: project)
|
|
|
|
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
|
|
|
|
note = build(:note, id: 999, project: project)
|
|
|
|
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
|
2016-09-13 17:45:13 +05:30
|
|
|
end
|
|
|
|
|
2018-12-13 13:39:08 +05:30
|
|
|
context 'noteable highlight cache clearing' do
|
|
|
|
let(:project_with_repo) { create(:project, :repository) }
|
|
|
|
let(:merge_request) do
|
|
|
|
create(:merge_request, source_project: project_with_repo,
|
|
|
|
target_project: project_with_repo)
|
|
|
|
end
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
let(:new_opts) do
|
|
|
|
opts.merge(in_reply_to_discussion_id: nil,
|
|
|
|
type: 'DiffNote',
|
|
|
|
noteable_type: 'MergeRequest',
|
|
|
|
noteable_id: merge_request.id,
|
|
|
|
position: position.to_h)
|
|
|
|
end
|
|
|
|
|
|
|
|
before do
|
|
|
|
allow_any_instance_of(Gitlab::Diff::Position)
|
|
|
|
.to receive(:unfolded_diff?) { true }
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'clears noteable diff cache when it was unfolded for the note position' do
|
|
|
|
expect_any_instance_of(Gitlab::Diff::HighlightCache).to receive(:clear)
|
|
|
|
|
|
|
|
described_class.new(project_with_repo, user, new_opts).execute
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not clear cache when note is not the first of the discussion' do
|
|
|
|
prev_note =
|
|
|
|
create(:diff_note_on_merge_request, noteable: merge_request,
|
|
|
|
project: project_with_repo)
|
|
|
|
reply_opts =
|
|
|
|
opts.merge(in_reply_to_discussion_id: prev_note.discussion_id,
|
|
|
|
type: 'DiffNote',
|
|
|
|
noteable_type: 'MergeRequest',
|
|
|
|
noteable_id: merge_request.id,
|
|
|
|
position: position.to_h)
|
|
|
|
|
|
|
|
expect(merge_request).not_to receive(:diffs)
|
|
|
|
|
|
|
|
described_class.new(project_with_repo, user, reply_opts).execute
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-11-08 19:23:39 +05:30
|
|
|
context 'note diff file' do
|
|
|
|
let(:project_with_repo) { create(:project, :repository) }
|
|
|
|
let(:merge_request) do
|
|
|
|
create(:merge_request,
|
|
|
|
source_project: project_with_repo,
|
|
|
|
target_project: project_with_repo)
|
|
|
|
end
|
|
|
|
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
|
|
|
|
let(:previous_note) do
|
|
|
|
create(:diff_note_on_merge_request, noteable: merge_request, project: project_with_repo)
|
|
|
|
end
|
|
|
|
|
2019-02-02 18:00:53 +05:30
|
|
|
before do
|
|
|
|
project_with_repo.add_maintainer(user)
|
|
|
|
end
|
|
|
|
|
2018-11-08 19:23:39 +05:30
|
|
|
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,
|
|
|
|
position: position.to_h)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'note is 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_present
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
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,
|
|
|
|
position: position.to_h)
|
|
|
|
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
|
|
|
|
|
|
|
|
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,
|
|
|
|
position: image_position.to_h)
|
|
|
|
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
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-03-27 19:54:05 +05:30
|
|
|
context 'note with commands' do
|
|
|
|
context 'as a user who can update the target' do
|
|
|
|
context '/close, /label, /assign & /milestone' do
|
|
|
|
let(:note_text) { %(HELLO\n/close\n/assign @#{user.username}\nWORLD) }
|
2016-09-13 17:45:13 +05:30
|
|
|
|
2018-03-27 19:54:05 +05:30
|
|
|
it 'saves the note and does not alter the note text' do
|
2018-11-20 20:47:30 +05:30
|
|
|
service = double(:service)
|
|
|
|
allow(Issues::UpdateService).to receive(:new).and_return(service)
|
|
|
|
expect(service).to receive(:execute)
|
2016-09-13 17:45:13 +05:30
|
|
|
|
2018-03-27 19:54:05 +05:30
|
|
|
note = described_class.new(project, user, opts.merge(note: note_text)).execute
|
2016-09-13 17:45:13 +05:30
|
|
|
|
2018-03-27 19:54:05 +05:30
|
|
|
expect(note.note).to eq "HELLO\nWORLD"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context '/merge with sha option' do
|
|
|
|
let(:note_text) { %(HELLO\n/merge\nWORLD) }
|
|
|
|
let(:params) { opts.merge(note: note_text, merge_request_diff_head_sha: 'sha') }
|
|
|
|
|
|
|
|
it 'saves the note and exectues merge command' do
|
|
|
|
note = described_class.new(project, user, params).execute
|
|
|
|
|
|
|
|
expect(note.note).to eq "HELLO\nWORLD"
|
|
|
|
end
|
2016-09-13 17:45:13 +05:30
|
|
|
end
|
2019-05-18 00:54:41 +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)
|
|
|
|
|
|
|
|
note = described_class.new(project, user, opts.merge(note: note_text)).execute
|
|
|
|
|
|
|
|
expect(note.errors[:commands_only]).to be_present
|
|
|
|
end
|
|
|
|
end
|
2016-09-13 17:45:13 +05:30
|
|
|
end
|
2015-11-26 14:37:03 +05:30
|
|
|
|
2018-03-27 19:54:05 +05:30
|
|
|
context 'as a user who cannot update the target' do
|
|
|
|
let(:note_text) { "HELLO\n/todo\n/assign #{user.to_reference}\nWORLD" }
|
|
|
|
let(:note) { described_class.new(project, user, opts.merge(note: note_text)).execute }
|
2015-11-26 14:37:03 +05:30
|
|
|
|
2018-03-27 19:54:05 +05:30
|
|
|
before do
|
|
|
|
project.team.find_member(user.id).update!(access_level: Gitlab::Access::GUEST)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'applies commands the user can execute' do
|
|
|
|
expect { note }.to change { user.todos_pending_count }.from(0).to(1)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not apply commands the user cannot execute' do
|
|
|
|
expect { note }.not_to change { issue.assignees }
|
|
|
|
end
|
2015-11-26 14:37:03 +05:30
|
|
|
|
2018-03-27 19:54:05 +05:30
|
|
|
it 'saves the note' do
|
2017-08-17 22:00:37 +05:30
|
|
|
expect(note.note).to eq "HELLO\nWORLD"
|
|
|
|
end
|
|
|
|
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 }
|
2016-06-16 23:09:34 +05:30
|
|
|
|
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
|
2016-06-16 23:09:34 +05:30
|
|
|
|
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
|
|
|
|
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-05-18 00:54:41 +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-05-18 00:54:41 +05:30
|
|
|
it 'converts existing note to DiscussionNote' do
|
|
|
|
expect do
|
|
|
|
existing_note
|
2019-03-02 22:35:43 +05:30
|
|
|
|
2019-05-18 00:54:41 +05:30
|
|
|
Timecop.freeze(Time.now + 1.minute) { subject }
|
2019-03-02 22:35:43 +05:30
|
|
|
|
2019-05-18 00:54:41 +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
|
|
|
|
end
|
2015-11-26 14:37:03 +05:30
|
|
|
end
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|