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

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

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

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

R
Robert Speicher 已提交
34 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
  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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

175 176 177
      it { expect(@abilities.allowed?(@u1, :write_note, @p1)).to be_falsey }
      it { expect(@abilities.allowed?(@u2, :write_note, @p1)).to be_truthy }
      it { expect(@abilities.allowed?(@u3, :write_note, @p1)).to be_falsey }
G
gitlabhq 已提交
178 179
    end

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

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

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

R
Robert Speicher 已提交
196
    let(:project) { create(:project) }
197 198 199 200
    let(:issue) { create :issue, project: project }
    let(:backref_text) { issue.gfm_reference }
    let(:set_mentionable_text) { ->(txt) { subject.note = txt } }
  end
G
gitlabhq 已提交
201
end