note_spec.rb 6.7 KB
Newer Older
gitlabhq's avatar
gitlabhq committed
1 2
require 'spec_helper'

Douwe Maan's avatar
Douwe Maan committed
3
describe Note, models: true do
4
  describe 'associations' do
5
    it { is_expected.to belong_to(:project) }
6
    it { is_expected.to belong_to(:noteable).touch(true) }
7
    it { is_expected.to belong_to(:author).class_name('User') }
8

9
    it { is_expected.to have_many(:todos).dependent(:destroy) }
gitlabhq's avatar
gitlabhq committed
10 11
  end

12
  describe 'validation' do
13 14
    it { is_expected.to validate_presence_of(:note) }
    it { is_expected.to validate_presence_of(:project) }
gitlabhq's avatar
gitlabhq committed
15 16
  end

17
  describe "Commit notes" do
Riyad Preukschas's avatar
Riyad Preukschas committed
18 19
    let!(:note) { create(:note_on_commit, note: "+1 from me") }
    let!(:commit) { note.noteable }
20

Riyad Preukschas's avatar
Riyad Preukschas committed
21
    it "should be accessible through #noteable" do
22 23 24
      expect(note.commit_id).to eq(commit.id)
      expect(note.noteable).to be_a(Commit)
      expect(note.noteable).to eq(commit)
Riyad Preukschas's avatar
Riyad Preukschas committed
25 26
    end

27
    it "should save a valid note" do
28
      expect(note.commit_id).to eq(commit.id)
Riyad Preukschas's avatar
Riyad Preukschas committed
29
      note.noteable == commit
Riyad Preukschas's avatar
Riyad Preukschas committed
30 31 32
    end

    it "should be recognized by #for_commit?" do
33
      expect(note).to be_for_commit
34
    end
Riyad Preukschas's avatar
Riyad Preukschas committed
35 36
  end

37
  describe 'authorization' do
Nihad Abbasov's avatar
Nihad Abbasov committed
38
    before do
39
      @p1 = create(:project)
40 41 42 43
      @p2 = create(:project)
      @u1 = create(:user)
      @u2 = create(:user)
      @u3 = create(:user)
gitlabhq's avatar
gitlabhq committed
44 45 46 47
      @abilities = Six.new
      @abilities << Ability
    end

48
    describe 'read' do
Nihad Abbasov's avatar
Nihad Abbasov committed
49
      before do
50 51
        @p1.project_members.create(user: @u2, access_level: ProjectMember::GUEST)
        @p2.project_members.create(user: @u3, access_level: ProjectMember::GUEST)
gitlabhq's avatar
gitlabhq committed
52 53
      end

54 55 56
      it { expect(@abilities.allowed?(@u1, :read_note, @p1)).to be_falsey }
      it { expect(@abilities.allowed?(@u2, :read_note, @p1)).to be_truthy }
      it { expect(@abilities.allowed?(@u3, :read_note, @p1)).to be_falsey }
gitlabhq's avatar
gitlabhq committed
57 58
    end

59
    describe 'write' do
Nihad Abbasov's avatar
Nihad Abbasov committed
60
      before do
61 62
        @p1.project_members.create(user: @u2, access_level: ProjectMember::DEVELOPER)
        @p2.project_members.create(user: @u3, access_level: ProjectMember::DEVELOPER)
gitlabhq's avatar
gitlabhq committed
63 64
      end

65 66 67
      it { expect(@abilities.allowed?(@u1, :create_note, @p1)).to be_falsey }
      it { expect(@abilities.allowed?(@u2, :create_note, @p1)).to be_truthy }
      it { expect(@abilities.allowed?(@u3, :create_note, @p1)).to be_falsey }
gitlabhq's avatar
gitlabhq committed
68 69
    end

70
    describe 'admin' do
Nihad Abbasov's avatar
Nihad Abbasov committed
71
      before do
72 73 74
        @p1.project_members.create(user: @u1, access_level: ProjectMember::REPORTER)
        @p1.project_members.create(user: @u2, access_level: ProjectMember::MASTER)
        @p2.project_members.create(user: @u3, access_level: ProjectMember::MASTER)
gitlabhq's avatar
gitlabhq committed
75 76
      end

77 78 79
      it { expect(@abilities.allowed?(@u1, :admin_note, @p1)).to be_falsey }
      it { expect(@abilities.allowed?(@u2, :admin_note, @p1)).to be_truthy }
      it { expect(@abilities.allowed?(@u3, :admin_note, @p1)).to be_falsey }
gitlabhq's avatar
gitlabhq committed
80 81
    end
  end
82 83

  it_behaves_like 'an editable mentionable' do
Douwe Maan's avatar
Douwe Maan committed
84
    subject { create :note, noteable: issue, project: issue.project }
85

Douwe Maan's avatar
Douwe Maan committed
86
    let(:issue) { create :issue }
87 88 89
    let(:backref_text) { issue.gfm_reference }
    let(:set_mentionable_text) { ->(txt) { subject.note = txt } }
  end
90

