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

196 lines
6.5 KiB
Ruby
Raw Normal View History

2019-07-31 22:56:46 +05:30
# frozen_string_literal: true
2016-04-02 18:10:28 +05:30
require 'spec_helper'
2020-07-28 23:09:34 +05:30
RSpec.describe Notes::UpdateService do
2019-12-21 20:55:43 +05:30
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-04-02 18:10:28 +05:30
let(:user) { create(:user) }
let(:user2) { create(:user) }
2017-08-17 22:00:37 +05:30
let(:user3) { create(:user) }
2016-04-02 18:10:28 +05:30
let(:issue) { create(:issue, project: project) }
2019-12-21 20:55:43 +05:30
let(:issue2) { create(:issue, project: private_project) }
2017-08-17 22:00:37 +05:30
let(:note) { create(:note, project: project, noteable: issue, author: user, note: "Old note #{user2.to_reference}") }
2016-04-02 18:10:28 +05:30
before do
2018-11-18 11:00:15 +05:30
project.add_maintainer(user)
2018-03-17 18:26:18 +05:30
project.add_developer(user2)
project.add_developer(user3)
2019-12-21 20:55:43 +05:30
group.add_developer(user3)
private_group.add_developer(user)
private_group.add_developer(user2)
private_project.add_developer(user3)
2016-04-02 18:10:28 +05:30
end
describe '#execute' do
def update_note(opts)
@note = Notes::UpdateService.new(project, user, opts).execute(note)
@note.reload
end
2020-03-09 13:42:32 +05:30
it 'does not update the note when params is blank' do
Timecop.freeze(1.day.from_now) do
expect { update_note({}) }.not_to change { note.reload.updated_at }
end
end
2019-02-15 15:39:39 +05:30
context 'suggestions' do
it 'refreshes note suggestions' do
markdown = <<-MARKDOWN.strip_heredoc
```suggestion
foo
```
```suggestion
bar
```
MARKDOWN
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
2020-07-28 23:09:34 +05:30
end
context 'setting confidentiality' do
let(:opts) { { confidential: true } }
context 'simple note' do
it 'updates the confidentiality' do
expect { update_note(opts) }.to change { note.reload.confidential }.from(nil).to(true)
end
end
context 'discussion notes' do
let(:note) { create(:discussion_note, project: project, noteable: issue, author: user, note: "Old note #{user2.to_reference}") }
let!(:response_note_1) { create(:discussion_note, project: project, noteable: issue, in_reply_to: note) }
let!(:response_note_2) { create(:discussion_note, project: project, noteable: issue, in_reply_to: note, confidential: false) }
let!(:other_note) { create(:note, project: project, noteable: issue) }
context 'when updating the root note' do
it 'updates the confidentiality of the root note and all the responses' do
update_note(opts)
expect(note.reload.confidential).to be_truthy
expect(response_note_1.reload.confidential).to be_truthy
expect(response_note_2.reload.confidential).to be_truthy
expect(other_note.reload.confidential).to be_falsey
end
end
context 'when updating one of the response notes' do
it 'updates only the confidentiality of the note that is being updated' do
Notes::UpdateService.new(project, user, opts).execute(response_note_1)
expect(note.reload.confidential).to be_falsey
expect(response_note_1.reload.confidential).to be_truthy
expect(response_note_2.reload.confidential).to be_falsey
expect(other_note.reload.confidential).to be_falsey
end
end
end
2019-02-15 15:39:39 +05:30
end
2016-04-02 18:10:28 +05:30
context 'todos' do
2019-12-21 20:55:43 +05:30
shared_examples 'does not update todos' do
it 'keep todos' do
expect(todo.reload).to be_pending
end
2016-04-02 18:10:28 +05:30
2019-12-21 20:55:43 +05:30
it 'does not create any new todos' do
expect(Todo.count).to eq(1)
2016-04-02 18:10:28 +05:30
end
2019-12-21 20:55:43 +05:30
end
2016-04-02 18:10:28 +05:30
2019-12-21 20:55:43 +05:30
shared_examples 'creates one todo' do
2016-04-02 18:10:28 +05:30
it 'marks todos as done' do
expect(todo.reload).to be_done
end
2017-08-17 22:00:37 +05:30
it 'creates only 1 new todo' do
expect(Todo.count).to eq(2)
end
2016-04-02 18:10:28 +05:30
end
2019-12-21 20:55:43 +05:30
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-04-02 18:10:28 +05:30
end
2019-12-21 20:55:43 +05:30
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-04-02 18:10:28 +05:30
end
2017-08-17 22:00:37 +05:30
2019-12-21 20:55:43 +05:30
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-08-17 22:00:37 +05:30
end
2016-04-02 18:10:28 +05:30
end
end
end
end