note_spec.rb 5.83 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
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
18 19
#

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

describe Note do
23
  describe 'associations' do
24
    it { is_expected.to belong_to(:project) }
25
    it { is_expected.to belong_to(:noteable) }
26
    it { is_expected.to belong_to(:author).class_name('User') }
gitlabhq's avatar
gitlabhq committed
27 28
  end

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

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

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

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

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

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

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

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

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

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

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

169
    describe 'write' do
Nihad Abbasov's avatar
Nihad Abbasov committed
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)
gitlabhq's avatar
gitlabhq committed
173 174
      end

175 176 177
      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
178 179
    end

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

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

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