note_spec.rb 7.0 KB
Newer Older
D
Dmitriy Zaporozhets 已提交
1 2 3 4
# == Schema Information
#
# Table name: notes
#
D
Dmitriy Zaporozhets 已提交
5
#  id            :integer          not null, primary key
D
Dmitriy Zaporozhets 已提交
6 7 8
#  note          :text
#  noteable_type :string(255)
#  author_id     :integer
D
Dmitriy Zaporozhets 已提交
9 10
#  created_at    :datetime
#  updated_at    :datetime
D
Dmitriy Zaporozhets 已提交
11 12 13
#  project_id    :integer
#  attachment    :string(255)
#  line_code     :string(255)
D
Dmitriy Zaporozhets 已提交
14 15
#  commit_id     :string(255)
#  noteable_id   :integer
D
Dmitriy Zaporozhets 已提交
16
#  system        :boolean          default(FALSE), not null
D
Dmitriy Zaporozhets 已提交
17
#  st_diff       :text
S
Stan Hu 已提交
18
#  updated_by_id :integer
S
Stan Hu 已提交
19
#  is_award      :boolean          default(FALSE), not null
D
Dmitriy Zaporozhets 已提交
20 21
#

G
gitlabhq 已提交
22 23
require 'spec_helper'

D
Douwe Maan 已提交
24
describe Note, models: true do
R
Robert Speicher 已提交
25
  describe 'associations' do
26
    it { is_expected.to belong_to(:project) }
27
    it { is_expected.to belong_to(:noteable).touch(true) }
28
    it { is_expected.to belong_to(:author).class_name('User') }
29

30
    it { is_expected.to have_many(:todos).dependent(:destroy) }
G
gitlabhq 已提交
31 32
  end

R
Robert Speicher 已提交
33
  describe 'validation' do
34 35
    it { is_expected.to validate_presence_of(:note) }
    it { is_expected.to validate_presence_of(:project) }
G
gitlabhq 已提交
36 37
  end

38
  describe "Commit notes" do
R
Riyad Preukschas 已提交
39 40
    let!(:note) { create(:note_on_commit, note: "+1 from me") }
    let!(:commit) { note.noteable }
D
Dmitriy Zaporozhets 已提交
41

R
Riyad Preukschas 已提交
42
    it "should be accessible through #noteable" do
43 44 45
      expect(note.commit_id).to eq(commit.id)
      expect(note.noteable).to be_a(Commit)
      expect(note.noteable).to eq(commit)
R
Riyad Preukschas 已提交
46 47
    end

D
Dmitriy Zaporozhets 已提交
48
    it "should save a valid note" do
49
      expect(note.commit_id).to eq(commit.id)
R
Riyad Preukschas 已提交
50
      note.noteable == commit
R
Riyad Preukschas 已提交
51 52 53
    end

    it "should be recognized by #for_commit?" do
54
      expect(note).to be_for_commit
D
Dmitriy Zaporozhets 已提交
55
    end
R
Riyad Preukschas 已提交
56 57 58
  end

  describe "Commit diff line notes" do
D
Dmitriy Zaporozhets 已提交
59
    let!(:note) { create(:note_on_commit_diff, note: "+1 from me") }
R
Riyad Preukschas 已提交
60
    let!(:commit) { note.noteable }
D
Dmitriy Zaporozhets 已提交
61 62

    it "should save a valid note" do
63 64
      expect(note.commit_id).to eq(commit.id)
      expect(note.noteable.id).to eq(commit.id)
R
Riyad Preukschas 已提交
65 66 67
    end

    it "should be recognized by #for_diff_line?" do
68
      expect(note).to be_for_diff_line
R
Riyad Preukschas 已提交
69 70 71
    end

    it "should be recognized by #for_commit_diff_line?" do
72
      expect(note).to be_for_commit_diff_line
R
Riyad Preukschas 已提交
73 74 75
    end
  end

R
Robert Speicher 已提交
76
  describe 'authorization' do
N
Nihad Abbasov 已提交
77
    before do
78
      @p1 = create(:project)
79 80 81 82
      @p2 = create(:project)
      @u1 = create(:user)
      @u2 = create(:user)
      @u3 = create(:user)
