note_spec.rb 6.2 KB
Newer Older
G
gitlabhq 已提交
1 2
require 'spec_helper'

D
Douwe Maan 已提交
3
describe Note, models: true do
R
Robert Speicher 已提交
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) }
G
gitlabhq 已提交
10 11
  end

R
Robert Speicher 已提交
12
  describe 'validation' do
13 14
    it { is_expected.to validate_presence_of(:note) }
    it { is_expected.to validate_presence_of(:project) }
G
gitlabhq 已提交
15 16
  end

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

R
Riyad Preukschas 已提交
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)
R
Riyad Preukschas 已提交
25 26
    end

D
Dmitriy Zaporozhets 已提交
27
    it "should save a valid note" do
28
      expect(note.commit_id).to eq(commit.id)
R
Riyad Preukschas 已提交
29
      note.noteable == commit
R
Riyad Preukschas 已提交
30 31 32
    end

    it "should be recognized by #for_commit?" do
33
      expect(note).to be_for_commit
D
Dmitriy Zaporozhets 已提交
34
    end
R
Riyad Preukschas 已提交
35 36
  end

R
Robert Speicher 已提交
37
  describe 'authorization' do
N
Nihad Abbasov 已提交
38
    before do
39
      @p1 = create(:project)
40 41 42 43
      @p2 = create(:project)
      @u1 = create(:user)
      @u2 = create(:user)
      @u3 = create(:user)
G
gitlabhq 已提交
44 45 46 47
      @abilities = Six.new
      @abilities << Ability
    end

R
Robert Speicher 已提交
48
    describe 'read' do
N
Nihad Abbasov 已提交
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)
G
gitlabhq 已提交
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 }
G
gitlabhq 已提交
57 58
    end

R
Robert Speicher 已提交
59
    describe 'write' do
N
Nihad Abbasov 已提交
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)
G
gitlabhq 已提交
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 }
G
gitlabhq 已提交
68 69
    end

R
Robert Speicher 已提交
70
    describe 'admin' do
N
Nihad Abbasov 已提交
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)
G
gitlabhq 已提交
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 }
G
gitlabhq 已提交
80 81
    end
  end
82 83

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

D
Douwe Maan 已提交
86
    let(:issue) { create :issue }
87 88 89
    let(:backref_text) { issue.gfm_reference }
    let(:set_mentionable_text) { ->(txt) { subject.note = txt } }
  end
90

D
Douwe Maan 已提交
91 92 93 94 95 96 97 98 99 100 101 102 103
  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

104 105
  describe '.search' do
    let(:note) { create(:note, note: 'WoW') }
106

107 108 109 110 111 112 113
    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
114
  end
V
Valery Sizov 已提交
115

116
  describe '.grouped_awards' do
V
Valery Sizov 已提交
117
    before do
V
Valery Sizov 已提交
118 119
      create :note, note: "smile", is_award: true
      create :note, note: "smile", is_award: true
V
Valery Sizov 已提交
120 121
    end

122 123 124 125 126 127 128 129
    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 已提交
130 131
    end
  end
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148

  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 已提交
149

150 151 152 153 154 155 156 157
  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 已提交
158
    let(:note) do
159 160 161 162
      create :note,
        noteable: ext_issue, project: ext_proj,
        note: "mentioned in issue #{private_issue.to_reference(ext_proj)}",
        system: true
R
Rémy Coutable 已提交
163
    end
164 165 166 167 168 169 170 171 172 173

    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 已提交
174
  describe "set_award!" do
175
    let(:merge_request) { create :merge_request }
V
Valery Sizov 已提交
176 177

    it "converts aliases to actual name" do
178
      note = create(:note, note: ":+1:", noteable: merge_request)
V
Valery Sizov 已提交
179
      expect(note.reload.note).to eq("thumbsup")
V
Valery Sizov 已提交
180
    end
181 182

    it "is not an award emoji when comment is on a diff" do
D
Douwe Maan 已提交
183
      note = create(:note_on_merge_request_diff, note: ":blowfish:", noteable: merge_request, line_code: "11d5d2e667e9da4f7f610f81d86c974b146b13bd_0_2")
184 185 186 187
      note = note.reload

      expect(note.note).to eq(":blowfish:")
      expect(note.is_award?).to be_falsy
188
    end
V
Valery Sizov 已提交
189
  end
190 191 192 193 194 195 196 197

  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
G
gitlabhq 已提交
198
end