note_spec.rb 4.95 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
Stan Hu's avatar
Stan Hu committed
19
#  is_award      :boolean          default(FALSE), not null
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
20 21
#

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

Douwe Maan's avatar
Douwe Maan committed
24
describe Note, models: true do
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') }
gitlabhq's avatar
gitlabhq committed
29 30
  end

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

36
  describe "Commit notes" do
Riyad Preukschas's avatar
Riyad Preukschas committed
37 38
    let!(:note) { create(:note_on_commit, note: "+1 from me") }
    let!(:commit) { note.noteable }
39

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

46
    it "should save a valid note" do
47
      expect(note.commit_id).to eq(commit.id)
Riyad Preukschas's avatar
Riyad Preukschas committed
48
      note.noteable == commit
Riyad Preukschas's avatar
Riyad Preukschas committed
49 50 51
    end

    it "should be recognized by #for_commit?" do
52
      expect(note).to be_for_commit
53
    end
Riyad Preukschas's avatar
Riyad Preukschas committed
54 55 56
  end

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

    it "should be recognized by #for_diff_line?" do
66
      expect(note).to be_for_diff_line
Riyad Preukschas's avatar
Riyad Preukschas committed
67 68 69
    end

    it "should be recognized by #for_commit_diff_line?" do
70
      expect(note).to be_for_commit_diff_line
Riyad Preukschas's avatar
Riyad Preukschas committed
71 72 73
    end
  end

74
  describe 'authorization' do
Nihad Abbasov's avatar
Nihad Abbasov committed
75
    before do
76
      @p1 = create(:project)
77 78 79 80
      @p2 = create(:project)
      @u1 = create(:user)
      @u2 = create(:user)
      @u3 = create(:user)
gitlabhq's avatar
gitlabhq committed
81 82 83 84
      @abilities = Six.new
      @abilities << Ability
    end

85
    describe 'read' do
Nihad Abbasov's avatar
Nihad Abbasov committed
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)
gitlabhq's avatar
gitlabhq committed
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 }
gitlabhq's avatar
gitlabhq committed
94 95
    end

96
    describe 'write' do
Nihad Abbasov's avatar
Nihad Abbasov committed
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)
gitlabhq's avatar
gitlabhq committed
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 }
gitlabhq's avatar
gitlabhq committed
105 106
    end

107
    describe 'admin' do
Nihad Abbasov's avatar
Nihad Abbasov committed
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)
gitlabhq's avatar
gitlabhq committed
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 }
gitlabhq's avatar
gitlabhq committed
117 118
    end
  end
119 120

  it_behaves_like 'an editable mentionable' do
Douwe Maan's avatar
Douwe Maan committed
121
    subject { create :note, noteable: issue, project: issue.project }
122

Douwe Maan's avatar
Douwe Maan committed
123
    let(:issue) { create :issue }
124 125 126
    let(:backref_text) { issue.gfm_reference }
    let(:set_mentionable_text) { ->(txt) { subject.note = txt } }
  end
127 128 129 130 131 132

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

    it { expect(Note.search('wow')).to include(note) }
  end
Valery Sizov's avatar
Valery Sizov committed
133 134 135

  describe :grouped_awards do
    before do
Valery Sizov's avatar
Valery Sizov committed
136 137
      create :note, note: "smile", is_award: true
      create :note, note: "smile", is_award: true
Valery Sizov's avatar
Valery Sizov committed
138 139 140 141 142 143 144
    end

    it "returns grouped array of notes" do
      expect(Note.grouped_awards.first.first).to eq("smile")
      expect(Note.grouped_awards.first.last).to match_array(Note.all)
    end
  end
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161

  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
Valery Sizov's avatar
Valery Sizov committed
162 163 164 165 166 167 168 169 170
  
  describe "set_award!" do
    let(:issue) { create :issue }

    it "converts aliases to actual name" do
      note = create :note, note: ":thumbsup:", noteable: issue
      expect(note.reload.note).to eq("+1")
    end
  end
gitlabhq's avatar
gitlabhq committed
171
end