G
gitlabhq 已提交
83 84 85 86
      @abilities = Six.new
      @abilities << Ability
    end

R
Robert Speicher 已提交
87
    describe 'read' do
N
Nihad Abbasov 已提交
88
      before do
89 90
        @p1.project_members.create(user: @u2, access_level: ProjectMember::GUEST)
        @p2.project_members.create(user: @u3, access_level: ProjectMember::GUEST)
G
gitlabhq 已提交
91 92
      end

93 94 95
      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 }
G
gitlabhq 已提交
96 97
    end

R
Robert Speicher 已提交
98
    describe 'write' do
N
Nihad Abbasov 已提交
99
      before do
100 101
        @p1.project_members.create(user: @u2, access_level: ProjectMember::DEVELOPER)
        @p2.project_members.create(user: @u3, access_level: ProjectMember::DEVELOPER)
G
gitlabhq 已提交
102 103
      end

104 105 106
      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 }
G
gitlabhq 已提交
107 108
    end

R
Robert Speicher 已提交
109
    describe 'admin' do
N
Nihad Abbasov 已提交
110
      before do
111 112 113
        @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)
G
gitlabhq 已提交
114 115
      end

116 117 118
      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 }
G
gitlabhq 已提交
119 120
    end
  end
121 122

  it_behaves_like 'an editable mentionable' do
D
Douwe Maan 已提交
123
    subject { create :note, noteable: issue, project: issue.project }
124

D
Douwe Maan 已提交
125
    let(:issue) { create :issue }
126 127 128
    let(:backref_text) { issue.gfm_reference }
    let(:set_mentionable_text) { ->(txt) { subject.note = txt } }
  end
129

D
Douwe Maan 已提交
130 131 132 133 134 135 136 137 138 139 140 141 142
  describe "#all_references" do
    let!(:note1) { create(:note) }
    let!(:note2) { create(:note) }

    it "reads the rendered note body from the cache" do
      expect(Banzai::Renderer).to receive(:render).with(note1.note, pipeline: :note, cache_key: [note1, "note"], project: note1.project)
      expect(Banzai::Renderer).to receive(:render).with(note2.note, pipeline: :note, cache_key: [note2, "note"], project: note2.project)

      note1.all_references
      note2.all_references
    end
  end

143 144
  describe '.search' do
    let(:note) { create(:note, note: 'WoW') }
145

146 147 148 149 150 151 152
    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
153
  end
V
Valery Sizov 已提交
154 155 156

  describe :grouped_awards do
    before do
V
Valery Sizov 已提交
157 158
      create :note, note: "smile", is_award: true
      create :note, note: "smile", is_award: true
V
Valery Sizov 已提交
159 160
    end

161 162 163 164 165 166 167 168
    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)
V
Valery Sizov 已提交
169 170
    end
  end
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187

  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
D
Douwe Maan 已提交
188

189 190 191 192 193 194 195 196
  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) }

R
Rémy Coutable 已提交
197
    let(:note) do
198 199 200 201
      create :note,
        noteable: ext_issue, project: ext_proj,
        note: "mentioned in issue #{private_issue.to_reference(ext_proj)}",
        system: true
R
Rémy Coutable 已提交
202
    end
203 204 205 206 207 208 209 210 211 212

    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

V
Valery Sizov 已提交
213
  describe "set_award!" do
214
    let(:merge_request) { create :merge_request }
V
Valery Sizov 已提交
215 216

    it "converts aliases to actual name" do
217
      note = create(:note, note: ":+1:", noteable: merge_request)
V
Valery Sizov 已提交
218
      expect(note.reload.note).to eq("thumbsup")
V
Valery Sizov 已提交
219
    end
220 221 222

    it "is not an award emoji when comment is on a diff" do
      note = create(:note, note: ":blowfish:", noteable: merge_request, line_code: "11d5d2e667e9da4f7f610f81d86c974b146b13bd_0_2")
223 224 225 226
      note = note.reload

      expect(note.note).to eq(":blowfish:")
      expect(note.is_award?).to be_falsy
227
    end
V
Valery Sizov 已提交
228
  end
G
gitlabhq 已提交
229
end