2019-10-16 05:07:51 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2014-04-28 05:48:18 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-16 14:09:01 -04:00
|
|
|
RSpec.describe NotesFinder do
|
2014-04-28 05:48:18 -04:00
|
|
|
let(:user) { create :user }
|
2017-08-02 15:55:11 -04:00
|
|
|
let(:project) { create(:project) }
|
2014-04-28 05:48:18 -04:00
|
|
|
|
|
|
|
before do
|
2018-07-11 10:36:08 -04:00
|
|
|
project.add_maintainer(user)
|
2014-04-28 05:48:18 -04:00
|
|
|
end
|
|
|
|
|
2016-07-11 18:12:31 -04:00
|
|
|
describe '#execute' do
|
2018-10-23 05:49:45 -04:00
|
|
|
context 'when notes filter is present' do
|
|
|
|
let!(:comment) { create(:note_on_issue, project: project) }
|
|
|
|
let!(:system_note) { create(:note_on_issue, project: project, system: true) }
|
|
|
|
|
2018-11-05 07:30:14 -05:00
|
|
|
it 'returns only user notes when using only_comments filter' do
|
2019-07-30 14:25:49 -04:00
|
|
|
finder = described_class.new(user, project: project, notes_filter: UserPreference::NOTES_FILTERS[:only_comments])
|
2018-10-23 05:49:45 -04:00
|
|
|
|
|
|
|
notes = finder.execute
|
|
|
|
|
|
|
|
expect(notes).to match_array(comment)
|
|
|
|
end
|
|
|
|
|
2018-11-05 07:30:14 -05:00
|
|
|
it 'returns only system notes when using only_activity filters' do
|
2019-07-30 14:25:49 -04:00
|
|
|
finder = described_class.new(user, project: project, notes_filter: UserPreference::NOTES_FILTERS[:only_activity])
|
2018-11-05 07:30:14 -05:00
|
|
|
|
|
|
|
notes = finder.execute
|
|
|
|
|
|
|
|
expect(notes).to match_array(system_note)
|
|
|
|
end
|
|
|
|
|
2018-10-23 05:49:45 -04:00
|
|
|
it 'gets all notes' do
|
2019-07-30 14:25:49 -04:00
|
|
|
finder = described_class.new(user, project: project, notes_filter: UserPreference::NOTES_FILTERS[:all_activity])
|
2018-10-23 05:49:45 -04:00
|
|
|
|
|
|
|
notes = finder.execute
|
|
|
|
|
|
|
|
expect(notes).to match_array([comment, system_note])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-12-08 20:56:31 -05:00
|
|
|
it 'finds notes on merge requests' do
|
|
|
|
create(:note_on_merge_request, project: project)
|
|
|
|
|
2019-07-30 14:25:49 -04:00
|
|
|
notes = described_class.new(user, project: project).execute
|
2016-12-08 20:56:31 -05:00
|
|
|
|
|
|
|
expect(notes.count).to eq(1)
|
2014-04-28 05:48:18 -04:00
|
|
|
end
|
|
|
|
|
2016-12-08 20:56:31 -05:00
|
|
|
it 'finds notes on snippets' do
|
|
|
|
create(:note_on_project_snippet, project: project)
|
|
|
|
|
2019-07-30 14:25:49 -04:00
|
|
|
notes = described_class.new(user, project: project).execute
|
2016-12-08 20:56:31 -05:00
|
|
|
|
|
|
|
expect(notes.count).to eq(1)
|
2014-04-28 05:48:18 -04:00
|
|
|
end
|
2014-04-28 05:53:37 -04:00
|
|
|
|
2016-12-08 20:56:31 -05:00
|
|
|
it "excludes notes on commits the author can't download" do
|
2017-01-25 16:49:52 -05:00
|
|
|
project = create(:project, :private, :repository)
|
2016-12-08 20:56:31 -05:00
|
|
|
note = create(:note_on_commit, project: project)
|
|
|
|
params = { target_type: 'commit', target_id: note.noteable.id }
|
|
|
|
|
2019-07-30 14:25:49 -04:00
|
|
|
notes = described_class.new(create(:user), params).execute
|
2016-12-08 20:56:31 -05:00
|
|
|
|
|
|
|
expect(notes.count).to eq(0)
|
2014-04-28 05:53:37 -04:00
|
|
|
end
|
2014-04-28 06:13:29 -04:00
|
|
|
|
2016-12-08 20:56:31 -05:00
|
|
|
it 'succeeds when no notes found' do
|
2019-07-30 14:25:49 -04:00
|
|
|
notes = described_class.new(create(:user), project: project).execute
|
2016-12-08 20:56:31 -05:00
|
|
|
|
|
|
|
expect(notes.count).to eq(0)
|
2014-04-28 06:13:29 -04:00
|
|
|
end
|
2016-06-14 13:37:41 -04:00
|
|
|
|
2016-12-08 20:56:31 -05:00
|
|
|
context 'on restricted projects' do
|
|
|
|
let(:project) do
|
2017-08-02 15:55:11 -04:00
|
|
|
create(:project,
|
2017-02-02 05:10:25 -05:00
|
|
|
:public,
|
|
|
|
:issues_private,
|
|
|
|
:snippets_private,
|
|
|
|
:merge_requests_private)
|
2016-12-08 20:56:31 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'publicly excludes notes on merge requests' do
|
|
|
|
create(:note_on_merge_request, project: project)
|
|
|
|
|
2019-07-30 14:25:49 -04:00
|
|
|
notes = described_class.new(create(:user), project: project).execute
|
2016-12-08 20:56:31 -05:00
|
|
|
|
|
|
|
expect(notes.count).to eq(0)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'publicly excludes notes on issues' do
|
|
|
|
create(:note_on_issue, project: project)
|
|
|
|
|
2019-07-30 14:25:49 -04:00
|
|
|
notes = described_class.new(create(:user), project: project).execute
|
2016-12-08 20:56:31 -05:00
|
|
|
|
|
|
|
expect(notes.count).to eq(0)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'publicly excludes notes on snippets' do
|
|
|
|
create(:note_on_project_snippet, project: project)
|
|
|
|
|
2019-07-30 14:25:49 -04:00
|
|
|
notes = described_class.new(create(:user), project: project).execute
|
2016-12-08 20:56:31 -05:00
|
|
|
|
|
|
|
expect(notes.count).to eq(0)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-03-05 08:25:56 -05:00
|
|
|
context 'for target type' do
|
|
|
|
let(:project) { create(:project, :repository) }
|
|
|
|
let!(:note1) { create :note_on_issue, project: project }
|
|
|
|
let!(:note2) { create :note_on_commit, project: project }
|
|
|
|
|
|
|
|
it 'finds only notes for the selected type' do
|
2019-07-30 14:25:49 -04:00
|
|
|
notes = described_class.new(user, project: project, target_type: 'issue').execute
|
2018-03-05 08:25:56 -05:00
|
|
|
|
|
|
|
expect(notes).to eq([note1])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-12-08 20:56:31 -05:00
|
|
|
context 'for target' do
|
2017-01-25 16:49:52 -05:00
|
|
|
let(:project) { create(:project, :repository) }
|
2019-07-30 14:25:49 -04:00
|
|
|
let!(:note1) { create :note_on_commit, project: project }
|
|
|
|
let!(:note2) { create :note_on_commit, project: project }
|
2016-12-08 20:56:31 -05:00
|
|
|
let(:commit) { note1.noteable }
|
2020-07-16 20:09:37 -04:00
|
|
|
let(:params) { { project: project, target_id: commit.id, target_type: 'commit', last_fetched_at: 1.hour.ago } }
|
2019-07-27 00:28:56 -04:00
|
|
|
|
2016-12-08 20:56:31 -05:00
|
|
|
it 'finds all notes' do
|
2019-07-30 14:25:49 -04:00
|
|
|
notes = described_class.new(user, params).execute
|
2016-12-08 20:56:31 -05:00
|
|
|
expect(notes.size).to eq(2)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'finds notes on merge requests' do
|
|
|
|
note = create(:note_on_merge_request, project: project)
|
2019-07-30 14:25:49 -04:00
|
|
|
params = { project: project, target_type: 'merge_request', target_id: note.noteable.id }
|
2016-12-08 20:56:31 -05:00
|
|
|
|
2019-07-30 14:25:49 -04:00
|
|
|
notes = described_class.new(user, params).execute
|
2016-12-08 20:56:31 -05:00
|
|
|
|
|
|
|
expect(notes).to include(note)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'finds notes on snippets' do
|
|
|
|
note = create(:note_on_project_snippet, project: project)
|
2019-07-30 14:25:49 -04:00
|
|
|
params = { project: project, target_type: 'snippet', target_id: note.noteable.id }
|
2016-06-14 13:37:41 -04:00
|
|
|
|
2019-07-30 14:25:49 -04:00
|
|
|
notes = described_class.new(user, params).execute
|
2016-06-14 13:37:41 -04:00
|
|
|
|
2016-12-08 20:56:31 -05:00
|
|
|
expect(notes.count).to eq(1)
|
2016-06-14 13:37:41 -04:00
|
|
|
end
|
|
|
|
|
2017-04-27 06:41:26 -04:00
|
|
|
it 'finds notes on personal snippets' do
|
|
|
|
note = create(:note_on_personal_snippet)
|
2019-07-30 14:25:49 -04:00
|
|
|
params = { project: project, target_type: 'personal_snippet', target_id: note.noteable_id }
|
2017-04-27 06:41:26 -04:00
|
|
|
|
2019-07-30 14:25:49 -04:00
|
|
|
notes = described_class.new(user, params).execute
|
2017-04-27 06:41:26 -04:00
|
|
|
|
|
|
|
expect(notes.count).to eq(1)
|
|
|
|
end
|
|
|
|
|
2016-12-08 20:56:31 -05:00
|
|
|
it 'raises an exception for an invalid target_type' do
|
2017-02-21 18:50:22 -05:00
|
|
|
params[:target_type] = 'invalid'
|
2019-07-30 14:25:49 -04:00
|
|
|
expect { described_class.new(user, params).execute }.to raise_error("invalid target_type '#{params[:target_type]}'")
|
2016-12-08 20:56:31 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'filters out old notes' do
|
|
|
|
note2.update_attribute(:updated_at, 2.hours.ago)
|
2019-07-30 14:25:49 -04:00
|
|
|
notes = described_class.new(user, params).execute
|
2016-12-08 20:56:31 -05:00
|
|
|
expect(notes).to eq([note1])
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'confidential issue notes' do
|
|
|
|
let(:confidential_issue) { create(:issue, :confidential, project: project, author: user) }
|
|
|
|
let!(:confidential_note) { create(:note, noteable: confidential_issue, project: confidential_issue.project) }
|
|
|
|
|
2020-07-16 20:09:37 -04:00
|
|
|
let(:params) { { project: confidential_issue.project, target_id: confidential_issue.id, target_type: 'issue', last_fetched_at: 1.hour.ago } }
|
2016-12-08 20:56:31 -05:00
|
|
|
|
|
|
|
it 'returns notes if user can see the issue' do
|
2019-07-30 14:25:49 -04:00
|
|
|
expect(described_class.new(user, params).execute).to eq([confidential_note])
|
2016-12-08 20:56:31 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'raises an error if user can not see the issue' do
|
|
|
|
user = create(:user)
|
2019-07-30 14:25:49 -04:00
|
|
|
expect { described_class.new(user, params).execute }.to raise_error(ActiveRecord::RecordNotFound)
|
2016-12-08 20:56:31 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'raises an error for project members with guest role' do
|
|
|
|
user = create(:user)
|
2017-12-22 03:18:28 -05:00
|
|
|
project.add_guest(user)
|
2016-12-08 20:56:31 -05:00
|
|
|
|
2019-07-30 14:25:49 -04:00
|
|
|
expect { described_class.new(user, params).execute }.to raise_error(ActiveRecord::RecordNotFound)
|
2016-12-08 20:56:31 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2019-07-30 14:25:49 -04:00
|
|
|
|
|
|
|
context 'for explicit target' do
|
|
|
|
let(:project) { create(:project, :repository) }
|
|
|
|
let!(:note1) { create :note_on_commit, project: project, created_at: 1.day.ago, updated_at: 2.hours.ago }
|
|
|
|
let!(:note2) { create :note_on_commit, project: project }
|
|
|
|
let(:commit) { note1.noteable }
|
|
|
|
let(:params) { { project: project, target: commit } }
|
|
|
|
|
|
|
|
it 'returns the expected notes' do
|
|
|
|
expect(described_class.new(user, params).execute).to eq([note1, note2])
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns the expected notes when last_fetched_at is given' do
|
2020-07-16 20:09:37 -04:00
|
|
|
params = { project: project, target: commit, last_fetched_at: 1.hour.ago }
|
2019-07-30 14:25:49 -04:00
|
|
|
expect(described_class.new(user, params).execute).to eq([note2])
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'fails when nil is provided' do
|
|
|
|
params = { project: project, target: nil }
|
|
|
|
expect { described_class.new(user, params).execute }.to raise_error(RuntimeError)
|
|
|
|
end
|
|
|
|
end
|
2016-12-08 20:56:31 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
describe '.search' do
|
2017-08-02 15:55:11 -04:00
|
|
|
let(:project) { create(:project, :public) }
|
2016-12-08 20:56:31 -05:00
|
|
|
let(:note) { create(:note_on_issue, note: 'WoW', project: project) }
|
|
|
|
|
|
|
|
it 'returns notes with matching content' do
|
2019-07-30 14:25:49 -04:00
|
|
|
expect(described_class.new(nil, project: note.project, search: note.note).execute).to eq([note])
|
2016-12-08 20:56:31 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns notes with matching content regardless of the casing' do
|
2019-07-30 14:25:49 -04:00
|
|
|
expect(described_class.new(nil, project: note.project, search: 'WOW').execute).to eq([note])
|
2016-12-08 20:56:31 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns commit notes user can access' do
|
|
|
|
note = create(:note_on_commit, project: project)
|
|
|
|
|
2019-07-30 14:25:49 -04:00
|
|
|
expect(described_class.new(create(:user), project: note.project, search: note.note).execute).to eq([note])
|
2016-12-08 20:56:31 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
context "confidential issues" do
|
|
|
|
let(:user) { create(:user) }
|
|
|
|
let(:confidential_issue) { create(:issue, :confidential, project: project, author: user) }
|
|
|
|
let(:confidential_note) { create(:note, note: "Random", noteable: confidential_issue, project: confidential_issue.project) }
|
|
|
|
|
|
|
|
it "returns notes with matching content if user can see the issue" do
|
2019-07-30 14:25:49 -04:00
|
|
|
expect(described_class.new(user, project: confidential_note.project, search: confidential_note.note).execute).to eq([confidential_note])
|
2016-12-08 20:56:31 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "does not return notes with matching content if user can not see the issue" do
|
2016-06-14 13:37:41 -04:00
|
|
|
user = create(:user)
|
2019-07-30 14:25:49 -04:00
|
|
|
expect(described_class.new(user, project: confidential_note.project, search: confidential_note.note).execute).to be_empty
|
2016-06-14 13:37:41 -04:00
|
|
|
end
|
2016-06-14 21:13:58 -04:00
|
|
|
|
2016-12-08 20:56:31 -05:00
|
|
|
it "does not return notes with matching content for project members with guest role" do
|
2016-06-14 21:13:58 -04:00
|
|
|
user = create(:user)
|
2017-12-22 03:18:28 -05:00
|
|
|
project.add_guest(user)
|
2019-07-30 14:25:49 -04:00
|
|
|
expect(described_class.new(user, project: confidential_note.project, search: confidential_note.note).execute).to be_empty
|
2016-12-08 20:56:31 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "does not return notes with matching content for unauthenticated users" do
|
2019-07-30 14:25:49 -04:00
|
|
|
expect(described_class.new(nil, project: confidential_note.project, search: confidential_note.note).execute).to be_empty
|
2016-12-08 20:56:31 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'inlines SQL filters on subqueries for performance' do
|
2019-07-30 14:25:49 -04:00
|
|
|
let(:sql) { described_class.new(nil, project: note.project, search: note.note).execute.to_sql }
|
2016-12-08 20:56:31 -05:00
|
|
|
let(:number_of_noteable_types) { 4 }
|
|
|
|
|
|
|
|
specify 'project_id check' do
|
|
|
|
expect(sql.scan(/project_id/).count).to be >= (number_of_noteable_types + 2)
|
|
|
|
end
|
2016-06-14 21:13:58 -04:00
|
|
|
|
2016-12-08 20:56:31 -05:00
|
|
|
specify 'search filter' do
|
|
|
|
expect(sql.scan(/LIKE/).count).to be >= number_of_noteable_types
|
2016-06-14 21:13:58 -04:00
|
|
|
end
|
2016-06-14 13:37:41 -04:00
|
|
|
end
|
2014-04-28 05:48:18 -04:00
|
|
|
end
|
2017-03-09 20:29:11 -05:00
|
|
|
|
|
|
|
describe '#target' do
|
2019-07-30 14:25:49 -04:00
|
|
|
subject { described_class.new(user, params) }
|
2017-03-17 15:25:52 -04:00
|
|
|
|
|
|
|
context 'for a issue target' do
|
|
|
|
let(:issue) { create(:issue, project: project) }
|
2019-07-30 14:25:49 -04:00
|
|
|
let(:params) { { project: project, target_type: 'issue', target_id: issue.id } }
|
2017-03-17 15:25:52 -04:00
|
|
|
|
|
|
|
it 'returns the issue' do
|
|
|
|
expect(subject.target).to eq(issue)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'for a merge request target' do
|
|
|
|
let(:merge_request) { create(:merge_request, source_project: project) }
|
2019-07-30 14:25:49 -04:00
|
|
|
let(:params) { { project: project, target_type: 'merge_request', target_id: merge_request.id } }
|
2017-03-17 15:25:52 -04:00
|
|
|
|
|
|
|
it 'returns the merge_request' do
|
|
|
|
expect(subject.target).to eq(merge_request)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'for a snippet target' do
|
|
|
|
let(:snippet) { create(:project_snippet, project: project) }
|
2019-07-30 14:25:49 -04:00
|
|
|
let(:params) { { project: project, target_type: 'snippet', target_id: snippet.id } }
|
2017-03-17 15:25:52 -04:00
|
|
|
|
|
|
|
it 'returns the snippet' do
|
|
|
|
expect(subject.target).to eq(snippet)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'for a commit target' do
|
|
|
|
let(:project) { create(:project, :repository) }
|
|
|
|
let(:commit) { project.commit }
|
2019-07-30 14:25:49 -04:00
|
|
|
let(:params) { { project: project, target_type: 'commit', target_id: commit.id } }
|
2017-03-17 15:25:52 -04:00
|
|
|
|
|
|
|
it 'returns the commit' do
|
|
|
|
expect(subject.target).to eq(commit)
|
|
|
|
end
|
|
|
|
end
|
2019-06-15 01:10:58 -04:00
|
|
|
|
|
|
|
context 'target_iid' do
|
|
|
|
let(:issue) { create(:issue, project: project) }
|
|
|
|
let(:merge_request) { create(:merge_request, source_project: project, target_project: project) }
|
|
|
|
|
|
|
|
it 'finds issues by iid' do
|
2019-07-30 14:25:49 -04:00
|
|
|
iid_params = { project: project, target_type: 'issue', target_iid: issue.iid }
|
|
|
|
expect(described_class.new(user, iid_params).target).to eq(issue)
|
2019-06-15 01:10:58 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'finds merge requests by iid' do
|
2019-07-30 14:25:49 -04:00
|
|
|
iid_params = { project: project, target_type: 'merge_request', target_iid: merge_request.iid }
|
|
|
|
expect(described_class.new(user, iid_params).target).to eq(merge_request)
|
2019-06-15 01:10:58 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns nil if both target_id and target_iid are not given' do
|
2019-07-30 14:25:49 -04:00
|
|
|
params_without_any_id = { project: project, target_type: 'issue' }
|
|
|
|
expect(described_class.new(user, params_without_any_id).target).to be_nil
|
2019-06-15 01:10:58 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'prioritizes target_id over target_iid' do
|
|
|
|
issue2 = create(:issue, project: project)
|
2019-07-30 14:25:49 -04:00
|
|
|
iid_params = { project: project, target_type: 'issue', target_id: issue2.id, target_iid: issue.iid }
|
|
|
|
expect(described_class.new(user, iid_params).target).to eq(issue2)
|
2019-06-15 01:10:58 -04:00
|
|
|
end
|
|
|
|
end
|
2017-03-09 20:29:11 -05:00
|
|
|
end
|
2014-04-28 05:48:18 -04:00
|
|
|
end
|