note_spec.rb 5.9 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 9
#  note          :text
#  noteable_id   :string(255)
#  noteable_type :string(255)
#  author_id     :integer
D
Dmitriy Zaporozhets 已提交
10 11
#  created_at    :datetime         not null
#  updated_at    :datetime         not null
D
Dmitriy Zaporozhets 已提交
12 13 14 15 16
#  project_id    :integer
#  attachment    :string(255)
#  line_code     :string(255)
#

G
gitlabhq 已提交
17 18 19 20 21
require 'spec_helper'

describe Note do
  describe "Associations" do
    it { should belong_to(:project) }
22 23
    it { should belong_to(:noteable) }
    it { should belong_to(:author).class_name('User') }
G
gitlabhq 已提交
24 25
  end

26 27 28 29 30
  describe "Mass assignment" do
    it { should_not allow_mass_assignment_of(:author) }
    it { should_not allow_mass_assignment_of(:author_id) }
  end

G
gitlabhq 已提交
31 32 33 34 35
  describe "Validation" do
    it { should validate_presence_of(:note) }
    it { should validate_presence_of(:project) }
  end

D
Drew 已提交
36 37 38 39 40
  describe "Scopes" do
    it "should have a today named scope that returns ..." do
      Note.today.where_values.should == ["created_at >= '#{Date.today}'"]
    end
  end
D
Dmitriy Zaporozhets 已提交
41

42
  describe "Voting score" do
43
    let(:project) { create(:project) }
44 45

    it "recognizes a neutral note" do
R
Riyad Preukschas 已提交
46
      note = create(:votable_note, note: "This is not a +1 note")
47
      note.should_not be_upvote
48 49 50 51
      note.should_not be_downvote
    end

    it "recognizes a neutral emoji note" do
R
Riyad Preukschas 已提交
52
      note = build(:votable_note, note: "I would :+1: this, but I don't want to")
53 54
      note.should_not be_upvote
      note.should_not be_downvote
55 56 57
    end

    it "recognizes a +1 note" do
R
Riyad Preukschas 已提交
58
      note = create(:votable_note, note: "+1 for this")
59 60 61
      note.should be_upvote
    end

62
    it "recognizes a +1 emoji as a vote" do
R
Riyad Preukschas 已提交
63
      note = build(:votable_note, note: ":+1: for this")
64 65 66
      note.should be_upvote
    end

67
    it "recognizes a -1 note" do
R
Riyad Preukschas 已提交
68
      note = create(:votable_note, note: "-1 for this")
69 70 71 72
      note.should be_downvote
    end

    it "recognizes a -1 emoji as a vote" do
R
Riyad Preukschas 已提交
73
      note = build(:votable_note, note: ":-1: for this")
74
      note.should be_downvote
75
    end
76 77
  end

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

R
Riyad Preukschas 已提交
82
    it "should be accessible through #noteable" do
R
Riyad Preukschas 已提交
83 84 85
      note.noteable_id.should == commit.id
      note.noteable.should be_a(Commit)
      note.noteable.should == commit
R
Riyad Preukschas 已提交
86 87
    end

D
Dmitriy Zaporozhets 已提交
88
    it "should save a valid note" do
R
Riyad Preukschas 已提交
89 90
      note.noteable_id.should == commit.id
      note.noteable == commit
R
Riyad Preukschas 已提交
91 92 93
    end

    it "should be recognized by #for_commit?" do
R
Riyad Preukschas 已提交
94
      note.should be_for_commit
D
Dmitriy Zaporozhets 已提交
95 96
    end

R
Riyad Preukschas 已提交
97 98
    it "should not be votable" do
      note.should_not be_votable
D
Dmitriy Zaporozhets 已提交
99
    end
R
Riyad Preukschas 已提交
100 101 102 103 104
  end

  describe "Commit diff line notes" do
    let!(:note) { create(:note_on_commit_line, note: "+1 from me") }
    let!(:commit) { note.noteable }
D
Dmitriy Zaporozhets 已提交
105 106

    it "should save a valid note" do
