note_spec.rb 5.6 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) }
28
    it { is_expected.to belong_to(:author).class_name('User') }
G
gitlabhq 已提交
29 30
  end

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

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

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

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

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

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

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

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

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

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

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

91 92 93
      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 已提交
94 95
    end

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

102 103 104
      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 已提交
105 106
    end

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

114 115 116
      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 已提交
117 118
    end
  end
119 120

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

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

D
Douwe Maan 已提交
128 129 130 131 132 133 134 135 136 137 138 139 140
  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

141 142 143 144 145
  describe :search do
    let!(:note) { create(:note, note: "WoW") }

    it { expect(Note.search('wow')).to include(note) }
  end
V
Valery Sizov 已提交
146 147 148

  describe :grouped_awards do
    before do
V
Valery Sizov 已提交
149 150
      create :note, note: "smile", is_award: true
      create :note, note: "smile", is_award: true
V
Valery Sizov 已提交
151 152
    end

153 154 155 156 157 158 159 160
    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 已提交
161 162
    end
  end
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179

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

V
Valery Sizov 已提交
181 182 183 184
  describe "set_award!" do
    let(:issue) { create :issue }

    it "converts aliases to actual name" do
V
Valery Sizov 已提交
185 186
      note = create :note, note: ":+1:", noteable: issue
      expect(note.reload.note).to eq("thumbsup")
V
Valery Sizov 已提交
187 188
    end
  end
G
gitlabhq 已提交
189
end