notes_finder_spec.rb 8.17 KB
Newer Older
Jacob Vosmaer's avatar
Jacob Vosmaer committed
1 2 3 4
require 'spec_helper'

describe NotesFinder do
  let(:user) { create :user }
5
  let(:project) { create(:empty_project) }
Jacob Vosmaer's avatar
Jacob Vosmaer committed
6 7 8 9 10

  before do
    project.team << [user, :master]
  end

11
  describe '#execute' do
12 13 14 15 16 17
    it 'finds notes on merge requests' do
      create(:note_on_merge_request, project: project)

      notes = described_class.new(project, user).execute

      expect(notes.count).to eq(1)
Jacob Vosmaer's avatar
Jacob Vosmaer committed
18 19
    end

20 21 22 23 24 25
    it 'finds notes on snippets' do
      create(:note_on_project_snippet, project: project)

      notes = described_class.new(project, user).execute

      expect(notes.count).to eq(1)
Jacob Vosmaer's avatar
Jacob Vosmaer committed
26
    end
27

28
    it "excludes notes on commits the author can't download" do
29
      project = create(:project, :private, :repository)
30 31 32 33 34 35
      note = create(:note_on_commit, project: project)
      params = { target_type: 'commit', target_id: note.noteable.id }

      notes = described_class.new(project, create(:user), params).execute

      expect(notes.count).to eq(0)
36
    end
37

38 39 40 41
    it 'succeeds when no notes found' do
      notes = described_class.new(project, create(:user)).execute

      expect(notes.count).to eq(0)
42
    end
43

44 45 46 47 48 49 50 51 52 53 54
    context 'for a certain discussion' do
      let!(:note_in_a_commit_discussion) { create(:note_on_commit, project: project) }
      let!(:other_note) { create(:note_on_merge_request, project: project) }

      it 'finds the only the notes for a certain discussion id' do
        notes = described_class.new(project, user, discussion_id: note_in_a_commit_discussion.discussion_id).execute

        expect(notes).to contain_exactly(note_in_a_commit_discussion)
      end
    end

55 56
    context 'on restricted projects' do
      let(:project) do
57 58 59 60 61
        create(:empty_project,
               :public,
               :issues_private,
               :snippets_private,
               :merge_requests_private)
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
      end

      it 'publicly excludes notes on merge requests' do
        create(:note_on_merge_request, project: project)

        notes = described_class.new(project, create(:user)).execute

        expect(notes.count).to eq(0)
      end

      it 'publicly excludes notes on issues' do
        create(:note_on_issue, project: project)

        notes = described_class.new(project, create(:user)).execute

        expect(notes.count).to eq(0)
      end

      it 'publicly excludes notes on snippets' do
        create(:note_on_project_snippet, project: project)

        notes = described_class.new(project, create(:user)).execute

        expect(notes.count).to eq(0)
      end
    end

    context 'for target' do
90
      let(:project) { create(:project, :repository) }
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
      let(:note1) { create :note_on_commit, project: project }
      let(:note2) { create :note_on_commit, project: project }
      let(:commit) { note1.noteable }
      let(:params)  { { target_id: commit.id, target_type: 'commit', last_fetched_at: 1.hour.ago.to_i } }

      before do
        note1
        note2
      end

      it 'finds all notes' do
        notes = described_class.new(project, user, params).execute
        expect(notes.size).to eq(2)
      end

      it 'finds notes on merge requests' do
        note = create(:note_on_merge_request, project: project)
        params = { target_type: 'merge_request', target_id: note.noteable.id }

        notes = described_class.new(project, user, params).execute

        expect(notes).to include(note)
      end

      it 'finds notes on snippets' do
        note = create(:note_on_project_snippet, project: project)
        params = { target_type: 'snippet', target_id: note.noteable.id }
118

119
        notes = described_class.new(project, user, params).execute
120

121
        expect(notes.count).to eq(1)
122 123
      end

124
      it 'raises an exception for an invalid target_type' do
125
        params[:target_type] = 'invalid'
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
        expect { described_class.new(project, user, params).execute }.to raise_error('invalid target_type')
      end

      it 'filters out old notes' do
        note2.update_attribute(:updated_at, 2.hours.ago)
        notes = described_class.new(project, user, params).execute
        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) }

        let(:params) { { target_id: confidential_issue.id, target_type: 'issue', last_fetched_at: 1.hour.ago.to_i } }

        it 'returns notes if user can see the issue' do
          expect(described_class.new(project, user, params).execute).to eq([confidential_note])
        end

        it 'raises an error if user can not see the issue' do
          user = create(:user)
          expect { described_class.new(project, user, params).execute }.to raise_error(ActiveRecord::RecordNotFound)
        end

        it 'raises an error for project members with guest role' do
          user = create(:user)
          project.team << [user, :guest]

          expect { described_class.new(project, user, params).execute }.to raise_error(ActiveRecord::RecordNotFound)
        end
      end
    end
  end

160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
  describe "#first_discussion" do
    let!(:discussion_note) { create(:note_on_merge_request, project: project) }

    it 'returns a discussion' do
      discussion = described_class.new(project, user, discussion_id: discussion_note.discussion_id).first_discussion

      expect(discussion).is_a? Discussion
    end

    it 'includes the notes in the discussion' do
      discussion = described_class.new(project, user, discussion_id: discussion_note.discussion_id).first_discussion

      expect(discussion.notes).to include(discussion_note)
    end

    it 'returns nil when no notes are found' do
      discussion = described_class.new(project, user, discussion_id: 'non-existant').first_discussion

      expect(discussion).to be(nil)
    end
  end

182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
  describe '.search' do
    let(:project) { create(:empty_project, :public) }
    let(:note) { create(:note_on_issue, note: 'WoW', project: project) }

    it 'returns notes with matching content' do
      expect(described_class.new(note.project, nil, search: note.note).execute).to eq([note])
    end

    it 'returns notes with matching content regardless of the casing' do
      expect(described_class.new(note.project, nil, search: 'WOW').execute).to eq([note])
    end

    it 'returns commit notes user can access' do
      note = create(:note_on_commit, project: project)

      expect(described_class.new(note.project, create(:user), search: note.note).execute).to eq([note])
    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
        expect(described_class.new(confidential_note.project, user, search: confidential_note.note).execute).to eq([confidential_note])
      end

      it "does not return notes with matching content if user can not see the issue" do
210
        user = create(:user)
211
        expect(described_class.new(confidential_note.project, user, search: confidential_note.note).execute).to be_empty
212
      end
213

214
      it "does not return notes with matching content for project members with guest role" do
215 216
        user = create(:user)
        project.team << [user, :guest]
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
        expect(described_class.new(confidential_note.project, user, search: confidential_note.note).execute).to be_empty
      end

      it "does not return notes with matching content for unauthenticated users" do
        expect(described_class.new(confidential_note.project, nil, search: confidential_note.note).execute).to be_empty
      end
    end

    context 'inlines SQL filters on subqueries for performance' do
      let(:sql) { described_class.new(note.project, nil, search: note.note).execute.to_sql }
      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
232

233 234
      specify 'search filter' do
        expect(sql.scan(/LIKE/).count).to be >= number_of_noteable_types
235
      end
236
    end
Jacob Vosmaer's avatar
Jacob Vosmaer committed
237 238
  end
end