R
Riyad Preukschas 已提交
107 108
      note.noteable_id.should == commit.id
      note.noteable.id.should == commit.id
R
Riyad Preukschas 已提交
109 110 111
    end

    it "should be recognized by #for_diff_line?" do
R
Riyad Preukschas 已提交
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
      note.should be_for_diff_line
    end

    it "should be recognized by #for_commit_diff_line?" do
      note.should be_for_commit_diff_line
    end

    it "should not be votable" do
      note.should_not be_votable
    end
  end

  describe "Issue notes" do
    let!(:note) { create(:note_on_issue, note: "+1 from me") }

    it "should not be votable" do
      note.should be_votable
    end
  end

  describe "Merge request notes" do
    let!(:note) { create(:note_on_merge_request, note: "+1 from me") }

    it "should not be votable" do
      note.should be_votable
    end
  end

  describe "Merge request diff line notes" do
    let!(:note) { create(:note_on_merge_request_line, note: "+1 from me") }

    it "should not be votable" do
      note.should_not be_votable
D
Dmitriy Zaporozhets 已提交
145 146 147
    end
  end

148
  describe '#create_status_change_note' do
149 150 151
    let(:project)  { create(:project) }
    let(:thing)    { create(:issue, project: project) }
    let(:author)   { create(:user) }
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
    let(:status)   { 'new_status' }

    subject { Note.create_status_change_note(thing, author, status) }

    it 'creates and saves a Note' do
      should be_a Note
      subject.id.should_not be_nil
    end

    its(:noteable) { should == thing }
    its(:project)  { should == thing.project }
    its(:author)   { should == author }
    its(:note)     { should =~ /Status changed to #{status}/ }
  end

N
Nihad Abbasov 已提交
167 168
  describe :authorization do
    before do
169
      @p1 = create(:project)
170 171 172 173
      @p2 = create(:project)
      @u1 = create(:user)
      @u2 = create(:user)
      @u3 = create(:user)
G
gitlabhq 已提交
174 175 176 177
      @abilities = Six.new
      @abilities << Ability
    end

N
Nihad Abbasov 已提交
178 179
    describe :read do
      before do
180 181
        @p1.users_projects.create(user: @u2, project_access: UsersProject::GUEST)
        @p2.users_projects.create(user: @u3, project_access: UsersProject::GUEST)
G
gitlabhq 已提交
182 183 184 185 186 187 188
      end

      it { @abilities.allowed?(@u1, :read_note, @p1).should be_false }
      it { @abilities.allowed?(@u2, :read_note, @p1).should be_true }
      it { @abilities.allowed?(@u3, :read_note, @p1).should be_false }
    end

N
Nihad Abbasov 已提交
189 190
    describe :write do
      before do
191 192
        @p1.users_projects.create(user: @u2, project_access: UsersProject::DEVELOPER)
        @p2.users_projects.create(user: @u3, project_access: UsersProject::DEVELOPER)
G
gitlabhq 已提交
193 194 195 196 197 198 199
      end

      it { @abilities.allowed?(@u1, :write_note, @p1).should be_false }
      it { @abilities.allowed?(@u2, :write_note, @p1).should be_true }
      it { @abilities.allowed?(@u3, :write_note, @p1).should be_false }
    end

N
Nihad Abbasov 已提交
200 201
    describe :admin do
      before do
202 203 204
        @p1.users_projects.create(user: @u1, project_access: UsersProject::REPORTER)
        @p1.users_projects.create(user: @u2, project_access: UsersProject::MASTER)
        @p2.users_projects.create(user: @u3, project_access: UsersProject::MASTER)
G
gitlabhq 已提交
205 206 207 208 209 210 211 212
      end

      it { @abilities.allowed?(@u1, :admin_note, @p1).should be_false }
      it { @abilities.allowed?(@u2, :admin_note, @p1).should be_true }
      it { @abilities.allowed?(@u3, :admin_note, @p1).should be_false }
    end
  end
end