2019-04-11 08:17:24 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-02-16 17:21:24 -05:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-24 11:08:50 -04:00
|
|
|
RSpec.describe Notes::UpdateService do
|
2019-10-11 14:06:15 -04:00
|
|
|
let(:group) { create(:group, :public) }
|
|
|
|
let(:project) { create(:project, :public, group: group) }
|
|
|
|
let(:private_group) { create(:group, :private) }
|
|
|
|
let(:private_project) { create(:project, :private, group: private_group) }
|
2016-02-16 17:21:24 -05:00
|
|
|
let(:user) { create(:user) }
|
|
|
|
let(:user2) { create(:user) }
|
2017-03-28 02:25:43 -04:00
|
|
|
let(:user3) { create(:user) }
|
2016-02-16 17:21:24 -05:00
|
|
|
let(:issue) { create(:issue, project: project) }
|
2019-10-11 14:06:15 -04:00
|
|
|
let(:issue2) { create(:issue, project: private_project) }
|
2017-03-28 02:25:43 -04:00
|
|
|
let(:note) { create(:note, project: project, noteable: issue, author: user, note: "Old note #{user2.to_reference}") }
|
2020-08-12 23:10:13 -04:00
|
|
|
let(:markdown) do
|
|
|
|
<<-MARKDOWN.strip_heredoc
|
|
|
|
```suggestion
|
|
|
|
foo
|
|
|
|
```
|
|
|
|
|
|
|
|
```suggestion
|
|
|
|
bar
|
|
|
|
```
|
|
|
|
MARKDOWN
|
|
|
|
end
|
2016-02-16 17:21:24 -05:00
|
|
|
|
|
|
|
before do
|
2018-07-11 10:36:08 -04:00
|
|
|
project.add_maintainer(user)
|
2017-12-22 03:18:28 -05:00
|
|
|
project.add_developer(user2)
|
|
|
|
project.add_developer(user3)
|
2019-10-11 14:06:15 -04:00
|
|
|
group.add_developer(user3)
|
|
|
|
private_group.add_developer(user)
|
|
|
|
private_group.add_developer(user2)
|
|
|
|
private_project.add_developer(user3)
|
2016-02-16 17:21:24 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
describe '#execute' do
|
|
|
|
def update_note(opts)
|
|
|
|
@note = Notes::UpdateService.new(project, user, opts).execute(note)
|
|
|
|
@note.reload
|
|
|
|
end
|
|
|
|
|
2020-01-30 10:09:15 -05:00
|
|
|
it 'does not update the note when params is blank' do
|
2020-10-02 08:09:03 -04:00
|
|
|
travel_to(1.day.from_now) do
|
2020-01-30 10:09:15 -05:00
|
|
|
expect { update_note({}) }.not_to change { note.reload.updated_at }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-10-23 05:08:41 -04:00
|
|
|
it 'does not track usage data when params is blank' do
|
|
|
|
expect(Gitlab::UsageDataCounters::IssueActivityUniqueCounter).not_to receive(:track_issue_comment_edited_action)
|
2021-01-11 04:10:46 -05:00
|
|
|
expect(Gitlab::UsageDataCounters::MergeRequestActivityUniqueCounter).not_to receive(:track_edit_comment_action)
|
2020-10-23 05:08:41 -04:00
|
|
|
|
|
|
|
update_note({})
|
|
|
|
end
|
|
|
|
|
2021-01-11 04:10:46 -05:00
|
|
|
it 'tracks issue usage data', :clean_gitlab_redis_shared_state do
|
2020-10-23 05:08:41 -04:00
|
|
|
event = Gitlab::UsageDataCounters::IssueActivityUniqueCounter::ISSUE_COMMENT_EDITED
|
|
|
|
counter = Gitlab::UsageDataCounters::HLLRedisCounter
|
|
|
|
|
|
|
|
expect(Gitlab::UsageDataCounters::IssueActivityUniqueCounter).to receive(:track_issue_comment_edited_action).with(author: user).and_call_original
|
|
|
|
expect do
|
|
|
|
update_note(note: 'new text')
|
|
|
|
end.to change { counter.unique_events(event_names: event, start_date: 1.day.ago, end_date: 1.day.from_now) }.by(1)
|
|
|
|
end
|
|
|
|
|
2021-03-09 10:08:59 -05:00
|
|
|
context 'when note text was changed' do
|
|
|
|
let!(:note) { create(:note, project: project, noteable: issue, author: user2, note: "Old note #{user3.to_reference}") }
|
|
|
|
let(:edit_note_text) { update_note({ note: 'new text' }) }
|
|
|
|
|
|
|
|
it 'update last_edited_at' do
|
|
|
|
travel_to(1.day.from_now) do
|
|
|
|
expect { edit_note_text }.to change { note.reload.last_edited_at }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'update updated_by' do
|
|
|
|
travel_to(1.day.from_now) do
|
|
|
|
expect { edit_note_text }.to change { note.reload.updated_by }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when note text was not changed' do
|
|
|
|
let!(:note) { create(:note, project: project, noteable: issue, author: user2, note: "Old note #{user3.to_reference}") }
|
|
|
|
let(:does_not_edit_note_text) { update_note({}) }
|
|
|
|
|
|
|
|
it 'does not update last_edited_at' do
|
|
|
|
travel_to(1.day.from_now) do
|
|
|
|
expect { does_not_edit_note_text }.not_to change { note.reload.last_edited_at }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not update updated_by' do
|
|
|
|
travel_to(1.day.from_now) do
|
|
|
|
expect { does_not_edit_note_text }.not_to change { note.reload.updated_by }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-01-11 04:10:46 -05:00
|
|
|
context 'when the notable is a merge request' do
|
|
|
|
let(:merge_request) { create(:merge_request, source_project: project) }
|
|
|
|
let(:note) { create(:note, project: project, noteable: merge_request, author: user, note: "Old note #{user2.to_reference}") }
|
|
|
|
|
|
|
|
it 'tracks merge request usage data' do
|
2021-01-14 16:10:37 -05:00
|
|
|
expect(Gitlab::UsageDataCounters::MergeRequestActivityUniqueCounter).to receive(:track_edit_comment_action).with(note: note)
|
2021-01-11 04:10:46 -05:00
|
|
|
|
|
|
|
update_note(note: 'new text')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-07-29 17:09:52 -04:00
|
|
|
context 'with system note' do
|
|
|
|
before do
|
|
|
|
note.update_column(:system, true)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not update the note' do
|
|
|
|
expect { update_note(note: 'new text') }.not_to change { note.reload.note }
|
|
|
|
end
|
2020-10-23 05:08:41 -04:00
|
|
|
|
|
|
|
it 'does not track usage data' do
|
|
|
|
expect(Gitlab::UsageDataCounters::IssueActivityUniqueCounter).not_to receive(:track_issue_comment_edited_action)
|
|
|
|
|
|
|
|
update_note(note: 'new text')
|
|
|
|
end
|
2020-07-29 17:09:52 -04:00
|
|
|
end
|
|
|
|
|
2018-12-13 14:17:19 -05:00
|
|
|
context 'suggestions' do
|
|
|
|
it 'refreshes note suggestions' do
|
|
|
|
suggestion = create(:suggestion)
|
|
|
|
note = suggestion.note
|
|
|
|
|
|
|
|
expect { described_class.new(project, user, note: markdown).execute(note) }
|
|
|
|
.to change { note.suggestions.count }.from(1).to(2)
|
|
|
|
|
|
|
|
expect(note.suggestions.order(:relative_order).map(&:to_content))
|
|
|
|
.to eq([" foo\n", " bar\n"])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-02-20 08:59:59 -05:00
|
|
|
context 'todos' do
|
2019-10-11 14:06:15 -04:00
|
|
|
shared_examples 'does not update todos' do
|
|
|
|
it 'keep todos' do
|
|
|
|
expect(todo.reload).to be_pending
|
|
|
|
end
|
2016-02-16 17:21:24 -05:00
|
|
|
|
2019-10-11 14:06:15 -04:00
|
|
|
it 'does not create any new todos' do
|
|
|
|
expect(Todo.count).to eq(1)
|
2016-02-16 17:21:24 -05:00
|
|
|
end
|
2019-10-11 14:06:15 -04:00
|
|
|
end
|
2016-02-16 17:21:24 -05:00
|
|
|
|
2019-10-11 14:06:15 -04:00
|
|
|
shared_examples 'creates one todo' do
|
2016-02-20 08:59:59 -05:00
|
|
|
it 'marks todos as done' do
|
|
|
|
expect(todo.reload).to be_done
|
2016-02-16 17:21:24 -05:00
|
|
|
end
|
2017-03-28 02:25:43 -04:00
|
|
|
|
|
|
|
it 'creates only 1 new todo' do
|
|
|
|
expect(Todo.count).to eq(2)
|
|
|
|
end
|
2016-02-16 17:21:24 -05:00
|
|
|
end
|
|
|
|
|
2019-10-11 14:06:15 -04:00
|
|
|
context 'when note includes a user mention' do
|
|
|
|
let!(:todo) { create(:todo, :assigned, user: user, project: project, target: issue, author: user2) }
|
|
|
|
|
|
|
|
context 'when the note does not change mentions' do
|
|
|
|
before do
|
|
|
|
update_note({ note: "Old note #{user2.to_reference}" })
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'does not update todos'
|
2016-02-16 17:21:24 -05:00
|
|
|
end
|
|
|
|
|
2019-10-11 14:06:15 -04:00
|
|
|
context 'when the note changes to include one more user mention' do
|
|
|
|
before do
|
|
|
|
update_note({ note: "New note #{user2.to_reference} #{user3.to_reference}" })
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'creates one todo'
|
2016-02-16 17:21:24 -05:00
|
|
|
end
|
2017-03-28 02:25:43 -04:00
|
|
|
|
2019-10-11 14:06:15 -04:00
|
|
|
context 'when the note changes to include a group mentions' do
|
|
|
|
before do
|
|
|
|
update_note({ note: "New note #{private_group.to_reference}" })
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'creates one todo'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when note includes a group mention' do
|
|
|
|
context 'when the group is public' do
|
|
|
|
let(:note) { create(:note, project: project, noteable: issue, author: user, note: "Old note #{group.to_reference}") }
|
|
|
|
let!(:todo) { create(:todo, :assigned, user: user, project: project, target: issue, author: user2) }
|
|
|
|
|
|
|
|
context 'when the note does not change mentions' do
|
|
|
|
before do
|
|
|
|
update_note({ note: "Old note #{group.to_reference}" })
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'does not update todos'
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the note changes mentions' do
|
|
|
|
before do
|
|
|
|
update_note({ note: "New note #{user2.to_reference} #{user3.to_reference}" })
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'creates one todo'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the group is private' do
|
|
|
|
let(:note) { create(:note, project: project, noteable: issue, author: user, note: "Old note #{private_group.to_reference}") }
|
|
|
|
let!(:todo) { create(:todo, :assigned, user: user, project: project, target: issue, author: user2) }
|
|
|
|
|
|
|
|
context 'when the note does not change mentions' do
|
|
|
|
before do
|
|
|
|
update_note({ note: "Old note #{private_group.to_reference}" })
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'does not update todos'
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the note changes mentions' do
|
|
|
|
before do
|
|
|
|
update_note({ note: "New note #{user2.to_reference} #{user3.to_reference}" })
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'creates one todo'
|
|
|
|
end
|
2017-03-28 02:25:43 -04:00
|
|
|
end
|
2016-02-16 17:21:24 -05:00
|
|
|
end
|
|
|
|
end
|
2020-08-12 23:10:13 -04:00
|
|
|
|
|
|
|
context 'for a personal snippet' do
|
|
|
|
let_it_be(:snippet) { create(:personal_snippet, :public) }
|
2021-06-28 23:07:32 -04:00
|
|
|
|
2020-08-12 23:10:13 -04:00
|
|
|
let(:note) { create(:note, project: nil, noteable: snippet, author: user, note: "Note on a snippet with reference #{issue.to_reference}" ) }
|
|
|
|
|
|
|
|
it 'does not create todos' do
|
|
|
|
expect { update_note({ note: "Mentioning user #{user2}" }) }.not_to change { note.todos.count }
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not create suggestions' do
|
|
|
|
expect { update_note({ note: "Updated snippet with markdown suggestion #{markdown}" }) }
|
|
|
|
.not_to change { note.suggestions.count }
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not create mentions' do
|
|
|
|
expect(note).not_to receive(:create_new_cross_references!)
|
|
|
|
update_note({ note: "Updated with new reference: #{issue.to_reference}" })
|
|
|
|
end
|
2020-10-23 05:08:41 -04:00
|
|
|
|
|
|
|
it 'does not track usage data' do
|
|
|
|
expect(Gitlab::UsageDataCounters::IssueActivityUniqueCounter).not_to receive(:track_issue_comment_edited_action)
|
|
|
|
|
|
|
|
update_note(note: 'new text')
|
|
|
|
end
|
2020-08-12 23:10:13 -04:00
|
|
|
end
|
2016-02-16 17:21:24 -05:00
|
|
|
end
|
|
|
|
end
|