Douwe Maan's avatar
Douwe Maan committed
91 92 93 94 95
  describe "#all_references" do
    let!(:note1) { create(:note) }
    let!(:note2) { create(:note) }

    it "reads the rendered note body from the cache" do
Yorick Peterse's avatar
Yorick Peterse committed
96 97 98 99 100 101 102 103 104 105 106 107 108
      expect(Banzai::Renderer).to receive(:render).
        with(note1.note,
             pipeline: :note,
             cache_key: [note1, "note"],
             project: note1.project,
             author: note1.author)

      expect(Banzai::Renderer).to receive(:render).
        with(note2.note,
             pipeline: :note,
             cache_key: [note2, "note"],
             project: note2.project,
             author: note2.author)
Douwe Maan's avatar
Douwe Maan committed
109 110 111 112 113 114

      note1.all_references
      note2.all_references
    end
  end

115 116
  describe '.search' do
    let(:note) { create(:note, note: 'WoW') }
117

118 119 120 121 122 123 124
    it 'returns notes with matching content' do
      expect(described_class.search(note.note)).to eq([note])
    end

    it 'returns notes with matching content regardless of the casing' do
      expect(described_class.search('WOW')).to eq([note])
    end
125
  end
Valery Sizov's avatar
Valery Sizov committed
126

127
  describe '.grouped_awards' do
Valery Sizov's avatar
Valery Sizov committed
128
    before do
Valery Sizov's avatar
Valery Sizov committed
129 130
      create :note, note: "smile", is_award: true
      create :note, note: "smile", is_award: true
Valery Sizov's avatar
Valery Sizov committed
131 132
    end

133 134 135 136 137 138 139 140
    it "returns grouped hash of notes" do
      expect(Note.grouped_awards.keys.size).to eq(3)
      expect(Note.grouped_awards["smile"]).to match_array(Note.all)
    end

    it "returns thumbsup and thumbsdown always" do
      expect(Note.grouped_awards["thumbsup"]).to match_array(Note.none)
      expect(Note.grouped_awards["thumbsdown"]).to match_array(Note.none)
Valery Sizov's avatar
Valery Sizov committed
141 142
    end
  end
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159

  describe "editable?" do
    it "returns true" do
      note = build(:note)
      expect(note.editable?).to be_truthy
    end

    it "returns false" do
      note = build(:note, system: true)
      expect(note.editable?).to be_falsy
    end

    it "returns false" do
      note = build(:note, is_award: true, note: "smiley")
      expect(note.editable?).to be_falsy
    end
  end
Douwe Maan's avatar
Douwe Maan committed
160

161 162 163 164 165 166 167 168
  describe "cross_reference_not_visible_for?" do
    let(:private_user)    { create(:user) }
    let(:private_project) { create(:project, namespace: private_user.namespace).tap { |p| p.team << [private_user, :master] } }
    let(:private_issue)   { create(:issue, project: private_project) }

    let(:ext_proj)  { create(:project, :public) }
    let(:ext_issue) { create(:issue, project: ext_proj) }

169
    let(:note) do
170 171 172 173
      create :note,
        noteable: ext_issue, project: ext_proj,
        note: "mentioned in issue #{private_issue.to_reference(ext_proj)}",
        system: true
174
    end
175 176 177 178 179 180 181 182 183 184

    it "returns true" do
      expect(note.cross_reference_not_visible_for?(ext_issue.author)).to be_truthy
    end

    it "returns false" do
      expect(note.cross_reference_not_visible_for?(private_user)).to be_falsy
    end
  end

Valery Sizov's avatar
Valery Sizov committed
185
  describe "set_award!" do
186
    let(:merge_request) { create :merge_request }
Valery Sizov's avatar
Valery Sizov committed
187 188

    it "converts aliases to actual name" do
189
      note = create(:note, note: ":+1:", noteable: merge_request)
Valery Sizov's avatar
Valery Sizov committed
190
      expect(note.reload.note).to eq("thumbsup")
Valery Sizov's avatar
Valery Sizov committed
191
    end
192 193

    it "is not an award emoji when comment is on a diff" do
Douwe Maan's avatar
Douwe Maan committed
194
      note = create(:note_on_merge_request_diff, note: ":blowfish:", noteable: merge_request, line_code: "11d5d2e667e9da4f7f610f81d86c974b146b13bd_0_2")
195 196 197 198
      note = note.reload

      expect(note.note).to eq(":blowfish:")
      expect(note.is_award?).to be_falsy
199
    end
Valery Sizov's avatar
Valery Sizov committed
200
  end
201 202 203 204 205 206 207 208

  describe 'clear_blank_line_code!' do
    it 'clears a blank line code before validation' do
      note = build(:note, line_code: ' ')

      expect { note.valid? }.to change(note, :line_code).to(nil)
    end
  end
Yorick Peterse's avatar
Yorick Peterse committed
209 210 211 212 213 214 215 216 217 218

  describe '#participants' do
    it 'includes the note author' do
      project = create(:project, :public)
      issue = create(:issue, project: project)
      note = create(:note_on_issue, noteable: issue, project: project)

      expect(note.participants).to include(note.author)
    end
  end
gitlabhq's avatar
gitlabhq committed
219
end