diff options
Diffstat (limited to 'spec/services/notes')
-rw-r--r-- | spec/services/notes/copy_service_spec.rb | 157 | ||||
-rw-r--r-- | spec/services/notes/create_service_spec.rb | 9 | ||||
-rw-r--r-- | spec/services/notes/quick_actions_service_spec.rb | 27 | ||||
-rw-r--r-- | spec/services/notes/update_service_spec.rb | 50 |
4 files changed, 227 insertions, 16 deletions
diff --git a/spec/services/notes/copy_service_spec.rb b/spec/services/notes/copy_service_spec.rb new file mode 100644 index 00000000000..fd44aa7cf40 --- /dev/null +++ b/spec/services/notes/copy_service_spec.rb @@ -0,0 +1,157 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Notes::CopyService do + describe '#initialize' do + let_it_be(:noteable) { create(:issue) } + + it 'validates that we cannot copy notes to the same Noteable' do + expect { described_class.new(noteable, noteable) }.to raise_error(ArgumentError) + end + end + + describe '#execute' do + let_it_be(:user) { create(:user) } + let_it_be(:group) { create(:group) } + let_it_be(:from_project) { create(:project, :public, group: group) } + let_it_be(:to_project) { create(:project, :public, group: group) } + let(:from_noteable) { create(:issue, project: from_project) } + let(:to_noteable) { create(:issue, project: to_project) } + + subject(:execute_service) { described_class.new(user, from_noteable, to_noteable).execute } + + context 'rewriting the note body' do + context 'simple notes' do + let!(:notes) do + [ + create(:note, noteable: from_noteable, project: from_noteable.project, + created_at: 2.weeks.ago, updated_at: 1.week.ago), + create(:note, noteable: from_noteable, project: from_noteable.project), + create(:note, system: true, noteable: from_noteable, project: from_noteable.project) + ] + end + + it 'rewrites existing notes in valid order' do + execute_service + + expect(to_noteable.notes.order('id ASC').pluck(:note).first(3)).to eq(notes.map(&:note)) + end + + it 'copies all the issue notes' do + execute_service + + expect(to_noteable.notes.count).to eq(3) + end + + it 'does not change the note attributes' do + execute_service + + new_note = to_noteable.notes.first + + expect(new_note).to have_attributes( + note: notes.first.note, + author: notes.first.author + ) + end + + it 'copies the award emojis' do + create(:award_emoji, awardable: notes.first, name: 'thumbsup') + + execute_service + + new_award_emoji = to_noteable.notes.first.award_emoji.first + + expect(new_award_emoji.name).to eq('thumbsup') + end + + it 'copies system_note_metadata for system note' do + system_note_metadata = create(:system_note_metadata, note: notes.last) + + execute_service + + new_note = to_noteable.notes.last + + aggregate_failures do + expect(new_note.system_note_metadata.action).to eq(system_note_metadata.action) + expect(new_note.system_note_metadata.id).not_to eq(system_note_metadata.id) + end + end + + it 'returns success' do + aggregate_failures do + expect(execute_service).to be_kind_of(ServiceResponse) + expect(execute_service).to be_success + end + end + end + + context 'notes with mentions' do + let!(:note_with_mention) { create(:note, noteable: from_noteable, author: from_noteable.author, project: from_noteable.project, note: "note with mention #{user.to_reference}") } + let!(:note_with_no_mention) { create(:note, noteable: from_noteable, author: from_noteable.author, project: from_noteable.project, note: "note without mention") } + + it 'saves user mentions with actual mentions for new issue' do + execute_service + + aggregate_failures do + expect(to_noteable.user_mentions.first.mentioned_users_ids).to match_array([user.id]) + expect(to_noteable.user_mentions.count).to eq(1) + end + end + end + + context 'notes with reference' do + let(:other_issue) { create(:issue, project: from_noteable.project) } + let(:merge_request) { create(:merge_request) } + let(:text) { "See ##{other_issue.iid} and #{merge_request.project.full_path}!#{merge_request.iid}" } + let!(:note) { create(:note, noteable: from_noteable, note: text, project: from_noteable.project) } + + it 'rewrites the references correctly' do + execute_service + + new_note = to_noteable.notes.first + + expected_text = "See #{other_issue.project.path}##{other_issue.iid} and #{merge_request.project.full_path}!#{merge_request.iid}" + + aggregate_failures do + expect(new_note.note).to eq(expected_text) + expect(new_note.author).to eq(note.author) + end + end + end + + context 'notes with upload' do + let(:uploader) { build(:file_uploader, project: from_noteable.project) } + let(:text) { "Simple text with image: #{uploader.markdown_link} "} + let!(:note) { create(:note, noteable: from_noteable, note: text, project: from_noteable.project) } + + it 'rewrites note content correctly' do + execute_service + new_note = to_noteable.notes.first + + aggregate_failures do + expect(note.note).to match(/Simple text with image: #{FileUploader::MARKDOWN_PATTERN}/) + expect(new_note.note).to match(/Simple text with image: #{FileUploader::MARKDOWN_PATTERN}/) + expect(note.note).not_to eq(new_note.note) + expect(note.note_html).not_to eq(new_note.note_html) + end + end + end + + context 'discussion notes' do + let(:note) { create(:note, noteable: from_noteable, note: 'sample note', project: from_noteable.project) } + let!(:discussion) { create(:discussion_note_on_issue, in_reply_to: note, note: 'reply to sample note') } + + it 'rewrites discussion correctly' do + execute_service + + aggregate_failures do + expect(to_noteable.notes.count).to eq(from_noteable.notes.count) + expect(to_noteable.notes.where(discussion_id: discussion.discussion_id).count).to eq(0) + expect(from_noteable.notes.where(discussion_id: discussion.discussion_id).count).to eq(1) + end + end + end + end + end +end diff --git a/spec/services/notes/create_service_spec.rb b/spec/services/notes/create_service_spec.rb index fd824621db7..f087f72ca46 100644 --- a/spec/services/notes/create_service_spec.rb +++ b/spec/services/notes/create_service_spec.rb @@ -117,6 +117,7 @@ RSpec.describe Notes::CreateService do 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", @@ -125,6 +126,7 @@ RSpec.describe Notes::CreateService do 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 @@ -432,6 +434,13 @@ RSpec.describe Notes::CreateService do .and change { existing_note.updated_at } end + 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 + context 'discussion to reply cannot be found' do before do existing_note.delete diff --git a/spec/services/notes/quick_actions_service_spec.rb b/spec/services/notes/quick_actions_service_spec.rb index d20824efaaa..e9decd44730 100644 --- a/spec/services/notes/quick_actions_service_spec.rb +++ b/spec/services/notes/quick_actions_service_spec.rb @@ -58,14 +58,29 @@ RSpec.describe Notes::QuickActionsService do end describe '/spend' do - let(:note_text) { '/spend 1h' } + context 'when note is not persisted' do + let(:note_text) { '/spend 1h' } - it 'updates the spent time on the noteable' do - content, update_params = service.execute(note) - service.apply_updates(update_params, note) + it 'adds time to noteable, adds timelog with nil note_id and has no content' do + content, update_params = service.execute(note) + service.apply_updates(update_params, note) - expect(content).to eq '' - expect(note.noteable.time_spent).to eq(3600) + expect(content).to eq '' + expect(note.noteable.time_spent).to eq(3600) + expect(Timelog.last.note_id).to be_nil + end + end + + context 'when note is persisted' do + let(:note_text) { "a note \n/spend 1h" } + + it 'updates the spent time and populates timelog with note_id' do + new_content, update_params = service.execute(note) + note.update!(note: new_content) + service.apply_updates(update_params, note) + + expect(Timelog.last.note_id).to eq(note.id) + end end end end diff --git a/spec/services/notes/update_service_spec.rb b/spec/services/notes/update_service_spec.rb index 70dea99de4a..47b8ba0cd72 100644 --- a/spec/services/notes/update_service_spec.rb +++ b/spec/services/notes/update_service_spec.rb @@ -13,6 +13,17 @@ RSpec.describe Notes::UpdateService do let(:issue) { create(:issue, project: project) } let(:issue2) { create(:issue, project: private_project) } let(:note) { create(:note, project: project, noteable: issue, author: user, note: "Old note #{user2.to_reference}") } + let(:markdown) do + <<-MARKDOWN.strip_heredoc + ```suggestion + foo + ``` + + ```suggestion + bar + ``` + MARKDOWN + end before do project.add_maintainer(user) @@ -36,18 +47,18 @@ RSpec.describe Notes::UpdateService do end end - context 'suggestions' do - it 'refreshes note suggestions' do - markdown = <<-MARKDOWN.strip_heredoc - ```suggestion - foo - ``` + context 'with system note' do + before do + note.update_column(:system, true) + end - ```suggestion - bar - ``` - MARKDOWN + it 'does not update the note' do + expect { update_note(note: 'new text') }.not_to change { note.reload.note } + end + end + context 'suggestions' do + it 'refreshes note suggestions' do suggestion = create(:suggestion) note = suggestion.note @@ -191,5 +202,24 @@ RSpec.describe Notes::UpdateService do end end end + + context 'for a personal snippet' do + let_it_be(:snippet) { create(:personal_snippet, :public) } + 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 + end end end |