BigW Consortium Gitlab

notes_finder_spec.rb 2 KB
Newer Older
Jacob Vosmaer committed
1 2 3 4 5 6 7
require 'spec_helper'

describe NotesFinder do
  let(:user) { create :user }
  let(:project) { create :project }
  let(:note1) { create :note_on_commit, project: project }
  let(:note2) { create :note_on_commit, project: project }
8
  let(:commit) { note1.noteable }
Jacob Vosmaer committed
9 10 11 12 13

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

14
  describe '#execute' do
15
    let(:params)  { { target_id: commit.id, target_type: 'commit', last_fetched_at: 1.hour.ago.to_i } }
16

Jacob Vosmaer committed
17 18 19 20 21
    before do
      note1
      note2
    end

22
    it 'finds all notes' do
Jacob Vosmaer committed
23
      notes = NotesFinder.new.execute(project, user, params)
24
      expect(notes.size).to eq(2)
Jacob Vosmaer committed
25
    end
26

27
    it 'raises an exception for an invalid target_type' do
28
      params.merge!(target_type: 'invalid')
29 30
      expect { NotesFinder.new.execute(project, user, params) }.to raise_error('invalid target_type')
    end
31 32 33 34

    it 'filters out old notes' do
      note2.update_attribute(:updated_at, 2.hours.ago)
      notes = NotesFinder.new.execute(project, user, params)
35
      expect(notes).to eq([note1])
36
    end
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51

    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(NotesFinder.new.execute(project, user, params)).to eq([confidential_note])
      end

      it 'raises an error if user can not see the issue' do
        user = create(:user)
        expect { NotesFinder.new.execute(project, user, params) }.to raise_error(ActiveRecord::RecordNotFound)
      end
52 53 54 55 56 57 58

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

        expect { NotesFinder.new.execute(project, user, params) }.to raise_error(ActiveRecord::RecordNotFound)
      end
59
    end
Jacob Vosmaer committed
60 61
  end
end