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

gitlabhq's avatar
gitlabhq committed
21 22 23
require 'spec_helper'

describe Note do
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') }
gitlabhq's avatar
gitlabhq committed
28 29
  end

30
  describe 'validation' do
31 32
    it { is_expected.to validate_presence_of(:note) }
    it { is_expected.to validate_presence_of(:project) }
gitlabhq's avatar
gitlabhq committed
33 34
  end

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
Riyad Preukschas's avatar
Riyad Preukschas committed
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
Riyad Preukschas's avatar
Riyad Preukschas committed
107 108
    let!(:note) { create(:note_on_commit, note: "+1 from me") }
    let!(:commit) { note.noteable }
109

Riyad Preukschas's avatar
Riyad Preukschas committed
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)
Riyad Preukschas's avatar
Riyad Preukschas committed
114 115
    end

116
    it "should save a valid note" do
117
      expect(note.commit_id).to eq(commit.id)
Riyad Preukschas's avatar
Riyad Preukschas committed
118
      note.noteable == commit
Riyad Preukschas's avatar
Riyad Preukschas committed
119 120 121
    end

    it "should be recognized by #for_commit?" do
122
      expect(note).to be_for_commit
123
    end
Riyad Preukschas's avatar
Riyad Preukschas committed
124 125 126
  end

  describe "Commit diff line notes" do
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
127
    let!(:note) { create(:note_on_commit_diff, note: "+1 from me") }
Riyad Preukschas's avatar
Riyad Preukschas committed
128
    let!(:commit) { note.noteable }
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)
Riyad Preukschas's avatar
Riyad Preukschas committed
133 134 135
    end

    it "should be recognized by #for_diff_line?" do
136
      expect(note).to be_for_diff_line
Riyad Preukschas's avatar
Riyad Preukschas committed
137 138 139
    end

    it "should be recognized by #for_commit_diff_line?" do
140
      expect(note).to be_for_commit_diff_line
Riyad Preukschas's avatar
Riyad Preukschas committed
141 142 143
    end

    it "should not be votable" do
144
      expect(note).not_to be_votable
Riyad Preukschas's avatar
Riyad Preukschas committed
145 146 147
    end
  end

148
  describe 'authorization' do
Nihad Abbasov's avatar
Nihad Abbasov committed
149
    before do
150
      @p1 = create(:project)
151 152 153 154
      @p2 = create(:project)
      @u1 = create(:user)
      @u2 = create(:user)
      @u3 = create(:user)
gitlabhq's avatar
gitlabhq committed
155 156 157 158
      @abilities = Six.new
      @abilities << Ability
    end

159
    describe 'read' do
Nihad Abbasov's avatar
Nihad Abbasov committed
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)
gitlabhq's avatar
gitlabhq committed
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 }
gitlabhq's avatar
gitlabhq committed
168 169
    end

170
    describe 'write' do
Nihad Abbasov's avatar
Nihad Abbasov committed
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)
gitlabhq's avatar
gitlabhq committed
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 }
gitlabhq's avatar
gitlabhq committed
179 180
    end

181
    describe 'admin' do
Nihad Abbasov's avatar
Nihad Abbasov committed
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)
gitlabhq's avatar
gitlabhq committed
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 }
gitlabhq's avatar
gitlabhq committed
191 192
    end
  end
193 194

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

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
gitlabhq's avatar
gitlabhq committed
208
end