note_spec.rb 6.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
D
Dmitriy Zaporozhets 已提交
19 20
#

G
gitlabhq 已提交
21 22 23
require 'spec_helper'

describe Note do
R
Robert Speicher 已提交
24
  describe 'associations' do
25
    it { is_expected.to belong_to(:project) }
26
    it { is_expected.to belong_to(:noteable) }
27
    it { is_expected.to belong_to(:author).class_name('User') }
G
gitlabhq 已提交
28 29
  end

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

R
Robert Speicher 已提交
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
  describe '#votable?' do
    it 'is true for issue notes' do
      note = build(:note_on_issue)
      expect(note).to be_votable
    end

    it 'is true for merge request notes' do
      note = build(:note_on_merge_request)
      expect(note).to be_votable
    end

    it 'is false for merge request diff notes' do
      note = build(:note_on_merge_request_diff)
      expect(note).not_to be_votable
    end

    it 'is false for commit notes' do
      note = build(:note_on_commit)
      expect(note).not_to be_votable
    end

    it 'is false for commit diff notes' do
      note = build(:note_on_commit_diff)
      expect(note).not_to be_votable
    end
  end

62 63 64
  describe 'voting score' do
    it 'recognizes a neutral note' do
      note = build(:votable_note, note: 'This is not a +1 note')
65 66
      expect(note).not_to be_upvote
      expect(note).not_to be_downvote
67 68
    end

69
    it 'recognizes a neutral emoji note' do
R
Riyad Preukschas 已提交
70
      note = build(:votable_note, note: "I would :+1: this, but I don't want to")
71 72
      expect(note).not_to be_upvote
      expect(note).not_to be_downvote
73 74
    end

75 76
    it 'recognizes a +1 note' do
      note = build(:votable_note, note: '+1 for this')
77
      expect(note).to be_upvote
78 79
    end

80 81
    it 'recognizes a +1 emoji as a vote' do
      note = build(:votable_note, note: ':+1: for this')
82
      expect(note).to be_upvote
83 84
    end

85 86
    it 'recognizes a thumbsup emoji as a vote' do
      note = build(:votable_note, note: ':thumbsup: for this')
87
      expect(note).to be_upvote
88 89
    end

90 91
    it 'recognizes a -1 note' do
      note = build(:votable_note, note: '-1 for this')
92
      expect(note).to be_downvote
93 94
    end

95 96
    it 'recognizes a -1 emoji as a vote' do
      note = build(:votable_note, note: ':-1: for this')
97
      expect(note).to be_downvote
98
    end
99

100 101
    it 'recognizes a thumbsdown emoji as a vote' do
      note = build(:votable_note, note: ':thumbsdown: for this')
102
      expect(note).to be_downvote
103
    end
104 105
  end

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

R
Riyad Preukschas 已提交
110
    it "should be accessible through #noteable" do
111 112 113
      expect(note.commit_id).to eq(commit.id)
      expect(note.noteable).to be_a(Commit)
      expect(note.noteable).to eq(commit)
R
Riyad Preukschas 已提交
114 115
    end

D
Dmitriy Zaporozhets 已提交
116
    it "should save a valid note" do
117
      expect(note.commit_id).to eq(commit.id)
R
Riyad Preukschas 已提交
118
      note.noteable == commit
R
Riyad Preukschas 已提交
119 120 121
    end

    it "should be recognized by #for_commit?" do
122
      expect(note).to be_for_commit
D
Dmitriy Zaporozhets 已提交
123
    end
R
Riyad Preukschas 已提交
124 125 126
  end

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

    it "should save a valid note" do
131 132
      expect(note.commit_id).to eq(commit.id)
      expect(note.noteable.id).to eq(commit.id)
R
Riyad Preukschas 已提交
133 134 135
    end

    it "should be recognized by #for_diff_line?" do
136
      expect(note).to be_for_diff_line
R
Riyad Preukschas 已提交
137 138 139
    end

    it "should be recognized by #for_commit_diff_line?" do
140
      expect(note).to be_for_commit_diff_line
R
Riyad Preukschas 已提交
141 142 143
    end

    it "should not be votable" do
144
      expect(note).not_to be_votable
R
Riyad Preukschas 已提交
145 146 147
    end
  end

R
Robert Speicher 已提交
148
  describe 'authorization' do
N
Nihad Abbasov 已提交
149
    before do
150
      @p1 = create(:project)
151 152 153 154
      @p2 = create(:project)
      @u1 = create(:user)
      @u2 = create(:user)
      @u3 = create(:user)
G
gitlabhq 已提交
155 156 157 158
      @abilities = Six.new
      @abilities << Ability
    end

R
Robert Speicher 已提交
159
    describe 'read' do
N
Nihad Abbasov 已提交
160
      before do
161 162
        @p1.project_members.create(user: @u2, access_level: ProjectMember::GUEST)
        @p2.project_members.create(user: @u3, access_level: ProjectMember::GUEST)
G
gitlabhq 已提交
163 164
      end

165 166 167
      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 已提交
168 169
    end

R
Robert Speicher 已提交
170
    describe 'write' do
N
Nihad Abbasov 已提交
171
      before do
172 173
        @p1.project_members.create(user: @u2, access_level: ProjectMember::DEVELOPER)
        @p2.project_members.create(user: @u3, access_level: ProjectMember::DEVELOPER)
G
gitlabhq 已提交
174 175
      end

176 177 178
      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 已提交
179 180
    end

R
Robert Speicher 已提交
181
    describe 'admin' do
N
Nihad Abbasov 已提交
182
      before do
183 184 185
        @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 已提交
186 187
      end

188 189 190
      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 已提交
191 192
    end
  end
193 194

  it_behaves_like 'an editable mentionable' do
195 196
    subject { create :note, noteable: issue, project: project }

R
Robert Speicher 已提交
197
    let(:project) { create(:project) }
198 199 200 201
    let(:issue) { create :issue, project: project }
    let(:backref_text) { issue.gfm_reference }
    let(:set_mentionable_text) { ->(txt) { subject.note = txt } }
  end
202 203 204 205 206 207

  describe :search do
    let!(:note) { create(:note, note: "WoW") }

    it { expect(Note.search('wow')).to include(note) }
  end
G
gitlabhq 已提交
208
end